在我的应用程序中,有一个带有左右分隔的位置面板。在左侧,我使用动作和减速器功能列出了api的主要位置。在右侧,我正在从服务提供的jobdetailjson中检索某些位置,该位置包含特定作业ID的位置详细信息。我已经通过ref回调完成了这一部分的检索。两者都可以正常工作。
现在,我必须从左侧主要位置列表向右侧检索列表中添加一些位置项。怎么做的,因为他们不在同一个状态。
操作代码(主要位置部分)
export const loadData = () => {
return (dispatch) => {
return axios.get(serviceUrl)
.then(response=>{
dispatch(getLocationData(response.data.mruPreferences))
})
.catch(error=>{
throw(error);
});
};
};
export const addLocation = mruCode =>({
type: ADD_LOCATION,
payload:mruCode
});
减速器代码(主要位置部分)
case 'GET_DATA_SUCC':
return{
...state,
location:action.location
}
case 'ADD_LOCATION':
let addedLoc = state.location.find(obj=>(obj.mruCode === action.payload))
return{
...state,
conLocations: [...state.conLocations,addedLoc]
};
组件代码:
export class NewLocationPanel extends React.Component{
constructor(props){
super(props);
this.state={
open:false,
conLocations:[]
//configuredList:[]
};
this.configLocation = this.configLocation.bind(this);
this.togglePanel = this.togglePanel.bind(this);
this.handleClick = this.handleClick.bind(this);
this.allLocations = this.allLocations.bind(this);
this.clearall = this.clearall.bind(this);
this.getLocationData = this.getLocationData.bind(this);
}
togglePanel (e){
this.setState({open : !this.state.open});
}
handleClick (mruCode){
this.props.addLocation(mruCode)
}
allLocations (){
this.props.addAllLocation()
}
clearall (){
this.props.removeAllLocation()
}
componentDidMount() {
this.props.loadData();
}
componentDidUpdate(prevProps){
if ((prevProps.jobId != this.props.jobId || prevProps.jobDetailJson != this.props.jobDetailJson) && this.props.jobDetailJson != undefined) {
this.configLocation(this.props.jobDetailJson);
}
}
configLocation(jobDetailJson){
let conLocations = jobDetailJson.locations.locationDetails;
this.setState({conLocations});
console.log(conLocations);
}
render(){
//const{configuredList} = this.state;
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.state.conLocations.map((loc,index)=><span key={index}>{loc.mruCode} - {_labels[loc.division]} - {loc.country}</span>)}
<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 style={{ display: this.props.location.length === this.props.conLocations.length ? "none" : "block" }} 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.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>))}
</div>
</div>
<div className="col-sm-6">
<div className="col-padding">
<div className="pos-div"><h3>Configured Location</h3><button className="allLargeBtn" onClick={()=>{this.clearall()}}>Remove all location</button></div><hr/>
<div>{this.state.conLocations.map((locc,index)=><table className="table" key={index}><tbody><tr><td><b>{locc.mruCode} - {_labels[locc.division]} - {locc.country}</b></td><td className="text-right"><img alt="DeleteIcon" onClick={()=>this.handleRemove(loct.mruCode)}className="deleteIconStyle" src="img/delete_large_active.png" /></td></tr></tbody></table>)}</div>
<div><ConfiguredLocation/></div>
</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())},
removeAllLocation: () =>{dispatch(removeAllLocation())}
}
}
export default connect(mapStateToProps,mapDispatchToProps,null,{withRef:true})(NewLocationPanel);