diff --git a/src/App.js b/src/App.js index ba0139e..e6fa053 100644 --- a/src/App.js +++ b/src/App.js @@ -77,23 +77,50 @@ class App extends React.Component { this.setState({ search: '' }) } - addTask(e) { + async addTask(e) { e.preventDefault() - if (this.state.newTask === '') { - console.log('Not adding empty task.') + const newTask = { + desc: this.state.newTask, + projectName: this.state.selectedProject, + // the backend expects projectName because it creates the project on the fly if none exists with that name + // this feature comes at the price that there can't be two independent projects with the same name + // which could be handy if users want to have private projects + } + if (newTask.desc === '') { + alert('Task description is empty.') + return + } else if (!newTask.projectName) { + alert('Select a project for new task.') return } - this.setState({ - tasks: this.state.tasks.concat({ - id: this.state.tasks.length, - desc: this.state.newTask, - project: this.state.project, - }), - newTask: '', - }) + try { + console.log('Adding task: ', newTask) + const { data, error } = await axios.post(api + '/api/tasks', newTask) + if (error) { + alert('Received error when adding task: ', error) + } else if (data.desc && data.projectId) { + console.log('Successfully added task:', data) + this.setState({ tasks: this.state.tasks.concat(data), newTask: '' }) + } else { + console.log('Received malformed data for added task: ', data) + } + } catch (e) { + alert('Failed to add task: ', e) + } } - deleteTask(id) { + async deleteTask(id) { this.setState({ tasks: this.state.tasks.filter(t => t.id !== id) }) + try { + const { data, error } = await axios.post(api + `/api/tasks/${id}/delete`) + if (error) { + alert('Received error when deleting task: ', error) + } else { + console.log('Successfully deleted task: ', data) + this.setState({ tasks: this.state.tasks.filter(t => t.id !== data.id), newTask: '' }) + } + } catch (e) { + alert('Failed to delete task: ', e) + } } completeTask(id) { const otherTasks = this.state.tasks.filter(t => t.id !== id) @@ -183,7 +210,7 @@ class App extends React.Component { const filtered = this.state.tasks.filter( task => task.completed !== true && - this.state.search === task.desc.slice(0, this.state.search.length) + this.state.search === task.desc.slice(0, this.state.search.length), ) const renderComponent = () => @@ -203,11 +230,7 @@ class App extends React.Component { return (