diff --git a/functions/todos-delete-batch.js b/functions/todos-delete-batch.js new file mode 100644 index 0000000..9ae8c1c --- /dev/null +++ b/functions/todos-delete-batch.js @@ -0,0 +1,33 @@ +/* code from functions/todos-delete-batch.js */ +/* Import faunaDB sdk */ +const faunadb = require('faunadb') + +const q = faunadb.query +const client = new faunadb.Client({ + secret: process.env.FAUNADB_SERVER_SECRET +}) + +exports.handler = async (event, context) => { + const data = JSON.parse(event.body) + console.log('data', data) + console.log('Function `todo-delete-batch` invoked', data.ids) + // construct batch query from IDs + const deleteAllCompletedTodoQuery = data.ids.map((id) => { + return q.Delete(q.Ref(`classes/todos/${id}`)) + }) + // Hit fauna with the query to delete the completed items + return client.query(deleteAllCompletedTodoQuery) + .then((response) => { + console.log('success', response) + return { + statusCode: 200, + body: JSON.stringify(response) + } + }).catch((error) => { + console.log('error', error) + return { + statusCode: 400, + body: JSON.stringify(error) + } + }) +} \ No newline at end of file diff --git a/functions/todos-delete.js b/functions/todos-delete.js new file mode 100644 index 0000000..7424735 --- /dev/null +++ b/functions/todos-delete.js @@ -0,0 +1,28 @@ +/* code from functions/todos-update.js */ +const faunadb = require('faunadb') +const getId = require('./utils/getId') + +const q = faunadb.query +const client = new faunadb.Client({ + secret: process.env.FAUNADB_SERVER_SECRET +}) + +exports.handler = (event, context) => { + const data = JSON.parse(event.body) + const id = getId(event.path) + console.log(`Function 'todo-update' invoked. update id: ${id}`) + return client.query(q.Update(q.Ref(`classes/todos/${id}`), { data })) + .then((response) => { + console.log('success', response) + return { + statusCode: 200, + body: JSON.stringify(response) + } + }).catch((error) => { + console.log('error', error) + return { + statusCode: 400, + body: JSON.stringify(error) + } + }) +} \ No newline at end of file diff --git a/functions/todos-read-all.js b/functions/todos-read-all.js new file mode 100644 index 0000000..6d76d4f --- /dev/null +++ b/functions/todos-read-all.js @@ -0,0 +1,35 @@ +/* code from functions/todos-read-all.js */ +/* Import faunaDB sdk */ +const faunadb = require('faunadb') + +const q = faunadb.query +const client = new faunadb.Client({ + secret: process.env.FAUNADB_SERVER_SECRET +}) + +exports.handler = (event, context) => { + console.log('Function `todo-read-all` invoked') + return client.query(q.Paginate(q.Match(q.Ref('indexes/all_todos')))) + .then((response) => { + const todoRefs = response.data + console.log('Todo refs', todoRefs) + console.log(`${todoRefs.length} todos found`) + // create new query out of todo refs. http://bit.ly/2LG3MLg + const getAllTodoDataQuery = todoRefs.map((ref) => { + return q.Get(ref) + }) + // then query the refs + return client.query(getAllTodoDataQuery).then((ret) => { + return { + statusCode: 200, + body: JSON.stringify(ret) + } + }) + }).catch((error) => { + console.log('error', error) + return { + statusCode: 400, + body: JSON.stringify(error) + } + }) +} \ No newline at end of file diff --git a/functions/todos-read.js b/functions/todos-read.js new file mode 100644 index 0000000..9681a20 --- /dev/null +++ b/functions/todos-read.js @@ -0,0 +1,28 @@ +/* code from functions/todos-read.js */ +/* Import faunaDB sdk */ +const faunadb = require('faunadb') +const getId = require('./utils/getId') + +const q = faunadb.query +const client = new faunadb.Client({ + secret: process.env.FAUNADB_SERVER_SECRET +}) + +exports.handler = (event, context) => { + const id = getId(event.path) + console.log(`Function 'todo-read' invoked. Read id: ${id}`) + return client.query(q.Get(q.Ref(`classes/todos/${id}`))) + .then((response) => { + console.log('success', response) + return { + statusCode: 200, + body: JSON.stringify(response) + } + }).catch((error) => { + console.log('error', error) + return { + statusCode: 400, + body: JSON.stringify(error) + } + }) +} \ No newline at end of file diff --git a/functions/todos-update.js b/functions/todos-update.js new file mode 100644 index 0000000..99478e5 --- /dev/null +++ b/functions/todos-update.js @@ -0,0 +1,28 @@ +/* code from functions/todos-update.js */ +const faunadb = require('faunadb') +const getId = require('./utils/getId') + +const q = faunadb.query +const client = new faunadb.Client({ + secret: process.env.FAUNADB_SERVER_SECRET +}) + +exports.handler = (event, context) => { + const data = JSON.parse(event.body) + const id = getId(event.path) + console.log(`Function 'todo-update' invoked. update id: ${id}`) + return client.query(q.Update(q.Ref(`classes/todos/${id}`), { data })) + .then((response) => { + console.log('success', response) + return { + statusCode: 200, + body: JSON.stringify(response) + } + }).catch((error) => { + console.log('error', error) + return { + statusCode: 400, + body: JSON.stringify(error) + } + }) +} diff --git a/src/pages/todo.js b/src/pages/todo.js new file mode 100644 index 0000000..d9f2d2d --- /dev/null +++ b/src/pages/todo.js @@ -0,0 +1,39 @@ +import React from "react" +import { Link } from "gatsby" + +import Layout from "../components/layout" +import Image from "../components/image" +import SEO from "../components/seo" + +function createTodo(data) { + return fetch('/.netlify/functions/todos-create', { + body: JSON.stringify(data), + method: 'POST' + }).then(response => { + return response.json() + }) +} + +// Todo data +const myTodo = { + title: 'My todo title', + completed: false, +} + +// create it! +createTodo(myTodo).then((response) => { + console.log('API response', response) + // set app state +}).catch((error) => { + console.log('API error', error) +}) + +const IndexPage = () => ( + + + + Go to page 2 + +) + +export default IndexPage