Skip to content

Commit

Permalink
Merge pull request #279 from gisce/hotfix/bug_webclient#436
Browse files Browse the repository at this point in the history
Fix for adjusting return value in submitForm for reports, actions, related and attachments
  • Loading branch information
mguellsegarra authored Apr 11, 2023
2 parents eb1e043 + d9675a4 commit fc43dc8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 24 deletions.
4 changes: 2 additions & 2 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gisce/react-ooui",
"version": "1.1.33",
"version": "1.1.34",
"files": [
"dist",
"src",
Expand Down
71 changes: 56 additions & 15 deletions src/actionbar/FormActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,13 @@ function FormActionBar({ toolbar }: { toolbar: any }) {
if (!action) {
return;
}
const savedSucceed = await onFormSave?.();

if (!savedSucceed) {
const result = await saveNewDocumentIfNeeded({
onFormSave,
currentId,
});

if (!result.succeed) {
return;
}

Expand All @@ -361,15 +365,21 @@ function FormActionBar({ toolbar }: { toolbar: any }) {
if (!report) {
return;
}
const savedSucceed = await onFormSave?.();
const result = await saveNewDocumentIfNeeded({
onFormSave,
currentId,
});

if (!savedSucceed) {
if (!result.succeed) {
return;
}

runAction({
...report,
datas: { ...(report.datas || {}), ids: [currentId as number] },
datas: {
...(report.datas || {}),
ids: [result.currentId as number],
},
});
}}
/>
Expand All @@ -383,9 +393,12 @@ function FormActionBar({ toolbar }: { toolbar: any }) {
return;
}

const savedSucceed = await onFormSave?.();
const result = await saveNewDocumentIfNeeded({
onFormSave,
currentId,
});

if (!savedSucceed) {
if (!result.succeed) {
return;
}

Expand All @@ -402,13 +415,16 @@ function FormActionBar({ toolbar }: { toolbar: any }) {
disabled={mustDisableButtons}
attachments={attachments}
onAddNewAttachment={async () => {
const savedSucceed = await onFormSave?.();
const result = await saveNewDocumentIfNeeded({
onFormSave,
currentId,
});

if (!savedSucceed) {
if (!result.succeed) {
return;
}

const res_id = currentId as number;
const res_id = result.currentId as number;
const res_model = currentModel as string;
openDefaultActionForModel({
model: "ir.attachment",
Expand All @@ -423,13 +439,16 @@ function FormActionBar({ toolbar }: { toolbar: any }) {
});
}}
onListAllAttachments={async () => {
const savedSucceed = await onFormSave?.();
const result = await saveNewDocumentIfNeeded({
onFormSave,
currentId,
});

if (!savedSucceed) {
if (!result.succeed) {
return;
}

const res_id = currentId as number;
const res_id = result.currentId as number;
const res_model = currentModel as string;
openDefaultActionForModel({
model: "ir.attachment",
Expand All @@ -441,9 +460,12 @@ function FormActionBar({ toolbar }: { toolbar: any }) {
});
}}
onViewAttachmentDetails={async (attachment: Attachment) => {
const savedSucceed = await onFormSave?.();
const result = await saveNewDocumentIfNeeded({
onFormSave,
currentId,
});

if (!savedSucceed) {
if (!result.succeed) {
return;
}

Expand All @@ -462,4 +484,23 @@ function separator() {
return <div className="inline-block w-2" />;
}

async function saveNewDocumentIfNeeded({
onFormSave,
currentId,
}: {
onFormSave?: () => Promise<{ succeed: boolean; id: number }>;
currentId: number | undefined;
}): Promise<{ succeed: boolean; currentId?: number }> {
if (currentId !== undefined) {
return { succeed: true, currentId: currentId };
}
const result = await onFormSave?.();

if (result?.succeed) {
return { succeed: true, currentId: result.id };
} else {
return { succeed: false, currentId: undefined };
}
}

export default FormActionBar;
4 changes: 2 additions & 2 deletions src/context/ActionViewContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type ActionViewContextType = {
setFormIsSaving?: (value: boolean) => void;
formHasChanges?: boolean;
setFormHasChanges?: (value: boolean) => void;
onFormSave?: () => Promise<void>;
onFormSave?: () => Promise<{ succeed: boolean; id: number }>;
formRef?: any;
searchTreeRef?: any;
onNewClicked: () => void;
Expand Down Expand Up @@ -142,7 +142,7 @@ const ActionViewProvider = (props: ActionViewProviderProps): any => {
}, [currentView]);

const callOnFormSave = async () => {
await (formRef.current as any)?.submitForm();
return await (formRef.current as any)?.submitForm();
};

return (
Expand Down
2 changes: 1 addition & 1 deletion src/context/FormContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type FormContextType = {
domain: any[];
submitForm?: (options?: {
callOnSubmitSucceed?: boolean;
}) => Promise<boolean>;
}) => Promise<{ succeed: boolean; id: number }>;
fetchValues?: () => void;
formHasChanges?: () => boolean;
elementHasLostFocus?: () => void;
Expand Down
6 changes: 3 additions & 3 deletions src/widgets/views/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -614,13 +614,13 @@ function Form(props: FormProps, ref: any) {
formSubmitting.current = false;
setFormHasChanges?.(false);
onCancel?.();
return true;
return { succeed: true, id: getCurrentId()! };
}

if (await checkIfFormHasErrors()) {
formSubmitting.current = false;
formErrorsDialog(lang);
return false;
return { succeed: false, id: getCurrentId()! };
}

setIsSubmitting(true);
Expand Down Expand Up @@ -653,7 +653,7 @@ function Form(props: FormProps, ref: any) {
setIsSubmitting(false);
}

return submitSucceed;
return { succeed: submitSucceed, id: getCurrentId()! };
};

const parseForm = ({
Expand Down

0 comments on commit fc43dc8

Please sign in to comment.