Skip to content

Commit

Permalink
Fix editorjs error when saving new translation (#4991)
Browse files Browse the repository at this point in the history
* Fix editorjs error when loading

* Add changeset

* Update test
  • Loading branch information
poulch committed Jun 24, 2024
1 parent 2c855d1 commit ff54430
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-ducks-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

An error is no longer raised and visible to the user when saving translations
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,6 @@ const TranslationFields: React.FC<TranslationFieldsProps> = props => {
onDiscard={onDiscard}
onSubmit={data => onSubmit(field, data)}
/>
) : // FIXME
// For now this is the only way to fix the issue
// of initializing the editor with fetched data.
// Without this the editor doesn't get the saved data
// and is empty
disabled ? (
<Skeleton />
) : (
<TranslationFieldsRich
resetKey={richTextResetKey}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ const TranslationFieldsRich: React.FC<TranslationFieldsRichProps> = ({
onSubmit,
}) => {
const intl = useIntl();

const {
isReadyForMount,
handleSubmit,
defaultValue,
handleChange,
editorRef,
} = useRichTextSubmit(initial, onSubmit);
} = useRichTextSubmit(initial, onSubmit, disabled);

return edit ? (
<form onSubmit={handleSubmit}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ describe("useRichTextSubmit", () => {
const getValue = jest.fn(() => textEditorValue);
(useRichText as jest.Mock).mockImplementation(() => ({ getValue }));
const submitFn = jest.fn();
const { result } = renderHook(() => useRichTextSubmit("initial", submitFn));
const { result } = renderHook(() =>
useRichTextSubmit("initial", submitFn, false),
);

// When
await result.current.handleSubmit();
Expand Down
25 changes: 11 additions & 14 deletions src/translations/components/TranslationFields/useRichTextSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ import React from "react";
export function useRichTextSubmit(
initial: string,
onSubmit: (data: OutputData) => SubmitPromise,
loading: boolean,
) {
const { setIsDirty, setExitDialogSubmitRef } = useExitFormDialog();

const {
defaultValue,
editorRef,
isReadyForMount,
handleChange,
getValue,
} = useRichText({
initial,
triggerChange: () => setIsDirty(true),
});
const { defaultValue, editorRef, isReadyForMount, handleChange, getValue } =
useRichText({
initial,
loading,
triggerChange: () => setIsDirty(true),
});

const handleSubmit = React.useCallback(async () => {
const result = onSubmit(await getValue());
Expand All @@ -34,10 +31,10 @@ export function useRichTextSubmit(
return errors;
}, [getValue, onSubmit, setIsDirty]);

React.useEffect(() => setExitDialogSubmitRef(handleSubmit), [
handleSubmit,
setExitDialogSubmitRef,
]);
React.useEffect(
() => setExitDialogSubmitRef(handleSubmit),
[handleSubmit, setExitDialogSubmitRef],
);

return {
defaultValue,
Expand Down

0 comments on commit ff54430

Please sign in to comment.