hide the button after clicking inside map function -Redux

Viewed 116

I have multiple location info list with add button each of that location item. I want to hide that 'add location' button after adding operation. Only particular add button should be hided after clicking not all add button in that list

i have tried with button state true and false using setState and disabled. But it is disabling all the button in that list. I am using redux to add operation. I don't know how to do that part for unique location id

action for add opeartion:

export const addLocation = mruCode =>({
  type: ADD_LOCATION,
  payload:mruCode
});

action code for add all location:

export const addAllLocation = () =>({
    type : ALL_LOCATION
  });

reducer code for add all location:

case 'ALL_LOCATION':
            return{
                ...state,
                conLocations:[...state.location]
            }

reducer for add opeartion:

case 'ADD_LOCATION':
  let addedLoc = state.location.find(obj=>(obj.mruCode === action.payload))
  return{
    ...state,
    conLocations: [...state.conLocations,addedLoc]
  };

Component full code:

export class NewLocationPanel extends React.Component{
    constructor(props){
        super(props);
        this.state={
               open:false,
               disableButton:-1
        };
        this.togglePanel = this.togglePanel.bind(this);
        this.handleClick = this.handleClick.bind(this);
        this.allLocations = this.allLocations.bind(this);
    }
    togglePanel (e){
        this.setState({open : !this.state.open});
    }
    handleClick (mruCode){
        return this.props.addLocation(mruCode);
     }
     allLocations (){
       return this.props.addAllLocation();
    }

    componentDidMount() {
        this.props.loadData();
      }
    render(){
        const _labels = store.getLabels();
        let collapsedToggle = this.props.open ? 'collapsed' : ''
        return(
            <div className="panel panel-default">
            <div className="panel-heading" onClick={(e)=>this.togglePanel(e)}>
              <div className="row">
              <div className="col-xs-12 col-sm-8 col-md-6 col-lg-6 panelHeadingLabel">
                     <span>{this.props.title}</span>
                     </div>
                        <div className="pull-right">
                        <span className="defaultHeaderTextColor">{this.props.location.map((loc,index)=>loc.primary===true ? (<span>{loc.mruCode} - {_labels[loc.division]} - {loc.country}</span>):null)}
                           <span onClick={(e)=>this.togglePanel(e)} className={this.state.open ? "collapse-chevronn" : "collapse-chevron"} aria-hidden="true"></span>
                   </span>
                    </div>
                </div>
           </div>
              {this.state.open?(
                        <div className="panel-body">
                             <div className="row grid-divider">
                             <div className="col-sm-6">
                             <div className="col-padding"><div className="pos-div"><h3>Locations List</h3><button className="allLargeBtn" onClick={()=>{this.allLocations()}}>Add all locations</button></div><hr/>
                             {this.props.location.map((item,index)=>(
                             <div key={index}><div><b>{item.mruCode} - {_labels[item.division]} - {item.country}</b>{!this.props.conLocations.includes(item.mruCode)&&(<div className="pull-right jd"><button className="call-to-action" onClick={()=>{this.handleClick(item.mruCode)}}>Add Location</button></div>)}<hr/></div></div>))}
                            </div>
                             </div> 
                                  <div className="col-sm-6">
                                   <div><ConfiguredLocation/></div>
                                  </div>
                                  </div> 
                    </div>):null}

            </div>

        );
    }
}

const mapStateToProps = state =>{
    return{
        location:state.locationRed.location,
        conLocations:state.locationRed.conLocations
    };
};

const mapDispatchToProps = (dispatch) => {
    return{
        loadData:()=>{dispatch(loadData())},
        addLocation:(mruCode)=>{dispatch(addLocation(mruCode))},
        addAllLocation:() =>{dispatch(addAllLocation())}
    }
}


export default connect(mapStateToProps,mapDispatchToProps)(NewLocationPanel);

mruCode is primary unique id. particular add button should be hided after clicking. Please suggest me how to do that

1 Answers

If I'm understanding correctly, you have a list of buttons corresponding to locations, and you want to hide the buttons that have already been clicked. Right now your component doesn't have the list of already added locations, so you'll first need to pass that through in mapStateToProps

const mapStateToProps = state =>{
    return{
        location:state.locationRed.location,
        conLocations:state.conLocations
    };
};

Then in your view code, you can remove the button if the location is already added

{this.props.location.map((item,index)=>(
  <div key={index}>
    <div>
      <b>{item.mruCode} - {_labels[item.division]} - {item.country}</b>
      {!this.props.conLocations.find(item2 => item.mruCode === item2.mruCode) && (
        <div className="pull-right jd">
          <button
            className="call-to-action"
            onClick={()=>{this.handleClick(item.mruCode)}}
          >
            Add Location
          </button>
        </div>
      )}
      <hr/>
    </div>
  </div>
))}
Related