|
| 1 | +import classNames from 'classnames'; |
| 2 | +import Button from 'components/common/Button'; |
| 3 | +import DateFilter from 'components/common/DateFilter'; |
| 4 | +import DropDown from 'components/common/DropDown'; |
| 5 | +import FormLayout, { |
| 6 | + FormButtons, |
| 7 | + FormError, |
| 8 | + FormMessage, |
| 9 | + FormRow, |
| 10 | +} from 'components/layout/FormLayout'; |
| 11 | +import DataTable from 'components/metrics/DataTable'; |
| 12 | +import FilterTags from 'components/metrics/FilterTags'; |
| 13 | +import { Field, Form, Formik } from 'formik'; |
| 14 | +import useApi from 'hooks/useApi'; |
| 15 | +import useDateRange from 'hooks/useDateRange'; |
| 16 | +import { useState, useEffect } from 'react'; |
| 17 | +import { FormattedMessage } from 'react-intl'; |
| 18 | +import styles from './EventDataForm.module.css'; |
| 19 | +import useTimezone from 'hooks/useTimezone'; |
| 20 | + |
| 21 | +export const filterOptions = [ |
| 22 | + { label: 'Count', value: 'count' }, |
| 23 | + { label: 'Average', value: 'avg' }, |
| 24 | + { label: 'Minimum', value: 'min' }, |
| 25 | + { label: 'Maxmimum', value: 'max' }, |
| 26 | + { label: 'Sum', value: 'sum' }, |
| 27 | +]; |
| 28 | + |
| 29 | +export const dateOptions = [ |
| 30 | + { label: <FormattedMessage id="label.today" defaultMessage="Today" />, value: '1day' }, |
| 31 | + { |
| 32 | + label: ( |
| 33 | + <FormattedMessage id="label.last-hours" defaultMessage="Last {x} hours" values={{ x: 24 }} /> |
| 34 | + ), |
| 35 | + value: '24hour', |
| 36 | + }, |
| 37 | + { |
| 38 | + label: <FormattedMessage id="label.yesterday" defaultMessage="Yesterday" />, |
| 39 | + value: '-1day', |
| 40 | + }, |
| 41 | + { |
| 42 | + label: <FormattedMessage id="label.this-week" defaultMessage="This week" />, |
| 43 | + value: '1week', |
| 44 | + divider: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + label: ( |
| 48 | + <FormattedMessage id="label.last-days" defaultMessage="Last {x} days" values={{ x: 7 }} /> |
| 49 | + ), |
| 50 | + value: '7day', |
| 51 | + }, |
| 52 | + { |
| 53 | + label: <FormattedMessage id="label.this-month" defaultMessage="This month" />, |
| 54 | + value: '1month', |
| 55 | + divider: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + label: ( |
| 59 | + <FormattedMessage id="label.last-days" defaultMessage="Last {x} days" values={{ x: 30 }} /> |
| 60 | + ), |
| 61 | + value: '30day', |
| 62 | + }, |
| 63 | + { |
| 64 | + label: ( |
| 65 | + <FormattedMessage id="label.last-days" defaultMessage="Last {x} days" values={{ x: 90 }} /> |
| 66 | + ), |
| 67 | + value: '90day', |
| 68 | + }, |
| 69 | + { label: <FormattedMessage id="label.this-year" defaultMessage="This year" />, value: '1year' }, |
| 70 | + { |
| 71 | + label: <FormattedMessage id="label.custom-range" defaultMessage="Custom range" />, |
| 72 | + value: 'custom', |
| 73 | + divider: true, |
| 74 | + }, |
| 75 | +]; |
| 76 | + |
| 77 | +export default function EventDataForm({ websiteId, onClose, className }) { |
| 78 | + const { post } = useApi(); |
| 79 | + const [message, setMessage] = useState(); |
| 80 | + const [columns, setColumns] = useState({}); |
| 81 | + const [filters, setFilters] = useState({}); |
| 82 | + const [data, setData] = useState([]); |
| 83 | + const [dateRange, setDateRange] = useDateRange('report'); |
| 84 | + const { startDate, endDate, value } = dateRange; |
| 85 | + const [timezone] = useTimezone(); |
| 86 | + const [isValid, setIsValid] = useState(false); |
| 87 | + |
| 88 | + useEffect(() => { |
| 89 | + if (Object.keys(columns).length > 0) { |
| 90 | + setIsValid(true); |
| 91 | + } else { |
| 92 | + setIsValid(false); |
| 93 | + } |
| 94 | + }, [columns]); |
| 95 | + |
| 96 | + const handleAddTag = (value, list, setState, resetForm) => { |
| 97 | + setState({ ...list, [`${value.field}`]: value.value }); |
| 98 | + resetForm(); |
| 99 | + }; |
| 100 | + |
| 101 | + const handleRemoveTag = (value, list, setState) => { |
| 102 | + const newList = { ...list }; |
| 103 | + |
| 104 | + delete newList[`${value}`]; |
| 105 | + |
| 106 | + setState(newList); |
| 107 | + }; |
| 108 | + |
| 109 | + const handleSubmit = async () => { |
| 110 | + const params = { |
| 111 | + website_id: websiteId, |
| 112 | + start_at: +startDate, |
| 113 | + end_at: +endDate, |
| 114 | + timezone, |
| 115 | + columns, |
| 116 | + filters, |
| 117 | + }; |
| 118 | + |
| 119 | + const { ok, data } = await post(`/websites/${websiteId}/eventdata`, params); |
| 120 | + |
| 121 | + if (!ok) { |
| 122 | + setMessage(<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />); |
| 123 | + setData([]); |
| 124 | + } else { |
| 125 | + setData(data); |
| 126 | + setMessage(null); |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + return ( |
| 131 | + <> |
| 132 | + <FormMessage>{message}</FormMessage> |
| 133 | + <div className={classNames(styles.container, className)}> |
| 134 | + <div className={styles.form}> |
| 135 | + <FormLayout> |
| 136 | + <div className={styles.filters}> |
| 137 | + <FormRow> |
| 138 | + <label htmlFor="date-range"> |
| 139 | + <FormattedMessage id="label.date-range" defaultMessage="Date Range" /> |
| 140 | + </label> |
| 141 | + <DateFilter |
| 142 | + value={value} |
| 143 | + startDate={startDate} |
| 144 | + endDate={endDate} |
| 145 | + onChange={setDateRange} |
| 146 | + options={dateOptions} |
| 147 | + /> |
| 148 | + </FormRow> |
| 149 | + </div> |
| 150 | + <div className={styles.filters}> |
| 151 | + <Formik |
| 152 | + initialValues={{ field: '', value: '' }} |
| 153 | + onSubmit={(value, { resetForm }) => |
| 154 | + handleAddTag(value, columns, setColumns, resetForm) |
| 155 | + } |
| 156 | + > |
| 157 | + {({ values, setFieldValue }) => ( |
| 158 | + <Form> |
| 159 | + <FormRow> |
| 160 | + <label htmlFor="field"> |
| 161 | + <FormattedMessage id="label.field-name" defaultMessage="Field Name" /> |
| 162 | + </label> |
| 163 | + <div> |
| 164 | + <Field name="field" type="text" /> |
| 165 | + <FormError name="field" /> |
| 166 | + </div> |
| 167 | + </FormRow> |
| 168 | + <FormRow> |
| 169 | + <label htmlFor="value"> |
| 170 | + <FormattedMessage id="label.type" defaultMessage="Type" /> |
| 171 | + </label> |
| 172 | + <div> |
| 173 | + <DropDown |
| 174 | + value={values.value} |
| 175 | + onChange={value => setFieldValue('value', value)} |
| 176 | + className={styles.dropdown} |
| 177 | + name="value" |
| 178 | + options={filterOptions} |
| 179 | + /> |
| 180 | + <FormError name="value" /> |
| 181 | + </div> |
| 182 | + </FormRow> |
| 183 | + <FormButtons className={styles.formButtons}> |
| 184 | + <Button |
| 185 | + variant="action" |
| 186 | + type="submit" |
| 187 | + disabled={!values.field || !values.value} |
| 188 | + > |
| 189 | + <FormattedMessage id="label.add-column" defaultMessage="Add Column" /> |
| 190 | + </Button> |
| 191 | + </FormButtons> |
| 192 | + </Form> |
| 193 | + )} |
| 194 | + </Formik> |
| 195 | + <FilterTags |
| 196 | + className={styles.filterTag} |
| 197 | + params={columns} |
| 198 | + onClick={value => handleRemoveTag(value, columns, setColumns)} |
| 199 | + /> |
| 200 | + </div> |
| 201 | + <div className={styles.filters}> |
| 202 | + <Formik |
| 203 | + initialValues={{ field: '', value: '' }} |
| 204 | + onSubmit={(value, { resetForm }) => |
| 205 | + handleAddTag(value, filters, setFilters, resetForm) |
| 206 | + } |
| 207 | + > |
| 208 | + {({ values }) => ( |
| 209 | + <Form> |
| 210 | + <FormRow> |
| 211 | + <label htmlFor="field"> |
| 212 | + <FormattedMessage id="label.field-name" defaultMessage="Field Name" /> |
| 213 | + </label> |
| 214 | + <div> |
| 215 | + <Field name="field" type="text" /> |
| 216 | + <FormError name="field" /> |
| 217 | + </div> |
| 218 | + </FormRow> |
| 219 | + <FormRow> |
| 220 | + <label htmlFor="value"> |
| 221 | + <FormattedMessage id="label.value" defaultMessage="Value" /> |
| 222 | + </label> |
| 223 | + <div> |
| 224 | + <Field name="value" type="text" /> |
| 225 | + <FormError name="value" /> |
| 226 | + </div> |
| 227 | + </FormRow> |
| 228 | + <FormButtons className={styles.formButtons}> |
| 229 | + <Button |
| 230 | + variant="action" |
| 231 | + type="submit" |
| 232 | + disabled={!values.field || !values.value} |
| 233 | + > |
| 234 | + <FormattedMessage id="label.add-filter" defaultMessage="Add Filter" /> |
| 235 | + </Button> |
| 236 | + </FormButtons> |
| 237 | + </Form> |
| 238 | + )} |
| 239 | + </Formik> |
| 240 | + <FilterTags |
| 241 | + className={styles.filterTag} |
| 242 | + params={filters} |
| 243 | + onClick={value => handleRemoveTag(value, filters, setFilters)} |
| 244 | + /> |
| 245 | + </div> |
| 246 | + </FormLayout> |
| 247 | + </div> |
| 248 | + <div> |
| 249 | + <DataTable className={styles.table} data={data} title="Results" showPercentage={false} /> |
| 250 | + </div> |
| 251 | + </div> |
| 252 | + <FormButtons> |
| 253 | + <Button variant="action" onClick={handleSubmit} disabled={!isValid}> |
| 254 | + <FormattedMessage id="label.search" defaultMessage="Search" /> |
| 255 | + </Button> |
| 256 | + <Button onClick={onClose}> |
| 257 | + <FormattedMessage id="label.cancel" defaultMessage="Cancel" /> |
| 258 | + </Button> |
| 259 | + </FormButtons> |
| 260 | + </> |
| 261 | + ); |
| 262 | +} |
0 commit comments