import React, { Component } from 'react' import logo from './logo.svg' import './App.css' class App extends Component { state = { todos: [] } componentDidMount() { console.log('fetch items') fetch('/.netlify/functions/todos-read-all') .then((response) => { console.log(response) return response.json() }) .then((myJson) => { console.log(myJson) this.setState({ todos: myJson }) }) } saveTodo = (e) => { e.preventDefault(); if (!this.inputElement.value) { alert('Please add todo text') return false } fetch('/.netlify/functions/todos-create', { body: JSON.stringify({ title: this.inputElement.value }), method: 'POST', // mode: 'cors', // no-cors, cors, *same-origin }) .then(response => response.json()) .then((json) => { console.log(json) }).catch((e) => { console.log('errrrr', e) }) } deleteTodo = (e) => { const todoId = e.target.dataset.id console.log(`Delete todo ${todoId}`) fetch(`/.netlify/functions/todos-delete/${todoId}`, { method: 'POST', }) .then(response => response.json()) .then((json) => { console.log(json) }).catch((e) => { console.log('errrrr', e) }) } renderTodos() { const { todos } = this.state return todos.map((todo, i) => { const { data } = todo const id = todo.ref['@ref'].split('/').pop() return (
Using FaunaDB & netlify functions