Skip to content

Commit

Permalink
fix: improve url actions in order to ditch fields and re get them on …
Browse files Browse the repository at this point in the history
…resume (#844)

* fix: bug where domain was null in handle open action url

* fix: form rendering issue

* fix: pass plain values to actions

* fix: improve url actions in order to ditch fields and re get them on resume

* fix: only pass certain values and reread register on resume
  • Loading branch information
mguellsegarra authored Jan 24, 2025
1 parent 0c233d5 commit e56f6c3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/actionbar/FormActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function FormActionBar({ toolbar }: { toolbar: any }) {
(actionData: any) => {
processAction?.({
actionData,
values: (formRef.current as any).getValues(),
values: (formRef.current as any).getPlainValues(),
fields: (formRef.current as any).getFields(),
context: (formRef.current as any).getContext(),
onRefreshParentValues: () => (formRef.current as any).fetchValues(),
Expand Down Expand Up @@ -329,7 +329,7 @@ function FormActionBar({ toolbar }: { toolbar: any }) {
if (result.succeed) {
openRelate({
relateData: relate,
values: (formRef.current as any).getValues(),
values: (formRef.current as any).getPlainValues(),
fields: (formRef.current as any).getFields(),
action_id: relate.id,
action_type: relate.type,
Expand Down
35 changes: 19 additions & 16 deletions src/helpers/shareUrlHelper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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) => {
const url = new URL(window.location.origin);
url.pathname = OPEN_ACTION_PATH;

// Parameters to exclude from the URL
const ignoredParams = ["target", "context", "domain"];

const finalAction = {
...action,
actionRawData:
Expand All @@ -18,7 +18,7 @@ export const createShareOpenUrl = (action: ActionInfo) => {
// Add all non-null properties from action to URL
Object.entries(finalAction).forEach(([key, value]) => {
if (
!ignoredParams.includes(key) &&
!IGNORED_PARAMS.includes(key) &&
value &&
(!Array.isArray(value) || value.length > 0)
) {
Expand All @@ -37,7 +37,7 @@ const convertToString = (value: any): string => {
};

const filterActionRawData = (actionRawData: ActionRawData) => {
const { context, domain, values, fields } = actionRawData;
const { context, domain, values } = actionRawData;

const filteredData: Partial<ActionRawData> = {};

Expand Down Expand Up @@ -67,25 +67,28 @@ const filterActionRawData = (actionRawData: ActionRawData) => {
}
}

// Include values and fields only if they are non-empty objects
// Include values only if they are non-empty objects
if (
(filteredData.domain || filteredData.context) &&
values &&
typeof values === "object" &&
Object.keys(values).length > 0
) {
const { arch, ...restValues } = values; // ignore arch if exists
filteredData.values = restValues;
}
if (
(filteredData.domain || filteredData.context) &&
fields &&
typeof fields === "object" &&
Object.keys(fields).length > 0
) {
filteredData.fields = fields;
// 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)),
);
};
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ type GetViewRequest = {

type GetFieldsRequest = {
model: string;
fields: string[];
fields?: string[];
context?: any;
};

Expand Down
43 changes: 32 additions & 11 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 @@ -75,7 +76,28 @@ 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 (
Expand All @@ -89,8 +111,8 @@ function RootView(props: RootViewProps, ref: any) {
actionRawData &&
parseContext({
context: actionRawData.context,
fields: actionRawData.fields || {},
values: { ...globalValues, ...(actionRawData.values || {}) },
fields,
values: { ...globalValues, ...(values || {}) },
});
} else {
parsedContext = {};
Expand All @@ -111,14 +133,9 @@ function RootView(props: RootViewProps, ref: any) {
) {
return await ConnectionProvider.getHandler().evalDomain({
domain: actionRawData.domain,
values: actionRawData.fields
? transformPlainMany2Ones({
fields: actionRawData.fields,
values: { ...(actionRawData.values || {}), ...globalValues },
})
: {},
values: { ...(values || {}), ...globalValues },
context: { ...rootContext, ...parsedContext },
fields: actionRawData.fields,
fields,
});
}
return [];
Expand All @@ -132,7 +149,11 @@ function RootView(props: RootViewProps, ref: any) {
...action,
context: { ...rootContext, ...parsedContext },
domain: parsedDomain,
actionRawData,
actionRawData: {
...actionRawData,
values,
fields,
},
});
}

Expand Down
11 changes: 7 additions & 4 deletions src/widgets/views/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,13 @@ function Form(props: FormProps, ref: any) {
);
};

const getCurrentValues = (fields: any) => {
const currentValues = antForm.getFieldsValue(true);
return processValues(currentValues, fields);
};
const getCurrentValues = useCallback(
(fields: any) => {
const currentValues = antForm.getFieldsValue(true);
return processValues(currentValues, fields);
},
[antForm],
);

const setFieldValue = (field: string, value?: string) => {
assignNewValuesToForm({
Expand Down

0 comments on commit e56f6c3

Please sign in to comment.