6

After getting the API response inside MenuItemDetail, I set it to the responses state, which is then passed down to Tabs, then TabContent. Here, I want to read correctly its resp and name props then display whatever they contain in the two corresponding editors i.e respEditor and nameEditor (resp and name fields in my json from API).

I can retrieve my resp and name from my API, but when I try to click on the textarea zone in order to add or modify the content I got a blank page, and get this error in the console:

Uncaught TypeError: contentState.getBlockMap is not a function

i've check this question on the forum : DraftJS - contentState.getBlockMap is not a function

I really really don't know why

const TabContent = ({  onChange, resp, , status }) => {
  const [respEditor, setRespEditor] = useState(
    EditorState.createWithContent(convertFromHTML(resp !== null ? resp : ""))
  );

  function respChange(state) {
    setRespEditor(state);
    onChange({
      resp: convertToRaw(state.getCurrentContent()),
      status
    });
  }



  let handle = async (e) => {
    e.preventDefault();
    try {
      let res = await fetch("", {
        method: "POST",
        body: JSON.stringify({
          resp: convertToHTML(respEditor.getCurrentContent()),             
          status: status
        })
      });
    } catch (err) {
      console.log(err);
    }
  };

  return (
    <form onSubmit={handle}>        
      <Editor
        editorState={respEditor}
        onEditorStateChange={respChange}
        wrapperclassName="wrapperclassName"
        editorclassName="editorclassName"
        toolbar={{
          options: ["inline"],
          inline: { inDropdown: false }
        }}
      />

      
          <input
            type="radio"
            value="B"
            onChange={(e) =>
              onChange({
                resp,
                status: e.target.value
              })
            }          
          <input
         
        </span>
      </div>

    
    </form>
  );
};

Here is my json from api for menuId:1:

[
  {
    "menuId": 1,
    "name": "Menu1", 
    "trust":1,   
    "dishes": {
      "meat": "N/A",
      "vegetables": "pea"
    },
    "list": [
      {
        "resp": "resp1",
        "question": "question1",
        "link": "",
        "name": "Name1",
        "status": "Finished"
      },
      {
        "resp": "resp2",
        "question": "question2",
        "link": "http://mylink.com",
        "name": "Name2",
        "status": "Saved"
      }
    ]
  }
]
Zokulko
  • 211
  • 4
  • 25

1 Answers1

6

Solution Codesanbox: https://codesandbox.io/s/react-wysiwyg-draft-js-ecmpth?file=/src/TabContent.js

Edited

You can use convertFromHTML from draft-convert to parse HTML string to EditorContent. Install draft-convert, which is the recommended way in the official example. (reference: https://stackoverflow.com/a/36904924/9744063)

npm install draft-convert
import { convertFromHTML } from ‘draft-convert’;

const [respState, setRespState] = useState(EditorState.createWithContent(convertFromHTML(resp)));
const [nameState, setNameState] = useState(EditorState.createWithContent(convertFromHTML(name)));
Son Nguyen
  • 1,472
  • 8
  • 16
  • Thanks for your reply, i've tried but I still can't see my data from my api, typically, I'm not able to see my `resp` and `name` fields... I've added the output of my json for example in the issue – Zokulko Jun 20 '22 at 19:10
  • @Zokulko Those `resp` and `name` fields that you don't see, what are you referring to? Is it (1) the API response from your server, or (2) the body fields of your API request, or (3) the props that `TabContent` accept? – Son Nguyen Jun 21 '22 at 00:47
  • If you can get the plain/html text like I said then the request should have no problem sending those fields. See my Codesandbox, open the console and press submit to see it correctly gets the HTML text of each editor – Son Nguyen Jun 21 '22 at 00:49
  • The thing is that I'm not even able to retrieve those 2 fields in my data i.e `resp` and `name` from the output json from my API (I've added it in the issue) and display them (I can display all the other fields except those 2). I can see in my console log the content of my data but I'm enable to display the content of `resp` and `name` in the textarea box... The output that I've added in my issue is the data from my API. – Zokulko Jun 21 '22 at 10:45
  • @Zokulko oh wait, do you mean that you want to pass something like `Hello` into `resp` prop of `TabContent` and see the bold `Hello` text inside that resp editor? – Son Nguyen Jun 21 '22 at 14:30
  • Yeah could be this example with bold, italic or without it. But it's not working for both cases `resp` and `name`. Suppose that the user have written `Hello` in my textarea, the data in my api will have this field : `resp:Hello`, but if I want to get this info from my api: `resp` in bold (or not) nothing is printed. Same issue for `name`. – Zokulko Jun 21 '22 at 14:56
  • what is the difference between this `const [respState, setRespState] = useState(EditorState.createEmpty());` and what I've written `const [respState, setRespState] = useState(() => EditorState.createEmpty());` please ? – Zokulko Jun 21 '22 at 17:52
  • 1
    @Zokulko the expression you wrote means `respState` is a function and thus is not valid to pass into Editor. Removing the `() =>` makes it an EditorState, which is what it should be – Son Nguyen Jun 21 '22 at 23:16
  • @Zokulko this is confusing. Is the API you are referring to the `fetch` call inside TabContent or something else, because you’re not reading the response from that fetch call, but in Tabs you do pass in some `data` which I assume is the API response you are talking about. Please try to explain your data flow through each component clearly – Son Nguyen Jun 21 '22 at 23:24
  • Right, I've modified my issue by adding more code. I'm calling my `Tabs` in another component `MenuItemDetail`. Is it better ? – Zokulko Jun 22 '22 at 10:36
  • @Zokulko It is. To confirm one more time before I write you a solution, is this what you want: after getting the API response inside `MenuItemDetail`, you set it to the `responses` state, which is then passed down to Tabs, then TabContent. Here, you want to read its `resp` and `name` props then display whatever they contain in the two corresponding editors, correct? – Son Nguyen Jun 22 '22 at 10:49
  • Yeah correct ! it's what I want... – Zokulko Jun 22 '22 at 10:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245817/discussion-between-son-nguyen-and-zokulko). – Son Nguyen Jun 22 '22 at 11:21
  • I've got a weird thing happening, I let you a message in the chat (don't know if we receive the notifs in the chat?) – Zokulko Jun 22 '22 at 12:04
  • @Zokulko I've replied – Son Nguyen Jun 22 '22 at 15:05
  • I've send you the link for my code – Zokulko Jun 24 '22 at 08:59
  • @Zokulko Let's continue in the chat – Son Nguyen Jun 27 '22 at 04:25