Skip to content

Commit

Permalink
Fix date filter default values
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-elias committed Sep 27, 2024
1 parent ef7eba1 commit 36eccfd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import {
VariableDateViewFilterValueUnit,
} from '@/views/utils/view-filter-value/resolveDateViewFilterValue';
import { resolveFilterValue } from '@/views/utils/view-filter-value/resolveFilterValue';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { isDefined } from 'twenty-ui';
import { z } from 'zod';
import { FieldMetadataType } from '~/generated-metadata/graphql';

export const ObjectFilterDropdownDateInput = () => {
Expand All @@ -40,19 +41,36 @@ export const ObjectFilterDropdownDateInput = () => {
const initialFilterValue = selectedFilter
? resolveFilterValue(selectedFilter)
: null;
console.log(
'selectedFilter',
selectedFilter,
'initialFilterValue',
initialFilterValue,
);
const [internalDate, setInternalDate] = useState<Date | null>(
initialFilterValue instanceof Date ? initialFilterValue : new Date(),
initialFilterValue instanceof Date ? initialFilterValue : null,
);

const isDateTimeInput =
filterDefinitionUsedInDropdown?.type === FieldMetadataType.DateTime;

useEffect(() => {
if (selectedFilter?.operand !== ViewFilterOperand.IsRelative) {
if (!filterDefinitionUsedInDropdown || !selectedOperandInDropdown) return;

const now = new Date();
const dateResult = z.coerce.date().safeParse(selectedFilter?.value);
const date = dateResult.success ? dateResult.data : now;
const value = date.toISOString();
const displayValue = isDateTimeInput
? date.toLocaleString()
: date.toLocaleDateString();

selectFilter?.({
id: selectedFilter?.id ? selectedFilter.id : v4(),
fieldMetadataId: filterDefinitionUsedInDropdown.fieldMetadataId,
value,
displayValue,
operand: selectedOperandInDropdown,
definition: filterDefinitionUsedInDropdown,
});
}
}, [selectedOperandInDropdown]);

const handleAbsoluteDateChange = (newDate: Date | null) => {
setInternalDate(newDate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,15 +444,6 @@ export const InternalDatePicker = ({
zone: isDateTimeInput ? timeZone : 'local',
});

console.log(
'date',
date,
'internalDate',
internalDate,
'dateParsed',
dateParsed,
);

// We have to force a end of day on the computer local timezone with the given date
// Because JS Date API cannot hold a timezone other than the local one
// And if we don't do that workaround we will have problems when changing the date
Expand Down

0 comments on commit 36eccfd

Please sign in to comment.