Skip to content
Open
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
43 changes: 38 additions & 5 deletions pages/admin/sponsors/add.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,9 +15,40 @@ function isAuthorized(user: any): boolean {
export default function AddSponsorPage() {
const { user, isSignedIn } = useAuthContext();
const router = useRouter();
const [error, setError] = useState<string | null>(null);
const [existingSponsors, setExistingSponsors] = useState<Sponsor[]>([]);

// fetch existing sponsors on component mount
React.useEffect(() => {
const fetchSponsors = async () => {
try {
const { data, status } = await RequestHelper.get<Sponsor[]>('/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<Sponsor, { msg?: string }>(
'/api/sponsors',
{
Expand All @@ -28,22 +60,22 @@ 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;
}

if (status >= 200 && status < 300) {
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);
}
};
Expand All @@ -62,6 +94,7 @@ export default function AddSponsorPage() {
</div>
</Link>
</div>
{error && <div className="text-red-500 text-sm mt-1">{error}</div>}
<div className="mt-4">
<SponsorForm
formAction="Add"
Expand Down
44 changes: 40 additions & 4 deletions pages/admin/sponsors/edit/[name].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import SponsorForm from '@/components/adminComponents/sponsorComponents/AdminSpo
import { RequestHelper } from '@/lib/request-helper';
import { useAuthContext } from '@/lib/user/AuthContext';
import { Sponsor } from '../index';
import { useState, useEffect } from 'react';

interface EditSponsorPageProps {
sponsor: Sponsor;
Expand All @@ -15,9 +16,43 @@ export default function EditSponsorPage({ sponsor }: EditSponsorPageProps) {
const { user, isSignedIn } = useAuthContext();
const router = useRouter();
const { name: originalName } = router.query;
const [error, setError] = useState<string | null>(null);
const [existingSponsors, setExistingSponsors] = useState<Sponsor[]>([]);

// fetch all sponsors on component mount
useEffect(() => {
const fetchSponsors = async () => {
try {
const { data, status } = await RequestHelper.get<Sponsor[]>('/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<any, { msg?: string }>(
'/api/sponsors',
{
Expand All @@ -32,22 +67,22 @@ 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;
}

if (status >= 200 && status < 300) {
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);
}
};
Expand All @@ -66,6 +101,7 @@ export default function EditSponsorPage({ sponsor }: EditSponsorPageProps) {
</div>
</Link>
</div>
{error && <div className="text-red-500 text-sm mt-1">{error}</div>}
<div className="mt-4">
<SponsorForm
sponsor={sponsor}
Expand Down