Skip to content

Commit

Permalink
ESSNTL(4056): Adds checks against edge cases for last seen filter (#1787
Browse files Browse the repository at this point in the history
)

* fix(ESSNTL-4056): updates for edge cases

* Add additional edge cases
  • Loading branch information
adonispuente authored Mar 20, 2023
1 parent 2eddb15 commit 8809e40
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/Utilities/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ export const generateFilter = (status,
rhcdFilter: Array.isArray(rhcdFilter) ? rhcdFilter : [rhcdFilter]
},
!isEmpty(lastSeenFilter) && {
lastSeenFilter: Array.isArray(lastSeenFilter) ? lastSeenFilter : [lastSeenFilter]
lastSeenFilter: Array.isArray(lastSeenFilter)
? lastSeenItems.filter((item)=> item.value.mark === lastSeenFilter[0])[0].value : [lastSeenFilter]
},
!isEmpty(updateMethodFilter) && {
updateMethodFilter: Array.isArray(updateMethodFilter) ? updateMethodFilter : [updateMethodFilter]
Expand Down
18 changes: 13 additions & 5 deletions src/components/InventoryTable/EntityTableToolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ const EntityTableToolbar = ({
const [registeredFilter, registeredChip, registeredWithFilter, setRegisteredWithFilter] = useRegisteredWithFilter(reducer);
const [rhcdFilterConfig, rhcdFilterChips, rhcdFilterValue, setRhcdFilterValue] = useRhcdFilter(reducer);
const [lastSeenFilter, lastSeenChip, lastSeenFilterValue, setLastSeenFilterValue,
toValidator, onFromChange, onToChange, endDate, startDate, rangeValidator] = useLastSeenFilter(reducer);
toValidator, onFromChange, onToChange, endDate, startDate, fromValidator,
setStartDate, setEndDate] = useLastSeenFilter(reducer);
const [osFilterConfig, osFilterChips, osFilterValue, setOsFilterValue] = useOperatingSystemFilter();
const [updateMethodConfig, updateMethodChips, updateMethodValue, setUpdateMethodValue] = useUpdateMethodFilter(reducer);
const [hostGroupConfig, hostGroupChips, hostGroupValue, setHostGroupValue] = useGroupFilter();
Expand Down Expand Up @@ -326,7 +327,12 @@ const EntityTableToolbar = ({
),
[OS_CHIP]: (deleted) => setOsFilterValue(xor(osFilterValue, deleted.chips.map(({ value }) => value))),
[RHCD_FILTER_KEY]: (deleted) => setRhcdFilterValue(onDeleteFilter(deleted, rhcdFilterValue)),
[LAST_SEEN_CHIP]: (deleted) => setLastSeenFilterValue(onDeleteFilter(deleted, [lastSeenFilterValue.mark])),
[LAST_SEEN_CHIP]: (deleted) =>
{
setLastSeenFilterValue(onDeleteFilter(deleted, [lastSeenFilterValue.mark])),
setStartDate(),
setEndDate();
},
[UPDATE_METHOD_KEY]: (deleted) => setUpdateMethodValue(onDeleteFilter(deleted, updateMethodValue)),
[HOST_GROUP_CHIP]: (deleted) => setHostGroupValue(onDeleteFilter(deleted, hostGroupValue))
};
Expand All @@ -343,6 +349,8 @@ const EntityTableToolbar = ({
enabledFilters.lastSeenFilter && setLastSeenFilterValue([]);
enabledFilters.updateMethodFilter && setUpdateMethodValue([]);
enabledFilters.hostGroupFilter && setHostGroupValue([]);
setStartDate();
setEndDate();
dispatch(setFilter([]));
updateData({ page: 1, filters: [] });
};
Expand Down Expand Up @@ -394,7 +402,6 @@ const EntityTableToolbar = ({
] : [],
...filterConfig?.items || []
];

return <Fragment>
<PrimaryToolbar
{...props}
Expand Down Expand Up @@ -439,8 +446,8 @@ const EntityTableToolbar = ({
<DatePicker
onChange={onFromChange}
aria-label="Start date"
validators={[rangeValidator]}

validators={[fromValidator]}
placeholder='Start'
/>
</SplitItem>
<SplitItem style={{ padding: '6px 12px 0 12px' }}>
Expand All @@ -453,6 +460,7 @@ const EntityTableToolbar = ({
rangeStart={startDate}
validators={[toValidator]}
aria-label="End date"
placeholder='End'
/>
</SplitItem>
</Split>}
Expand Down
69 changes: 61 additions & 8 deletions src/components/filters/useLastSeenFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,80 @@ export const useLastSeenFilter = (

const [startDate, setStartDate] = useState();
const [endDate, setEndDate] = useState();
const todaysDate = new Date();

const manageStartDate = (apiStartDate, apiEndDate)=> {
if (isNaN(apiEndDate) && isNaN(apiStartDate)) {
setValue({ ...lastSeenValue, updatedStart: null, updatedEnd: null });
} else if (apiStartDate > apiEndDate || isNaN(apiStartDate) || apiStartDate > todaysDate) {
setValue({ ...lastSeenValue, updatedStart: null, updatedEnd: apiEndDate.toISOString() });
} else {
setValue({ ...lastSeenValue, updatedStart: apiStartDate.toISOString() });
}
};

const manageEndDate = (apiStartDate, apiEndDate)=> {
if (isNaN(apiEndDate) && isNaN(apiStartDate)) {
setValue({ ...lastSeenValue, updatedStart: null, updatedEnd: null });
} else if (apiStartDate > apiEndDate || isNaN(apiEndDate)) {
setValue({ ...lastSeenValue, updatedStart: apiStartDate.toISOString(), updatedEnd: null });
} else {
setValue({ ...lastSeenValue, updatedEnd: apiEndDate.toISOString() });
}
};

const toValidator = (date) => {
date >= startDate ? '' : 'To date must be less than from date.';
const newDate = new Date(date);
const minDate = new Date(startDate);

if (minDate >= newDate) {
return 'Start date must be earlier than End date.';
} else if (newDate > todaysDate) {
return `Date must be ${todaysDate.toISOString().split('T')[0]} or earlier`;
} else {
return '';
}
};

const rangeValidator = (date) => {
const fromValidator = (date) => {
const minDate = new Date(1950, 1, 1);
const maxDate = new Date(endDate);

if (date < minDate) {
return 'Date is before the allowable range.';
} else if (date > maxDate) {
return `End date must be later than Start date.`;
} else if (date > todaysDate) {
return ' Start date must be earlier than End date.';
} else {
return '';
}
};

const onFromChange = (_str, date) => {
const onFromChange = (date) => {
const newToDate = new Date(endDate);
if (date > newToDate) {
setStartDate();
return 'End date must be later than Start date.';
}

setStartDate(date);
setValue({ ...lastSeenValue, updatedStart: new Date(date).toISOString() });
date.setDate(date.getDate() + 1);
const apiStartDate = new Date(date);
apiStartDate.setUTCHours(0);
manageStartDate(apiStartDate, newToDate);
};

const onToChange = (date) => {
setEndDate(date);
setValue({ ...lastSeenValue, updatedEnd: new Date(date).toISOString() });
if (startDate > new Date(date)) {
return 'Start date must be earlier than End date.';
} else if (new Date(date) > todaysDate) {
return 'End date must be later than Start date.';
} else {
setEndDate(date);
const apiEndDate = new Date(date);
apiEndDate.setUTCHours(23, 59);
manageEndDate(new Date(startDate), apiEndDate);
}
};

return [
Expand All @@ -79,6 +130,8 @@ export const useLastSeenFilter = (
onToChange,
endDate,
startDate,
rangeValidator
fromValidator,
setStartDate,
setEndDate
];
};
4 changes: 2 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ export const getSearchParams = () => {
const groupHostsFilter = searchParams.getAll(HOST_GROUP_CHIP);
const page = searchParams.getAll('page');
const perPage = searchParams.getAll('per_page');
// const lastSeenFilter = searchParams.getAll('last_seen');
return { status, source, tagsFilter, filterbyName, operatingSystem, rhcdFilter, updateMethodFilter,
const lastSeenFilter = searchParams.getAll('last_seen');
return { status, source, tagsFilter, filterbyName, operatingSystem, rhcdFilter, updateMethodFilter, lastSeenFilter,
page, perPage, groupHostsFilter };
};

Expand Down
6 changes: 3 additions & 3 deletions src/routes/InventoryTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ const filterMapper = {
flatMap(tagFilters, mapTags)
),
rhcdFilter: ({ rhcdFilter }, searchParams) => rhcdFilter?.forEach(item => searchParams.append(RHCD_FILTER_KEY, item)),
//TODO: Add a way to add to url in a way that allows shareable links
// lastSeenFilter: ({ lastSeenFilter }, searchParams) =>
// Object.values(lastSeenFilter)?.forEach(item => searchParams.append('last_seen', item)),
lastSeenFilter: ({ lastSeenFilter }, searchParams) =>
Object.keys(lastSeenFilter)?.forEach(item => item === 'mark' &&
searchParams.append('last_seen', lastSeenFilter[item])),
updateMethodFilter: ({ updateMethodFilter }, searchParams) =>
updateMethodFilter?.forEach(item => searchParams.append(UPDATE_METHOD_KEY, item)),
groupHostFilter: ({ groupHostFilter }, searchParams) => groupHostFilter
Expand Down

0 comments on commit 8809e40

Please sign in to comment.