Skip to content

Instantly share code, notes, and snippets.

@dimovdaniel
Created November 13, 2018 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimovdaniel/2bc6c2b6028598ad52b63b312e5bfcf2 to your computer and use it in GitHub Desktop.
Save dimovdaniel/2bc6c2b6028598ad52b63b312e5bfcf2 to your computer and use it in GitHub Desktop.
addItemToArray(name,howLongItIs){
console.log("Add item to array "+name);
console.log("Is just array "+this.state.isJustArray);
console.log("Data ");
console.log(this.state.currentInsertStructure);
var dataToInsert=null;
var correctPathToInsertIn="";
if(this.state.currentInsertStructure){
if(this.state.isJustArray){
console.log("THIS IS Array");
//Update on v 9.1.0 - the first item of inserts structure may not be numberic index
//Get the first key
var usedIndex=(Object.keys(this.state.currentInsertStructure))[0];
console.log("Used index is: "+usedIndex)
dataToInsert=this.state.currentInsertStructure[usedIndex];
correctPathToInsertIn=this.state.firebasePath+"/"+(parseInt(howLongItIs));
}else{
dataToInsert=this.state.currentInsertStructure[name];
dataToInsert=dataToInsert?dataToInsert[0]:null;
correctPathToInsertIn=this.state.firebasePath+"/"+name+"/"+(parseInt(howLongItIs));
}
}
console.log("Data to insert");
console.log(dataToInsert);
ReactDOM.findDOMNode(this).scrollIntoView();
if(dataToInsert!=null){
console.log("Save path: "+correctPathToInsertIn);
firebase.app.database().ref(correctPathToInsertIn).set(dataToInsert).then(()=>{
this.setState({notifications:[{type:"success",content:"Item is inserted."}]});
if(Config.adminConfig.goDirectlyInTheInsertedNode){
console.log("redirect to new page "+(parseInt(howLongItIs)));
this.props.router.push(this.props.route.path.replace(":sub","")+(this.props.params&&this.props.params.sub?this.props.params.sub:"")+Config.adminConfig.urlSeparator+(parseInt(howLongItIs)));
}
this.refreshDataAndHideNotification();
});
}else{
this.setState({notifications:[{type:"danger",content:"Error 201: We are missing correct insert schema."}]})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment