// import Component from the react module
import React, { Component } from "react";
import Modal from "./components/Modal";
import axios from 'axios';
class App extends Component {
constructor(props) {
super(props);
this.state = {
viewCompleted: false,
activeItem: {
title: "",
description: "",
completed: false
},
customersList: []
};
}
componentDidMount() {
this.refreshList();
}
refreshList = () => {
axios //Axios to send and receive HTTP requests
.get("http://localhost:8000/api/customers/")
.then(res => this.setState({ customersList: res.data }))
.catch(err => console.log(err));
};
displayCompleted = status => {
if (status) {
return this.setState({ viewCompleted: true });
}
return this.setState({ viewCompleted: false });
};
renderTabList = () => {
return (
this.displayCompleted(true)}
className={this.state.viewCompleted ? "active" : ""}
>
completed
this.displayCompleted(false)}
className={this.state.viewCompleted ? "" : "active"}
>
Incompleted
);
};
renderItems = () => {
const { viewCompleted } = this.state;
const newItems = this.state.taskList.filter(
(item) => item.completed === viewCompleted
);
return newItems.map((item) => (
{item.title}
));
};
toggle = () => {
//add this after modal creation
this.setState({ modal: !this.state.modal });
};
handleSubmit = (item) => {
this.toggle();
alert("save" + JSON.stringify(item));
if (item.id) {
// if old post to edit and submit
axios
.put(`http://localhost:8000/api/tasks/${item.id}/`, item)
.then((res) => this.refreshList());
return;
}
axios
.post("http://localhost:8000/api/customers/", item)
.then((res) => this.refreshList());
};
handleDelete = (item) => {
alert("delete" + JSON.stringify(item));
axios
.delete(`http://localhost:8000/api/tasks/${item.id}/`)
.then((res) => this.refreshList());
};
createItem = () => {
const item = { title: "", description: "", completed: false };
this.setState({ activeItem: item, modal: !this.state.modal });
};
editItem = (item) => {
this.setState({ activeItem: item, modal: !this.state.modal });
};
render() {
return (
GFG Task Manager
{this.renderTabList()}
{this.state.modal ? (
) : null}
);
}
}
export default App;