diff --git a/src/components/Controller.tsx b/src/components/Controller.tsx index 1a34f32..d2d6b9f 100644 --- a/src/components/Controller.tsx +++ b/src/components/Controller.tsx @@ -1,4 +1,4 @@ -import { useState } from 'react' +import { useState, useEffect } from 'react' import axios from 'axios' import { useRouter } from 'next/router' import type { SelectChangeEvent } from '@mui/material/Select' @@ -30,44 +30,46 @@ export default function Controller( { isDefaultCollapsed = false, documentOptions, - perspectiveOptions, + graphTypeOptions, graphOptions, - onDocumentChange, + onWorkbookChange, onYRangeChange, - onPerspectiveChange, + onGraphTypeChange, onAddGraph, onWarning }: { isDefaultCollapsed: boolean, documentOptions: Array, - perspectiveOptions: Array, + graphTypeOptions: Array, graphOptions: Array, - onDocumentChange: (d: string) => void, + onWorkbookChange: (d: string) => void, onYRangeChange: (y: boolean) => void, - onPerspectiveChange: (p: string) => void, + onGraphTypeChange: (p: string) => void, onAddGraph: (g: string) => void onWarning: (w: string) => void, } ) { + useEffect(()=>{ + // this is a limited solution. if the workbook is changed and the graph type is clients + // or employees, the graph is not selected + changeGraphType('clients') + // eslint-disable-next-line + }, []) + const SPECIAL_DISPLAY = ['client_trends', 'employee_trends'] const [collapsed, setCollapsed] = useState(isDefaultCollapsed) - const [documentSelect, setDocument] = useState(documentOptions[0]) - + const [workbook, setWorkbook] = useState(documentOptions[0]) + const [searchInp, setSearchInp] = useState('') const [searchValue, setSearchValue] = useState(null) // display options - const [displayType, setDisplayType] = useState('clients') + const [graphType, setGraphType] = useState('clients') const [yRelative, setYRelative] = useState(true) const router = useRouter() - const handleDocumentChange = (e: SelectChangeEvent) => { - setDocument(e.target.value as string) - onDocumentChange(e.target.value as string) - } - // References // https://gist.github.com/ndpniraj/2735c3af00a7c4cbe50602ffe6209fc3 // https://stackoverflow.com/questions/59233036/react-typescript-get-files-from-file-input @@ -93,24 +95,42 @@ export default function Controller( } + const handleWorkbookChange = (e: SelectChangeEvent) => { + clearSearch() + + if (e.target.value === 'trends' && !SPECIAL_DISPLAY.includes(graphType)) { + changeGraphType('client_trends') + } + + if (e.target.value !== 'trends' && SPECIAL_DISPLAY.includes(graphType)) { + changeGraphType('clients') + } + + setWorkbook(e.target.value as string) + onWorkbookChange(e.target.value as string) + } - const handleChangeYRange = (event: React.MouseEvent, yRange: boolean) => { + const handleChangeYRange = (_event: React.MouseEvent, yRange: boolean) => { if (yRange !== null) { setYRelative(yRange) onYRangeChange(yRange) } } - const handlePerspectiveChange = (e: React.MouseEvent, perspective: string) => { - if (perspective !== null) { - setDisplayType(perspective) + const handleGraphTypeChange = (_e: React.MouseEvent, type: string) => { + changeGraphType(type) + } + + const changeGraphType = (type: string) => { + if (type !== null) { + setGraphType(type) clearSearch() - onPerspectiveChange(perspective) + onGraphTypeChange(type) } } const handleSearchChange = (_e: React.SyntheticEvent, value: string) => setSearchInp(value) - const handleSearchValChange = (e: any, newVal: string | null) => setSearchValue(newVal) + const handleSearchValChange = (_e: any, newVal: string | null) => setSearchValue(newVal) const handleAddGraph = () => { clearSearch() @@ -135,8 +155,8 @@ export default function Controller(