2

In order to have my check boxes on click i get the document/box id and the document number . Like this :

handleCheckboxClick = (e) => {
    let parsedVal = JSON.parse(e.target.value);
    let newDocList = { ...this.state.docList };
    if (e.target.checked) {
        newDocList[parsedVal.documentId] = parsedVal.documentNumber;
    } else {
        delete newDocList[parsedVal.documentId];
    }
    this.setState({
        docList: newDocList,
    }, () => {
        console.log(this.state.docList)
    });
};

After that when I do a call to the back end it returns me a pdf's which I convert into a ZIP file and after that, I automatically download them for the user. Besides that, I get the JSON response with the downloaded document ID's . My goal is to disable my check boxes when a document I downloaded. At the moment I have this :

 .then((response) => {
            this.setState({
                downloadedIDS : response.data.downloadedIds
            })

And this is the result ouput in the console.log

enter image description here

Further more my Checkbox is treating the disable like this :

  <Checkbox disabled ={ downloadedIDS > 0 && true }
            color='default' 
            value={JSON.stringify({documentId:rowData.documentId,documentNumber: rowData.documentNumber })} 
            onClick={this.handleCheckboxClick} />

And I end up with result that each one of the boxes no matter selected or not are disabled. So I ask myself what am I doing wrong ? Can somebody help me? Thank you.

2
  • When do you want to disable your checkbox? Commented Nov 18, 2019 at 8:36
  • @ShivratnaKumar after the response from the back end . Or from my side when the download button is clicked and user recieves the ZIP i want to disable the downloaded documents ( or the previously selected i should say ) Commented Nov 18, 2019 at 8:38

2 Answers 2

2

If I am getting your condition correctly, you can do something like this:-

<Checkbox disabled={downloadedIDS && downloadedIDS.indexOf(rowData.documentId) >= 0 ? true : false}
     color='default' 
     value={JSON.stringify({documentId:rowData.documentId,documentNumber: rowData.documentNumber })} 
     onClick={this.handleCheckboxClick}
/>
Sign up to request clarification or add additional context in comments.

6 Comments

I'm afraid that this will not help me at all . Because the app is crashing after this change.
Invoices.js:585 Uncaught TypeError: Cannot read property 'length' of undefined
@PandaMastr I have updated my answer {downloadedIDS && downloadedIDS.length ? true : false}, please check.
@PandaMastr I have the disable condition as disabled={downloadedIDS && downloadedIDS.indexOf(rowData.documentId) >= 0 ? true : false}. Please try this.
Update your answer so i can mark it as working . Thanks mate !
|
1

downloadedIDS is the state object key .SO you need call this.state.downloadedIDS

<Checkbox disabled={this.state.downloadedIDS && this.state.downloadedIDS.length > 0 ? true : false}
     color='default' 
     value={JSON.stringify({documentId:rowData.documentId,documentNumber: rowData.documentNumber })} 
     onClick={this.handleCheckboxClick}
/>

Updated

You need trigger getZip function while componentMount.Add the code on invoices.

componentMount(){
 this.getZip()
}

And remove the do string on end of response.data.downloadedIds

this.setState({
          downloadedIDS: response.data.downloadedIds
        });

And add constructor on class start

constructor(props) {
    super(props);
    this.setState({
      downloadedIDS: []
    });
  }

7 Comments

there is no need to call this.state if i have declared it after my render like const { downloadedIDS } = this.state . I got the same result all the check boxes are disabled ( no matter selected or not ) after the document is downloaded
ok can you provide working sanbox to me codesandbox.io/s/new
i have tried to create a sandbox , but it doesn't work .
can you share that sandbox link
your sandbox was empty.please save the code before sandbox share
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.