Files
alexis/lib/components/todo/proxy.js
2023-08-01 12:47:58 +02:00

61 lines
1.9 KiB
JavaScript

'use strict'
const {receivedTodos, addTodo, addedTodo} = require('./actions')
module.exports = {
// urls that should be called for the remote actions
urls: {
fetch: 'https://layla.amazon.de/api/todos?type=TASK&size=100',
add: 'https://layla.amazon.de/api/todos',
complete: 'https://layla.amazon.de/api/todos/{ID}',
delete: 'https://layla.amazon.de/api/todos/{ID}',
update: 'https://layla.amazon.de/api/todos/{ID}',
},
// called before an remote http call to the todos API gets executed
preHooks: {
fetch: async options => [{method: 'GET', url: options.url}],
// adds a new TODO
add: async options => {
let todoTemplate = {
text: options.text,
type: 'TASK',
itemId: null,
lastLocalUpdatedDate: null,
lastUpdatedDate: null,
createdDate: Date.now(),
utteranceId: null,
nbestItems: null,
complete: false,
version: null,
deleted: false,
reminderTime: null
}
// dispatch data about the item to be added
store.dispatch(addTodo(todoTemplate))
// build request options
// TODO: Add error handling
return [{method: 'POST', url: options.url, body: JSON.stringify(todoTemplate)}]
}
},
// called after an remote http call to the todos API has been successfully executed
postHooks: {
// called after the todo list has been fetched
fetch: async (store, raw_data) => {
let data = []
try {
data = JSON.parse(raw_data)
store.dispatch(receivedTodos(data.values))
} catch (error) {
// TODO: Add error handling
}
return data
},
// called after the new todo has been created
add: async (store, raw_data) => {
// TODO: Add error handling
const data = JSON.parse(raw_data)
store.dispatch(addedTodo(data))
return data
}
}
}