Files
freeplanet/src/store/Modules/Todos.ts

195 lines
4.5 KiB
TypeScript
Raw Normal View History

2019-02-02 20:13:06 +01:00
import { ITodo, ITodosState } from 'model'
import { storeBuilder } from './Store/Store'
import Api from '@api'
import { rescodes } from './rescodes'
import { UserStore } from '@store'
const state: ITodosState = {
visuOnlyUncompleted: false,
networkDataReceived: false,
todos: []
}
const b = storeBuilder.module<ITodosState>('TodosModule', state)
const stateGetter = b.state()
// Getters
namespace Getters {
const visuOnlyUncompleted = b.read(state => state.visuOnlyUncompleted, 'visuOnlyUncompleted')
export const getters = {
get visuOnlyUncompleted() {
return visuOnlyUncompleted
}
}
}
namespace Mutations {
}
namespace Actions {
2019-02-02 20:13:06 +01:00
function json2array(json) {
let result = []
let keys = Object.keys(json)
keys.forEach(function (key) {
result.push(json[key])
2019-02-02 20:13:06 +01:00
})
return result
}
async function dbLoadTodo(context) {
console.log('dbLoadTodo')
2019-02-02 20:13:06 +01:00
const token = UserStore.state.idToken
let call = process.env.MONGODB_HOST + '/todos/' + UserStore.state.userId
2019-02-02 20:13:06 +01:00
state.networkDataReceived = false
let ris = await Api.SendReq(call, UserStore.state.lang, token, 'GET', null)
.then((res) => {
return res.json()
}).then((resData) => {
2019-02-02 20:13:06 +01:00
state.networkDataReceived = true
console.log('******* UPDATE TODOS.STATE.TODOS !:', resData.todos)
state.todos = [...resData.todos]
2019-02-02 20:13:06 +01:00
// After Login will store into the indexedDb...
console.log('state.todos', state.todos)
return rescodes.OK
})
.catch((error) => {
if (process.env.DEV) {
console.log('ERROREEEEEEEEE')
console.log(error)
}
return rescodes.ERR_GENERICO
})
2019-02-02 20:13:06 +01:00
}
async function dbSaveTodo(context, itemtodo: ITodo) {
return await dbInsertSaveTodo(context, itemtodo, 'PATCH')
}
async function dbInsertTodo(context, itemtodo: ITodo) {
return await dbInsertSaveTodo(context, itemtodo, 'POST')
}
function UpdateNewIdFromDB(oldItem, newItem, method) {
console.log('PRIMA state.todos', state.todos)
console.log('ITEM', newItem)
if (method === 'POST') {
state.todos.push(newItem)
// } else if (method === 'PATCH') {
// state.todos.map(item => {
// if (item._id === newItem._id) {
// return newItem
// }
// })
}
console.log('DOPO state.todos', state.todos)
}
async function dbInsertSaveTodo(context, itemtodo: ITodo, method) {
console.log('dbInsertSaveTodo', itemtodo, method)
let call = process.env.MONGODB_HOST + '/todos/' + itemtodo._id
2019-02-02 20:13:06 +01:00
const token = UserStore.state.idToken
let res = await Api.SendReq(call, UserStore.state.lang, token, method, itemtodo)
.then( function(response) {
if (response)
return response.json()
else
return null
}).then(newItem => {
console.log('RESDATA =', newItem)
if (newItem) {
const newId = newItem._id
// if (method === 'PATCH') {
// newItem = newItem.todo
// }
// Update ID on local
UpdateNewIdFromDB(itemtodo, newItem, method)
}
})
.catch((error) => {
if (process.env.DEV) {
console.log('ERROREEEEEEEEE')
console.log(error)
}
return rescodes.ERR_GENERICO
})
2019-02-02 20:13:06 +01:00
return res
}
async function dbDeleteTodo(context, item: ITodo) {
console.log('dbDeleteTodo', item)
let call = process.env.MONGODB_HOST + '/todos/' + item._id
2019-02-02 20:13:06 +01:00
const token = UserStore.state.idToken
let res = await Api.SendReq(call, UserStore.state.lang, token, 'DELETE', item)
2019-02-02 20:13:06 +01:00
.then(function (res) {
// Delete Item in to Array
state.todos.splice(state.todos.indexOf(item), 1)
2019-02-02 20:13:06 +01:00
return rescodes.OK
})
.catch((error) => {
if (process.env.DEV) {
console.log('ERROREEEEEEEEE', error)
}
return rescodes.ERR_GENERICO
})
return res
}
async function getTodosByCategory(context, category: string) {
let myarr = state.todos.filter((p) => {
return p.category === category
})
return myarr
}
export const actions = {
dbInsertTodo: b.dispatch(dbInsertTodo),
dbSaveTodo: b.dispatch(dbSaveTodo),
dbLoadTodo: b.dispatch(dbLoadTodo),
2019-02-02 20:13:06 +01:00
dbDeleteTodo: b.dispatch(dbDeleteTodo),
getTodosByCategory: b.dispatch(getTodosByCategory)
}
}
// Module
const TodosModule = {
get state() {
return stateGetter()
},
getters: Getters.getters,
2019-02-02 20:13:06 +01:00
// mutations: Mutations.mutations,
actions: Actions.actions
}
export default TodosModule