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

Add edit, delete, create event to catalog #37

Merged
merged 7 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
moved the modals to separate components
  • Loading branch information
michellelin1 committed Feb 16, 2024
commit 3d4cec957b197b18033c6749b9a43633fabaf54a
86 changes: 42 additions & 44 deletions src/components/CreateEventForm/CreateEventForm.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable react/jsx-props-no-spreading */
/* eslint-disable react/prop-types */
import PropTypes from 'prop-types';
import {
Box,
FormLabel,
Expand All @@ -13,14 +12,8 @@ import {
} from '@chakra-ui/react';
import { yupResolver } from '@hookform/resolvers/yup';
import { useForm } from 'react-hook-form';
import axios from 'axios';
import * as yup from 'yup';
import React from 'react';

const AISSBackend = axios.create({
baseURL: import.meta.env.VITE_BACKEND_HOST,
withCredentials: true,
});
import { NPOBackend } from '../../utils/auth_utils';

const schema = yup.object({
// id: yup.string().required('ID required').max(10, 'ID exceeds 10 character limit'),
Expand All @@ -35,71 +28,51 @@ const schema = yup.object({
year: yup.string().required('Year required'),
});

const CreateEventForm = ({ event }) => {
const CreateEventForm = ({ eventData, setModified, closeModal }) => {
const toast = useToast();
const {
handleSubmit,
setValue,
register,
reset,
formState: { errors },
} = useForm({
resolver: yupResolver(schema),
defaultValues: { ...eventData },
});

let [editId, editTitle, editHost, editYear, editEventType, editSubject, editDescription] = [
null,
null,
null,
null,
null,
null,
null,
];

if (event) {
[editId, editTitle, editHost, editYear, editEventType, editSubject, editDescription] = event;
}

React.useEffect(() => {
if (event) {
Object.entries(event).forEach(([key, value]) => {
setValue(key, value);
});
}
}, [event, setValue]);

const submitData = async data => {
const { host, title, eventType, subject, description, year } = data;
toast.closeAll();

// make post request to catalog backend route
try {
let response;
if (event) {
response = await AISSBackend.put(`/catalog/${editId}`, {
let id;
if (eventData) {
response = await NPOBackend.put(`/catalog/${eventData.id}`, {
host: host,
title: title,
eventType: eventType,
subject: subject,
description: description,
year: year,
});
id = response.data[0].id
} else {
response = await AISSBackend.post(`/catalog`, {
response = await NPOBackend.post(`/catalog`, {
host: host,
title: title,
eventType: eventType,
subject: subject,
description: description,
year: year,
});
id = response.data.id
}
console.log(response);
reset();
toast({
title: 'Event submitted!',
description: `Event has been submitted. ID: ${response.data.id}`,
description: `Event has been submitted. ID: ${id}`,
status: 'success',
variant: 'subtle',
position: 'bottom',
Expand All @@ -109,6 +82,11 @@ const CreateEventForm = ({ event }) => {
duration: 3000,
isClosable: true,
});
if (setModified) {
setModified(true);
}
closeModal();

} catch (error) {
toast({
title: `Error: ${error}`,
Expand Down Expand Up @@ -141,7 +119,7 @@ const CreateEventForm = ({ event }) => {
<Box mb="4vh">
<FormControl isInvalid={errors && errors.host} width="80%">
<FormLabel fontWeight="bold">Host</FormLabel>
<Textarea {...register('host')} border="1px solid" defaultValue={editHost ?? ''} />
<Textarea {...register('host')} border="1px solid" />
<FormErrorMessage>{errors.host && errors.host.message}</FormErrorMessage>
</FormControl>
</Box>
Expand All @@ -150,7 +128,7 @@ const CreateEventForm = ({ event }) => {
<Box mb="4vh">
<FormControl isInvalid={errors && errors.title} width="80%">
<FormLabel fontWeight="bold">Title</FormLabel>
<Textarea {...register('title')} border="1px solid" defaultValue={editTitle ?? ''} />
<Textarea {...register('title')} border="1px solid" />
<FormErrorMessage>{errors.title && errors.title.message}</FormErrorMessage>
</FormControl>
</Box>
Expand All @@ -159,7 +137,7 @@ const CreateEventForm = ({ event }) => {
<Box mb="4vh">
<FormControl width="47%">
<FormLabel fontWeight="bold">Event Type</FormLabel>
<Select {...register('eventType')} defaultValue={editEventType ?? ''}>
<Select {...register('eventType')}>
<option value="guest speaker">Guest Speaker</option>
<option value="study-trip">Study Trip</option>
<option value="workshop">Workshop</option>
Expand All @@ -173,7 +151,7 @@ const CreateEventForm = ({ event }) => {
<Box mb="4vh">
<FormControl width="47%">
<FormLabel fontWeight="bold">Subject</FormLabel>
<Select {...register('subject')} defaultValue={editSubject ?? ''}>
<Select {...register('subject')}>
<option value="life skills">Life Skills</option>
<option value="science">Science</option>
<option value="technology">Technology</option>
Expand All @@ -192,7 +170,6 @@ const CreateEventForm = ({ event }) => {
<Input
{...register('description')}
border="1px solid"
defaultValue={editDescription ?? ''}
/>
<FormErrorMessage>
{errors.description && errors.description.message}
Expand All @@ -204,7 +181,7 @@ const CreateEventForm = ({ event }) => {
<Box mb="4vh">
<FormControl width="47%">
<FormLabel fontWeight="bold">Year</FormLabel>
<Select {...register('year')} defaultValue={editYear ?? ''}>
<Select {...register('year')} >
<option value="junior">Junior</option>
<option value="senior">Senior</option>
<option value="both">Both</option>
Expand All @@ -219,4 +196,25 @@ const CreateEventForm = ({ event }) => {
</Box>
);
};

CreateEventForm.propTypes = {
eventData: PropTypes.shape({
id: PropTypes.number,
title: PropTypes.string,
host: PropTypes.string,
year: PropTypes.number,
eventType: PropTypes.string,
subject: PropTypes.string,
description: PropTypes.string
}),
setModified: PropTypes.func,
closeModal:PropTypes.func,
}

CreateEventForm.defaultProps = {
eventData: undefined,
setModified: undefined,
closeModal: () => {},
};

export default CreateEventForm;
50 changes: 50 additions & 0 deletions src/components/CreateEventForm/CreateEventFormModal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import PropTypes from 'prop-types';
import {
Modal,
ModalBody,
ModalContent,
ModalOverlay,
ModalCloseButton,
} from '@chakra-ui/react';
import CreateEventForm from './CreateEventForm';

const CreateEventFormModal = ({isOpen, onClose, eventData, setModified}) => {
return (
<Modal isOpen={isOpen} onClose={onClose} size="full">
<ModalOverlay />
<ModalContent margin={0} rounded="none">
<ModalCloseButton />
<ModalBody>
{ eventData ? (
<CreateEventForm eventData={eventData} setModified={setModified} closeModal={onClose} />
) : (
<CreateEventForm setModified={setModified} closeModal={onClose} />
)}
</ModalBody>
</ModalContent>
</Modal>
);
}


CreateEventFormModal.propTypes = {
eventData: PropTypes.shape({
id: PropTypes.number,
title: PropTypes.string,
host: PropTypes.string,
year: PropTypes.number,
eventType: PropTypes.string,
subject: PropTypes.string,
description: PropTypes.string
}),
isOpen: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired,
setModified: PropTypes.func.isRequired,
}

CreateEventForm.defaultProps = {
eventData: undefined,
};


export default CreateEventFormModal;
48 changes: 48 additions & 0 deletions src/components/DeleteEventModal/DeleteEventModal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import PropTypes from 'prop-types';
import {
Button,
Modal,
ModalBody,
ModalContent,
ModalOverlay,
ModalHeader,
ModalCloseButton,
ModalFooter,
} from '@chakra-ui/react';
import { NPOBackend } from '../../utils/auth_utils';

const DeleteEventModal = ({ isOpen, onClose, deleteItemId, setModified }) => {
const handleConfirmDelete = async idToDelete => {
try {
await NPOBackend.delete(`/catalog/${idToDelete}`);
setModified(true);
onClose();
} catch (error) {
console.error(error);
}
};

return (
<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalHeader>Confirm Delete</ModalHeader>
<ModalCloseButton />
<ModalBody>Are you sure you want to delete this row?</ModalBody>
<ModalFooter>
<Button onClick={() => handleConfirmDelete(deleteItemId)}>Delete</Button>
<Button onClick={onClose}>Cancel</Button>
</ModalFooter>
</ModalContent>
</Modal>
);
}

DeleteEventModal.propTypes = {
isOpen: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired,
deleteItemId: PropTypes.number.isRequired,
setModified: PropTypes.func.isRequired,
}

export default DeleteEventModal;
Loading