diff --git a/client/components/Generic/TrackingTrafficLight.js b/client/components/Generic/TrackingTrafficLight.js index f854ad4c..8d77f29e 100644 --- a/client/components/Generic/TrackingTrafficLight.js +++ b/client/components/Generic/TrackingTrafficLight.js @@ -1,13 +1,16 @@ import React, { useState } from 'react' import { useDispatch, useSelector } from 'react-redux' -import { Button, Menu, Header } from 'semantic-ui-react' +import { Button, Menu, Header, Grid } from 'semantic-ui-react' import { useTranslation } from 'react-i18next' import { updateFormField } from 'Utilities/redux/formReducer' +import DatePicker, { registerLocale } from 'react-datepicker' +import { fi, enGB, sv } from 'date-fns/locale' import './Generic.scss' const TrackingTrafficLight = ({ id, form }) => { const dispatch = useDispatch() const { t } = useTranslation() + const lang = useSelector(state => state.language) const fieldName = `${id}_lights_history` const lightsHistory = useSelector(({ form }) => form.data[fieldName]) || [] const displayedHistory = lightsHistory.slice(Math.max(lightsHistory.length - 4, 0)) @@ -15,6 +18,11 @@ const TrackingTrafficLight = ({ id, form }) => { const value = useSelector(({ form }) => form.data[fieldName]) const [showChooser, setShowChooser] = useState(false) + const [customDate, setCustomDate] = useState(new Date().toISOString()) + + registerLocale('fi', fi) + registerLocale('en', enGB) + registerLocale('se', sv) const colorValueMap = { green: 1, @@ -25,12 +33,15 @@ const TrackingTrafficLight = ({ id, form }) => { const chooseLight = color => { if (!reduxViewOnly) { const value = colorValueMap[color] !== undefined ? colorValueMap[color] : null - const newEntry = { color, value, date: new Date().toISOString() } - dispatch(updateFormField(fieldName, [...lightsHistory, newEntry], form)) + const newEntry = { color, value, date: customDate } + + // Insert the new entry in chronological order + const updatedHistory = [...lightsHistory, newEntry].sort((a, b) => new Date(a.date) - new Date(b.date)) + + dispatch(updateFormField(fieldName, updatedHistory, form)) setShowChooser(false) } } - const toggleChooser = () => { setShowChooser(prev => !prev) } @@ -76,17 +87,31 @@ const TrackingTrafficLight = ({ id, form }) => { {t('noTrafficLight')} )} - + {!showChooser && ( + + )} {showChooser && ( - <> + {t('chooseTrafficLight')} -
-
+ + + + +
{ />

{t('greenFaculty')}

+ +
{ />

{t('yellowFaculty')}

+ +
{ />

{t('redFaculty')}

-
-
- +
+ + )} )