-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRoutineCreator.tsx
177 lines (159 loc) · 5.12 KB
/
RoutineCreator.tsx
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import React, { useState } from 'react';
import { CirclePicker, ColorResult } from 'react-color';
import { FormProvider, useForm } from 'react-hook-form';
import {
Button,
Drawer,
DrawerBody,
DrawerCloseButton,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerOverlay,
Flex,
Input,
Text,
useToast,
} from '@chakra-ui/react';
import { IEmojiData } from 'emoji-picker-react';
import { Routine, routinesService } from './database/RoutinesService';
import { EmojiPicker } from './EmojiPicker';
import { JobScheduler } from './JobScheduler';
import { NotificationScheduler } from './NotificationScheduler';
const getHourAndMinute = (time: string | Date): [string, string] => {
if (typeof time === 'string') {
const [hour, minute] = time.split(':');
return [hour, minute];
}
return [time.getHours().toString(), time.getMinutes().toString()];
};
const getScheduleDays = (days: string | string[]): string => {
return typeof days === 'string' ? days : days.join(',');
};
type Props = {
isOpen: boolean;
onClose: () => void;
};
export enum ScheduleFrequency {
Daily = 'Daily',
Weekly = 'Weekly',
}
type FormValues = {
title: string;
scheduleDays: string | string[];
timeOfDay: 'custom' | string;
customTime: Date;
scheduleFrequency: ScheduleFrequency;
};
const defaultFormValues = {
title: '',
scheduleFrequency: ScheduleFrequency.Daily,
scheduleDays: ['1', '2', '3', '4', '5'],
timeOfDay: '9:00',
customTime: new Date(),
};
export const RoutineCreator: React.FC<Props> = ({ isOpen, onClose }) => {
const toast = useToast();
const methods = useForm<FormValues>({
defaultValues: defaultFormValues,
});
const { register, handleSubmit, errors, reset } = methods;
const [emoji, setEmoji] = useState<IEmojiData>();
const [color, setColor] = useState('#22506d');
const handleColorChange = (color: ColorResult) => {
setColor(color.hex);
};
const onSubmit = async (form: FormValues) => {
const { title, timeOfDay, customTime } = form;
const time = timeOfDay === 'custom' ? customTime : timeOfDay;
const [hour, minute] = getHourAndMinute(time);
const scheduleDays = getScheduleDays(form.scheduleDays);
const scheduleAt = `${minute} ${hour} * * ${scheduleDays}`;
const routine: Routine = {
title,
color,
scheduleAt,
emoji: emoji?.emoji,
};
// Save to Database
await routinesService.createRoutine(routine);
// Schedule Notification
const job = new JobScheduler(routine);
job.schedule();
// Display the toast
toast({
position: 'bottom',
title: 'Yay!',
description: 'Reminder scheduled successfully ✅',
status: 'success',
duration: 3210,
isClosable: true,
});
// Reset to default values and close the form
reset(defaultFormValues);
onClose();
};
return (
<Drawer isOpen={isOpen} placement="bottom" size="full" onClose={onClose}>
<DrawerOverlay>
<DrawerContent>
<DrawerCloseButton color="white" />
<DrawerHeader background={color} color="white">
New Routine
</DrawerHeader>
<DrawerBody>
<FormProvider {...methods}>
<form onSubmit={handleSubmit(onSubmit)} id="formCreator">
<Flex direction="column">
<Flex direction="column" my={4}>
<Text mb={4} fontWeight="bold">
Name your routine
</Text>
<Flex>
<EmojiPicker onEmojiSelected={(emoji) => setEmoji(emoji)} />
<Input
focusBorderColor={color}
type="text"
name="title"
isInvalid={errors.title != null}
ref={register({ required: true })}
autoFocus
placeholder="My Awesome Routine"
/>
</Flex>
{errors.title && (
<Text textColor="red.500" ml={1}>
Name is required
</Text>
)}
</Flex>
<Flex my={4} justifyContent="center" direction="column" width="100%">
<Text mb={2} fontWeight="bold">
Pick a color
</Text>
<CirclePicker
onChangeComplete={handleColorChange}
circleSpacing={8}
width="100%"
/>
</Flex>
</Flex>
<Flex my={4} direction="column">
<NotificationScheduler />
</Flex>
</form>
</FormProvider>
</DrawerBody>
<DrawerFooter>
<Button variant="outline" mr={3} onClick={onClose}>
Cancel
</Button>
<Button backgroundColor={color} colorScheme="white" form="formCreator" type="submit">
Save
</Button>
</DrawerFooter>
</DrawerContent>
</DrawerOverlay>
</Drawer>
);
};