Skip to content

Commit

Permalink
Merge branch 'fix/improve-url-handling' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
mguellsegarra committed Jan 24, 2025
2 parents edc341c + 2887b23 commit 6760280
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/helpers/shareUrlHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ActionInfo, ActionRawData } from "@/types";

const OPEN_ACTION_PATH = "action";
// Parameters to exclude from the URL
const ALLOWED_VALUES_KEYS = ["active_id", "active_ids", "id"];
const IGNORED_PARAMS = ["target", "context", "domain", "fields"];

export const createShareOpenUrl = (action: ActionInfo) => {
Expand Down Expand Up @@ -73,9 +74,21 @@ const filterActionRawData = (actionRawData: ActionRawData) => {
typeof values === "object" &&
Object.keys(values).length > 0
) {
const { arch, ...restValues } = values; // ignore arch if exists
filteredData.values = restValues;
// Only include allowed keys from values
const filteredValues = filterAllowedValues(values);
filteredData.values =
Object.keys(filteredValues).length > 0 ? filteredValues : undefined;
}

// Return undefined if no properties were added to filteredData
return Object.keys(filteredData).length > 0 ? filteredData : undefined;
};

export const filterAllowedValues = (values: any) => {
if (!values || typeof values !== "object") {
return {};
}
return Object.fromEntries(
Object.entries(values).filter(([key]) => ALLOWED_VALUES_KEYS.includes(key)),
);
};
24 changes: 21 additions & 3 deletions src/views/RootView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { nanoid } from "nanoid";
import { useLocale } from "@gisce/react-formiga-components";
import { useConfigContext } from "@/context/ConfigContext";
import { DEFAULT_SEARCH_LIMIT } from "@/models/constants";
import { filterAllowedValues } from "@/helpers/shareUrlHelper";

type RootViewProps = {
children: ReactNode;
Expand Down Expand Up @@ -76,13 +77,29 @@ function RootView(props: RootViewProps, ref: any) {
}

async function handleOpenActionUrl(action: ActionInfo) {
const { actionRawData } = action;
const { actionRawData, res_id, initialView } = action;

const fields = await ConnectionProvider.getHandler().getFields({
model: action.model,
context: rootContext,
});

let values: Record<string, any> = filterAllowedValues(
actionRawData?.values,
);

const finalIdToRead: number | undefined =
res_id || values.active_id || values.id;

if (finalIdToRead) {
const readObjects = await ConnectionProvider.getHandler().readObjects({
model: action.model,
context: rootContext,
ids: [finalIdToRead],
});
values = { ...values, ...readObjects[0] };
}

let parsedContext;
if (
actionRawData?.context &&
Expand All @@ -96,7 +113,7 @@ function RootView(props: RootViewProps, ref: any) {
parseContext({
context: actionRawData.context,
fields,
values: { ...globalValues, ...(actionRawData.values || {}) },
values: { ...globalValues, ...(values || {}) },
});
} else {
parsedContext = {};
Expand All @@ -117,7 +134,7 @@ function RootView(props: RootViewProps, ref: any) {
) {
return await ConnectionProvider.getHandler().evalDomain({
domain: actionRawData.domain,
values: { ...(actionRawData.values || {}), ...globalValues },
values: { ...(values || {}), ...globalValues },
context: { ...rootContext, ...parsedContext },
fields,
});
Expand All @@ -135,6 +152,7 @@ function RootView(props: RootViewProps, ref: any) {
domain: parsedDomain,
actionRawData: {
...actionRawData,
values,
fields,
},
});
Expand Down

0 comments on commit 6760280

Please sign in to comment.