Skip to content

Commit

Permalink
Fix Timezone Issue in Leave Request (#118)
Browse files Browse the repository at this point in the history
* initial commit for Fix Timezone Issue in Leave Request

* fixed timezone issue
  • Loading branch information
JensRavens authored Jan 23, 2025
1 parent 5d6aeec commit 1c33c25
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/frontend/components/date_field/date_field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { ReactNode } from 'react';
import { Text } from '../text/text';
import { FormField, useInputId } from '../form_field/form_field';
import classnames from 'classnames';
import { isoDate } from '../../util/date';

interface Props extends FormField<Date | null> {
label?: ReactNode;
Expand Down Expand Up @@ -56,7 +57,7 @@ export function DateField({
name={name}
className={classnames('textfield__input')}
readOnly={readOnly}
value={value?.toJSON().slice(0, 10) ?? ''}
value={value ? isoDate(value) : ''}
type="date"
onChange={(event) => {
if (readOnly) {
Expand Down
11 changes: 11 additions & 0 deletions app/frontend/util/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function isoDate(date: Date): string {
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
}

export function sameDay(date1: Date, date2: Date): boolean {
return (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
);
}
4 changes: 4 additions & 0 deletions app/frontend/util/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sameDay } from './date';
import type { Locale } from './i18n';

export class Formatter {
Expand Down Expand Up @@ -107,6 +108,9 @@ export class Formatter {
if (!startDate || !endDate) {
return null;
}
if (sameDay(startDate, endDate)) {
return this.date(startDate);
}
if (startDate.getFullYear() === endDate.getFullYear()) {
return `${this.dateWithoutYear(startDate)} - ${this.date(endDate)}`;
} else {
Expand Down
9 changes: 8 additions & 1 deletion app/views/leaves/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Form } from '../../frontend/components/form/form';
import { handleError } from '../../frontend/util/errors';
import { Box } from '../../frontend/components/box/box';
import { Stack } from '../../frontend/components/stack/stack';
import { isoDate } from '../../frontend/util/date';

interface Form {
userId: string;
Expand Down Expand Up @@ -43,10 +44,16 @@ export default function ({
type: 'required',
},
onSubmit: async ({ model }) => {
const params = {
leave: {
...model,
days: model.days.map(isoDate),
},
};
await reaction.call({
path: '/leaves',
method: 'POST',
params: { leave: model },
params,
refresh: true,
});
modal.close();
Expand Down

0 comments on commit 1c33c25

Please sign in to comment.