codex-team / codex-team/editor.js

Editor render issue with data props

Open
#2,509 8 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
31.9k
Forks
2.2k
Avg merge
1d 1h
Merged PRs (30d)
1

Description

I am waiting when I will get response with some structure and I need to pass it to Editor.

( I also see such error in console: `Block «paragraph» skipped because saved data is invalid` )

so on initial render editor triggers `onChange` method with empty data `{time: 1697134792817, blocks: Array(0), version: '2.28.0'}` ( I tested it using setTimeout )

and it overrides data I save in useState. That's why I see empty editor.

Full code :

```
import React from "react";
import { Button, styled, TextField } from "@mui/material";
import { PolicyCybertaskContext } from "..";
import { Column, Row } from "components/containers";
import Widget from "components/widget";
import { CopyAll } from "@mui/icons-material";
import {
UpdatePolicySectionDtoInput,
useUpdatePolicySectionMutation,
} from "lib/apollo/types";
import dynamic from "next/dynamic";
import { editorDataTransform } from "lib/utils/editorDataTransform";

interface Props {
onPrev: () => void;
onNext: () => void;
section: number;
item: number;
}

const Editor = dynamic(() => import("components/Editor"), {
ssr: false,
});

const PolicySections: React.FC = ({ onPrev, onNext, section, item }) => {
const { template, policy } = React.useContext(PolicyCybertaskContext);
const [error, setError] = React.useState();
const [answer, setAnswer] = React.useState({});
const [updateSection] = useUpdatePolicySectionMutation();

React.useEffect(() => {
if (!policy || !policy.policySections[section]) {
setAnswer({});
return;
}
const currentItem = policy.policySections[section].items[item];
if (!currentItem) {
setAnswer({});
} else {
setAnswer(editorDataTransform(currentItem));
}
}, [section, item, policy]);

const save = () => {
setError(undefined);
if (!policy?.policySections[section]) return;

const dto: UpdatePolicySectionDtoInput = {
id: policy.policySections[section].id,
items: policy.policySections[section].items[item]
? policy.policySections[section].items.map((a, index) => {
if (index === item) {
return JSON.stringify(answer);
}
return a;
})
: [...policy.policySections[section].items, JSON.stringify(answer)],
};

updateSection({ variables: { dto } });
};

const saveAndGoBack = () => {
if (answer.length === 0) {
setError("This field is required.");
return;
}
save();
onPrev();
};

const saveAndContinue = () => {
if (answer.blocks.length === 0) {
setError("This field is required.");
return;
}
save();
onNext();
};

const handleChange = (e: React.ChangeEvent) => {
setError(undefined);
setAnswer(e.target.value);
};

const copyExample = () => {
if (
template?.templatePolicy.templatePolicySections[section]
.templatePolicySectionItems[item].exampleAnswer
)
setAnswer(
template?.templatePolicy.templatePolicySections[section]
.templatePolicySectionItems[item].exampleAnswer
);
};

console.log(policy?.policySections)
console.log(item)
console.log(answer)

return (


{
template?.templatePolicy.templatePolicySections[section]
.templatePolicySectionItems[item].question
}




Example answer






{template?.templatePolicy.templatePolicySections[
section
].templatePolicySectionItems[item].exampleAnswer
.split("\n")
.map((line, i) => (

{line}


))}



Your answer



{JSON.stringify(answer)}


{
console.log('data', data)
setAnswer(data)
// if(data.blocks.length !== 0){
// setTimeout(()=> {setAnswer(data)},3000)
// }
}} editorblock={`editorjs-container-${section}`}/>



);
};

```
![Безымянный](https://github.com/codex-team/editor.js/assets/145126851/97a51f48-4ad8-4d5d-9222-e1128e0e762d)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.