From 35861202ca3a6357e8515fb12c36d301d8e0398d Mon Sep 17 00:00:00 2001 From: Jojo Ortiz Date: Tue, 20 Aug 2024 18:11:49 -0700 Subject: [PATCH] [feat] add delete report functionality --- .../sustainabilityReport/deleteReports.ts | 24 +++++++++++++++++++ .../SustainabilityReport/FileTag.tsx | 21 ++++++++++++++-- .../SustainabilityReport/FilesContainer.tsx | 4 ++-- .../SustainabilityReportDeleteModal.tsx | 22 +++++++++++++---- .../useSustainabilityReportDeleteModal.ts | 4 ++++ app/types/SustainabilityReportTypes.ts | 21 ++++++++++++++++ 6 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 app/actions/sustainabilityReport/deleteReports.ts diff --git a/app/actions/sustainabilityReport/deleteReports.ts b/app/actions/sustainabilityReport/deleteReports.ts new file mode 100644 index 0000000..ee1cf48 --- /dev/null +++ b/app/actions/sustainabilityReport/deleteReports.ts @@ -0,0 +1,24 @@ +import axios from 'axios'; + +interface IParams { + userId: string; + projectId: string; + reportIds: string[]; +} + +export const deleteReports = async ({ userId, projectId, reportIds }: IParams) => { + const data = { + userId: userId, + projectId: projectId, + reportIds: reportIds, + }; + return await axios + .patch(process.env.NEXT_PUBLIC_PORTFOLIO_INSIGHT_API_URL + '/delete-reports', data) + .then((response) => { + return response.data; + }) + .catch((error) => { + console.error('Error adding attribute. Please try again.'); + throw error; + }); +}; diff --git a/app/components/SustainabilityReport/FileTag.tsx b/app/components/SustainabilityReport/FileTag.tsx index f444761..72c94db 100644 --- a/app/components/SustainabilityReport/FileTag.tsx +++ b/app/components/SustainabilityReport/FileTag.tsx @@ -1,6 +1,8 @@ import React from 'react'; import { Typography } from '@material-tailwind/react'; import { Report } from '@/app/types/SustainabilityReportTypes'; +import useSustainabilityReportDeleteModal from '@/app/hooks/sustainabilityReport/useSustainabilityReportDeleteModal'; +import { X } from '@phosphor-icons/react'; function truncateMiddle(text: string, maxLength = 25) { if (text.length <= maxLength) return text; @@ -11,14 +13,29 @@ function truncateMiddle(text: string, maxLength = 25) { interface FileTagProps { report: Report; + projectId: string; } -const FileTag = ({ report }: FileTagProps) => { +const FileTag = ({ report, projectId }: FileTagProps) => { + const sustainabilityReportDeleteModal = useSustainabilityReportDeleteModal(); + + const handleDeleteReport = () => { + console.log('Deleting report:', report); + sustainabilityReportDeleteModal.setProjectId(projectId); + sustainabilityReportDeleteModal.setDeleteItem(report); + sustainabilityReportDeleteModal.onOpen(); + }; return ( -
+
{truncateMiddle(report.name, 25)} +
+ +
); }; diff --git a/app/components/SustainabilityReport/FilesContainer.tsx b/app/components/SustainabilityReport/FilesContainer.tsx index 52afa17..673e43a 100644 --- a/app/components/SustainabilityReport/FilesContainer.tsx +++ b/app/components/SustainabilityReport/FilesContainer.tsx @@ -19,14 +19,14 @@ const FilesContainer = ({ reports, projectId }: FilesContainerProps) => { return (
{reports.map((report, i) => ( - + ))}
diff --git a/app/components/modals/sustainabilityReport/SustainabilityReportDeleteModal.tsx b/app/components/modals/sustainabilityReport/SustainabilityReportDeleteModal.tsx index 94e98ab..5c32a7d 100644 --- a/app/components/modals/sustainabilityReport/SustainabilityReportDeleteModal.tsx +++ b/app/components/modals/sustainabilityReport/SustainabilityReportDeleteModal.tsx @@ -2,6 +2,7 @@ import { useState } from 'react'; import { FieldValues, SubmitHandler, useForm } from 'react-hook-form'; +import { isAttribute, isReport } from '@/app/types/SustainabilityReportTypes'; import FormModal from '../FormModal'; import Heading from '../../Heading'; @@ -10,12 +11,14 @@ import { Typography } from '@material-tailwind/react'; import useSustainabilityReportDeleteModal from '@/app/hooks/sustainabilityReport/useSustainabilityReportDeleteModal'; import useSustainabilityStore from '@/app/hooks/sustainabilityReport/sustainabilityReportStore'; import { deleteAttribute } from '@/app/actions/sustainabilityReport/deleteAttribute'; +import { deleteReports } from '@/app/actions/sustainabilityReport/deleteReports'; +import useFetchSustainabilityData from '@/app/hooks/sustainabilityReport/useFetchSustainabilityData'; const SustainabilityReportDeleteModal = () => { const SustainabilityReportDeleteModal = useSustainabilityReportDeleteModal(); const [isLoading, setIsLoading] = useState(false); const { userId, deleteStoreAttribute } = useSustainabilityStore(); - + const { fetchAttributesThenProjects } = useFetchSustainabilityData(); const { handleSubmit, reset } = useForm({}); const onSubmit: SubmitHandler = async () => { @@ -25,14 +28,25 @@ const SustainabilityReportDeleteModal = () => { if (!deleteItemId) { throw new Error('Delete ID is missing'); } - await deleteAttribute({ userId, attributeId: deleteItemId }); - deleteStoreAttribute(deleteItemId); + if (isAttribute(SustainabilityReportDeleteModal.deleteItem)) { + await deleteAttribute({ userId, attributeId: deleteItemId }); + deleteStoreAttribute(deleteItemId); + } else if (isReport(SustainabilityReportDeleteModal.deleteItem)) { + await deleteReports({ + userId, + projectId: SustainabilityReportDeleteModal.projectId, + reportIds: [deleteItemId], + }); + console.log('deleting report', JSON.stringify(SustainabilityReportDeleteModal.deleteItem)); + } SustainabilityReportDeleteModal.onClose(); + fetch; } catch (error) { toast.error('Delete deleteItem failed. Please try again.'); } finally { setIsLoading(false); reset(); + fetchAttributesThenProjects(); } }; @@ -47,7 +61,7 @@ const SustainabilityReportDeleteModal = () => {
- Are you sure you want to delete this deleteItem? + {`Are you sure you want to delete ${SustainabilityReportDeleteModal.deleteItem?.name}?`}
diff --git a/app/hooks/sustainabilityReport/useSustainabilityReportDeleteModal.ts b/app/hooks/sustainabilityReport/useSustainabilityReportDeleteModal.ts index 75a7374..5d5a9d7 100644 --- a/app/hooks/sustainabilityReport/useSustainabilityReportDeleteModal.ts +++ b/app/hooks/sustainabilityReport/useSustainabilityReportDeleteModal.ts @@ -4,17 +4,21 @@ import { create } from 'zustand'; interface SustainabilityReportDeleteModalStore { isOpen: boolean; deleteItem: Attribute | Report | null; + projectId: string; onOpen: () => void; onClose: () => void; setDeleteItem: (deleteItem: Attribute | Report) => void; + setProjectId: (projectId: string) => void; } const useSustainabilityReportDeleteModal = create((set) => ({ isOpen: false, deleteItem: null, + projectId: '', onOpen: () => set({ isOpen: true }), onClose: () => set({ isOpen: false }), setDeleteItem: (deleteItem: Attribute | Report) => set({ deleteItem }), + setProjectId: (projectId: string) => set({ projectId }), })); export default useSustainabilityReportDeleteModal; diff --git a/app/types/SustainabilityReportTypes.ts b/app/types/SustainabilityReportTypes.ts index 78f0813..6fb5ed7 100644 --- a/app/types/SustainabilityReportTypes.ts +++ b/app/types/SustainabilityReportTypes.ts @@ -44,3 +44,24 @@ export type RawReport = { uploadedTimestamp: string; userId: string; }; + +export function isReport(obj: any): obj is Report { + return obj && typeof obj.id === 'string' && typeof obj.name === 'string'; +} + +export function isProject(obj: any): obj is Project { + return ( + obj && + typeof obj.id === 'string' && + typeof obj.name === 'string' && + typeof obj.description === 'string' && + typeof obj.projectResults === 'object' && + Array.isArray(obj.reports) && + obj.reports.every(isReport) && + Object.values(GenerationStatus).includes(obj.status) + ); +} + +export function isAttribute(obj: any): obj is Attribute { + return obj && typeof obj.id === 'string' && typeof obj.name === 'string' && typeof obj.description === 'string'; +}