Skip to content

Commit

Permalink
fix: wait for active_id in form indicators (#748)
Browse files Browse the repository at this point in the history
* fix: adjust form indicators to not to fetch data until active id is set

gisce/webclient#1483

* fix: adjust dependencies

gisce/webclient#1483

* fix: adjust graphcard content

* fix: improve indicators with title
mguellsegarra authored Dec 11, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent b38035e commit 39db405
Showing 7 changed files with 79 additions and 20 deletions.
26 changes: 25 additions & 1 deletion src/context/FormContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState } from "react";
import React, { useContext, useRef, useState } from "react";
import { FormView } from "..";

export type FormContextType = {
@@ -80,4 +80,28 @@ const FormProvider = (props: FormProviderProps): any => {
);
};

export const useFormContext = () => {
const context = useContext(FormContext) as FormContextType;
if (!context) {
return {
activeId: undefined,
activeModel: "",
setFieldValue: () => {},
getFieldValue: () => "",
executeButtonAction: async () => {},
getContext: async () => ({}),
getValues: async () => ({}),
getPlainValues: () => ({}),
getFields: async () => ({}),
domain: [],
submitForm: async () => ({ succeed: false, id: 0 }),
fetchValues: () => {},
formHasChanges: () => false,
elementHasLostFocus: () => {},
formView: undefined,
} as FormContextType;
}
return context;
};

export default FormProvider;
8 changes: 7 additions & 1 deletion src/hooks/useFormGraphData.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ export const useFormGraphData = (actionId: number) => {
const formContext = useContext(FormContext) as FormContextType;
const { getContext, getPlainValues, activeId } = formContext || {};
const { globalValues, rootContext } = useConfigContext();
const context = useMemo(() => {
const context: any = useMemo(() => {
return { ...getContext?.(), ...rootContext };
}, [getContext, rootContext]);

@@ -28,6 +28,12 @@ export const useFormGraphData = (actionId: number) => {
...getPlainValues(),
},
});

if (!context.active_id) {
setActionData(result);
throw new Error("active_id_not_found");
}

const { views } = result as any;

const treeView = views.find((view: any[]) => {
1 change: 1 addition & 0 deletions src/locales/ca_ES.ts
Original file line number Diff line number Diff line change
@@ -108,4 +108,5 @@ export default {
resetTableView: "Restablir vista de taula",
not: "No",
loading: "Carregant...",
pendingToCalculate: "Pendent de calcular",
};
1 change: 1 addition & 0 deletions src/locales/en_US.ts
Original file line number Diff line number Diff line change
@@ -104,4 +104,5 @@ export default {
resetTableView: "Reset table view",
not: "Not",
loading: "Loading...",
pendingToCalculate: "Pending to calculate",
};
1 change: 1 addition & 0 deletions src/locales/es_ES.ts
Original file line number Diff line number Diff line change
@@ -110,4 +110,5 @@ export default {
resetTableView: "Restablecer vista de tabla",
not: "No",
loading: "Cargando...",
pendingToCalculate: "Pendiente de calcular",
};
42 changes: 31 additions & 11 deletions src/widgets/custom/Indicator.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useContext, useEffect } from "react";
import { Tooltip, theme, Statistic, Card } from "antd";
import { Tooltip, theme, Statistic, Card, Empty } from "antd";
import { Indicator as IndicatorOoui } from "@gisce/ooui";
import { WidgetProps } from "@/types";
import Field from "@/common/Field";
@@ -18,6 +18,9 @@ import {
TabManagerContextType,
} from "@/context/TabManagerContext";
import { GraphCard } from "../views/Graph";
import { useFormContext } from "@/context/FormContext";
import { useLocale } from "@gisce/react-formiga-components";
import styled from "styled-components";
const { useToken } = theme;

type IndicatorProps = WidgetProps & {
@@ -90,6 +93,7 @@ const GraphIndicatorInput = (props: IndicatorInputProps) => {
const { ooui } = props;
const { actionId, height } = ooui;

const { activeId } = useFormContext();
const { actionData, treeShortcut, loading, error, fetchData } =
useFormGraphData(actionId!);

@@ -107,9 +111,9 @@ const GraphIndicatorInput = (props: IndicatorInputProps) => {
}
fetchData();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ooui]);
}, [ooui, activeId]);

if (error) {
if (error && error.message !== "active_id_not_found") {
return <ErrorAlert error={error} />;
}

@@ -127,15 +131,31 @@ const GraphIndicatorInput = (props: IndicatorInputProps) => {
>
{loading && <CenteredSpinner />}
{!loading && (
<GraphComponent
view_id={initialView.id}
model={model}
context={context}
domain={domain}
limit={limit}
fixedHeight={height}
/>
<>
{!activeId ? (
<StyledEmpty
image={Empty.PRESENTED_IMAGE_SIMPLE}
imageStyle={{ height: 15 }}
/>
) : (
<GraphComponent
view_id={initialView.id}
model={model}
context={context}
domain={domain}
limit={limit}
fixedHeight={height}
/>
)}
</>
)}
</GraphCard>
);
};

const StyledEmpty = styled(Empty)`
&.ant-empty.ant-empty-normal {
margin: 0;
margin-top: 5px;
}
`;
20 changes: 13 additions & 7 deletions src/widgets/views/Dashboard/dashboardHelper.ts
Original file line number Diff line number Diff line change
@@ -25,13 +25,7 @@ export async function fetchAction({
fields: {},
});

const parsedDomain = dataForAction.domain
? await ConnectionProvider.getHandler().evalDomain({
domain: dataForAction.domain,
values: globalValues,
context: { ...rootContext, ...parsedContext },
})
: [];
const finalContext = { ...rootContext, ...parsedContext };

const {
res_model: model,
@@ -42,6 +36,18 @@ export async function fetchAction({
} = dataForAction;
const treeExpandable = view_type === "tree";

if (!finalContext.active_id) {
return { title };
}

const parsedDomain = dataForAction.domain
? await ConnectionProvider.getHandler().evalDomain({
domain: dataForAction.domain,
values: globalValues,
context: finalContext,
})
: [];

const finalViews = [];

for (const viewArray of views) {

0 comments on commit 39db405

Please sign in to comment.