first try presistent storage

This commit is contained in:
Tim Gröger 2021-11-25 23:10:55 +01:00
parent 053fdae384
commit 849e79345c
10 changed files with 2194 additions and 270 deletions

View File

@ -6,3 +6,4 @@ export * from './src/utils/datetime';
export * from './src/utils/permission';
export * from './src/utils/validators';
export * from './src/utils/misc';
export * from './src/database';

View File

@ -21,6 +21,7 @@
"devDependencies": {
"@flaschengeist/types": "^1.0.0-alpha.5",
"@types/node": "^12.20.37",
"@types/cordova-sqlite-storage": "^1.5.5",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"eslint": "^8.3.0",

124
api/src/database.ts Normal file
View File

@ -0,0 +1,124 @@
import { LocalStorage, Platform } from 'quasar';
import { api } from '../index';
import { useMainStore } from './stores';
import { baseURL } from 'src/config';
export let db: SQLitePlugin.Database | null = null;
if (Platform.is.cordova) {
document.addEventListener('deviceready', onDeviceReady);
}
interface ConfigResult {
name: string;
value: string;
}
function onDeviceReady() {
db = window.sqlitePlugin.openDatabase({ name: 'flaschengeist.db', location: 'default' });
db.transaction(function (transaction) {
transaction.executeSql(
'CREATE TABLE IF NOT EXISTS config_table (name STRING NOT NULL PRIMARY KEY, value STRING NOT NULL)'
);
transaction.executeSql(`INSERT INTO config_table VALUES ('baseUrl', '${baseURL.value}')`);
});
console.log('finish init database');
const url = loadConfig();
console.log('url is', url);
if (url) {
}
}
export function insertConfig(config: string, value: string) {
console.log('insert config', config, value)
console.log(db)
db?.transaction((tx) => {
console.log('insert', config, value)
tx.executeSql(
`INSERT INTO config_table VALUES ('${config}', '${value}')`,
[],
(tx1) => {
console.log('success insert into database', tx1);
},
(tx, err) => {
console.log('error insert into database', err);
console.log('update database', config, value)
tx.executeSql(
`UPDATE config_table SET value='${value}' WHERE name='${config}'`,
[],
(tx1) => {
console.log('success update database', tx1);
},
(tx, err) => {
console.log('error update databes', err);
}
);
}
);
})
}
export function deleteConfig(config: string) {
db?.transaction((tx) => {
tx.executeSql(
`DELETE FROM config_table WHERE name='${config}'`,
[],
(tx1, results) => {
console.log('success delete from database', tx1, results, results.rows.item(0));
},
(tx2, err) => {
console.log('error delete from database', tx2, err);
}
);
});
}
export function loadConfig() {
let result = null;
db?.transaction((tx) => {
tx.executeSql(
'SELECT value FROM config_table WHERE name="baseUrl"',
[],
(tx1, results) => {
console.log('results', tx1, results, results.rows.item(0));
result = (<ConfigResult>results.rows.item(0)).value;
if (result !== LocalStorage.getItem<string>('baseURL')) {
console.log('url has to change', result, LocalStorage.getItem<string>('baseURL'));
LocalStorage.set('baseURL', result);
api.defaults.baseURL = result;
setTimeout(() => {
window.location.reload();
}, 1000);
}
},
(tx2, err) => {
console.log('error', tx2, err);
}
);
tx.executeSql(
'SELECT value FROM config_table WHERE name="session"',
[],
(tx1, results) => {
console.log('results session', tx1, results, results.rows.item(0));
let session: string | FG.Session = (<ConfigResult>results.rows.item(0)).value;
console.log(session);
if (session) {
console.log('update session')
session = <FG.Session>JSON.parse(session);
console.log(session)
session.expires = new Date(session.expires);
console.log(session)
const store = useMainStore();
store.session = session;
LocalStorage.set('session', store.session);
}
},
(tx2, err) => {
console.log('error load session', err);
}
);
});
return result;
}

View File

@ -4,6 +4,7 @@ import { useSessionStore, useUserStore } from '.';
import { AxiosResponse } from 'axios';
import { api } from '../internal';
import { defineStore } from 'pinia';
import { insertConfig, deleteConfig } from '../database';
function loadCurrentSession() {
const session = LocalStorage.getItem<FG.Session>('session');
@ -66,6 +67,8 @@ export const useMainStore = defineStore({
this.session = data;
this.session.expires = new Date(this.session.expires);
LocalStorage.set('session', this.session);
console.log('insert config', JSON.stringify(this.session))
insertConfig('session', JSON.stringify(this.session));
return true;
} catch ({ response }) {
return (<AxiosResponse | undefined>response)?.status || false;
@ -145,7 +148,7 @@ export const useMainStore = defineStore({
},
handleLoggedOut() {
LocalStorage.clear();
deleteConfig('session');
this.$patch({
session: undefined,
user: undefined,

View File

@ -74,7 +74,7 @@ export const useUserStore = defineStore({
},
async deleteAvatar(user: FG.User) {
await api.delete(`/users/${user.userid}/avatar`)
await api.delete(`/users/${user.userid}/avatar`);
},
async getPermissions(force = false) {

View File

@ -8,6 +8,7 @@
"dom"
],
"types": [
"@types/cordova-sqlite-storage",
"@flaschengeist/types",
"@quasar/app",
"node"

View File

@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<widget id="de.wu5.flaschengeist" version="2.0.0-alpha.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<widget id="flaschengeist.wu5.de" version="2.0.0-alpha.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>Flaschengeist</name>
<description>Modular student club administration system</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
@ -70,6 +70,7 @@
<splash src="res/screen/ios/Default@3x~iphone~comany.png" />
<splash src="res/screen/ios/Default@2x~ipad~anyany.png" />
<splash src="res/screen/ios/Default@2x~ipad~comany.png" />
<preference name="Allow3DTouchLinkPreview" value="false" />
</platform>
<allow-navigation href="about:*" />
<preference name="SplashMaintainAspectRatio" value="true" />

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "de.wu5.flaschengeist",
"displayName": "Flaschengeist",
"name": "flaschengeist.wu5.de",
"displayName": "flaschengeist-frontend",
"version": "1.0.0",
"description": "A sample Apache Cordova application that responds to the deviceready event.",
"main": "index.js",
@ -13,19 +13,25 @@
"author": "Apache Cordova Team",
"license": "Apache-2.0",
"devDependencies": {
"cordova-android": "^9.0.0",
"cordova-ios": "^6.1.1",
"cordova-android": "^9.1.0",
"cordova-browser": "^6.0.0",
"cordova-ios": "^6.2.0",
"cordova-plugin-splashscreen": "^6.0.0",
"cordova-plugin-whitelist": "^1.3.4"
"cordova-plugin-whitelist": "^1.3.4",
"cordova-plugin-wkwebview-engine": "^1.2.2",
"cordova-sqlite-storage": "^6.0.0"
},
"cordova": {
"plugins": {
"cordova-plugin-whitelist": {},
"cordova-plugin-splashscreen": {}
"cordova-plugin-wkwebview-engine": {},
"cordova-plugin-splashscreen": {},
"cordova-sqlite-storage": {}
},
"platforms": [
"ios",
"android"
"android",
"browser"
]
}
}

View File

@ -1,4 +1,4 @@
import { useMainStore, api } from '@flaschengeist/api';
import { useMainStore, api, insertConfig } from '@flaschengeist/api';
import { LocalStorage, Notify } from 'quasar';
import { AxiosError } from 'axios';
import { boot } from 'quasar/wrappers';
@ -99,13 +99,14 @@ export { api };
export const setBaseURL = (url: string) => {
LocalStorage.set('baseURL', url);
api.defaults.baseURL = url;
insertConfig('baseUrl', url);
Notify.create({
message: 'Serveraddresse gespeichert',
position: 'bottom',
caption: `${url}`,
color: 'positive',
});
setTimeout(() => {
/*setTimeout(() => {
window.location.reload();
}, 5000);
}, 5000);*/
};