Skip to content

Commit

Permalink
Add Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
SurabhiKeesara committed Dec 3, 2023
1 parent fb4cbec commit 2264369
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 21 deletions.
27 changes: 27 additions & 0 deletions src/api/test/protectedApiClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,33 @@ describe('Protected API Client Tests', () => {
expect(result).toEqual(response);
});
});

describe('deleteSiteImage', () => {
it('makes the right request', async () => {
const response = 'Image Deleted Correctly';

nock(BASE_URL)
.post(ParameterizedApiRoutes.DELETE_IMAGE(1))
.reply(200, response);

const result = await ProtectedApiClient.deleteImage(1);

expect(result).toEqual(response);
});
it('makes a bad request', async () => {
const response = 'Invalid Image ID';

nock(BASE_URL)
.post(ParameterizedApiRoutes.DELETE_IMAGE(-1))
.reply(400, response);

const result = await ProtectedApiClient.deleteImage(-1).catch(
(err) => err.response.data,
);

expect(result).toEqual(response);
});
});
});

describe('updateSite', () => {
Expand Down
9 changes: 3 additions & 6 deletions src/components/careEntry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import { EditOutlined, DeleteOutlined } from '@ant-design/icons';
import { useDispatch, useSelector } from 'react-redux';
import { isAdmin, getUserID } from '../../auth/ducks/selectors';
import styled from 'styled-components';
import {
EditButton,
StyledClose,
} from '../themedComponents';
import { EditButton, StyledClose } from '../themedComponents';
import StewardshipForm from '../forms/stewardshipForm';
import { LinkButton } from '../linkButton';
import { useParams } from 'react-router-dom';
Expand All @@ -29,7 +26,7 @@ import { C4CState } from '../../store';
import { useTranslation } from 'react-i18next';
import { site } from '../../constants';
import { n } from '../../utils/stringFormat';
import ConfirmationModel from '../confirmationModal';
import ConfirmationModal from '../confirmationModal';

const Entry = styled.div`
margin: 15px;
Expand Down Expand Up @@ -179,7 +176,7 @@ const CareEntry: React.FC<CareEntryProps> = ({ activity }) => {
initialDate={treeCareToMoment(activity)}
/>
</Modal>
<ConfirmationModel
<ConfirmationModal
visible={showDeleteForm}
onOk={() => setShowDeleteForm(false)}
onCancel={() => setShowDeleteForm(false)}
Expand Down
6 changes: 3 additions & 3 deletions src/components/confirmationModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ConfirmDeleteButton = styled(Button)`
}
`;

interface ConfirmationModelProps {
interface ConfirmationModalProps {
visible: boolean;
onOk: () => void;
onCancel: () => void;
Expand All @@ -22,7 +22,7 @@ interface ConfirmationModelProps {
onConfirm: () => void;
}

const ConfirmationModel: React.FC<ConfirmationModelProps> = ({
const ConfirmationModal: React.FC<ConfirmationModalProps> = ({
visible,
onOk,
onCancel,
Expand All @@ -47,4 +47,4 @@ const ConfirmationModel: React.FC<ConfirmationModelProps> = ({
);
};

export default ConfirmationModel;
export default ConfirmationModal;
44 changes: 32 additions & 12 deletions src/components/treePage/siteImageCarousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import React, { useState } from 'react';
import { Carousel, message, Space } from 'antd';
import LeftOutlined from '@ant-design/icons/lib/icons/LeftOutlined';
import RightOutlined from '@ant-design/icons/lib/icons/RightOutlined';
import { isAdmin, getUserID } from '../../auth/ducks/selectors';
import { useSelector } from 'react-redux';
import { useDispatch } from 'react-redux';
import { useTranslation } from 'react-i18next';
import styled from 'styled-components';
import { getSiteData } from '../../containers/treePage/ducks/thunks';
import { TreeParams } from '../../containers/treePage';
import { useParams } from 'react-router-dom';

import { getLatestEntrySiteImages } from '../../containers/treePage/ducks/selectors';
import { C4CState } from '../../store';
Expand All @@ -13,7 +18,7 @@ import { site } from '../../constants';
import { LinkButton } from '../linkButton';
import { LIGHT_GREY, LIGHT_RED, WHITE } from '../../utils/colors';
import protectedApiClient from '../../api/protectedApiClient';
import ConfirmationModel from '../confirmationModal';
import ConfirmationModal from '../confirmationModal';

const CarouselContainer = styled.div`
margin-top: 20px;
Expand Down Expand Up @@ -69,6 +74,8 @@ export const DeleteSiteImageButton = styled(LinkButton)`

export const SiteImageCarousel: React.FC = () => {
const { t } = useTranslation(n(site, 'treePage'), { nsMode: 'fallback' });
const dispatch = useDispatch();
const id = Number(useParams<TreeParams>().id);

const [showDeleteForm, setShowDeleteForm] = useState<boolean>(false);

Expand All @@ -81,12 +88,22 @@ export const SiteImageCarousel: React.FC = () => {
const onAfterChange = (currentSlide: number) =>
setCurrSlideIndex(currentSlide);
function onClickDeleteImage(imageId: number) {
protectedApiClient.deleteImage(imageId).then((ok) => {
message.success('success');
setShowDeleteForm(false);
});
protectedApiClient
.deleteImage(imageId)
.then((ok) => {
message.success('success');
setShowDeleteForm(false);
})
.finally(() => dispatch(getSiteData(id)));
}

const userIsAdmin: boolean = useSelector((state: C4CState) =>
isAdmin(state.authenticationState.tokens),
);

const userId: number = useSelector((state: C4CState) =>
getUserID(state.authenticationState.tokens),
);
return (
<>
{latestEntrySiteImages.length > 0 && (
Expand Down Expand Up @@ -118,16 +135,19 @@ export const SiteImageCarousel: React.FC = () => {
t('site_image.no_upload_date')}
</div>
<div>
<DeleteSiteImageButton
type="primary"
onClick={() => setShowDeleteForm(!showDeleteForm)}
>
Delete
</DeleteSiteImageButton>
{(latestEntrySiteImages[currSlideIndex].uploaderId === userId ||
userIsAdmin) && (
<DeleteSiteImageButton
type="primary"
onClick={() => setShowDeleteForm(!showDeleteForm)}
>
Delete
</DeleteSiteImageButton>
)}
</div>
</Space>
</FooterContainer>
<ConfirmationModel
<ConfirmationModal
visible={showDeleteForm}
onOk={() => setShowDeleteForm(false)}
onCancel={() => setShowDeleteForm(false)}
Expand Down
1 change: 1 addition & 0 deletions src/containers/treePage/ducks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export interface SiteEntryImage {
uploaderUsername: string;
uploadedAt: string;
imageUrl: string;
uploaderId: number;
}

export interface TreeBenefits {
Expand Down
5 changes: 5 additions & 0 deletions src/containers/treePage/test/selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ describe('Tree Page Selectors', () => {
imageUrl: 'http://www.some-address.com',
uploadedAt: '09/06/2023',
uploaderUsername: 'First Last',
uploaderId: 1,
},
{
imageId: 2,
imageUrl: 'http://www.some-other-address.com',
uploadedAt: '01/01/2023',
uploaderUsername: 'Hello World',
uploaderId: 2,
},
],
},
Expand All @@ -147,6 +149,7 @@ describe('Tree Page Selectors', () => {
imageUrl: 'http://www.should-not-be-returned.com',
uploadedAt: '01/01/2022',
uploaderUsername: 'Code4Community',
uploaderId: 1,
},
],
},
Expand Down Expand Up @@ -281,12 +284,14 @@ describe('Tree Page Selectors', () => {
imageUrl: 'http://www.some-address.com',
uploadedAt: '09/06/2023',
uploaderUsername: 'First Last',
uploaderId: 1,
},
{
imageId: 2,
imageUrl: 'http://www.some-other-address.com',
uploadedAt: '01/01/2023',
uploaderUsername: 'Hello World',
uploaderId: 2,
},
];

Expand Down

0 comments on commit 2264369

Please sign in to comment.