|
| 1 | +/* eslint-disable react/jsx-props-no-spreading */ |
| 2 | +import * as Mantine from '@mantine/core'; |
| 3 | +import { DatePicker } from '@mantine/dates'; |
| 4 | +import { useForm, useToggle } from '@mantine/hooks'; |
| 5 | +import { showNotification } from '@mantine/notifications'; |
| 6 | +import PropTypes from 'prop-types'; |
| 7 | +import React, { useState } from 'react'; |
| 8 | +import { Check, Calendar, X } from 'tabler-icons-react'; |
| 9 | + |
| 10 | +import convertDateToDBFormat from '../../../utils/datefns/convertDate'; |
| 11 | +import BarChart from '../../analytics/BarChart'; |
| 12 | +import PieChart from '../../analytics/PieChart'; |
| 13 | +import { |
| 14 | + defaultTableSchema, |
| 15 | + formInputFields, |
| 16 | + formDateFields, |
| 17 | +} from '../../../utils/schema/analyticsFormSchema'; |
| 18 | +import prepareBarChart from '../../../utils/analytics/prepareBarChart'; |
| 19 | +import preparePieChart from '../../../utils/analytics/preparePieChart'; |
| 20 | +import getAnalyticsData from '../../../utils/api/getAnalyticsData'; |
| 21 | + |
| 22 | +function AnalyticsForm({ setOpened, setAnalyticsButtonDisabled }) { |
| 23 | + const form = useForm({ |
| 24 | + initialValues: { ...defaultTableSchema }, |
| 25 | + validate: {}, |
| 26 | + }); |
| 27 | + |
| 28 | + const [modalOpened, setModalOpened] = useState(false); |
| 29 | + const [pieChartData, setPieChartData] = useState([{ id: '', label: 'null', value: 0 }]); |
| 30 | + const [barChartData, setBarChartData] = useState([{ id: '', label: 'null', value: 0 }]); |
| 31 | + const [chartToggle, setChartToggle] = useToggle('pie', ['pie', 'bar']); |
| 32 | + |
| 33 | + const handleFormSubmission = async (values) => { |
| 34 | + // Loop through formInputFields |
| 35 | + formInputFields.forEach((field) => { |
| 36 | + const fieldValue = form.getInputProps(field.htmlFor)?.value || ''; |
| 37 | + // If fieldValue is null or undefined, store null |
| 38 | + defaultTableSchema[field.htmlFor] = fieldValue; |
| 39 | + }); |
| 40 | + |
| 41 | + // Loop through formDateFields |
| 42 | + formDateFields.forEach((field) => { |
| 43 | + if (field.type === 'db_date') { |
| 44 | + defaultTableSchema[field.htmlFor] = convertDateToDBFormat(values[field.htmlFor.toString()]); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + try { |
| 49 | + // Submit defaultTableSchema to the database |
| 50 | + /* |
| 51 | + Successful data submission example |
| 52 | + 0: {rowsAffected: 1} |
| 53 | + 1: {rows: 48583} |
| 54 | + 2: {rows: 6} |
| 55 | + 3: {rows: 1084} |
| 56 | + */ |
| 57 | + |
| 58 | + const payload = { |
| 59 | + clear_date: [defaultTableSchema.clear_date_start, defaultTableSchema.clear_date_end], |
| 60 | + due_in_date: [defaultTableSchema.due_in_date_start, defaultTableSchema.due_in_date_end], |
| 61 | + baseline_create_date: [ |
| 62 | + defaultTableSchema.baseline_create_date_start, |
| 63 | + defaultTableSchema.baseline_create_date_end, |
| 64 | + ], |
| 65 | + invoice_currency: defaultTableSchema.invoice_currency, |
| 66 | + }; |
| 67 | + const analyticsData = await getAnalyticsData(payload); |
| 68 | + if (analyticsData.length > 0) { |
| 69 | + setPieChartData(preparePieChart({ data: analyticsData })); |
| 70 | + setBarChartData(prepareBarChart({ data: analyticsData })); |
| 71 | + setModalOpened(true); // Open the modal toggling the state of it. |
| 72 | + |
| 73 | + setAnalyticsButtonDisabled(false); |
| 74 | + showNotification({ |
| 75 | + title: 'Alert!', |
| 76 | + message: `The analytics data are ready!`, |
| 77 | + color: 'teal', |
| 78 | + autoClose: 10000, |
| 79 | + disallowClose: false, |
| 80 | + icon: <Check size={18} />, |
| 81 | + }); |
| 82 | + } else { |
| 83 | + setAnalyticsButtonDisabled(false); |
| 84 | + showNotification({ |
| 85 | + title: 'Alert!', |
| 86 | + message: 'The server returned error.', |
| 87 | + color: 'red', |
| 88 | + disallowClose: false, |
| 89 | + icon: <X size={18} />, |
| 90 | + }); |
| 91 | + } |
| 92 | + } catch (e) { |
| 93 | + console.error(e); |
| 94 | + showNotification({ |
| 95 | + title: 'Alert!', |
| 96 | + message: e.message, |
| 97 | + color: 'red', |
| 98 | + disallowClose: false, |
| 99 | + icon: <X size={18} />, |
| 100 | + }); |
| 101 | + } |
| 102 | + }; |
| 103 | + const [shouldSubmitBeDisabled, setShouldSubmitBeDisabled] = React.useState(true); |
| 104 | + |
| 105 | + return ( |
| 106 | + <> |
| 107 | + <Mantine.Box sx={{ maxWidth: 300 }} mx="auto" oncl> |
| 108 | + <form onSubmit={form.onSubmit((values) => handleFormSubmission(values))}> |
| 109 | + <Mantine.Divider my="sm" variant="dashed" label="Get Insights" labelPosition="center" /> |
| 110 | + |
| 111 | + <Mantine.ScrollArea style={{ height: 550 }} className="px-5"> |
| 112 | + {formInputFields.map((field) => { |
| 113 | + switch (field.type) { |
| 114 | + case 'number': |
| 115 | + return ( |
| 116 | + <Mantine.NumberInput |
| 117 | + key={field.htmlFor} |
| 118 | + required={false} |
| 119 | + {...field} |
| 120 | + {...form.getInputProps(field.htmlFor)} |
| 121 | + radius="md" |
| 122 | + /> |
| 123 | + ); |
| 124 | + default: |
| 125 | + return ( |
| 126 | + <Mantine.TextInput |
| 127 | + key={field.htmlFor} |
| 128 | + required={false} |
| 129 | + {...field} |
| 130 | + {...form.getInputProps(field.htmlFor)} |
| 131 | + radius="md" |
| 132 | + /> |
| 133 | + ); |
| 134 | + } |
| 135 | + })} |
| 136 | + |
| 137 | + {/* Date Fields */} |
| 138 | + {formDateFields.map((field) => ( |
| 139 | + <DatePicker |
| 140 | + key={field.htmlFor} |
| 141 | + placeholder="Pick date" |
| 142 | + required={false} |
| 143 | + icon={<Calendar size={16} />} |
| 144 | + {...field} |
| 145 | + {...form.getInputProps(field.htmlFor)} |
| 146 | + allowLevelChange |
| 147 | + firstDayOfWeek="sunday" |
| 148 | + allowFreeInput |
| 149 | + dateParser={(dateString) => new Date(Date.parse(dateString))} |
| 150 | + /> |
| 151 | + ))} |
| 152 | + |
| 153 | + <Mantine.Checkbox |
| 154 | + mt="md" |
| 155 | + label="I agree to the truthness of the above data." |
| 156 | + color="orange" |
| 157 | + key="checkConsent" |
| 158 | + htmlFor="checkConsent" |
| 159 | + onClick={() => setShouldSubmitBeDisabled(!shouldSubmitBeDisabled)} |
| 160 | + {...form.getInputProps('checkConsent', { type: 'checkbox' })} |
| 161 | + /> |
| 162 | + <Mantine.Group position="right" mt="md"> |
| 163 | + <Mantine.Button |
| 164 | + type="submit" |
| 165 | + color="orange" |
| 166 | + className="bg-orange-400" |
| 167 | + disabled={shouldSubmitBeDisabled} |
| 168 | + > |
| 169 | + Submit |
| 170 | + </Mantine.Button> |
| 171 | + </Mantine.Group> |
| 172 | + </Mantine.ScrollArea> |
| 173 | + </form> |
| 174 | + </Mantine.Box> |
| 175 | + <Mantine.Modal |
| 176 | + opened={modalOpened} |
| 177 | + onClose={() => { |
| 178 | + setModalOpened(false); |
| 179 | + setOpened(false); // Close the drawer |
| 180 | + }} |
| 181 | + title={<Mantine.Title order={2}>Analytics View</Mantine.Title>} |
| 182 | + size="70%" |
| 183 | + > |
| 184 | + <Mantine.Button |
| 185 | + onClick={() => setChartToggle()} |
| 186 | + color="blue" |
| 187 | + className=" justify-center items-center mb-8 w-1/2 text-center bg-orange-400 hover:bg-orange-300" |
| 188 | + > |
| 189 | + {chartToggle.toUpperCase()} |
| 190 | + </Mantine.Button> |
| 191 | + <Mantine.Paper className="h-96 bg-lime-200"> |
| 192 | + {chartToggle === 'pie' && <PieChart data={pieChartData} />} |
| 193 | + {chartToggle === 'bar' && <BarChart data={barChartData} />} |
| 194 | + </Mantine.Paper> |
| 195 | + </Mantine.Modal> |
| 196 | + </> |
| 197 | + ); |
| 198 | +} |
| 199 | + |
| 200 | +// Props Validation |
| 201 | +AnalyticsForm.propTypes = { |
| 202 | + setOpened: PropTypes.func.isRequired, |
| 203 | + setAnalyticsButtonDisabled: PropTypes.func.isRequired, |
| 204 | +}; |
| 205 | + |
| 206 | +export default AnalyticsForm; |
0 commit comments