|
| 1 | +import React from "react"; |
| 2 | +import SidebarComponent from "./noteSidebar/noteSidebar"; |
| 3 | +import EditorComponent from "./noteEditor/noteEditor"; |
| 4 | +import "./styles/App.css"; |
| 5 | + |
| 6 | +const firebase = require("firebase"); |
| 7 | + |
| 8 | +class App extends React.Component { |
| 9 | + constructor() { |
| 10 | + super(); |
| 11 | + this.state = { |
| 12 | + selectedNoteIndex: null, |
| 13 | + selectedNote: null, |
| 14 | + notes: null, |
| 15 | + }; |
| 16 | + } |
| 17 | + |
| 18 | + render() { |
| 19 | + return ( |
| 20 | + <div className="app-container"> |
| 21 | + <SidebarComponent |
| 22 | + selectedNoteIndex={this.state.selectedNoteIndex} |
| 23 | + notes={this.state.notes} |
| 24 | + deleteNote={this.deleteNote} |
| 25 | + selectNote={this.selectNote} |
| 26 | + newNote={this.newNote} |
| 27 | + ></SidebarComponent> |
| 28 | + {this.state.selectedNote ? ( |
| 29 | + <EditorComponent |
| 30 | + selectedNote={this.state.selectedNote} |
| 31 | + selectedNoteIndex={this.state.selectedNoteIndex} |
| 32 | + notes={this.state.notes} |
| 33 | + noteUpdate={this.noteUpdate} |
| 34 | + ></EditorComponent> |
| 35 | + ) : null} |
| 36 | + </div> |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + componentDidMount = () => { |
| 41 | + firebase |
| 42 | + .firestore() |
| 43 | + .collection("notebook") |
| 44 | + .onSnapshot((serverUpdate) => { |
| 45 | + const notes = serverUpdate.docs.map((_doc) => { |
| 46 | + const data = _doc.data(); |
| 47 | + data["id"] = _doc.id; |
| 48 | + return data; |
| 49 | + }); |
| 50 | + console.log(notes); |
| 51 | + this.setState({ notes: notes }); |
| 52 | + }); |
| 53 | + }; |
| 54 | + |
| 55 | + selectNote = (note, index) => |
| 56 | + this.setState({ selectedNoteIndex: index, selectedNote: note }); |
| 57 | + noteUpdate = (id, noteObj) => { |
| 58 | + firebase.firestore().collection("notebook").doc(id).update({ |
| 59 | + title: noteObj.title, |
| 60 | + body: noteObj.body, |
| 61 | + timestamp: firebase.firestore.FieldValue.serverTimestamp(), |
| 62 | + }); |
| 63 | + }; |
| 64 | + newNote = async (title) => { |
| 65 | + const note = { |
| 66 | + title: title, |
| 67 | + body: "", |
| 68 | + }; |
| 69 | + const newFromDB = await firebase.firestore().collection("notebook").add({ |
| 70 | + title: note.title, |
| 71 | + body: note.body, |
| 72 | + timestamp: firebase.firestore.FieldValue.serverTimestamp(), |
| 73 | + }); |
| 74 | + const newID = newFromDB.id; |
| 75 | + await this.setState({ notes: [...this.state.notes, note] }); |
| 76 | + const newNoteIndex = this.state.notes.indexOf( |
| 77 | + this.state.notes.filter((_note) => _note.id === newID)[0] |
| 78 | + ); |
| 79 | + this.setState({ |
| 80 | + selectedNote: this.state.notes[newNoteIndex], |
| 81 | + selectedNoteIndex: newNoteIndex, |
| 82 | + }); |
| 83 | + }; |
| 84 | + deleteNote = async (note) => { |
| 85 | + const noteIndex = this.state.notes.indexOf(note); |
| 86 | + await this.setState({ |
| 87 | + notes: this.state.notes.filter((_note) => _note !== note), |
| 88 | + }); |
| 89 | + if (this.state.selectedNoteIndex === noteIndex) { |
| 90 | + this.setState({ selectedNoteIndex: null, selectedNote: null }); |
| 91 | + } else { |
| 92 | + this.state.notes.length > 1 |
| 93 | + ? this.selectNote( |
| 94 | + this.state.notes[this.state.selectedNoteIndex - 1], |
| 95 | + this.state.selectedNoteIndex - 1 |
| 96 | + ) |
| 97 | + : this.setState({ selectedNoteIndex: null, selectedNote: null }); |
| 98 | + } |
| 99 | + |
| 100 | + firebase.firestore().collection("notebook").doc(note.id).delete(); |
| 101 | + }; |
| 102 | +} |
| 103 | + |
| 104 | +export default App; |
0 commit comments