refactor event creation api

This commit is contained in:
Michael Fatemi 2021-07-01 00:25:48 -04:00
parent 66b5c8aff6
commit 7dc5e92c46

View File

@ -26,9 +26,11 @@ const DAY_NAMES = [
function DaysOfWeekSelector({ function DaysOfWeekSelector({
daysOfWeek, daysOfWeek,
update, update,
disabled = false,
}: { }: {
daysOfWeek: number; daysOfWeek: number;
update: Dispatch<SetStateAction<number>>; update: Dispatch<SetStateAction<number>>;
disabled?: boolean;
}) { }) {
const toggleDayOfWeek = useCallback( const toggleDayOfWeek = useCallback(
function (idx: 1 | 2 | 3 | 4 | 5 | 6 | 7) { function (idx: 1 | 2 | 3 | 4 | 5 | 6 | 7) {
@ -53,7 +55,11 @@ function DaysOfWeekSelector({
style={{ style={{
borderRadius: '100%', borderRadius: '100%',
cursor: 'pointer', cursor: 'pointer',
backgroundColor: active ? green : lightgrey, backgroundColor: active
? disabled
? green + '77'
: green
: lightgrey,
color: active ? 'white' : 'black', color: active ? 'white' : 'black',
userSelect: 'none', userSelect: 'none',
width: '2em', width: '2em',
@ -68,6 +74,7 @@ function DaysOfWeekSelector({
// @ts-ignore // @ts-ignore
toggleDayOfWeek(idx + 1) toggleDayOfWeek(idx + 1)
} }
key={name}
> >
{name.charAt(0)} {name.charAt(0)}
</div> </div>
@ -99,16 +106,26 @@ export default function EventCreator({ group }: { group: IGroup }) {
const createEvent = useCallback(() => { const createEvent = useCallback(() => {
if (!creating) { if (!creating) {
if (startTime === null) {
console.warn(
'Tried to create an event where the start time was unspecified.'
);
return;
}
const duration =
endTime !== null ? (endTime.getTime() - startTime.getTime()) / 60 : 0;
setCreating(true); setCreating(true);
post('/events', { post('/events', {
name, name,
startTime, startTime,
endTime, duration,
endDate,
groupId: group.id, groupId: group.id,
placeId, placeId,
recurring,
daysOfWeek, daysOfWeek,
endDate,
}) })
.then((response) => response.json()) .then((response) => response.json())
.then(({ id }) => { .then(({ id }) => {
@ -123,7 +140,6 @@ export default function EventCreator({ group }: { group: IGroup }) {
endTime, endTime,
group.id, group.id,
placeId, placeId,
recurring,
daysOfWeek, daysOfWeek,
endDate, endDate,
]); ]);