updated src/App.js, src/tasks.js, src/projects/js to fetch API data
This commit is contained in:
parent
4ac6288ac7
commit
df439a2d1f
71
src/App.js
71
src/App.js
@ -1,26 +1,18 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
import axios from 'axios'
|
||||||
|
|
||||||
import { About, Tasks, Profile, Projects, Project, Navbar } from './components'
|
import { About, Tasks, Profile, Projects, Project, Navbar } from './components'
|
||||||
|
|
||||||
const defaultState = {
|
const defaultState = {
|
||||||
user: {},
|
user: { name: 'Scott' },
|
||||||
loading: true,
|
loading: true,
|
||||||
tasks: [],
|
tasks: [],
|
||||||
search: '',
|
search: '',
|
||||||
newTask: '',
|
newTask: '',
|
||||||
newProject: '',
|
newProject: '',
|
||||||
project: 0,
|
project: {},
|
||||||
}
|
projects: [],
|
||||||
|
component: 'projects',
|
||||||
// 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' }],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class App extends React.Component {
|
class App extends React.Component {
|
||||||
@ -38,15 +30,29 @@ class App extends React.Component {
|
|||||||
this.deleteProject = this.deleteProject.bind(this)
|
this.deleteProject = this.deleteProject.bind(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchData() {
|
async fetchTasks() {
|
||||||
// later this can be an API request to some backend, or
|
console.log('Fetching tasks...')
|
||||||
// can get passed data from the native app
|
const { data } = await axios.get('/api/tasks')
|
||||||
return data
|
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() {
|
componentDidMount() {
|
||||||
const data = this.fetchData()
|
this.fetchData()
|
||||||
this.setState({ loading: false, ...data })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
navigate(component) {
|
navigate(component) {
|
||||||
@ -55,7 +61,6 @@ class App extends React.Component {
|
|||||||
|
|
||||||
handleChange(e) {
|
handleChange(e) {
|
||||||
this.setState({ [e.target.name]: e.target.value })
|
this.setState({ [e.target.name]: e.target.value })
|
||||||
//console.log( this.state )
|
|
||||||
}
|
}
|
||||||
handleSubmit(e) {
|
handleSubmit(e) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
@ -95,13 +100,19 @@ class App extends React.Component {
|
|||||||
newProject: '',
|
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')
|
this.navigate('project')
|
||||||
}
|
}
|
||||||
deleteProject(id) {
|
async deleteProject(id) {
|
||||||
//e.preventDefault()
|
//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() {
|
renderLoading() {
|
||||||
@ -143,7 +154,6 @@ class App extends React.Component {
|
|||||||
return <About {...this.state} />
|
return <About {...this.state} />
|
||||||
}
|
}
|
||||||
renderTasks(filtered, completed) {
|
renderTasks(filtered, completed) {
|
||||||
// {...this.state} passes everything on state to the component
|
|
||||||
return (
|
return (
|
||||||
<Tasks
|
<Tasks
|
||||||
handleChange={this.handleChange}
|
handleChange={this.handleChange}
|
||||||
@ -159,11 +169,12 @@ class App extends React.Component {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
render() {
|
render() {
|
||||||
|
console.log(this.state)
|
||||||
const completed = this.state.tasks.filter(task => task.completed === true)
|
const completed = this.state.tasks.filter(task => task.completed === true)
|
||||||
const filtered = this.state.tasks.filter(
|
const filtered = this.state.tasks.filter(
|
||||||
task =>
|
task =>
|
||||||
task.completed !== true &&
|
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 = () =>
|
const renderComponent = () =>
|
||||||
@ -183,7 +194,11 @@ class App extends React.Component {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Navbar handleChange={this.handleChange} navigate={this.navigate} {...this.state} />
|
<Navbar
|
||||||
|
handleChange={this.handleChange}
|
||||||
|
navigate={this.navigate}
|
||||||
|
{...this.state}
|
||||||
|
/>
|
||||||
{renderComponent()}
|
{renderComponent()}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
@ -2,13 +2,19 @@ import React from 'react'
|
|||||||
import Tasks from './tasks'
|
import Tasks from './tasks'
|
||||||
|
|
||||||
function Project(props) {
|
function Project(props) {
|
||||||
console.log(props.projects)
|
const projectName =
|
||||||
const filtered = props.tasks.filter(t => t.project === props.project && !t.completed)
|
props.projects.find(p => p.id === props.selectedProjectId).name || 'Default'
|
||||||
const completed = props.tasks.filter(t => t.project === props.project && t.completed === true)
|
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 (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h2>{props.projects[props.project].name}</h2>
|
<h2>{projectName}</h2>
|
||||||
<Tasks {...props} filtered={filtered} completed={completed} />
|
<Tasks {...props} filtered={filtered} completed={completed} />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
function Projects(props) {
|
function Projects(props) {
|
||||||
|
console.log('PROJECTS PROPS', props)
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h3 className="mt-4">Projects</h3>
|
<h3 className="mt-4">Projects</h3>
|
||||||
@ -13,7 +14,11 @@ function Projects(props) {
|
|||||||
>
|
>
|
||||||
X
|
X
|
||||||
</button>
|
</button>
|
||||||
<span onClick={() => props.editProject(project.id)}>{project.name}</span>
|
{props.selectedProjectId ? null : (
|
||||||
|
<span onClick={() => props.editProject(project.id)}>
|
||||||
|
{project.name}{' '}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
<form className="" onSubmit={props.createProject}>
|
<form className="" onSubmit={props.createProject}>
|
||||||
|
Loading…
Reference in New Issue
Block a user