- fix: Date problems... (it was a bad "copy" function, the object date was not valid...
- fix: error fetch on loading... (offline appeared)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
APP_VERSION="DEV 0.0.13"
|
||||
APP_VERSION="DEV 0.0.17"
|
||||
SERVICE_WORKER_FILE='service-worker.js'
|
||||
APP_ID='1'
|
||||
APP_URL='https://freeplanet.app'
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
// Questo è il swSrc
|
||||
|
||||
console.log(' [ VER-0.0.12 ] _---------________-----------_________------------__________________________ PAO: this is my custom service worker');
|
||||
console.log(' [ VER-0.0.17 ] _---------________-----------_________------------__________________________ PAO: this is my custom service worker');
|
||||
|
||||
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.0.0/workbox-sw.js'); //++Todo: Replace with local workbox.js
|
||||
importScripts('../statics/js/idb.js');
|
||||
@@ -20,12 +20,6 @@ const cfgenv = {
|
||||
dbversion: 11,
|
||||
}
|
||||
|
||||
// console.log('SW-06 2');
|
||||
|
||||
// console.log('SERVERWEB=', cfgenv.serverweb)
|
||||
|
||||
// console.log('serverweb', cfgenv.serverweb)
|
||||
|
||||
async function writeData(table, data) {
|
||||
// console.log('writeData', table, data);
|
||||
await idbKeyval.setdata(table, data);
|
||||
@@ -108,30 +102,43 @@ if (workbox) {
|
||||
new RegExp(cfgenv.serverweb + '/todos/'),
|
||||
function (args) {
|
||||
// console.log('registerRoute!')
|
||||
// console.log('DATABODY:', args.event.request.body)
|
||||
let myres = null
|
||||
return fetch(args.event.request, args.event.headers)
|
||||
.then(function (res) {
|
||||
console.log('1° ******* [[[ SERVICE-WORKER ]]] registerRoute fetch: -> ', args.event.headers)
|
||||
myres = res
|
||||
console.log('1° ******* [[[ SERVICE-WORKER ]]] registerRoute fetch: -> ', args.event.request, res)
|
||||
// LOAD FROM SERVER , AND SAVE INTO INDEXEDDB
|
||||
console.log('res.status', res.status)
|
||||
if (res.status === 200) {
|
||||
const clonedRes = res.clone();
|
||||
return clearAllData('todos')
|
||||
.then(function () {
|
||||
return clonedRes.json();
|
||||
})
|
||||
.then(function (data) {
|
||||
if (data.todos) {
|
||||
console.log('Records TODOS Received from Server [', data.todos.length, 'record]', data.todos)
|
||||
for (let key in data.todos) {
|
||||
writeData('todos', data.todos[key])
|
||||
}
|
||||
}
|
||||
});
|
||||
clearAllData('todos')
|
||||
return clonedRes
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
);
|
||||
.then((clonedRes) => {
|
||||
if (clonedRes !== null)
|
||||
return clonedRes.json();
|
||||
return null
|
||||
})
|
||||
.then(data => {
|
||||
if (data) {
|
||||
if (data.todos) {
|
||||
console.log('Records TODOS Received from Server [', data.todos.length, 'record]', data.todos)
|
||||
for (let key in data.todos) {
|
||||
writeData('todos', data.todos[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
return myres
|
||||
})
|
||||
.catch(err => {
|
||||
console.log('ERROR registerRoute FETCH:', err)
|
||||
return myres
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
workbox.routing.registerRoute(
|
||||
@@ -199,18 +206,18 @@ if (workbox) {
|
||||
})
|
||||
);
|
||||
|
||||
// workbox.routing.registerRoute(
|
||||
// new RegExp(/^http/),
|
||||
// workbox.strategies.networkFirst({
|
||||
// cacheName: 'all-stuff',
|
||||
// plugins: [
|
||||
// new workbox.expiration.Plugin({
|
||||
// maxAgeSeconds: 10 * 24 * 60 * 60,
|
||||
// // Only cache 10 requests.
|
||||
// }),
|
||||
// ]
|
||||
// })
|
||||
// );
|
||||
// workbox.routing.registerRoute(
|
||||
// new RegExp(/^http/),
|
||||
// workbox.strategies.networkFirst({
|
||||
// cacheName: 'all-stuff',
|
||||
// plugins: [
|
||||
// new workbox.expiration.Plugin({
|
||||
// maxAgeSeconds: 10 * 24 * 60 * 60,
|
||||
// // Only cache 10 requests.
|
||||
// }),
|
||||
// ]
|
||||
// })
|
||||
// );
|
||||
|
||||
|
||||
workbox.routing.registerRoute(
|
||||
@@ -227,13 +234,23 @@ if ('serviceWorker' in navigator) {
|
||||
|
||||
}
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
if (event.request.url === '/') {
|
||||
const staleWhileRevalidate = new workbox.strategies.StaleWhileRevalidate();
|
||||
event.respondWith(staleWhileRevalidate.handle({ event }));
|
||||
}
|
||||
self.addEventListener('fetch', function (event) {
|
||||
event.respondWith(
|
||||
fetch(event.request, event.headers)
|
||||
.catch(err => {
|
||||
console.log('_______________________ ERRORE FETCH SW: ', event.request, err)
|
||||
return caches.match(event.request);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// self.addEventListener('fetch', (event) => {
|
||||
// if (event.request.url === '/') {
|
||||
// const staleWhileRevalidate = new workbox.strategies.StaleWhileRevalidate();
|
||||
// event.respondWith(staleWhileRevalidate.handle({ event }));
|
||||
// }
|
||||
// });
|
||||
|
||||
// self.addEventListener('fetch', function (event) {
|
||||
// console.log('[Service Worker] Fetching something ....', event);
|
||||
// console.log('event.request.cache=', event.request.cache)
|
||||
@@ -295,6 +312,8 @@ self.addEventListener('sync', function (event) {
|
||||
|
||||
console.log('++++++++++++++++++ SYNCING !!!! ', rec.descr, table, 'FETCH: ', method, link, 'data:')
|
||||
|
||||
console.log('DATATOSAVE:', JSON.stringify(rec))
|
||||
|
||||
// Insert/Delete/Update table to the server
|
||||
fetch(link, {
|
||||
method: method,
|
||||
@@ -302,14 +321,15 @@ self.addEventListener('sync', function (event) {
|
||||
mode: 'cors', // 'no-cors',
|
||||
body: JSON.stringify(rec)
|
||||
})
|
||||
.then(resData => deleteItemFromData(table, rec._id))
|
||||
.then(() => {
|
||||
console.log('DELETE: ', mystrparam)
|
||||
console.log('DELETE SWMSG: ', mystrparam)
|
||||
deleteItemFromData('swmsg', mystrparam)
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.log('DELETE : ', table, mystrparam)
|
||||
deleteItemFromData(table, rec._id)
|
||||
console.log('!!!!!!!!!!!!!!! Error while sending data', err);
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -69,6 +69,11 @@ export default class SingleTodo extends Vue {
|
||||
this.updateClasses()
|
||||
}
|
||||
|
||||
dateToYYYYMMDD(date) {
|
||||
// may have timezone caveats https://stackoverflow.com/a/29774197/1850609
|
||||
return date && date.toISOString().split('T')[0]
|
||||
}
|
||||
|
||||
isTodo() {
|
||||
return this.isTodoByElem(this.itemtodo)
|
||||
}
|
||||
|
||||
@@ -49,9 +49,13 @@
|
||||
<div v-if="itemtodo.enableExpiring">
|
||||
<div :class="classExpiring">
|
||||
<q-datetime
|
||||
type="date"
|
||||
:class="classExpiringEx"
|
||||
v-model="itemtodo.expiring_at"
|
||||
class="myexpired">
|
||||
class="myexpired"
|
||||
format="DD/MM/YY"
|
||||
@change="val => { model = val }" >
|
||||
|
||||
</q-datetime>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -350,24 +350,11 @@ export default class Todo extends Vue {
|
||||
}, 10000)
|
||||
}
|
||||
|
||||
copy(o) {
|
||||
let output, v, key
|
||||
output = Array.isArray(o) ? [] : {}
|
||||
for (key in o) {
|
||||
v = o[key]
|
||||
output[key] = (typeof v === 'object') ? this.copy(v) : v
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
|
||||
initcat() {
|
||||
|
||||
var tomorrow = new Date();
|
||||
tomorrow.setDate(tomorrow.getDate() + 1);
|
||||
|
||||
let mydatenow = new Date().getDate()
|
||||
let mydateexp = new Date().getDate() + 10
|
||||
|
||||
// console.log('User:' + UserStore.state.userId)
|
||||
|
||||
const objtodo: ITodo = {
|
||||
// _id: new Date().toISOString(), // Create NEW
|
||||
@@ -376,11 +363,11 @@ export default class Todo extends Vue {
|
||||
descr: '',
|
||||
priority: rescodes.Todos.PRIORITY_NORMAL,
|
||||
completed: false,
|
||||
created_at: mydatenow,
|
||||
modify_at: mydatenow,
|
||||
completed_at: 0,
|
||||
created_at: new Date(),
|
||||
modify_at: new Date(),
|
||||
completed_at: new Date(),
|
||||
category: '',
|
||||
expiring_at: mydateexp,
|
||||
expiring_at: tomorrow,
|
||||
enableExpiring: false,
|
||||
id_prev: '',
|
||||
id_next: '',
|
||||
@@ -388,7 +375,8 @@ export default class Todo extends Vue {
|
||||
modified: false,
|
||||
progress: 0
|
||||
}
|
||||
return this.copy(objtodo)
|
||||
// return this.copy(objtodo)
|
||||
return objtodo
|
||||
|
||||
}
|
||||
|
||||
@@ -417,8 +405,6 @@ export default class Todo extends Vue {
|
||||
|
||||
const objtodo = this.initcat()
|
||||
|
||||
console.log('insertTodo ', UserStore.state.userId)
|
||||
|
||||
objtodo.descr = this.todo
|
||||
objtodo.category = this.getCategory()
|
||||
const lastelem: ITodo = this.getLastList()
|
||||
@@ -766,7 +752,7 @@ export default class Todo extends Vue {
|
||||
|
||||
modifyField(recOut, recIn, field) {
|
||||
if (recOut[field] !== recIn[field]) {
|
||||
// console.log('*************** CAMPO ', field, 'MODIFICATO!', recOut[field], recIn[field])
|
||||
// console.log('*************** CAMPO ', field, 'MODIFICATO!', recOut[field])
|
||||
recOut.modified = true
|
||||
recOut[field] = recIn[field]
|
||||
return true
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export let idbKeyval = (() => {
|
||||
let db;
|
||||
const fieldsData = ['completed_at', 'created_at', 'expiring_at', 'modify_at']
|
||||
|
||||
function getDB() {
|
||||
if (!db) {
|
||||
@@ -29,7 +30,7 @@ export let idbKeyval = (() => {
|
||||
return db;
|
||||
}
|
||||
|
||||
async function withStore(type, table, callback, ) {
|
||||
async function withStore(type, table, callback,) {
|
||||
const db = await getDB();
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction(table, type);
|
||||
@@ -47,14 +48,18 @@ export let idbKeyval = (() => {
|
||||
});
|
||||
return req.result;
|
||||
},
|
||||
jsonCopy(src) {
|
||||
return JSON.parse(JSON.stringify(src));
|
||||
},
|
||||
|
||||
async getdata(table, key) {
|
||||
let req;
|
||||
|
||||
await withStore('readonly', table, store => {
|
||||
console.log('store', store, 'key', key)
|
||||
// console.log('store', store, 'key', key)
|
||||
req = store.get(key);
|
||||
});
|
||||
// console.log('RISFINALE!', req.result)
|
||||
|
||||
return req.result;
|
||||
},
|
||||
async getalldata(table) {
|
||||
@@ -80,9 +85,8 @@ export let idbKeyval = (() => {
|
||||
},
|
||||
async setdata(table, value) {
|
||||
let req;
|
||||
// console.log('setdata', table, value)
|
||||
|
||||
await withStore('readwrite', table, store => {
|
||||
await withStore('readwrite', table, store => {
|
||||
req = store.put(value);
|
||||
});
|
||||
return req.result;
|
||||
|
||||
@@ -5,10 +5,10 @@ export interface ITodo {
|
||||
descr?: string,
|
||||
priority: number,
|
||||
completed: boolean,
|
||||
created_at: any,
|
||||
modify_at: any,
|
||||
completed_at: any,
|
||||
expiring_at: any,
|
||||
created_at: Date,
|
||||
modify_at: Date,
|
||||
completed_at: Date,
|
||||
expiring_at: Date,
|
||||
enableExpiring?: boolean,
|
||||
id_prev?: string,
|
||||
id_next?: string,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export interface IToken {
|
||||
access: string
|
||||
token: string
|
||||
data_login: Date
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -47,14 +47,28 @@ let idbKeyval = (() => {
|
||||
});
|
||||
return req.result;
|
||||
},
|
||||
|
||||
// jsonCopy(src) {
|
||||
// return JSON.parse(JSON.stringify(src));
|
||||
// },
|
||||
|
||||
// contains(a, b) {
|
||||
// // array matches
|
||||
// if (Array.isArray(b)) {
|
||||
// return b.some(x => a.indexOf(x) > -1);
|
||||
// }
|
||||
// // string match
|
||||
// return a.indexOf(b) > -1;
|
||||
// },
|
||||
|
||||
async getdata(table, key) {
|
||||
let req;
|
||||
|
||||
await withStore('readonly', table, store => {
|
||||
console.log('store', store, 'key', key)
|
||||
// console.log('store', store, 'key', key)
|
||||
req = store.get(key);
|
||||
});
|
||||
// console.log('RISFINALE!', req.result)
|
||||
|
||||
return req.result;
|
||||
},
|
||||
async getalldata(table) {
|
||||
@@ -73,7 +87,7 @@ let idbKeyval = (() => {
|
||||
},
|
||||
async setdata(table, value) {
|
||||
let req;
|
||||
console.log('setdata', table, value)
|
||||
// console.log('setdata', table, value)
|
||||
|
||||
await withStore('readwrite', table, store => {
|
||||
req = store.put(value);
|
||||
|
||||
@@ -31,9 +31,12 @@ async function sendRequest(url: string, lang: string, mytok: string, method: str
|
||||
method: method,
|
||||
cache: 'no-cache',
|
||||
mode: 'cors',
|
||||
body: JSON.stringify(mydata),
|
||||
headers: authHeader
|
||||
}
|
||||
|
||||
if (mydata !== null)
|
||||
configInit.body = JSON.stringify(mydata)
|
||||
|
||||
}
|
||||
|
||||
const request: Promise<Response> = fetch(url, configInit)
|
||||
|
||||
@@ -107,8 +107,10 @@ export namespace ApiTool {
|
||||
return resolve({ res, body })
|
||||
})
|
||||
.catch(e => {
|
||||
UserStore.mutations.setServerCode(rescodes.ERR_GENERICO)
|
||||
return reject({ code: rescodes.ERR_GENERICO, status: res.status })
|
||||
return resolve({ res, body: {} })
|
||||
// Array not found...
|
||||
// UserStore.mutations.setServerCode(rescodes.ERR_GENERICO)
|
||||
// return reject({ code: rescodes.ERR_GENERICO, status: res.status })
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
@@ -278,6 +278,8 @@ namespace Actions {
|
||||
|
||||
const token = UserStore.state.idToken
|
||||
|
||||
console.log('TODO TO SAVE: ', itemtodo)
|
||||
|
||||
let res = await Api.SendReq(call, UserStore.state.lang, token, method, itemtodo)
|
||||
.then(({ res, newItem }) => {
|
||||
console.log('dbInsertSaveTodo RIS =', newItem)
|
||||
|
||||
@@ -92,7 +92,7 @@ namespace Mutations {
|
||||
state.category = data.categorySel
|
||||
// @ts-ignore
|
||||
state.tokens = [
|
||||
{ access: 'auth', token: data.idToken }
|
||||
{ access: 'auth ' + navigator.userAgent, token: data.idToken, date_login: new Date() }
|
||||
]
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ namespace Mutations {
|
||||
if (!state.tokens) {
|
||||
state.tokens = []
|
||||
}
|
||||
state.tokens.push({ access: 'auth', token: data.idToken })
|
||||
state.tokens.push({ access: 'auth ' + navigator.userAgent, token: data.idToken, data_login: new Date() })
|
||||
}
|
||||
|
||||
function setServerCode(state: IUserState, num: number) {
|
||||
@@ -143,6 +143,7 @@ namespace Mutations {
|
||||
if (state.servercode !== rescodes.ERR_SERVERFETCH) {
|
||||
state.servercode = err
|
||||
}
|
||||
console.log('Err catch: (servercode:', err, ')')
|
||||
}
|
||||
|
||||
function getMsgError(state: IUserState, err: number) {
|
||||
|
||||
Reference in New Issue
Block a user