Skip to content

Commit

Permalink
add URL validation, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
angelathe committed Jan 15, 2025
1 parent 653f4de commit b4f3f80
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
43 changes: 39 additions & 4 deletions containers/ecr-viewer/src/app/components/Filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,55 @@ const FilterByDate = () => {

const resetFilterDate = () => {
const queryDateRange = searchParams.get(ParamName.DateRange);
if (!queryDateRange) {
const resetToDefault = () => {
setFilterDateOption(DEFAULT_DATE_RANGE);
setStartDate("");
setEndDate("");
};

if (!queryDateRange) {
resetToDefault();
} else if (queryDateRange !== filterDateOption) {
setFilterDateOption(queryDateRange);
}
if (queryDateRange === "custom") {
const queryCustomDates = searchParams.get(ParamName.Dates) || "";
const [startDate, endDate] = queryCustomDates?.split("|");
setStartDate(startDate);
setEndDate(endDate);
if (!queryCustomDates) {
// If dateRange is custom but no custom dates, reset to default date range
resetToDefault();
} else {
const [startDate, endDate] = queryCustomDates?.split("|");
setStartDate(startDate);
setEndDate(endDate);
}
}
};

const validateURLParams = () => {
const dateRegex = /^\d{4}-\d{2}-\d{2}\|\d{4}-\d{2}-\d{2}$/;
const queryDateRange = searchParams.get(ParamName.DateRange);
const queryCustomDates = searchParams.get(ParamName.Dates) || "";
const params = new URLSearchParams(searchParams.toString());

if (queryDateRange === "custom") {
// Reset URL params to default filter dates if no dates param or has bad inputs
if (!queryCustomDates || !dateRegex.test(queryCustomDates.trim())) {
let updatedParams = updateQueryParam(
params,
ParamName.DateRange,
DEFAULT_DATE_RANGE,
isFilterDateDefault,
);
updatedParams = deleteQueryParam(updatedParams, ParamName.Dates);
pushQueryUpdate(updatedParams, [ParamName.DateRange, ParamName.Dates]);
}
} else if (queryCustomDates) {
// Remove dates param if dateRange is not custom
const updatedParams = deleteQueryParam(params, ParamName.Dates);
pushQueryUpdate(updatedParams, [ParamName.Dates]);
}
};
validateURLParams();

// on mount, make sure the filters match the search params
useEffect(resetFilterDate, []);
Expand Down
16 changes: 2 additions & 14 deletions containers/ecr-viewer/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import NotFound from "./not-found";
import Filters from "@/app/components/Filters";
import { EcrTableLoading } from "./components/EcrTableClient";
import {
buildCustomDateRange,
convertDateOptionToDateRange,
DEFAULT_DATE_RANGE,
returnParamDates,
} from "@/app/view-data/utils/date-utils";
import { env } from "next-runtime-env";

Expand All @@ -37,18 +36,7 @@ const HomePage = async ({
const filterConditionsArr = filterConditions?.split("|");
const filterDateRange =
(searchParams?.dateRange as string) || DEFAULT_DATE_RANGE;
let filterDates;

if (filterDateRange === "custom") {
const datesParam = searchParams?.dates as string | undefined;
if (datesParam) {
filterDates = buildCustomDateRange(datesParam);
} else {
filterDates = convertDateOptionToDateRange(DEFAULT_DATE_RANGE); // TODO ANGELA: Change Default for invalid or missing dates
}
} else {
filterDates = convertDateOptionToDateRange(filterDateRange);
}
const filterDates = returnParamDates(filterDateRange, searchParams);

let totalCount: number = 0;
if (isNonIntegratedViewer) {
Expand Down
26 changes: 26 additions & 0 deletions containers/ecr-viewer/src/app/view-data/utils/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,29 @@ export function buildCustomDateRange(datesString: string) {
endDate.setHours(23, 59, 59, 999);
return { startDate: startDate, endDate: endDate };
}

/**
* Description
* @param dateRange - asdf
* @param searchParams - asdf
* @returns asdf
*/
export function returnParamDates(
dateRange: string,
searchParams: {
[key: string]: string | string[] | undefined;
},
): DateRangePeriod {
const dateRegex = /^\d{4}-\d{2}-\d{2}\|\d{4}-\d{2}-\d{2}$/;
const datesParam = searchParams?.dates as string | undefined;

if (dateRange === "custom") {
if (!datesParam || !dateRegex.test(datesParam)) {
return convertDateOptionToDateRange(DEFAULT_DATE_RANGE);
} else {
return buildCustomDateRange(datesParam);
}
} else {
return convertDateOptionToDateRange(dateRange);
}
}

0 comments on commit b4f3f80

Please sign in to comment.