diff --git a/frontend/src/pages/EditPlant/index.tsx b/frontend/src/pages/EditPlant/index.tsx index bf2fa95..f656f3f 100644 --- a/frontend/src/pages/EditPlant/index.tsx +++ b/frontend/src/pages/EditPlant/index.tsx @@ -4,11 +4,12 @@ import { NewPlant, Plant, addPlant, + deletePlant, getPlantById, updatePlant, } from '../../services/plantsService'; import { SubmitHandler, useForm } from 'react-hook-form'; -import { FaSeedling } from 'react-icons/fa6'; +import { FaSeedling, FaTrash } from 'react-icons/fa6'; import { Toaster } from 'react-hot-toast'; import { formatDate } from '../../utils/dateUtils'; import SubmitButton from '../../components/UI/SubmitButton'; @@ -24,6 +25,7 @@ const EditPlant = () => { const { user } = useAuth(); const { setHouseholds } = useHouseholds(); + const plantBeingEdited = searchParams.get('plant'); const { register, handleSubmit, @@ -33,7 +35,6 @@ const EditPlant = () => { useEffect(() => { const fetchPlant = async () => { - const plantBeingEdited = searchParams.get('plant'); if (plantBeingEdited) { const plant = await getPlantById(plantBeingEdited); setPlant(plant); @@ -59,7 +60,6 @@ const EditPlant = () => { if (user) { let response; const updatedData = { ...data, lastWateredBy: user.firstName }; - console.log(updatedData); if (plant) { response = await updatePlant(plant.id, householdId!, updatedData); navigate(`/${householdId}/plants/${response?.id}?saved=true`); @@ -73,6 +73,17 @@ const EditPlant = () => { } }; + const deletePlantFromHousehold = async () => { + if (plantBeingEdited) { + await deletePlant(plantBeingEdited); + navigate(`/${householdId}/plants/?deletedPlant=${plant?.name}`); + } + }; + + const closeModal = (id: string) => { + (document.getElementById(id) as HTMLDialogElement | null)?.close(); + }; + return (
{ }, }} /> -

- {plant ? plant.name : 'Add New Plant'} -

+ + {/* Delete plant modal */} + +
+

+ Would you like to delete {plant?.name}? +

+
+ + +
+
+
+ +
+ {plant ? ( +

{plant.name}

+ ) : ( + <> +

Add New Plant

+ + + )} +
+
{ const { householdId, plantId } = useParams(); const [searchParams] = useSearchParams(); const [plant, setPlant] = useState(null); const [isLoading, setIsLoading] = useState(true); + const [error, setError] = useState(false); const { user } = useAuth(); useEffect(() => { const fetchData = async () => { const saved = searchParams.get('saved'); const created = searchParams.get('created'); - if (plantId) { - const plant = await getPlantById(plantId); - setPlant(plant); - if (saved === 'true') { - toast.success(`${plant!.name} has been successfully saved`, { - id: 'save', - }); - } - if (created === 'true') { - toast.success(`${plant!.name} has been successfully created`, { - id: 'created', - }); + + try { + if (plantId) { + const plant = await getPlantById(plantId); + setPlant(plant); + if (saved === 'true') { + toast.success(`${plant!.name} has been successfully saved`, { + id: 'save', + }); + } + if (created === 'true') { + toast.success(`${plant!.name} has been successfully created`, { + id: 'created', + }); + } } + } catch (error) { + setError(true); + } finally { + setIsLoading(false); } - setIsLoading(false); }; fetchData(); }, [plantId, searchParams]); @@ -112,9 +120,21 @@ const PlantPage = () => {
- {isLoading && viewLoadingSkeleton()} + {/* Check for error state */} + {error && ( +
+ +

+ An error occured while fetching plant +

+ + + +
+ )} + {plant && (
{ const { households, setHouseholds } = useHouseholds(); const { user } = useAuth(); const { householdId } = useParams(); + const [searchParams] = useSearchParams(); + const navigate = useNavigate(); const users = households?.find((h) => h.id === householdId)?.users; const householdName = households?.find((h) => h.id === householdId)?.name; useEffect(() => { + const deletedPlant = searchParams.get('deletedPlant'); + if (deletedPlant) { + toast.success(`${deletedPlant} has been successfully deleted`, { + id: 'delete-plant', + }); + } + const fetchData = async () => { try { if (householdId) { diff --git a/frontend/src/services/plantsService.ts b/frontend/src/services/plantsService.ts index 9f4fd98..61e6ee6 100644 --- a/frontend/src/services/plantsService.ts +++ b/frontend/src/services/plantsService.ts @@ -83,3 +83,8 @@ export const updatePlant = async ( ); return response.data; }; + +export const deletePlant = async (plantId: string): Promise => { + const response = await api.delete(`${plantsUrlEndpoint}/${plantId}`); + return response.data; +};