From df439a2d1f83b863a2780b32bf319f72ddcc97c6 Mon Sep 17 00:00:00 2001 From: notnull Date: Fri, 21 Jun 2019 17:54:59 -0400 Subject: [PATCH] updated src/App.js, src/tasks.js, src/projects/js to fetch API data --- src/App.js | 71 +++++++++++++++++++++++--------------- src/components/project.js | 14 +++++--- src/components/projects.js | 7 +++- 3 files changed, 59 insertions(+), 33 deletions(-) diff --git a/src/App.js b/src/App.js index 00295d3..887c060 100644 --- a/src/App.js +++ b/src/App.js @@ -1,26 +1,18 @@ import React from 'react' +import axios from 'axios' + import { About, Tasks, Profile, Projects, Project, Navbar } from './components' const defaultState = { - user: {}, + user: { name: 'Scott' }, loading: true, tasks: [], search: '', newTask: '', newProject: '', - project: 0, -} - -// hard-coded for now, but can be passed anything and set on state -const data = { - user: { name: 'Scott', role: 'manager' }, - component: 'tasks', - tasks: [ - { id: 0, desc: 'arf', project: 1 }, - { id: 1, desc: 'bruf', project: 0 }, - { id: 2, desc: 'crap', project: 1 }, - ], - projects: [{ id: 0, name: 'mop' }, { id: 1, name: 'biz' }], + project: {}, + projects: [], + component: 'projects', } class App extends React.Component { @@ -38,15 +30,29 @@ class App extends React.Component { this.deleteProject = this.deleteProject.bind(this) } - fetchData() { - // later this can be an API request to some backend, or - // can get passed data from the native app - return data + async fetchTasks() { + console.log('Fetching tasks...') + const { data } = await axios.get('/api/tasks') + await this.setState({ tasks: data }) + } + + async fetchProjects() { + console.log('Fetching projects...') + const { data } = await axios.get('/api/projects') + await this.setState({ projects: data }) + } + + async fetchData() { + await this.fetchTasks() + await this.fetchProjects() + await this.setState({ loading: false }) + await console.log( + 'Ok, everything should be loaded now and nothing should render after this without input!' + ) } componentDidMount() { - const data = this.fetchData() - this.setState({ loading: false, ...data }) + this.fetchData() } navigate(component) { @@ -55,7 +61,6 @@ class App extends React.Component { handleChange(e) { this.setState({ [e.target.name]: e.target.value }) - //console.log( this.state ) } handleSubmit(e) { e.preventDefault() @@ -95,13 +100,19 @@ class App extends React.Component { newProject: '', }) } - editProject(id) { - this.setState({ project: id }) + + // TODO: 'editProject' sounds like the functionality refers to editing, not + // just setting it on the state, so this should maybe be renamed to + // selectProject + editProject(selectedProjectId) { + this.setState({ selectedProjectId }) this.navigate('project') } - deleteProject(id) { + async deleteProject(id) { //e.preventDefault() - this.setState({ projects: this.state.projects.filter(p => p.id !== id) }) + //this.setState({ projects: this.state.projects.filter(p => p.id !== id) }) + await axios.post(`/api/projects/${id}/delete`) + // TODO: some delete success function } renderLoading() { @@ -143,7 +154,6 @@ class App extends React.Component { return } renderTasks(filtered, completed) { - // {...this.state} passes everything on state to the component return ( task.completed === true) 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 = () => @@ -183,7 +194,11 @@ class App extends React.Component { return (
- + {renderComponent()}
) diff --git a/src/components/project.js b/src/components/project.js index e1e6471..8e5eda7 100644 --- a/src/components/project.js +++ b/src/components/project.js @@ -2,13 +2,19 @@ import React from 'react' import Tasks from './tasks' function Project(props) { - console.log(props.projects) - const filtered = props.tasks.filter(t => t.project === props.project && !t.completed) - const completed = props.tasks.filter(t => t.project === props.project && t.completed === true) + const projectName = + props.projects.find(p => p.id === props.selectedProjectId).name || 'Default' + const filtered = props.tasks.filter( + t => t.projectId === props.selectedProjectId && !t.completed + ) + const completed = props.tasks.filter( + t => + (t.projectId === props.selectedProjectId && t.completed === true) || null + ) return (
-

{props.projects[props.project].name}

+

{projectName}

) diff --git a/src/components/projects.js b/src/components/projects.js index 6e01336..2e4378d 100644 --- a/src/components/projects.js +++ b/src/components/projects.js @@ -1,6 +1,7 @@ import React from 'react' function Projects(props) { + console.log('PROJECTS PROPS', props) return (

Projects

@@ -13,7 +14,11 @@ function Projects(props) { > X - props.editProject(project.id)}>{project.name} + {props.selectedProjectId ? null : ( + props.editProject(project.id)}> + {project.name}{' '} + + )} ))}