first commit

This commit is contained in:
s.golasch
2023-08-01 12:47:58 +02:00
commit b1439a55bb
65 changed files with 3085 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
'use strict'
const RECEIVE_TODOS = 'RECEIVE_TODOS'
const FETCH_TODOS = 'FETCH_TODOS'
const ADD_TODO = 'ADD_TODO'
const ADDED_TODO = 'ADDED_TODO'
const UPDATE_TODO = 'UPDATE_TODO'
const DELETE_TODO = 'DELETE_TODO'
const COMPLETE_TODO = 'COMPLETE_TODO'
module.exports = {
RECEIVE_TODOS,
FETCH_TODOS,
ADD_TODO,
ADDED_TODO,
UPDATE_TODO,
DELETE_TODO,
COMPLETE_TODO,
}

View File

@@ -0,0 +1,61 @@
'use strict'
const {FETCH_TODOS, RECEIVED_TODOS, ADD_TODO, ADDED_TODO, UPDATE_TODO, DELETE_TODO, COMPLETE_TODO} = require('./action_constants')
const fetchTodos = todos => {
return {
type: FETCH_TODOS,
items: todos,
}
}
const receivedTodos = todos => {
return {
type: RECEIVED_TODOS,
items: todos,
}
}
const addTodo = todo => {
return {
type: ADD_TODO,
item: todo,
}
}
const addedTodo = todo => {
return {
type: ADDED_TODO,
item: todo,
}
}
const updateTodo = todo => {
return {
type: UPDATE_TODO,
item: todo,
}
}
const deleteTodo = todo => {
return {
type: DELETE_TODO,
item: todo,
}
}
const completeTodo = todo => {
return {
type: COMPLETE_TODO,
item: todo,
}
}
module.exports = {
receivedTodos,
addTodo,
addedTodo,
updateTodo,
deleteTodo,
completeTodo,
}

View File

@@ -0,0 +1,61 @@
'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
}
}
}

View File

@@ -0,0 +1,42 @@
'use strict'
const {RECEIVE_TODOS, RECEIVED_TODOS, ADD_TODO, ADDED_TODO} = require('./action_constants')
const INITIAL_STATE = {toBeAdded: [], items: [], size: 0, lastUpdated: null, fetching: false, error: null}
// create an array of todos that are still queued, but not yet added
const removeToBeAdded = (current, itemToBeRemoved) => {
if (current.length === 1) return []
return current.map(item => {
if (item.text !== itemToBeRemoved.text) return item
})
}
module.exports = (state = INITIAL_STATE, action) => {
switch (action.type) {
// received all todos
case RECEIVED_TODOS:
return Object.assign({}, state, {
items: action.items,
size: action.items.length,
lastUpdated: Date.now(),
})
// trying to add a todo (request to amazon not yet made)
case ADD_TODO:
return Object.assign({}, state, {
toBeAdded: state.toBeAdded.push(action.item),
lastUpdated: Date.now(),
})
// todo has been added successfully
case ADDED_TODO:
// remove the item from the toBeadded array
const toBeAdded = removeToBeAdded(state.toBeAdded, action.item)
state.items.push(action.item)
return Object.assign({}, state, {
items: state.items,
size: state.size + 1,
toBeAdded: toBeAdded,
lastUpdated: Date.now(),
})
default:
return state
}
}

View File

@@ -0,0 +1,31 @@
'use strict'
const {receivedTodos, addTodo} = require('./actions')
module.exports = {
// returns all todos
'/todos': (store, params, cb) => cb(store.getState().todos),
// adds a todo & returns all todos
'/todos/add': (store, params, cb, Proxy) => Proxy.act('todo', 'add', params, result => {
console.log('result', result)
cb(result)
}),
// deletes a todo & returns all todos
'/todos/delete': (store, params, cb, Proxy) => Proxy.act('todo', 'delete', params, async result => {
const data = await Proxy.fetch('todo')
store.dispatch(receivedTodos(data.todo.values))
cb(data.todo.values)
}),
// completes a todo & returns all todos
'/todos/complete': (store, params, cb, Proxy) => Proxy.act('todo', 'complete', params, async result => {
const data = await Proxy.fetch('todo')
store.dispatch(receivedTodos(data.todo.values))
cb(data.todo.values)
}),
// updates a todo & returns all todos
'/todos/update': (store, params, cb, Proxy) => Proxy.act('todo', 'update', params, async result => {
const data = await Proxy.fetch('todo')
store.dispatch(receivedTodos(data.todo.values))
cb(data.todo.values)
}),
}