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

479 lines
15 KiB
TypeScript
Raw Normal View History

import { ITodo, ITodosState, IParamTodo, IDrag, IProjectsState, IProject, Privacy } from 'model'
import { storeBuilder } from './Store/Store'
import Api from '@api'
import { tools } from './tools'
2019-07-12 14:09:44 +02:00
import { toolsext } from '@src/store/Modules/toolsext'
import { lists } from './lists'
2019-03-22 15:32:32 +01:00
import * as ApiTables from './ApiTables'
import { GlobalStore, Todos, UserStore } from '@store'
import globalroutines from './../../globalroutines/index'
import { Mutation } from 'vuex-module-decorators'
import { serv_constants } from '@src/store/Modules/serv_constants'
import { GetterTree } from 'vuex'
import objectId from '@src/js/objectId'
2019-03-04 17:28:29 +01:00
import { costanti } from '@src/store/Modules/costanti'
2019-04-29 01:01:31 +02:00
import { IAction } from '@src/model'
import * as Types from '@src/store/Api/ApiTypes'
import { static_data } from '@src/db/static_data'
const nametable = 'todos'
// import _ from 'lodash'
const state: ITodosState = {
2019-03-04 17:28:29 +01:00
showtype: costanti.ShowTypeTask.SHOW_LAST_N_COMPLETED,
2019-03-04 18:48:07 +01:00
todos: {},
categories: [],
// todos_changed: 1,
reload_fromServer: 0,
testpao: 'Test',
insidePending: false,
visuLastCompleted: 10
}
const listFieldsToChange: string [] = ['descr', 'statustodo', 'category', 'expiring_at', 'priority', 'id_prev', 'pos', 'enableExpiring', 'progress', 'phase', 'assigned_to_userId', 'hoursplanned', 'hoursworked', 'start_date', 'completed_at', 'themecolor', 'themebgcolor']
const b = storeBuilder.module<ITodosState>('Todos', state)
const stateGetter = b.state()
function getindexbycategory(category: string) {
return state.categories.indexOf(category)
}
2019-10-10 16:53:33 +02:00
function gettodosByCategory(category: string): any[] {
const indcat = state.categories.indexOf(category)
if (!state.todos[indcat]) {
return []
}
return state.todos[indcat]
}
function initcat() {
2019-04-29 01:01:31 +02:00
const rec = Getters.getters.getRecordEmpty()
rec.userId = UserStore.state.my._id
return rec
}
namespace Getters {
2019-04-29 01:01:31 +02:00
const getRecordEmpty = b.read((stateparamf: ITodosState) => (): ITodo => {
2019-04-05 16:16:29 +02:00
const tomorrow = tools.getDateNow()
tomorrow.setDate(tomorrow.getDate() + 1)
const objtodo: ITodo = {
2019-04-05 16:16:29 +02:00
// _id: tools.getDateNow().toISOString(), // Create NEW
_id: objectId(),
userId: UserStore.state.my._id,
descr: '',
priority: tools.Priority.PRIORITY_NORMAL,
statustodo: tools.Status.OPENED,
2019-04-05 16:16:29 +02:00
created_at: tools.getDateNow(),
modify_at: tools.getDateNow(),
completed_at: tools.getDateNull(),
category: '',
expiring_at: tomorrow,
enableExpiring: false,
id_prev: '',
pos: 0,
modified: false,
2019-04-05 16:16:29 +02:00
progress: 0,
progressCalc: 0,
phase: 0,
assigned_to_userId: '',
hoursplanned: 0,
hoursworked: 0,
start_date: tools.getDateNull(),
themecolor: 'blue',
themebgcolor: 'white'
}
// return this.copy(objtodo)
return objtodo
}, 'getRecordEmpty')
2019-04-29 01:01:31 +02:00
const items_dacompletare = b.read((stateparam: ITodosState) => (cat: string): ITodo[] => {
2019-07-10 11:37:00 +02:00
// console.log('items_dacompletare')
const indcat = getindexbycategory(cat)
2019-07-10 11:37:00 +02:00
let arrout = []
2019-04-29 01:01:31 +02:00
// console.log('items_dacompletare', 'indcat', indcat, stateparam.todos[indcat])
if (stateparam.todos[indcat]) {
2019-07-10 11:37:00 +02:00
arrout = stateparam.todos[indcat].filter((todo) => todo.statustodo !== tools.Status.COMPLETED)
} else {
2019-07-10 11:37:00 +02:00
arrout = []
}
2019-07-10 11:37:00 +02:00
// return tools.mapSort(arrout)
return arrout
2019-03-28 12:58:34 +01:00
}, 'items_dacompletare')
2019-04-29 01:01:31 +02:00
const todos_completati = b.read((stateparam: ITodosState) => (cat: string): ITodo[] => {
const indcat = getindexbycategory(cat)
2019-10-10 16:53:33 +02:00
console.log('todos_completati', cat, 'indcat=', indcat, 'state.categories=', state.categories)
2019-04-29 01:01:31 +02:00
if (stateparam.todos[indcat]) {
2019-07-10 11:37:00 +02:00
let arrout = []
2019-04-29 01:01:31 +02:00
if (stateparam.showtype === costanti.ShowTypeTask.SHOW_LAST_N_COMPLETED) { // Show only the first N completed
2019-07-10 11:37:00 +02:00
arrout = stateparam.todos[indcat].filter((todo) => todo.statustodo === tools.Status.COMPLETED).slice(0, stateparam.visuLastCompleted)
}
2019-04-29 01:01:31 +02:00
else if (stateparam.showtype === costanti.ShowTypeTask.SHOW_ONLY_TOCOMPLETE) {
2019-07-10 11:37:00 +02:00
arrout = []
}
2019-04-29 01:01:31 +02:00
else if (stateparam.showtype === costanti.ShowTypeTask.SHOW_ALL) {
2019-07-10 11:37:00 +02:00
arrout = stateparam.todos[indcat].filter((todo) => todo.statustodo === tools.Status.COMPLETED)
}
else {
2019-07-10 11:37:00 +02:00
arrout = []
}
2019-07-10 11:37:00 +02:00
2019-10-10 16:53:33 +02:00
console.log('arrout', arrout)
2019-07-10 11:37:00 +02:00
return arrout
// return tools.mapSort(arrout)
} else {
return []
}
}, 'todos_completati')
2019-04-29 01:01:31 +02:00
const doneTodosCount = b.read((stateparam: ITodosState) => (cat: string): number => {
return getters.todos_completati(cat).length
}, 'doneTodosCount')
2019-04-29 01:01:31 +02:00
const TodosCount = b.read((stateparam: ITodosState) => (cat: string): number => {
const indcat = getindexbycategory(cat)
2019-04-29 01:01:31 +02:00
if (stateparam.todos[indcat]) {
return stateparam.todos[indcat].length
} else {
return 0
}
}, 'TodosCount')
2019-04-29 01:01:31 +02:00
const getRecordById = b.read((stateparam: ITodosState) => (id: string, cat: string): ITodo => {
const indcat = getindexbycategory(cat)
2019-04-29 01:01:31 +02:00
if (stateparam.todos) {
return stateparam.todos[indcat].find((item) => item._id === id)
}
return null
}, 'getRecordById')
export const getters = {
get getRecordEmpty() {
return getRecordEmpty()
},
2019-03-28 12:58:34 +01:00
get items_dacompletare() {
return items_dacompletare()
},
get todos_completati() {
return todos_completati()
},
get doneTodosCount() {
return doneTodosCount()
},
get TodosCount() {
return TodosCount()
},
get getRecordById() {
return getRecordById()
}
}
}
namespace Mutations {
2019-04-29 01:01:31 +02:00
function findIndTodoById(stateparam: ITodosState, data: IParamTodo) {
const indcat = stateparam.categories.indexOf(data.categorySel)
if (indcat >= 0) {
2019-04-29 01:01:31 +02:00
return tools.getIndexById(stateparam.todos[indcat], data.id)
}
return -1
}
2019-04-29 01:01:31 +02:00
function createNewItem(stateparam: ITodosState, { objtodo, atfirst, categorySel }) {
let indcat = stateparam.categories.indexOf(categorySel)
if (indcat === -1) {
stateparam.categories.push(categorySel)
indcat = stateparam.categories.indexOf(categorySel)
2019-03-04 18:48:07 +01:00
}
2019-04-29 01:01:31 +02:00
console.log('createNewItem', objtodo, 'cat=', categorySel, 'stateparam.todos[indcat]', stateparam.todos[indcat])
if (stateparam.todos[indcat] === undefined) {
stateparam.todos[indcat] = []
stateparam.todos[indcat].push(objtodo)
console.log('push stateparam.todos[indcat]', stateparam.todos)
return
}
if (atfirst) {
2019-04-29 01:01:31 +02:00
stateparam.todos[indcat].unshift(objtodo)
}
else {
2019-04-29 01:01:31 +02:00
stateparam.todos[indcat].push(objtodo)
}
2019-04-29 01:01:31 +02:00
console.log('stateparam.todos[indcat]', stateparam.todos[indcat])
}
2019-04-29 01:01:31 +02:00
function deletemyitem(stateparam: ITodosState, myitem: ITodo) {
// Find record
2019-04-29 01:01:31 +02:00
const indcat = stateparam.categories.indexOf(myitem.category)
const ind = findIndTodoById(stateparam, { id: myitem._id, categorySel: myitem.category })
2019-04-29 01:01:31 +02:00
ApiTables.removeitemfromarray(stateparam.todos[indcat], ind)
}
async function movemyitem(stateparam: ITodosState, { myitemorig, myitemdest }) {
2019-04-29 01:01:31 +02:00
const indcat = stateparam.categories.indexOf(myitemorig.category)
const indorig = tools.getIndexById(stateparam.todos[indcat], myitemorig._id)
let indcatdest = stateparam.categories.indexOf(myitemdest.category)
console.log('stateparam.categories', stateparam.categories)
console.log('myitemdest', myitemdest)
console.log('indcat', indcat, 'indcatdest', indcatdest, 'indorig', indorig)
if (indcatdest === -1) {
stateparam.categories.push(myitemdest.category)
const newindcat = stateparam.categories.indexOf(myitemdest.category)
stateparam.todos[newindcat] = []
indcatdest = newindcat
}
stateparam.todos[indcat].splice(indorig, 1)
stateparam.todos[indcatdest].push(myitemdest)
await Actions.actions.modify({ myitem: myitemdest, field: 'category' })
}
export const mutations = {
deletemyitem: b.commit(deletemyitem),
2019-04-29 01:01:31 +02:00
createNewItem: b.commit(createNewItem),
movemyitem: b.commit(movemyitem)
}
}
namespace Actions {
2019-03-28 12:58:34 +01:00
async function dbLoad(context, { checkPending }) {
2019-10-10 16:53:33 +02:00
if (!static_data.functionality.ENABLE_PROJECTS_LOADING)
return null
console.log('dbLoad', nametable, checkPending, 'userid=', UserStore.state.my._id)
// if (UserStore.state.my._id === '') {
// return new Types.AxiosError(0, null, 0, '')
// }
let ris = null
ris = await Api.SendReq('/todos/' + UserStore.state.my._id, 'GET', null)
.then((res) => {
2019-03-22 15:32:32 +01:00
if (res.data.todos) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
state.todos = res.data.todos
state.categories = res.data.categories
} else {
state.todos = [[]]
}
state.showtype = parseInt(GlobalStore.getters.getConfigStringbyId({
id: costanti.CONFIG_ID_SHOW_TYPE_TODOS,
default: costanti.ShowTypeTask.SHOW_LAST_N_COMPLETED
}), 10)
2019-03-04 17:28:29 +01:00
// console.log('ARRAY TODOS = ', state.todos)
if (process.env.DEBUG === '1') {
2019-03-28 12:58:34 +01:00
console.log('dbLoad', 'state.todos', state.todos, 'state.categories', state.categories)
}
return res
})
.catch((error) => {
2019-03-28 12:58:34 +01:00
console.log('error dbLoad', error)
UserStore.mutations.setErrorCatch(error)
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, tools.ERR_GENERICO, error)
})
2019-02-02 20:13:06 +01:00
2019-03-22 15:32:32 +01:00
ApiTables.aftercalling(ris, checkPending, 'categories')
return ris
}
async function deleteItemtodo(context, { cat, idobj }) {
console.log('deleteItemtodo: KEY = ', idobj)
2019-03-22 20:51:42 +01:00
const myarr = gettodosByCategory(cat)
const myobjtrov = tools.getElemById(myarr, idobj)
if (!!myobjtrov) {
console.log('myobjtrov', myobjtrov.descr)
if (!!myobjtrov) {
const myobjnext = tools.getElemPrevById(myarr, myobjtrov._id)
if (!!myobjnext) {
myobjnext.id_prev = myobjtrov.id_prev
myobjnext.modified = true
await modify(context, { myitem: myobjnext, field: 'id_prev' })
}
ApiTables.table_DeleteRecord(nametable, myobjtrov, idobj)
}
}
}
2019-03-28 12:58:34 +01:00
async function dbInsert(context, { myobj, atfirst }) {
const objtodo = initcat()
objtodo.descr = myobj.descr
objtodo.category = myobj.category
let elemtochange: ITodo = null
2019-03-22 20:51:42 +01:00
const myarr = gettodosByCategory(objtodo.category)
if (atfirst) {
console.log('INSERT AT THE TOP')
2019-03-22 20:51:42 +01:00
elemtochange = tools.getFirstList(myarr)
2019-03-22 15:32:32 +01:00
objtodo.id_prev = ApiTables.LIST_START
} else {
console.log('INSERT AT THE BOTTOM')
// INSERT AT THE BOTTOM , so GET LAST ITEM
const lastelem = tools.getLastListNotCompleted(nametable, objtodo.category, this.tipoProj)
2019-03-22 15:32:32 +01:00
objtodo.id_prev = (!!lastelem) ? lastelem._id : ApiTables.LIST_START
}
objtodo.modified = false
2019-03-22 20:51:42 +01:00
Todos.mutations.createNewItem({ objtodo, atfirst, categorySel: objtodo.category }) // 1) Create record in Memory
2019-03-22 20:51:42 +01:00
const id = await globalroutines(context, 'write', nametable, objtodo) // 2) Insert into the IndexedDb
let field = ''
2019-03-22 15:32:32 +01:00
if (atfirst) { // update also the last elem
if (!!elemtochange) {
elemtochange.id_prev = id
console.log('elemtochange', elemtochange)
field = 'id_prev'
// Modify the other record
await modify(context, { myitem: elemtochange, field })
}
}
// 3) send to the Server
2019-03-22 15:32:32 +01:00
return await ApiTables.Sync_SaveItem(nametable, 'POST', objtodo)
.then((ris) => {
2019-03-22 20:51:42 +01:00
// *** Check if need to be moved because of the --- Priority Ordering --- ...
const indelem = tools.getIndexById(myarr, objtodo._id)
let itemdragend
if (atfirst) {
// Check the second item, if it's different priority, then move to the first position of the priority
const secondindelem = indelem + 1
2019-03-22 20:51:42 +01:00
if (tools.isOkIndex(myarr, secondindelem)) {
const secondelem = tools.getElemByIndex(myarr, secondindelem)
2019-03-04 17:28:29 +01:00
if (secondelem.priority !== objtodo.priority) {
itemdragend = {
field: 'priority',
idelemtochange: objtodo._id,
prioritychosen: objtodo.priority,
category: objtodo.category,
atfirst
}
}
}
} else {
// get previous of the last
const prevlastindelem = indelem - 1
2019-03-22 20:51:42 +01:00
if (tools.isOkIndex(myarr, prevlastindelem)) {
const prevlastelem = tools.getElemByIndex(myarr, prevlastindelem)
2019-03-04 17:28:29 +01:00
if (prevlastelem.priority !== objtodo.priority) {
itemdragend = {
field: 'priority',
idelemtochange: objtodo._id,
prioritychosen: objtodo.priority,
category: objtodo.category,
atfirst
}
}
}
}
if (itemdragend) {
swapElems(context, itemdragend)
}
return ris
})
}
async function modify(context, { myitem, field }) {
2019-04-05 23:59:52 +02:00
return await ApiTables.table_ModifyRecord(nametable, myitem, listFieldsToChange, field)
}
async function swapElems(context, itemdragend: IDrag) {
// console.log('TODOS swapElems', itemdragend, state.todos, state.categories)
2019-03-22 20:51:42 +01:00
const cat = itemdragend.category
const indcat = state.categories.indexOf(cat)
2019-03-22 20:51:42 +01:00
const myarr = state.todos[indcat]
2019-04-05 23:59:52 +02:00
tools.swapGeneralElem(nametable, myarr, itemdragend, listFieldsToChange)
}
2019-04-29 01:01:31 +02:00
async function ActionCutPaste(context, action: IAction) {
console.log('ActionCutPaste', action)
if (action.type === lists.MenuAction.CUT) {
2019-04-29 01:01:31 +02:00
GlobalStore.state.lastaction = action
} else if (action.type === lists.MenuAction.PASTE) {
if (GlobalStore.state.lastaction.type === lists.MenuAction.CUT) {
2019-04-29 01:01:31 +02:00
// Change id_parent
const orig_obj = Getters.getters.getRecordById(GlobalStore.state.lastaction._id, GlobalStore.state.lastaction.cat)
// const dest = Getters.getters.getRecordById(action._id, action.cat)
console.log('action', action, 'orig_obj', orig_obj)
const dest_obj = tools.jsonCopy(orig_obj)
if (!!dest_obj) {
dest_obj.category = action._id
dest_obj.modified = true
dest_obj.id_prev = null
GlobalStore.state.lastaction.type = 0
return await Mutations.mutations.movemyitem({ myitemorig: orig_obj, myitemdest: dest_obj })
}
}
}
}
export const actions = {
2019-03-28 12:58:34 +01:00
dbLoad: b.dispatch(dbLoad),
swapElems: b.dispatch(swapElems),
deleteItemtodo: b.dispatch(deleteItemtodo),
2019-03-28 12:58:34 +01:00
dbInsert: b.dispatch(dbInsert),
2019-04-29 01:01:31 +02:00
modify: b.dispatch(modify),
ActionCutPaste: b.dispatch(ActionCutPaste)
}
}
// Module
const TodosModule = {
get state() {
return stateGetter()
},
getters: Getters.getters,
mutations: Mutations.mutations,
actions: Actions.actions
}
export default TodosModule