vini
vini

Reputation: 4740

React - How to check/uncheck checkboxes in a loop whose values come from redux store

I am trying to build a Notification settings via React and Redux where my against each email address I have few checkboxes say something in the below format.

 import React from 'react'

export class NotificationSettings extends React.Component {
constructor(props) {
    super(props);
    this.getEmailUserRows = this.getEmailUserRows.bind(this)
}


getEmailUserRows() {
    let _this = this
    let emailRows = this.props.emailRows
    let emailTemplate = emailRows.map((row) => {
        return (
            <tr key={row.email}>
                <td height="70">
                    <span class="txt-limit">
                        <small>{row.email}</small>
                    </span>
                </td>
                <td class="no-spc" height="70">
                    <span class="pure-checkbox tog-check">
                        <input
                            id={row.email+"sttng1"}
                            name={row.email+"sttng1"}
                            type="checkbox"/>
                        <label for="sttng1"></label>
                    </span>
                </td>
                <td class="no-spc" height="70">
                    <span class="pure-checkbox tog-check">
                        <input
                            id={row.email+"sttng2"}
                            name={row.email+"sttng2"}
                            type="checkbox"/>
                        <label for="sttng2"></label>
                    </span>
                </td>
                <td class="no-spc" height="70">
                    <span class="pure-checkbox tog-check">
                        <input
                             id={row.email+"sttng3"}
                            name={row.email+"sttng3"}
                            type="checkbox"/>
                        <label for="sttng3"></label>
                    </span>
                </td>
                <td class="no-spc" height="70">
                    <span class="pure-checkbox tog-check">
                        <input
                             id={row.email+"sttng4"}
                            name={row.email+"sttng4"}
                            type="checkbox"/>
                        <label for="sttng4"></label>
                    </span>
                </td>
                <td class="no-spc" height="70">
                    <span class="pure-checkbox tog-check">
                        <input
                            id={row.email+"sttng5"}
                            name={row.email+"sttng5"}
                            type="checkbox"
                            />
                        <label for="sttng5"></label>
                    </span>
                </td>
                <td class="no-spc" height="70">
                    <span class="icon-delete"></span>
                </td>

            </tr>
        )
    })

    return emailTemplate

}
render() {
    return (

        <div class="accordian-container open">
            <div class="webhook-dtl">
                <h4>Notification Settings</h4>
                <table
                    width="100%"
                    border="0"
                    cellSpacing="0"
                    cellPadding="0"
                    class="transaction-detail">
                    <tbody>
                        <tr class="tbl-hdng">
                            <td width="24%" height="52" align="left">Email ID</td>
                            <td width="19%" class="no-spc">Transactional<br/>Emails</td>
                            <td width="13%" class="no-spc">Settlement<br/>Emails</td>
                            <td width="12%" class="no-spc">CRM<br/>Emails</td>
                            <td width="12%" class="no-spc">Onboarding<br/>Emails</td>
                            <td width="10%" class="no-spc">Other<br/>Emails</td>
                            <td width="10%" class="no-spc">Action</td>
                        </tr>
                        {this.getEmailUserRows()}

                    </tbody>
                </table><br/>
                <br/>
                <button class="cmn-btn right">ADD&nbsp;MORE</button>

            </div>
        </div>

    )
}
}

export default NotificationSettings

My JSON that is stored in the redux store:

{
  "status": 0,
  "rows": 0,
  "message": "Merchant Details returned successfully",
  "result": [
    {
      "email": "[email protected]",
      "emailCategoryList": {
        "crmEmails": true,
        "settlementEmails": true,
        "transactionalEmails": true,
        "onboardingEmails": true,
        "otherEmails": true
      },
      "isMerchantBusinessEmail": true
    },
    {
      "email": "[email protected]",
      "emailCategoryList": {
        "crmEmails": true,
        "settlementEmails": false,
        "transactionalEmails": false,
        "onboardingEmails": true,
        "otherEmails": true
      },
      "isMerchantBusinessEmail": false
    }
  ],
  "errorCode": null,
  "guid": null
}

Pleae help me check/uncheck checkboxes in React not able to figure out what would be the best approach.

EDIT:

Made changes as suggested:

`import React from 'react';


let NotificationSettingRow = (props) => {
  let { emailRowData } = props

  const toggleCheckbox = () => {
   console.log("Yay")
  }


  return (
     <tr key={emailRowData.email}>
                <td height="70">
                    <span class="txt-limit">
                        <small>{emailRowData.email}</small>
                    </span>
                </td>
                <td class="no-spc" height="70">
                    <span class="pure-checkbox tog-check">
                        <input
                            id={emailRowData.email+"sttng1"}
                            name={emailRowData.email+"sttng1"}
                            {...(emailRowData.emailCategoryList.transactionalEmails ? {checked: true} : {}) }
                            onChange={toggleCheckbox}
                            type="checkbox"/>
                        <label for="sttng1"></label>
                    </span>
                </td>
                <td class="no-spc" height="70">
                    <span class="pure-checkbox tog-check">
                        <input
                            id={emailRowData.email+"sttng2"}
                            name={emailRowData.email+"sttng2"}
                            type="checkbox"/>
                        <label for="sttng2"></label>
                    </span>
                </td>
                <td class="no-spc" height="70">
                    <span class="pure-checkbox tog-check">
                        <input
                             id={emailRowData.email+"sttng3"}
                            name={emailRowData.email+"sttng3"}
                            type="checkbox"/>
                        <label for="sttng3"></label>
                    </span>
                </td>
                <td class="no-spc" height="70">
                    <span class="pure-checkbox tog-check">
                        <input
                             id={emailRowData.email+"sttng4"}
                            name={emailRowData.email+"sttng4"}
                            type="checkbox"/>
                        <label for="sttng4"></label>
                    </span>
                </td>
                <td class="no-spc" height="70">
                    <span class="pure-checkbox tog-check">
                        <input
                            id={emailRowData.email+"sttng5"}
                            name={emailRowData.email+"sttng5"}
                            type="checkbox"
                            />
                        <label for="sttng5"></label>
                    </span>
                </td>
                <td class="no-spc" height="70">
                    <span class="icon-delete"></span>
                </td>

            </tr>
  )
}


export default NotificationSettingRow`

However i am unable to check/uncheck checkboxes. Please help

Upvotes: 1

Views: 3434

Answers (1)

Davorin Ruševljan
Davorin Ruševljan

Reputation: 4392

Look at he last line in input, it basically evaluates if email in current row is in particular category, and if yes it renders checked=true, otherwise renders nothing

<input
   id={row.email+"sttng1"}
   name={row.email+"sttng1"}
   type="checkbox"
   {...(row.emailCategoryList.crmEmails ? {checked: true} : {}) }
/>

While you are at it you could define your component that would be used like:

<MyMailCheck row={row} idPrefix="sttng1" rowKey="crmEmails"/>

to avoid typing pretty much the same code for each email category.

Now how to handle checking and unchecking by the user. You need action that will be fired by the onClick for each checkbox. Something along these lines:

const emailCategoryClick = (email, category) => ({
  type: 'EMAIL_CATEGORY_CLICK',
  email,
  category
})

And you need suitable reducer to recognize this action, and update state accordingly.

Lastly, you need to wire your checkboxes to dispatch correct emailCategoryClick actions. To do so, you should connect your component to get access to dispatch method from store. Something like:

export default connect(
 (state) => ({}),
 (dispatch) => ({catClick: (email, cat) => () => {dispatch(emailCategoryClick(email, cat))} })
)(NotificationSettings)

and in your checkbox you would add onClick handler as in:

<input
   id={row.email+"sttng1"}
   name={row.email+"sttng1"}
   type="checkbox"
   {...(row.emailCategoryList.crmEmails ? {checked: true} : {}) }
   onCLick={catClick(row.email, "crmEmails")}
/>

Upvotes: 2

Related Questions