2019-02-02 20:13:06 +01:00
|
|
|
import store from '../store'
|
|
|
|
|
import _ from 'lodash'
|
|
|
|
|
import { UserStore } from '@modules'
|
|
|
|
|
import { i18n } from '../plugins/i18n'
|
|
|
|
|
|
|
|
|
|
import {idbKeyval as storage} from '../js/storage.js';
|
|
|
|
|
|
|
|
|
|
function saveConfigIndexDb(context) {
|
|
|
|
|
|
|
|
|
|
let data = []
|
|
|
|
|
data['_id'] = 1
|
|
|
|
|
data['lang'] = UserStore.state.lang
|
|
|
|
|
data['token'] = UserStore.state.idToken
|
|
|
|
|
data['userId'] = UserStore.state.userId
|
|
|
|
|
|
|
|
|
|
writeConfigIndexDb('config', data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function writeConfigIndexDb(context, data) {
|
|
|
|
|
console.log('writeConfigIndexDb', data)
|
|
|
|
|
|
|
|
|
|
storage.setdata('config', data)
|
|
|
|
|
.then(ris => {
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readfromIndexDbToStateTodos(context) {
|
|
|
|
|
console.log('*** read from IndexDb to state.todos')
|
|
|
|
|
|
|
|
|
|
return await storage.getalldata('todos')
|
|
|
|
|
.then(ristodos => {
|
|
|
|
|
console.log('&&&&&&& readfromIndexDbToStateTodos OK: Num RECORD: ', ristodos.length)
|
2019-02-03 02:40:24 +01:00
|
|
|
console.log('ristodos:', ristodos)
|
|
|
|
|
UserStore.state.todos = [...ristodos]
|
2019-02-02 20:13:06 +01:00
|
|
|
}).catch((error) => {
|
|
|
|
|
console.log('err: ', error)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 21:16:49 +01:00
|
|
|
export default async (context, cmd, table, datakey, id) => {
|
2019-02-02 20:13:06 +01:00
|
|
|
if (cmd === 'loadapp') {
|
|
|
|
|
// ****** LOAD APP AL CARICAMENTO ! *******
|
|
|
|
|
return saveConfigIndexDb(context, datakey)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ('indexedDB' in window) {
|
|
|
|
|
if (!UserStore.state.networkDataReceived) {
|
|
|
|
|
return await readfromIndexDbToStateTodos(context)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (cmd === 'write') {
|
|
|
|
|
return await storage.setdata(table, datakey)
|
|
|
|
|
} else if (cmd === 'readall') {
|
|
|
|
|
return await storage.getalldata(table)
|
|
|
|
|
} else if (cmd === 'read') {
|
2019-02-02 21:16:49 +01:00
|
|
|
return await storage.getdata(table, id)
|
2019-02-02 20:13:06 +01:00
|
|
|
} else if (cmd === 'delete') {
|
2019-02-02 21:16:49 +01:00
|
|
|
return await storage.deletedata(table, id)
|
2019-02-02 20:13:06 +01:00
|
|
|
}
|
|
|
|
|
}
|