import React from 'react' import axios from 'axios' import { createArticle, updateArticle, deleteArticle, selectArticle, } from './controllers/articles' import { addTag, selectTag, removeTag } from './controllers/tags' import { createTask, updateTask, deleteTask, selectTask, completeTask, } from './controllers/tasks' import { createProject, //updateProject, deleteProject, selectProject, } from './controllers/projects' import { updateProfile } from './controllers/profile' import { About, Articles, Article, UpdateArticle, Tasks, UpdateTask, Profile, Projects, Project, Navbar, } from './components' const API = process.env.REACT_APP_API || 'http://localhost:1337' console.log('Using API at ' + API) const defaultState = { loading: true, user: { name: 'Scott' }, tasks: [], projects: [], component: 'projects', search: '', newArticle: '', newTask: '', newProject: '', project: {}, newTag: '', } class App extends React.Component { constructor() { super() this.state = defaultState this.navigate = this.navigate.bind(this) this.handleChange = this.handleChange.bind(this) this.handleSubmit = this.handleSubmit.bind(this) this.createArticle = createArticle.bind(this) this.updateArticle = updateArticle.bind(this) this.deleteArticle = deleteArticle.bind(this) this.addTag = addTag.bind(this) this.removeTag = removeTag.bind(this) this.createTask = createTask.bind(this) this.updateTask = updateTask.bind(this) this.deleteTask = deleteTask.bind(this) this.completeTask = completeTask.bind(this) this.createProject = createProject.bind(this) this.deleteProject = deleteProject.bind(this) this.updateProfile = updateProfile.bind(this) this.selectArticle = selectArticle.bind(this) this.selectTag = selectTag.bind(this) this.selectTask = selectTask.bind(this) this.selectProject = selectProject.bind(this) } async fetchArticles() { try { const { data } = await axios.get(API + '/api/articles') return data } catch (error) { console.log(error) this.setState({ error }) } } async fetchTasks() { try { const { data } = await axios.get(API + '/api/tasks') return data } catch (error) { console.log(error) this.setState({ error }) } } async fetchProjects() { try { const { data } = await axios.get(API + '/api/projects') return data } catch (error) { console.log(error) this.setState({ error }) } } async fetchComments() { try { const { data } = await axios.get(API + '/api/comments') return data } catch (error) { console.log(error) this.setState({ error }) } } async fetchTags() { try { const { data } = await axios.get(API + '/api/tags') return data } catch (error) { console.log(error) this.setState({ error }) } } async fetchVotes() { try { const { data } = await axios.get(API + '/api/votes') return data } catch (error) { console.log(error) this.setState({ error }) } } async fetchData() { const articles = await this.fetchArticles() const tags = await this.fetchTags() const tasks = await this.fetchTasks() const projects = await this.fetchProjects() const comments = await this.fetchComments() const votes = await this.fetchVotes() await this.setState({ loading: false, articles, tags, tasks, projects, comments, votes, }) } componentDidMount() { this.fetchData() } navigate(component) { this.setState({ component }) } handleChange(e) { this.setState({ [e.target.name]: e.target.value }) } handleSubmit(e) { e.preventDefault() this.setState({ search: '' }) } renderLoading() { return