Skip to content

Commit

Permalink
fix: format search date parameters when resuming from URL actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mguellsegarra committed Jan 24, 2025
1 parent 6efb319 commit cefbc06
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 32 deletions.
9 changes: 6 additions & 3 deletions src/context/ActionViewContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { convertParamsToValues } from "@/helpers/searchHelper";
import { DEFAULT_SEARCH_LIMIT } from "@/models/constants";
import { View } from "@/types";
import { convertParamsToValues } from "@/widgets/views/searchFilter/SideSearchFilter";
import { TreeView, View } from "@/types";
import { ColumnState } from "@gisce/react-formiga-table";
import { createContext, useContext, useEffect, useState } from "react";

Expand Down Expand Up @@ -132,7 +132,10 @@ const ActionViewProvider = (props: ActionViewProviderProps): any => {
const [graphIsLoading, setGraphIsLoading] = useState<boolean>(true);
const [previousView, setPreviousView] = useState<View>();
const [searchValues, setSearchValues] = useState<any>(
convertParamsToValues(initialSearchParams || []),
convertParamsToValues(
initialSearchParams || [],
(currentView as TreeView).fields,
),
);
const [treeFirstVisibleRow, setTreeFirstVisibleRow] = useState<number>(0);
const [searchQuery, setSearchQuery] = useState<SearchQueryParams>();
Expand Down
69 changes: 69 additions & 0 deletions src/helpers/searchHelper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import dayjs from "@/helpers/dayjs";

const convertBooleanParamIfNeeded = (value: any) => {
if ((typeof value === "string" && value === "true") || value === "false") {
return value === "true";
Expand Down Expand Up @@ -194,3 +196,70 @@ export const mergeParams = (searchParams: any[], domainParams: any[]) => {

return finalParams;
};

export const normalizeValues = (values: any) => {
// values object should be converted: fields that are empty strings should be undefined
return Object.keys(values).reduce((acc: any, key) => {
const value = values[key];
if (value !== "" && value !== undefined) {
acc[key] = value;
}
return acc;
}, {});
};

export const convertParamsToValues = (params: any[], fields?: any) => {
if (!params || !Array.isArray(params) || !fields) return {};

const values = params.reduce((acc: any, param) => {
// Handle array format [field, operator, value]
if (Array.isArray(param)) {
const [field, operator, value] = param;
const baseField = field.split("#")[0];
const type = fields?.[baseField]?.type;

if (type === "date") {
// Initialize array if not exists
if (!acc[baseField]) {
acc[baseField] = [null, null];
}
// Set the appropriate value in the array based on operator
if (operator === ">=") {
acc[baseField][0] = dayjs(value);
} else if (operator === "<=") {
acc[baseField][1] = dayjs(value);
}
} else if (type === "datetime") {
// For datetime, we need to split into date and time components
const dateObj = dayjs(value);
const baseKey = field.split("#")[0];

// Initialize arrays if they don't exist
if (!acc[baseKey + "#date"]) {
acc[baseKey + "#date"] = [null, null];
}
if (!acc[baseKey + "#time"]) {
acc[baseKey + "#time"] = [null, null];
}

// Set the appropriate values based on operator
if (operator === ">=") {
acc[baseKey + "#date"][0] = dateObj;
acc[baseKey + "#time"][0] = dateObj;
} else if (operator === "<=") {
acc[baseKey + "#date"][1] = dateObj;
acc[baseKey + "#time"][1] = dateObj;
}
} else {
// For other types, just set the value
acc[field] = value;
}
} else {
// Keep existing object format support
acc[param.id] = param.value;
}
return acc;
}, {});

return normalizeValues(values);
};
30 changes: 1 addition & 29 deletions src/widgets/views/searchFilter/SideSearchFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { SearchField } from "./SearchField";
import { SearchFields } from "@/types";

import { getParamsForFields } from "@/helpers/searchHelper";
import { getParamsForFields, normalizeValues } from "@/helpers/searchHelper";
import { useLocale } from "@gisce/react-formiga-components";
import { FloatingDrawer } from "@/ui/FloatingDrawer";
import debounce from "lodash.debounce";
Expand Down Expand Up @@ -255,31 +255,3 @@ export const SideSearchFooter = ({
</div>
);
};

const normalizeValues = (values: any) => {
// values object should be converted: fields that are empty strings should be undefined
return Object.keys(values).reduce((acc: any, key) => {
const value = values[key];
if (value !== "" && value !== undefined) {
acc[key] = value;
}
return acc;
}, {});
};

export const convertParamsToValues = (params: any[]) => {
if (!params || !Array.isArray(params)) return undefined;
return normalizeValues(
params.reduce((acc: any, param) => {
// Handle array format [field, operator, value]
if (Array.isArray(param)) {
const [field, , value] = param;
acc[field] = value;
} else {
// Keep existing object format support
acc[param.id] = param.value;
}
return acc;
}, {}),
);
};

0 comments on commit cefbc06

Please sign in to comment.