diff --git a/frontend/app/components/Dashboard/components/WidgetForm/WidgetForm.tsx b/frontend/app/components/Dashboard/components/WidgetForm/WidgetForm.tsx index 85676e8ed..bc3a67651 100644 --- a/frontend/app/components/Dashboard/components/WidgetForm/WidgetForm.tsx +++ b/frontend/app/components/Dashboard/components/WidgetForm/WidgetForm.tsx @@ -3,126 +3,128 @@ import { metricOf, issueOptions } from 'App/constants/filterOptions'; import { FilterKey } from 'Types/filter/filterType'; import { useStore } from 'App/mstore'; import { observer } from 'mobx-react-lite'; -import { Button, Icon } from 'UI' +import { Button, Icon } from 'UI'; import FilterSeries from '../FilterSeries'; import { confirm, Tooltip, Input } from 'UI'; -import Select from 'Shared/Select' -import { withSiteId, dashboardMetricDetails, metricDetails } from 'App/routes' +import Select from 'Shared/Select'; +import { withSiteId, dashboardMetricDetails, metricDetails } from 'App/routes'; import MetricTypeDropdown from './components/MetricTypeDropdown'; import MetricSubtypeDropdown from './components/MetricSubtypeDropdown'; -import { TIMESERIES, TABLE, CLICKMAP } from 'App/constants/card' -import { pageUrlOperators } from 'App/constants/filterOptions' +import { TIMESERIES, TABLE, CLICKMAP } from 'App/constants/card'; +import { pageUrlOperators } from 'App/constants/filterOptions'; import FilterAutoComplete from 'Shared/Filters/FilterAutoComplete'; import { clickmapFilter } from 'App/types/filter/newFilter'; -import { toJS } from 'mobx' +import { toJS } from 'mobx'; +import { List } from 'immutable'; interface Props { - history: any; - match: any; - onDelete: () => void; + history: any; + match: any; + onDelete: () => void; } const metricIcons = { - timeseries: 'graph-up', - table: 'table', - funnel: 'funnel', -} + timeseries: 'graph-up', + table: 'table', + funnel: 'funnel', +}; function WidgetForm(props: Props) { + const { + history, + match: { + params: { siteId, dashboardId }, + }, + } = props; + const { metricStore, dashboardStore } = useStore(); + const isSaving = metricStore.isSaving; + const metric: any = metricStore.instance; - const { history, match: { params: { siteId, dashboardId } } } = props; - const { metricStore, dashboardStore } = useStore(); - const isSaving = metricStore.isSaving; - const metric: any = metricStore.instance + const timeseriesOptions = metricOf.filter((i) => i.type === 'timeseries'); + const tableOptions = metricOf.filter((i) => i.type === 'table'); + const isTable = metric.metricType === 'table'; + const isFunnel = metric.metricType === 'funnel'; + const canAddSeries = metric.series.length < 3; + const eventsLength = metric.series[0].filter.filters.filter((i: any) => i.isEvent).length; + const cannotSaveFunnel = isFunnel && (!metric.series[0] || eventsLength <= 1); - const timeseriesOptions = metricOf.filter(i => i.type === 'timeseries'); - const tableOptions = metricOf.filter(i => i.type === 'table'); - const isTable = metric.metricType === 'table'; - const isFunnel = metric.metricType === 'funnel'; - const canAddSeries = metric.series.length < 3; - const eventsLength = metric.series[0].filter.filters.filter((i: any) => i.isEvent).length - const cannotSaveFunnel = isFunnel && (!metric.series[0] || eventsLength <= 1); + const writeOption = ({ value, name }: any) => { + value = Array.isArray(value) ? value : value.value; + const obj: any = { [name]: value }; - const writeOption = ({ value, name }: any) => { - value = Array.isArray(value) ? value : value.value - const obj: any = { [ name ]: value }; + if (name === 'metricValue') { + obj['metricValue'] = value; - if (name === 'metricValue') { - obj['metricValue'] = value; - - // handle issues (remove all when other option is selected) - if (Array.isArray(obj['metricValue']) && obj['metricValue'].length > 1) { - obj['metricValue'] = obj['metricValue'].filter(i => i.value !== 'all'); - } - } - - if (name === 'metricOf') { - // if (value === FilterKey.ISSUE) { - // obj['metricValue'] = [{ value: 'all', label: 'All' }]; - // } - } - - if (name === 'metricType') { - if (value === TIMESERIES) { - obj['metricOf'] = timeseriesOptions[0].value; - obj['viewType'] = 'lineChart'; - } else if (value === TABLE) { - obj['metricOf'] = tableOptions[0].value; - obj['viewType'] = 'table'; - } - if (value === CLICKMAP) { - obj['viewType'] = 'chart' - } - } - - metricStore.merge(obj); - }; - - const onSelect = (_: any, option: Record) => writeOption({ value: { value: option.value }, name: option.name}) - - const onSave = () => { - const wasCreating = !metric.exists() - metricStore.save(metric, dashboardId) - .then((metric: any) => { - if (wasCreating) { - if (parseInt(dashboardId) > 0) { - history.replace(withSiteId(dashboardMetricDetails(dashboardId, metric.metricId), siteId)); - const dashboard = dashboardStore.getDashboard(parseInt(dashboardId)) - dashboardStore.addWidgetToDashboard(dashboard, [metric.metricId]) - } else { - history.replace(withSiteId(metricDetails(metric.metricId), siteId)); - } - } - }); + // handle issues (remove all when other option is selected) + if (Array.isArray(obj['metricValue']) && obj['metricValue'].length > 1) { + obj['metricValue'] = obj['metricValue'].filter((i) => i.value !== 'all'); + } } - const onDelete = async () => { - if (await confirm({ - header: 'Confirm', - confirmButton: 'Yes, delete', - confirmation: `Are you sure you want to permanently delete this metric?` - })) { - metricStore.delete(metric).then(props.onDelete); - } + if (name === 'metricOf') { + // if (value === FilterKey.ISSUE) { + // obj['metricValue'] = [{ value: 'all', label: 'All' }]; + // } } - const updateClickMapURL = (_, item) => { - console.log('updating filter', item) - const newValues = { - value: item - } - metric.series[0].filter.updateFilter(0, newValues) - console.log(toJS(metric.series)) - } - console.log(metric.series, metric.series[0].filter) - return ( -
-
- -
- - - {/* {metric.metricType === 'timeseries' && ( + if (name === 'metricType') { + if (value === TIMESERIES) { + obj['metricOf'] = timeseriesOptions[0].value; + obj['viewType'] = 'lineChart'; + } else if (value === TABLE) { + obj['metricOf'] = tableOptions[0].value; + obj['viewType'] = 'table'; + } + if (value === CLICKMAP) { + obj['viewType'] = 'chart'; + if (metric.series[0].filter.filters.length < 1) { + metric.series[0].filter.addFilter({ + ...clickmapFilter, + value: [''], + },) + } + } + } + metricStore.merge(obj); + }; + + const onSave = () => { + const wasCreating = !metric.exists(); + metricStore.save(metric, dashboardId).then((metric: any) => { + if (wasCreating) { + if (parseInt(dashboardId) > 0) { + history.replace(withSiteId(dashboardMetricDetails(dashboardId, metric.metricId), siteId)); + const dashboard = dashboardStore.getDashboard(parseInt(dashboardId)); + dashboardStore.addWidgetToDashboard(dashboard, [metric.metricId]); + } else { + history.replace(withSiteId(metricDetails(metric.metricId), siteId)); + } + } + }); + }; + + const onDelete = async () => { + if ( + await confirm({ + header: 'Confirm', + confirmButton: 'Yes, delete', + confirmation: `Are you sure you want to permanently delete this metric?`, + }) + ) { + metricStore.delete(metric).then(props.onDelete); + } + }; + + console.log(toJS(metric)); + return ( +
+
+ +
+ + + + {/* {metric.metricType === 'timeseries' && ( <> of )} */} - {metric.metricOf === FilterKey.ISSUE && ( - <> - issue type - + + )} - {metric.metricType === 'table' && !(metric.metricOf === FilterKey.ERRORS || metric.metricOf === FilterKey.SESSIONS) && ( - <> - showing - null } - /> - {/* */} - -
- )} -
- -
-
- {`${(isTable || isFunnel) ? 'Filter by' : 'Chart Series'}`} - {!isTable && !isFunnel && ( - - )} -
- - {metric.series.length > 0 && metric.series.slice(0, (isTable || isFunnel) ? 1 : metric.series.length).map((series: any, index: number) => ( -
- metric.updateKey('hasChanged', true)} - hideHeader={ isTable } - seriesIndex={index} - series={series} - onRemoveSeries={() => metric.removeSeries(index)} - canDelete={metric.series.length > 1} - emptyMessage={isTable ? - 'Filter data using any event or attribute. Use Add Step button below to do so.' : - 'Add user event or filter to define the series by clicking Add Step.' - } - /> -
- ))} -
- -
- - - -
- {metric.exists() && ( - - )} -
-
+ {metric.metricType === 'table' && + !(metric.metricOf === FilterKey.ERRORS || metric.metricOf === FilterKey.SESSIONS) && ( + <> + showing +