import React, { Component } from ‘react’;
import * as firebase from ‘firebase’;
import swal from ‘sweetalert’;
import Nav from ‘./Nav’;
import NodeForm from ‘./NodeForm’;
export class Node extends Component {
constructor() {
super();
this.state = {
keys : [],
formAdd : false,
node : [],
edit : null
};
}
componentDidMount() {
const rootRef = firebase.database().ref();
const nodeRef = rootRef.child(‘node’);
nodeRef.on(‘value’, snap => {
if(snap.val()){
let node = snap.val();
let keys = Object.keys(snap.val());
let nodeTemp = node;
this.setState({
node : nodeTemp,
keys : keys
});
// console.log(‘Node on Firebase’);
// console.log(this.state.node);
swal(“Good job!”, “Load data node successful”, “success”);
} else {
swal(“NO DATA!”, “NO DATA node at present”, “warning”);
}
});
}
renderNode(){
if(JSON.stringify(this.state.node) === ‘{}’){
return (
);
} else {
return this.state.keys.map((id, index) =>{
return (
);
});
}
}
renderEditForm(id){
this.setState({
edit : id
})
console.log(‘REDER EDIT’);
// console.log(this.state.node[id]);
}
handleRemove(id){
swal({
title: “Are you sure?”,
text: “Once deleted, you will not be able to recover this data!”,
icon: “warning”,
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
const rootRef = firebase.database().ref();
const nodeRef = rootRef.child(‘node/’ + id);
rootRef.child(‘node/’ + id).remove();
this.setState({
node : nodeRef.val()
});
swal(“Poof! Your data has been deleted!”, {
icon: “success”,
});
} else {
swal(“Your data is safe!”);
}
});
}
toggleForm(){
this.setState({
formAdd : !this.state.formAdd
});
}
renderTopButton(){
if(this.state.formAdd){
return(
)
} else {
return(
)
}
}
addNode(data){
const rootRef = firebase.database().ref();
const nodeRef = rootRef.child(‘node’);
const newKeyNode = nodeRef.push().key;
this.setState({
edit : null
});
if(data.id){
nodeRef.update({
[data.id] : {
node_name : data.node_name
}
});
swal(“Good job!”, “Edit data node successful”, “success”);
} else {
nodeRef.update({
[newKeyNode] : {
node_name : data.node_name
}
});
swal(“Good job!”, “Add data node successful”, “success”);
}
}
render(){
return(
{ this.state.formAdd &&
}
node_name |
---|
)
}
}
export default Node;