Slide 32
Slide 32 text
addEntry = async (title = "Untitled", body) => {
const { currentUser } = this.props;
const newEntry = {
title,
body,
owner_id: currentUser.id,
author: currentUser.profile.data.email,
date: new Date(),
sharedWith: []
};
// Add newEntry to MongoDB here
const result = await this.entries.insertOne(newEntry);
newEntry._id = result.insertedId;
// Add newEntry to Component State
this.setState(({ entries }) => ({
entries: [...entries, newEntry]
}));
};
removeEntry = async entryId => {
// Delete the entry from MongoDB
await this.entries.deleteOne({ _id: entryId });
// Remove Entry from Component State
this.setState(({ entries }) => ({
entries: entries.filter(entry => entry._id !== entryId)
}));
};
updateEntry = async (entryId, newBody) => {
// Update the Entry body in MongoDB
await this.entries.updateOne({ _id: entryId }, { $set: { body: newBody } });
// Update the Entry body and disable editing in Component State
this.setState(({ entries }) => ({
entries: entries.map(
entry =>
entry._id === entryId
? { ...entry, body: newBody, isEditable: false }
: entry
)
}));
};