2019-02-02 20:13:06 +01:00
|
|
|
import { ITodo, ITodosState } from 'model'
|
2019-01-14 22:40:30 +01:00
|
|
|
import { storeBuilder } from './Store/Store'
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
import Api from '@api'
|
|
|
|
|
import { rescodes } from './rescodes'
|
|
|
|
|
import { UserStore } from '@store'
|
|
|
|
|
|
2019-01-14 22:40:30 +01:00
|
|
|
|
|
|
|
|
const state: ITodosState = {
|
2019-02-01 04:10:31 +01:00
|
|
|
visuOnlyUncompleted: false,
|
|
|
|
|
networkDataReceived: false,
|
|
|
|
|
todos: []
|
2019-01-14 22:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const b = storeBuilder.module<ITodosState>('TodosModule', state)
|
2019-02-01 04:10:31 +01:00
|
|
|
const stateGetter = b.state()
|
2019-01-14 22:40:30 +01:00
|
|
|
|
|
|
|
|
// 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) {
|
2019-02-01 04:10:31 +01:00
|
|
|
result.push(json[key])
|
2019-02-02 20:13:06 +01:00
|
|
|
})
|
|
|
|
|
return result
|
2019-02-01 04:10:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function dbLoadTodo(context) {
|
|
|
|
|
console.log('dbLoadTodo')
|
|
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
const token = UserStore.state.idToken
|
2019-02-01 04:10:31 +01:00
|
|
|
|
|
|
|
|
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)
|
2019-02-01 04:10:31 +01:00
|
|
|
.then((res) => {
|
|
|
|
|
return res.json()
|
|
|
|
|
}).then((resData) => {
|
2019-02-02 20:13:06 +01:00
|
|
|
state.networkDataReceived = true
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
state.todos = resData.todos
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
// After Login will store into the indexedDb...
|
2019-02-01 04:10:31 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function dbSaveTodo(context, itemtodo: ITodo) {
|
2019-02-03 00:51:58 +01:00
|
|
|
return await dbInsertSaveTodo(context, itemtodo, 'PATCH')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function dbInsertTodo(context, itemtodo: ITodo) {
|
|
|
|
|
return await dbInsertSaveTodo(context, itemtodo, 'POST')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function dbInsertSaveTodo(context, itemtodo: ITodo, method) {
|
|
|
|
|
console.log('dbInsertSaveTodo', itemtodo, method)
|
2019-02-01 04:10:31 +01:00
|
|
|
let call = process.env.MONGODB_HOST + '/todos/' + itemtodo._id
|
|
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
const token = UserStore.state.idToken
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-03 00:51:58 +01:00
|
|
|
let res = await Api.SendReq(call, UserStore.state.lang, token, method, itemtodo)
|
2019-02-01 04:10:31 +01:00
|
|
|
.then(function (res) {
|
|
|
|
|
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
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function dbDeleteTodo(context, id: String) {
|
|
|
|
|
console.log('dbDeleteTodo', id)
|
|
|
|
|
let call = process.env.MONGODB_HOST + '/todos/' + id
|
|
|
|
|
|
|
|
|
|
const token = UserStore.state.idToken
|
|
|
|
|
|
|
|
|
|
let res = await Api.SendReq(call, UserStore.state.lang, token, 'DELETE', id)
|
|
|
|
|
.then(function (res) {
|
|
|
|
|
return rescodes.OK
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
if (process.env.DEV) {
|
|
|
|
|
console.log('ERROREEEEEEEEE', error)
|
|
|
|
|
}
|
|
|
|
|
return rescodes.ERR_GENERICO
|
|
|
|
|
})
|
2019-02-01 04:10:31 +01:00
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getTodosByCategory(context, category: string) {
|
|
|
|
|
let myarr = state.todos.filter((p) => {
|
|
|
|
|
return p.category === category
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return myarr
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 22:40:30 +01:00
|
|
|
export const actions = {
|
2019-02-03 00:51:58 +01:00
|
|
|
dbInsertTodo: b.dispatch(dbInsertTodo),
|
2019-02-01 04:10:31 +01:00
|
|
|
dbSaveTodo: b.dispatch(dbSaveTodo),
|
|
|
|
|
dbLoadTodo: b.dispatch(dbLoadTodo),
|
2019-02-02 20:13:06 +01:00
|
|
|
dbDeleteTodo: b.dispatch(dbDeleteTodo),
|
2019-02-01 04:10:31 +01:00
|
|
|
getTodosByCategory: b.dispatch(getTodosByCategory)
|
2019-01-14 22:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Module
|
|
|
|
|
const TodosModule = {
|
|
|
|
|
get state() {
|
|
|
|
|
return stateGetter()
|
|
|
|
|
},
|
|
|
|
|
getters: Getters.getters,
|
2019-02-02 20:13:06 +01:00
|
|
|
// mutations: Mutations.mutations,
|
2019-01-14 22:40:30 +01:00
|
|
|
actions: Actions.actions
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
export default TodosModule
|