Skip to content

Finished proj #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 106 additions & 1 deletion frontend/components/App.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,114 @@
import React from 'react'
import axios from "axios"
import TodoList from "./TodoList"
import Form from "./Form"

const URL = 'http://localhost:9000/api/todos'

export default class App extends React.Component {
constructor(){
super();
this.state ={
todos: [],
error: {
message: "",
status: false
},
todoNameInput: ""
}
}


handleClearButton = () => {
this.setState({
...this.state,
todos: this.state.todos.filter(element => {
return (element.completed === false)
})
})
}

toggleClick = (id) => {
axios.patch(`${URL}/${id}`)
.then(res => {
this.setState({
...this.state, todos: this.state.todos.map(element => {
if (element.id !== id) return element
return res.data.data
})
})
})
.catch(this.setAxiosResponseError)
}



onChangeTodoInput = (event) => {
const { value } = event.target
this.setState({
...this.state,
todoNameInput: value
})
}

setAxiosResponseError = err => this.setState({
...this.state, error: {
message: err.response.data.message,
status: true
}
})

fetchAllTodos = () => {
axios.get(URL)
.then(res => {
this.setState({
...this.state, todos: res.data.data, error: {}
})
})
.catch(this.setAxiosResponseError)
}

resetForm = () => {
this.setState({
...this.state,
todoNameInput: ""
})
}

postNewTodo = () => {
axios.post(URL, {name: this.state.todoNameInput})
.then(res => {
this.setState({
...this.state,
todos: this.state.todos.concat(res.data.data),
error: {
message: "",
status: false
}
})
this.resetForm();
})
.catch(this.setAxiosResponseError)
}

onSubmitTodo = (event) => {
event.preventDefault();
this.postNewTodo();
}

componentDidMount(){
this.fetchAllTodos();
}

render() {
return null
const { todos } = this.state;
return (
<div>
{this.state.error.status? <div id="error">Error: {this.state.error.message}</div> : <></>}
<h2>Todos:</h2>
<TodoList data={todos} toggleClick={this.toggleClick}/>
<Form todoInput={this.state.todoNameInput} onChangeTodoInput={this.onChangeTodoInput} onSubmitTodo={this.onSubmitTodo} handleClearButton={this.handleClearButton}></Form>
</div>
)
}
}
29 changes: 28 additions & 1 deletion frontend/components/Form.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
import React from 'react'

export default class Form extends React.Component {

inputChangeHandler = (event) => {
this.props.onChangeTodoInput(event)
}

handleSubmit = (event) => {
this.props.onSubmitTodo(event);
}

handleClear = () => {
this.props.handleClearButton();
}


render() {
return null
return (
<div>
<form id="todoForm" onSubmit={this.handleSubmit}>
<input
value={this.props.todoInput}
onChange={this.inputChangeHandler}
type="text"
placeholder='Type todo'
/>
<button type="submit">Submit</button>
</form>
<button onClick={this.handleClear}>Clear Completed</button>
</div>
)
}
}
9 changes: 8 additions & 1 deletion frontend/components/Todo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import React from 'react'

export default class Todo extends React.Component {

handleClick = () => {
this.props.toggleClick(this.props.data.id)
}

render() {
return null
return (
<li onClick={this.handleClick}>{this.props.data.name}{this.props.data.completed? "✔" : ""}</li>
)
}
}
10 changes: 9 additions & 1 deletion frontend/components/TodoList.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import React from 'react'
import Todo from "./Todo"

export default class TodoList extends React.Component {
render() {
return null

return (
<ul>
{this.props.data.map(element => {
return(<Todo key={element.id} data={element} toggleClick={this.props.toggleClick}/>)
})}
</ul>
)
}
}
39 changes: 15 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.