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

fix(CBI-5660&&CBI-5794): date alignment #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 18 additions & 20 deletions src/components/Card/input.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import React, { useContext } from 'react';
import clsx from 'clsx';
import { RefCallBack } from 'react-hook-form';
import {get} from 'lodash-es';
import React, { useContext } from 'react';
import { RefCallBack } from 'react-hook-form';

import ActionButton from '../ActionButton';
import Component from '../Component';
import Controller from '../Controller';
import DateRange from '../DateRange';
import Json from '../Json';
import Context from '../Text/context';
import { dateIn, dateOut } from '../lib/dates';
import getType from '../lib/getType';
import testid from '../lib/testid';
import {
AutoComplete,
Calendar,
Expand All @@ -24,20 +33,11 @@ import {
TreeSelect,
TreeTable
} from '../prime';
import {Property, PropertyEditor, FormApi} from '../types';
import Context from '../Text/context';

import getType from '../lib/getType';
import testid from '../lib/testid';
import Table from './inputs/Table';
import {FormApi, Property, PropertyEditor} from '../types';
import {CHANGE} from './const';
import Ocr from './inputs/Ocr';
import Table from './inputs/Table';
import Webcam from './inputs/Webcam';
import ActionButton from '../ActionButton';
import DateRange from '../DateRange';
import Json from '../Json';
import Component from '../Component';
import {CHANGE} from './const';
import Controller from '../Controller';

const getFieldClass = (index, classes, name, className) =>
name === '' ? className : clsx(
Expand Down Expand Up @@ -104,7 +104,7 @@ function useInput(
case 'boolean': return event => fieldChange?.({...event, value: event.checked});
case 'chips': return event => fieldChange?.({...event, value: event.value.length ? event.value.join(' ') : null});
case 'date': return event => {
if (event.value instanceof Date) event.value = new Date(event.value.getTime() - event.value.getTimezoneOffset() * 60 * 1000);
if (event.value instanceof Date) event.value = dateOut(event.value);
fieldChange(event);
};
case 'dateRange': return event => fieldChange?.(event);
Expand Down Expand Up @@ -437,11 +437,9 @@ function useInput(
<Calendar
{...field}
showIcon
value={
field.value != null
? new Date(new Date(field.value).getTime() + new Date(field.value).getTimezoneOffset() * 60 * 1000)
: field.value
}
value={field.value != null
? dateIn(field.value)
: field.value}
onChange={onChange}
inputId={props.id}
{...props}
Expand Down
34 changes: 18 additions & 16 deletions src/components/lib/column.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import clsx from 'clsx';
import type Joi from 'joi';
import { TooltipProps } from 'primereact/tooltip';
import React from 'react';

import {CHANGE, INDEX, KEY} from '../Card/const';
import DateRange from '../DateRange';
import {ConfigField} from '../Form/DragDrop';
import type { FormatOptions } from '../Gate/Gate.types';
import Json from '../Json';
import Text from '../Text';
import type { ContextType } from '../Text/context';
import {
Calendar,
Checkbox,
Expand All @@ -14,20 +25,11 @@ import {
SelectButton,
TriStateCheckbox
} from '../prime';
import DateRange from '../DateRange';
import Json from '../Json';
import type { Property, PropertyEditor } from '../types';
import titleCase from './titleCase';
import { dateIn, dateOut } from './dates';
import getType from './getType';
import {KEY, INDEX, CHANGE} from '../Card/const';
import testid from '../lib/testid';
import {ConfigField} from '../Form/DragDrop';
import Text from '../Text';
import clsx from 'clsx';
import type Joi from 'joi';
import { TooltipProps } from 'primereact/tooltip';
import type { ContextType } from '../Text/context';
import type { FormatOptions } from '../Gate/Gate.types';
import testid from './testid';
import titleCase from './titleCase';

export interface TableFilter {
filters?: {
Expand Down Expand Up @@ -257,8 +259,8 @@ export default function columnProps({
body = function body(rowData) {
let value = rowData[fieldName];
if (value == null) return null;
value = new Date(value);
return ctx?.formatValue?.(new Date(value.getTime() + value.getTimezoneOffset() * 60 * 1000), { type: 'date', ...(formatOptions as FormatOptions)?.date });
value = dateIn(value);
return ctx?.formatValue?.(value, { type: 'date', ...(formatOptions as FormatOptions)?.date });
};
break;
case 'time':
Expand Down Expand Up @@ -493,11 +495,11 @@ export default function columnProps({
value={(() => {
const value = dataValue(inlineEdit, fieldName);
return value != null
? new Date(new Date(value).getTime() + new Date(value).getTimezoneOffset() * 60 * 1000)
? dateIn(value)
: value;
})()}
onChange={event => {
if (event.value instanceof Date) event.value = new Date(event.value.getTime() - event.value.getTimezoneOffset() * 60 * 1000);
if (event.value instanceof Date) event.value = dateOut(event.value);
onEdit(inlineEdit, event, event.value);
}}
showIcon
Expand Down
28 changes: 28 additions & 0 deletions src/components/lib/dates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const dateIn = (d) => {
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
let addition = 0;
const dd = new Date(d);
if (
tz === 'Asia/Baghdad' &&
dd.getFullYear() === 1994 &&
dd.getMonth() === 3 &&
dd.getDate() === 1
) {
// some glitch in the matrix between
//
// - AST(Arabia Standard Time)
// - Arabia Daylight Time
// as 01/04/1994 is in ADT
// and 31/03/1994 is in AST
//
// I guess on 1st of April, 1994 Iraq shifted from AST to ADT...
addition = 60;
}
return new Date(
new Date(d).getTime() +
(new Date(d).getTimezoneOffset() + addition) * 60 * 1000
);
};

export const dateOut = (d) =>
new Date(d.getTime() - d.getTimezoneOffset() * 60 * 1000);