diff --git a/pages/admin/sponsors/add.tsx b/pages/admin/sponsors/add.tsx index b71842d..6cea989 100644 --- a/pages/admin/sponsors/add.tsx +++ b/pages/admin/sponsors/add.tsx @@ -1,10 +1,11 @@ -import React from 'react'; +import React, { useState } from 'react'; import { useAuthContext } from '@/lib/user/AuthContext'; import { RequestHelper } from '@/lib/request-helper'; import { useRouter } from 'next/router'; import Link from 'next/link'; import ChevronLeftIcon from '@mui/icons-material/ChevronLeft'; import SponsorForm from '@/components/adminComponents/sponsorComponents/AdminSponsorForm'; +import { Sponsor } from './index'; function isAuthorized(user: any): boolean { if (!user || !user.permissions) return false; @@ -14,9 +15,40 @@ function isAuthorized(user: any): boolean { export default function AddSponsorPage() { const { user, isSignedIn } = useAuthContext(); const router = useRouter(); + const [error, setError] = useState(null); + const [existingSponsors, setExistingSponsors] = useState([]); + + // fetch existing sponsors on component mount + React.useEffect(() => { + const fetchSponsors = async () => { + try { + const { data, status } = await RequestHelper.get('/api/sponsors', {}); + if (status >= 200 && status < 300) { + setExistingSponsors(data); + } + } catch (error) { + console.error('Error fetching sponsors:', error); + } + }; + + fetchSponsors(); + }, []); + + const checkDuplicateName = (name: string): boolean => { + return existingSponsors.some((sponsor) => sponsor.name.toLowerCase() === name.toLowerCase()); + }; const submitAddSponsorRequest = async (sponsorData: Sponsor) => { try { + // check for duplicate name + if (checkDuplicateName(sponsorData.name)) { + setError( + `A sponsor with the name "${sponsorData.name}" already exists. Please use a different name.`, + ); + return; + } + + setError(null); const { data, status } = await RequestHelper.post( '/api/sponsors', { @@ -28,11 +60,11 @@ export default function AddSponsorPage() { ); if (status === 403) { - alert('You do not have the permission to perform this functionality'); + setError('You do not have the permission to perform this functionality'); return; } if (status >= 400) { - alert(`Unexpected HTTP error: ${status}`); + setError(`Unexpected HTTP error: ${status}`); return; } @@ -40,10 +72,10 @@ export default function AddSponsorPage() { alert('Sponsor created successfully!'); router.push('/admin/sponsors'); } else { - alert(`There was an error: ${data?.msg}`); + setError(`There was an error: ${data?.msg}`); } } catch (error) { - alert('Unexpected error! Please try again'); + setError('Unexpected error! Please try again'); console.error(error); } }; @@ -62,6 +94,7 @@ export default function AddSponsorPage() { + {error &&
{error}
}
(null); + const [existingSponsors, setExistingSponsors] = useState([]); + + // fetch all sponsors on component mount + useEffect(() => { + const fetchSponsors = async () => { + try { + const { data, status } = await RequestHelper.get('/api/sponsors', {}); + if (status >= 200 && status < 300) { + setExistingSponsors(data); + } + } catch (error) { + console.error('Error fetching sponsors:', error); + } + }; + + fetchSponsors(); + }, []); + + const checkDuplicateName = (newName: string): boolean => { + // check if the new name already exists, excluding the current sponsor + return existingSponsors.some( + (s) => s.name.toLowerCase() === newName.toLowerCase() && s.name !== originalName, + ); + }; const submitEditSponsorRequest = async (sponsorData: Sponsor) => { try { + // check for duplicate name only if the name has changed + if (sponsorData.name !== originalName && checkDuplicateName(sponsorData.name)) { + setError( + `A sponsor with the name "${sponsorData.name}" already exists. Please use a different name.`, + ); + return; + } + + setError(null); const { data, status } = await RequestHelper.post( '/api/sponsors', { @@ -32,11 +67,11 @@ export default function EditSponsorPage({ sponsor }: EditSponsorPageProps) { ); if (status === 403) { - alert('You do not have the permission to perform this functionality'); + setError('You do not have the permission to perform this functionality'); return; } if (status >= 400) { - alert(`Unexpected HTTP error: ${status}`); + setError(`Unexpected HTTP error: ${status}`); return; } @@ -44,10 +79,10 @@ export default function EditSponsorPage({ sponsor }: EditSponsorPageProps) { alert('Sponsor updated successfully!'); router.push('/admin/sponsors'); } else { - alert(`There was an error: ${data?.msg}`); + setError(`There was an error: ${data?.msg}`); } } catch (error) { - alert('Unexpected error! Please try again'); + setError('Unexpected error! Please try again'); console.error(error); } }; @@ -66,6 +101,7 @@ export default function EditSponsorPage({ sponsor }: EditSponsorPageProps) {
+ {error &&
{error}
}