From 399e0585d72bace51a0c81e9f889dda054e68519 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 23 Mar 2024 08:26:42 -0700 Subject: Better time maintenance formatting --- modern/src/settings/MaintenancePage.jsx | 52 +++++++++++++++++++------------- modern/src/settings/MaintenancesPage.jsx | 15 ++++++--- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/modern/src/settings/MaintenancePage.jsx b/modern/src/settings/MaintenancePage.jsx index 420e2b82..87f8ea0b 100644 --- a/modern/src/settings/MaintenancePage.jsx +++ b/modern/src/settings/MaintenancePage.jsx @@ -1,4 +1,5 @@ -import React, { useState } from 'react'; +import React, { useEffect, useState } from 'react'; +import dayjs from 'dayjs'; import { Accordion, AccordionSummary, @@ -46,14 +47,11 @@ const MaintenancePage = () => { return otherList; }; - const onMaintenanceTypeChange = (event) => { - const newValue = event.target.value; - setItem({ - ...item, type: newValue, start: 0, period: 0, - }); - - const attribute = positionAttributes[newValue]; - if (attribute && attribute.dataType) { + useEffect(() => { + const attribute = positionAttributes[item?.type]; + if (item?.type?.endsWith('Time')) { + setLabels({ ...labels, start: null, period: t('sharedDays') }); + } else if (attribute && attribute.dataType) { switch (attribute.dataType) { case 'distance': setLabels({ ...labels, start: t(prefixString('shared', distanceUnit)), period: t(prefixString('shared', distanceUnit)) }); @@ -68,11 +66,17 @@ const MaintenancePage = () => { } else { setLabels({ ...labels, start: null, period: null }); } - }; + }, [item?.type]); - const rawToValue = (value) => { + const rawToValue = (start, value) => { const attribute = positionAttributes[item.type]; - if (attribute && attribute.dataType) { + if (item.type.endsWith('Time')) { + if (start) { + return dayjs(value).locale('en').format('YYYY-MM-DD'); + } else { + return value / 86400000; + } + } else if (attribute && attribute.dataType) { switch (attribute.dataType) { case 'speed': return speedFromKnots(value, speedUnit); @@ -85,9 +89,15 @@ const MaintenancePage = () => { return value; }; - const valueToRaw = (value) => { + const valueToRaw = (start, value) => { const attribute = positionAttributes[item.type]; - if (attribute && attribute.dataType) { + if (item.type.endsWith('Time')) { + if (start) { + return dayjs(value, 'YYYY-MM-DD').valueOf(); + } else { + return value * 86400000; + } + } else if (attribute && attribute.dataType) { switch (attribute.dataType) { case 'speed': return speedToKnots(value, speedUnit); @@ -122,7 +132,7 @@ const MaintenancePage = () => { setItem({ ...item, name: event.target.value })} + onChange={(e) => setItem({ ...item, name: e.target.value })} label={t('sharedName')} /> @@ -130,7 +140,7 @@ const MaintenancePage = () => { setItem({ ...item, start: valueToRaw(event.target.value) })} + type={item.type.endsWith('Time') ? 'date' : 'number'} + value={rawToValue(true, item.start) || ''} + onChange={(e) => setItem({ ...item, start: valueToRaw(true, e.target.value) })} label={labels.start ? `${t('maintenanceStart')} (${labels.start})` : t('maintenanceStart')} /> setItem({ ...item, period: valueToRaw(event.target.value) })} + value={rawToValue(false, item.period) || ''} + onChange={(e) => setItem({ ...item, period: valueToRaw(false, e.target.value) })} label={labels.period ? `${t('maintenancePeriod')} (${labels.period})` : t('maintenancePeriod')} /> diff --git a/modern/src/settings/MaintenancesPage.jsx b/modern/src/settings/MaintenancesPage.jsx index 2a66590c..9170f9ee 100644 --- a/modern/src/settings/MaintenancesPage.jsx +++ b/modern/src/settings/MaintenancesPage.jsx @@ -1,4 +1,5 @@ import React, { useState } from 'react'; +import dayjs from 'dayjs'; import { Table, TableRow, TableCell, TableHead, TableBody, } from '@mui/material'; @@ -42,9 +43,15 @@ const MaintenacesPage = () => { } }, [timestamp]); - const convertAttribute = (key, value) => { + const convertAttribute = (key, start, value) => { const attribute = positionAttributes[key]; - if (attribute && attribute.dataType) { + if (key.endsWith('Time')) { + if (start) { + return dayjs(value).locale('en').format('YYYY-MM-DD'); + } else { + return `${value / 86400000} ${t('sharedDays')}`; + } + } else if (attribute && attribute.dataType) { switch (attribute.dataType) { case 'speed': return formatSpeed(value, speedUnit, t); @@ -76,8 +83,8 @@ const MaintenacesPage = () => { {item.name} {item.type} - {convertAttribute(item.type, item.start)} - {convertAttribute(item.type, item.period)} + {convertAttribute(item.type, true, item.start)} + {convertAttribute(item.type, false, item.period)} -- cgit v1.2.3