Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete survey button to answers page #119

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ type DeleteSurveyModalProps = {
surveyId: string;
isOpened: boolean;
closeModal: () => void;
onSuccess?: () => void;
};

export default function DeleteSurveyModal({
surveyId,
isOpened,
closeModal,
onSuccess,
}: DeleteSurveyModalProps) {
const { deleteSurvey, isRemoving } = useRemoveSurvey();

Expand Down Expand Up @@ -42,7 +44,7 @@ export default function DeleteSurveyModal({
</Button>
<Button
variant={ButtonVariant.DANGER}
onClick={deleteSurvey(surveyId, closeModal)}
onClick={deleteSurvey(surveyId, closeModal, onSuccess)}
icon={<TrashIcon className="h-5 w-5" />}
className="uppercase"
isLoading={isRemoving}
Expand Down
22 changes: 9 additions & 13 deletions src/features/surveys/components/SurveyRow/SurveyRow.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useState } from 'react';
import { LinkIcon, TrashIcon } from '@heroicons/react/outline';

import { useRouter } from 'next/router';
import useCopyToClipboard from 'shared/hooks/useCopyToClipboard';
import Button, { ButtonVariant } from 'shared/components/Button/Button';

import DeleteSurveyModal from 'features/surveys/components/DeleteSurveyModal/DeleteSurveyModal';
import useModal from 'features/surveys/hooks/useModal';

interface SurveyRowProps {
question: string;
Expand All @@ -20,15 +20,11 @@ export default function SurveyRow({
}: SurveyRowProps) {
const { copy } = useCopyToClipboard();
const navigate = useRouter();
const [isOpen, setIsOpen] = useState(false);

function closeDeleteModal() {
setIsOpen(false);
}

function openDeleteModal() {
setIsOpen(true);
}
const {
isModalOpen: isDeleteSurveyModalOpen,
closeModal: closeDeleteSurveyModal,
openModal: openDeleteSurveyModal,
} = useModal();

const handleCopyLink = () => {
const domain =
Expand Down Expand Up @@ -73,14 +69,14 @@ export default function SurveyRow({
variant={ButtonVariant.DANGER}
title="Delete survey"
className="mt-2 ml-2 w-full justify-center px-3 sm:mt-0 md:w-auto"
onClick={openDeleteModal}
onClick={openDeleteSurveyModal}
icon={<TrashIcon className="h-5 w-5" />}
/>
</div>
<DeleteSurveyModal
surveyId={id}
closeModal={closeDeleteModal}
isOpened={isOpen}
closeModal={closeDeleteSurveyModal}
isOpened={isDeleteSurveyModalOpen}
/>
</div>
);
Expand Down
19 changes: 19 additions & 0 deletions src/features/surveys/hooks/useModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useCallback, useState } from 'react';

export default function useModal() {
const [isModalOpen, setIsModalOpen] = useState(false);

const closeModal = useCallback(() => {
setIsModalOpen(false);
}, [setIsModalOpen]);

const openModal = useCallback(() => {
setIsModalOpen(true);
}, [setIsModalOpen]);

return {
isModalOpen,
closeModal,
openModal,
};
}
37 changes: 20 additions & 17 deletions src/features/surveys/hooks/useRemoveSurvey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@ export const useRemoveSurvey = () => {
const [isRemoving, setIsRemoving] = useState(false);

const deleteSurvey =
(id: string, closeDeleteModal: () => void) => async () => {
setIsRemoving(true);
try {
await deleteDoc(doc(db, 'surveys', id));
(id: string, closeDeleteModal: () => void, onSuccess?: () => void) =>
async () => {
setIsRemoving(true);
try {
await deleteDoc(doc(db, 'surveys', id));

const answersCollection = await getDocs(
query(collection(db, 'surveys', id, 'answers'))
);
const answersCollection = await getDocs(
query(collection(db, 'surveys', id, 'answers'))
);

answersCollection.forEach(async (answer) => {
await deleteDoc(answer.ref);
});
answersCollection.forEach(async (answer) => {
await deleteDoc(answer.ref);
});

closeDeleteModal();
toast.success('Survey deleted');
} catch (error) {
toast.error('Error deleting survey');
}
setIsRemoving(false);
};
if (onSuccess) onSuccess();
toast.success('Survey deleted');
} catch (error) {
toast.error('Error deleting survey');
} finally {
closeDeleteModal();
setIsRemoving(false);
}
};

return {
deleteSurvey,
Expand Down
30 changes: 29 additions & 1 deletion src/pages/survey/answer/[surveyId]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LinkIcon, RefreshIcon } from '@heroicons/react/outline';
import { LinkIcon, RefreshIcon, TrashIcon } from '@heroicons/react/outline';
import Head from 'next/head';
import withAnimation from 'shared/HOC/withAnimation';
import withProtectedRoute from 'shared/HOC/withProtectedRoute';
Expand All @@ -8,8 +8,13 @@ import Loader from 'shared/components/Loader/Loader';
import AnswerTableRow from 'features/surveys/components/AnswerTableRow/AnswerTableRow';
import { useSurveyResultsManager } from 'features/surveys/managers/surveyResultsManager';
import Button, { ButtonVariant } from 'shared/components/Button/Button';
import useModal from 'features/surveys/hooks/useModal';
import DeleteSurveyModal from 'features/surveys/components/DeleteSurveyModal/DeleteSurveyModal';
import { useRouter } from 'next/router';
import { useCallback } from 'react';

function SurveyResultsPage() {
const router = useRouter();
const {
isLoading,
title,
Expand All @@ -21,6 +26,16 @@ function SurveyResultsPage() {
votes,
createDate,
} = useSurveyResultsManager();
const {
isModalOpen: isDeleteSurveyModalOpen,
closeModal: closeDeleteSurveyModal,
openModal: openDeleteSurveyModal,
} = useModal();

const navigateToSurveys = useCallback(
() => router.push('/surveys'),
[router]
);

return (
<>
Expand Down Expand Up @@ -51,6 +66,13 @@ function SurveyResultsPage() {
>
Refresh
</Button>
<Button
variant={ButtonVariant.DANGER}
title="Delete survey"
className="mt-2 justify-center px-3 sm:ml-4 sm:mt-0"
onClick={openDeleteSurveyModal}
icon={<TrashIcon className="h-5 w-5" />}
/>
</div>

<hr className=" md:hidden" />
Expand All @@ -75,6 +97,12 @@ function SurveyResultsPage() {
)}
</div>
)}
<DeleteSurveyModal
surveyId={surveyId}
closeModal={closeDeleteSurveyModal}
isOpened={isDeleteSurveyModalOpen}
onSuccess={navigateToSurveys}
/>
</>
);
}
Expand Down