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

Validation on AddEvent #70

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
33 changes: 29 additions & 4 deletions src/components/event/AddEventPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,15 @@ const AddEventPage = () => {
setFormData({ ...formData, date });
};

const earlyBirdDateOnChange = (earlyBirdAccessDate: Dayjs | null) => {
setFormData({ ...formData, earlyBirdAccessDate });
};

const addEventMutation = useMutation({
mutationFn: async () => {
await addEvent(token, formData);
return await addEvent(token, formData);
},
onSuccess: async () => {
onSuccess: async (newEvent) => {
await queryClient.invalidateQueries({
queryKey: ['events', 'active', { page: 1 }],
exact: true,
Expand All @@ -97,11 +101,32 @@ const AddEventPage = () => {
earlyBirdAccessDate: dayjs().add(1, 'day'),
});
toast.success('Added the event successfully');
if (newEvent && newEvent.id) {
router.push(`/event/view/${newEvent.id}`);
} else {
toast.error('Failed to retrieve the new event ID');
}
},
onError: (error) => {
console.error('Error in mutation:', error); // Log the error
toast.error(error.message);
},
});

const onSubmit = async (event: React.FormEvent) => {
event.preventDefault();
if (!formData.description.trim() && !formData.title.trim()) {
toast.error('Event title and description are required.');
return;
}
if (!formData.title.trim()) {
toast.error('Event title is required.');
return;
}
if (!formData.description.trim()) {
toast.error('Event description is required.');
return;
}
addEventMutation.mutate();
};

Expand Down Expand Up @@ -178,7 +203,7 @@ const AddEventPage = () => {
Boolean(formData.hasEarlyBirdAccess) && (
<DatePicker
value={formData.earlyBirdAccessDate}
onChange={dateOnChange}
onChange={earlyBirdDateOnChange}
label="Early Bird Access Date"
name="earlyBirdAccessDate"
/>
Expand All @@ -202,4 +227,4 @@ const AddEventPage = () => {
);
};

export default AddEventPage;
export default AddEventPage;
4 changes: 2 additions & 2 deletions src/components/event/EventPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export default EventPage;

const getEventTitle = (eventType: 'active' | 'inactive') => {
return eventType === 'active'
? 'List of Active Events'
: 'List of Inactive Events';
? 'Active Events'
: 'Inactive Events';
};

const getFetchFunction = (eventType: 'active' | 'inactive') => {
Expand Down
1 change: 0 additions & 1 deletion src/utilities/fetch/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ export const addEvent = async (token: string, event: AddEditEventDTO) => {
throw new Error('Error in adding the event');
}
const data = await response.json();

return data;
};

Expand Down