-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathcreateNewEvent.jsx
More file actions
110 lines (101 loc) · 3.24 KB
/
createNewEvent.jsx
File metadata and controls
110 lines (101 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React, { useState } from 'react';
import { useSnackbar } from '../../context/snackbarContext';
import '../../sass/ManageProjects.scss';
import { findNextOccuranceOfDay } from './utilities/findNextDayOccuranceOfDay';
import { addDurationToTime } from './utilities/addDurationToTime';
import { timeConvertFromForm } from './utilities/timeConvertFromForm';
import validateEventForm from './utilities/validateEventForm';
import EventForm from './eventForm';
const CreateNewEvent = ({
projectToEdit,
projectID,
createNewRecurringEvent,
setIsCreateNew,
}) => {
// These are the initial form values
const initialFormValues = {
name: '',
eventType: 'Team Meeting',
description: '',
videoConferenceLink: '',
day: '0',
startTime: '7:00pm',
duration: '1',
};
const [formValues, setFormValues] = useState(initialFormValues);
const [formErrors, setFormErrors] = useState({});
const { showSnackbar } = useSnackbar();
// Handle form input changes
const handleInputChange = (event) => {
setFormValues({ ...formValues, [event.target.name]: event.target.value });
};
const handleEventCreate = () => {
const date = findNextOccuranceOfDay(formValues.day);
const startTimeDate = timeConvertFromForm(date, formValues.startTime);
const endTime = addDurationToTime(startTimeDate, formValues.duration);
// convert to ISO and GMT
const startDateTimeGMT = new Date(startTimeDate).toISOString();
const endTimeGMT = new Date(endTime).toISOString();
const createdDate = new Date().toISOString();
const updatedDate = new Date().toISOString();
// This is the new event object
// Certain values - like 'location' - are hard-coded here as they are constant
const theNewEvent = {
name: formValues.name,
location: {
city: 'Los Angeles',
state: 'CA',
country: 'USA',
},
hacknight: 'Online',
brigade: 'Hack for LA',
eventType: formValues.eventType,
description: formValues.description,
project: projectID,
date: startDateTimeGMT,
startTime: startDateTimeGMT,
endTime: endTimeGMT,
hours: formValues.duration.toString(),
createdDate,
updatedDate,
checkInReady: false,
videoConferenceLink: formValues.videoConferenceLink,
};
createNewRecurringEvent(theNewEvent);
};
// Handle submission of new recurring event form
const handleFormSubmit = async () => {
const errors = validateEventForm(formValues, projectToEdit);
if (!errors) {
handleEventCreate();
setFormValues(initialFormValues);
setIsCreateNew(false);
showSnackbar('Event created!', 'success');
}
setFormErrors(errors);
};
return (
<div>
<EventForm
handleInputChange={handleInputChange}
formValues={formValues}
formErrors={formErrors}
setFormErrors={setFormErrors}
title="Create New Recurring Event"
>
<div className="button-box">
<button
type="button"
className="create-form-button"
onClick={() => {
handleFormSubmit();
}}
>
Create New Event
</button>
</div>
</EventForm>
</div>
);
};
export default CreateNewEvent;