Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TechDebt: Moment -> DayJs #1189

Merged
merged 6 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"aws-sdk": "~2.742.0",
"axios": "~0.21.4",
"clamdjs": "~1.0.2",
"dayjs": "^1.11.8",
"dayjs": "^1.11.10",
"db-migrate": "~0.11.11",
"db-migrate-pg": "~1.2.2",
"express": "~4.17.1",
Expand All @@ -52,7 +52,6 @@
"knex": "~1.0.1",
"lodash": "~4.17.21",
"mime": "~2.5.2",
"moment": "~2.29.2",
"multer": "~1.4.3",
"pg": "~8.7.1",
"qs": "~6.10.1",
Expand Down
4 changes: 2 additions & 2 deletions api/src/models/project-survey-attachments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import moment from 'moment';
import { default as dayjs } from 'dayjs';
import { getLogger } from '../utils/logger';
import { SurveySupplementaryData } from './survey-view';

Expand Down Expand Up @@ -29,7 +29,7 @@ export class GetAttachmentsWithSupplementalData {
attachment.project_report_attachment_id;
this.fileName = attachment.file_name;
this.fileType = attachment.file_type || 'Report';
this.lastModified = moment(attachment.update_date || attachment.create_date).toISOString();
this.lastModified = dayjs(attachment.update_date || attachment.create_date).toISOString();
this.size = attachment.file_size;
this.status = attachment.status;
this.supplementaryAttachmentData = supplementaryData;
Expand Down
4 changes: 2 additions & 2 deletions api/src/services/project-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { default as dayjs } from 'dayjs';
import { Feature } from 'geojson';
import moment from 'moment';
import { COMPLETION_STATUS } from '../constants/status';
import { IDBConnection } from '../database/db';
import { HTTP400 } from '../errors/http-error';
Expand Down Expand Up @@ -63,7 +63,7 @@ export class ProjectService extends DBService {
start_date: row.start_date,
end_date: row.end_date,
completion_status:
(row.end_date && moment(row.end_date).endOf('day').isBefore(moment()) && COMPLETION_STATUS.COMPLETED) ||
(row.end_date && dayjs(row.end_date).endOf('day').isBefore(dayjs()) && COMPLETION_STATUS.COMPLETED) ||
COMPLETION_STATUS.ACTIVE,
project_programs: row.project_programs,
regions: row.regions
Expand Down
2 changes: 1 addition & 1 deletion api/src/utils/media/xlsx/xlsx-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dayjs from 'dayjs';
import { default as dayjs } from 'dayjs';
import xlsx, { CellObject } from 'xlsx';
import { safeTrim } from '../../string-utils';

Expand Down
2 changes: 1 addition & 1 deletion api/src/utils/xlsx-utils/cell-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dayjs from 'dayjs';
import { default as dayjs } from 'dayjs';
import { CellObject } from 'xlsx';
import { safeTrim } from '../string-utils';

Expand Down
4 changes: 2 additions & 2 deletions api/src/utils/xlsx-utils/worksheet-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import moment from 'moment';
import { default as dayjs } from 'dayjs';
import xlsx, { CellObject } from 'xlsx';
import { MediaFile } from '../media/media-file';
import { safeToLowerCase } from '../string-utils';
Expand Down Expand Up @@ -218,7 +218,7 @@
const type = typeof value;

if (rowValueTypes[index] === 'date') {
return moment(value).isValid();
return dayjs(value).isValid();

Check warning on line 221 in api/src/utils/xlsx-utils/worksheet-utils.ts

View check run for this annotation

Codecov / codecov/patch

api/src/utils/xlsx-utils/worksheet-utils.ts#L221

Added line #L221 was not covered by tests
}

return rowValueTypes[index] === type;
Expand Down
10 changes: 5 additions & 5 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@
"@turf/turf": "^6.5.0",
"axios": "~0.21.4",
"clsx": "~1.2.1",
"dayjs": "^1.11.10",
"express": "~4.17.1",
"formik": "~2.4.1",
"leaflet": "~1.9.4",
"leaflet-draw": "~1.0.4",
"leaflet-fullscreen": "~1.0.2",
"leaflet.locatecontrol": "~0.79.0",
"lodash-es": "~4.17.21",
"moment": "~2.29.4",
"node-sass": "~4.14.1",
"npm-run-all": "^4.1.5",
"oidc-client-ts": "^2.3.0",
Expand Down
8 changes: 4 additions & 4 deletions app/src/components/data-grid/DatePickerDataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import useEnhancedEffect from '@mui/material/utils/useEnhancedEffect';
import { GridRenderEditCellParams, GridValidRowModel, useGridApiContext } from '@mui/x-data-grid';
import { DatePicker, DatePickerProps, LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment';
import moment from 'moment';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { default as dayjs } from 'dayjs';
import { useRef } from 'react';

interface IDatePickerDataGridProps<DataGridType extends GridValidRowModel> {
Expand All @@ -22,10 +22,10 @@ const DatePickerDataGrid = <DataGridType extends GridValidRowModel>({
}
}, [dataGridProps.hasFocus]);
return (
<LocalizationProvider dateAdapter={AdapterMoment}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DatePicker
inputRef={ref}
value={(dataGridProps.value && moment(dataGridProps.value, 'HH:mm:ss')) || null}
value={(dataGridProps.value && dayjs(dataGridProps.value, 'HH:mm:ss')) || null}
onChange={(value) => {
apiRef?.current.setEditCellValue({ id: dataGridProps.id, field: dataGridProps.field, value: value });
}}
Expand Down
8 changes: 4 additions & 4 deletions app/src/components/data-grid/TimePickerDataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import useEnhancedEffect from '@mui/material/utils/useEnhancedEffect';
import { GridRenderEditCellParams, GridValidRowModel, useGridApiContext } from '@mui/x-data-grid';
import { LocalizationProvider, TimePicker, TimePickerProps } from '@mui/x-date-pickers';
import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment';
import moment from 'moment';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { default as dayjs } from 'dayjs';
import { useRef } from 'react';

interface ITimePickerDataGridProps<DataGridType extends GridValidRowModel> {
Expand All @@ -25,7 +25,7 @@ const TimePickerDataGrid = <DataGridType extends GridValidRowModel>({
const { slotProps, ...rest } = dateFieldProps ?? {};

return (
<LocalizationProvider dateAdapter={AdapterMoment}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<TimePicker
slotProps={{
...slotProps,
Expand All @@ -35,7 +35,7 @@ const TimePickerDataGrid = <DataGridType extends GridValidRowModel>({
}
}}
inputRef={ref}
value={(dataGridProps.value && moment(dataGridProps.value, 'HH:mm:ss')) || null}
value={(dataGridProps.value && dayjs(dataGridProps.value, 'HH:mm:ss')) || null}
onChange={(value) => {
apiRef?.current.setEditCellValue({ id: dataGridProps.id, field: dataGridProps.field, value: value });
}}
Expand Down
29 changes: 15 additions & 14 deletions app/src/components/fields/DateTimeFields.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Icon from '@mdi/react';
import Grid from '@mui/material/Grid';
import { DatePicker, TimePicker } from '@mui/x-date-pickers';
import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DATE_FORMAT, DATE_LIMIT, TIME_FORMAT } from 'constants/dateTimeFormats';
import { default as dayjs } from 'dayjs';
import { ISurveySampleMethodData } from 'features/surveys/components/MethodForm';
import { FormikContextType } from 'formik';
import get from 'lodash-es/get';
import moment from 'moment';
import React from 'react';

interface IDateTimeFieldsProps {
Expand Down Expand Up @@ -51,19 +51,19 @@
const rawDateValue = get(values, dateName);
const formattedDateValue =
(rawDateValue &&
moment(rawDateValue, DATE_FORMAT.ShortDateFormat).isValid() &&
moment(rawDateValue, DATE_FORMAT.ShortDateFormat)) ||
dayjs(rawDateValue, DATE_FORMAT.ShortDateFormat).isValid() &&
dayjs(rawDateValue, DATE_FORMAT.ShortDateFormat)) ||
null;

const rawTimeValue = get(values, timeName);
const formattedTimeValue =
(rawTimeValue &&
moment(rawTimeValue, TIME_FORMAT.LongTimeFormat24Hour).isValid() &&
moment(rawTimeValue, TIME_FORMAT.LongTimeFormat24Hour)) ||
dayjs(rawTimeValue, TIME_FORMAT.LongTimeFormat24Hour).isValid() &&
dayjs(rawTimeValue, TIME_FORMAT.LongTimeFormat24Hour)) ||
null;

return (
<LocalizationProvider dateAdapter={AdapterMoment}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Grid container>
<Grid item xs={6}>
<DatePicker
Expand Down Expand Up @@ -97,18 +97,19 @@
}}
label={dateLabel}
format={DATE_FORMAT.ShortDateFormat}
minDate={moment(DATE_LIMIT.min)}
maxDate={moment(DATE_LIMIT.max)}
minDate={dayjs(DATE_LIMIT.min)}
maxDate={dayjs(DATE_LIMIT.max)}
value={formattedDateValue}
onChange={(value) => {
if (!value || String(value.creationData().input) === 'Invalid Date') {
console.log(value);

Check warning on line 104 in app/src/components/fields/DateTimeFields.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/components/fields/DateTimeFields.tsx#L104

Added line #L104 was not covered by tests
if (!value || value === 'Invalid Date') {
// The creation input value will be 'Invalid Date' when the date field is cleared (empty), and will
// contain an actual date string value if the field is not empty but is invalid.
setFieldValue(dateName, null);
return;
}

setFieldValue(dateName, moment(value).format(DATE_FORMAT.ShortDateFormat));
setFieldValue(dateName, dayjs(value).format(DATE_FORMAT.ShortDateFormat));

Check warning on line 112 in app/src/components/fields/DateTimeFields.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/components/fields/DateTimeFields.tsx#L112

Added line #L112 was not covered by tests
}}
/>
</Grid>
Expand Down Expand Up @@ -146,14 +147,14 @@
label={timeLabel}
format={TIME_FORMAT.LongTimeFormat24Hour}
value={formattedTimeValue}
onChange={(value: moment.Moment | null) => {
if (!value || !moment(value).isValid()) {
onChange={(value: dayjs.Dayjs | null) => {
if (!value || !dayjs(value).isValid()) {
// Check if the value is null or invalid, and if so, clear the field.
setFieldValue(timeName, null);
return;
}

setFieldValue(timeName, moment(value).format(TIME_FORMAT.LongTimeFormat24Hour));
setFieldValue(timeName, dayjs(value).format(TIME_FORMAT.LongTimeFormat24Hour));

Check warning on line 157 in app/src/components/fields/DateTimeFields.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/components/fields/DateTimeFields.tsx#L157

Added line #L157 was not covered by tests
}}
views={['hours', 'minutes', 'seconds']}
timeSteps={{ hours: 1, minutes: 1, seconds: 1 }}
Expand Down
18 changes: 9 additions & 9 deletions app/src/components/fields/SingleDateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import Icon from '@mdi/react';
import { TextFieldProps } from '@mui/material/TextField';
import { DatePicker } from '@mui/x-date-pickers';
import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DATE_FORMAT, DATE_LIMIT } from 'constants/dateTimeFormats';
import { default as dayjs } from 'dayjs';
import { useFormikContext } from 'formik';
import get from 'lodash-es/get';
import moment from 'moment';
import React from 'react';

interface IDateProps {
Expand Down Expand Up @@ -39,12 +39,12 @@

const formattedDateValue =
(rawDateValue &&
moment(rawDateValue, DATE_FORMAT.ShortDateFormat).isValid() &&
moment(rawDateValue, DATE_FORMAT.ShortDateFormat)) ||
dayjs(rawDateValue, DATE_FORMAT.ShortDateFormat).isValid() &&
dayjs(rawDateValue, DATE_FORMAT.ShortDateFormat)) ||
null;

return (
<LocalizationProvider dateAdapter={AdapterMoment}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DatePicker
slots={{
openPickerIcon: CalendarIcon
Expand Down Expand Up @@ -75,19 +75,19 @@
}}
label={label}
format={DATE_FORMAT.ShortDateFormat}
minDate={moment(DATE_LIMIT.min)}
maxDate={moment(DATE_LIMIT.max)}
minDate={dayjs(DATE_LIMIT.min)}
maxDate={dayjs(DATE_LIMIT.max)}
value={formattedDateValue}
onChange={(value) => {
other?.onChange?.(value);
if (!value || String(value.creationData().input) === 'Invalid Date') {
if (!value || value === 'Invalid Date') {
// The creation input value will be 'Invalid Date' when the date field is cleared (empty), and will
// contain an actual date string value if the field is not empty but is invalid.
setFieldValue(name, null);
return;
}

setFieldValue(name, moment(value).format(DATE_FORMAT.ShortDateFormat));
setFieldValue(name, dayjs(value).format(DATE_FORMAT.ShortDateFormat));

Check warning on line 90 in app/src/components/fields/SingleDateField.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/components/fields/SingleDateField.tsx#L90

Added line #L90 was not covered by tests
}}
/>
</LocalizationProvider>
Expand Down
Loading
Loading