Skip to content

Commit

Permalink
Merge pull request #656 from EPFL-ENAC/dev
Browse files Browse the repository at this point in the history
feat(frontend): guest can store to local storage
  • Loading branch information
guilbep authored Mar 27, 2024
2 parents fc127da + 7ad2eb1 commit 0054b5f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 42 deletions.
73 changes: 48 additions & 25 deletions frontend/src/store/GhgModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { RootState } from ".";
import { updateMetaFieldsForUpdate } from "./documentUtils";
import { GridItem } from "./GhgReferenceGridModule";
import { ReferenceItemInterface } from "./GhgReferenceModule";
import { CouchUser } from "./UserModule";
import { CouchUser, Roles } from "./UserModule";

const MSG_DB_DOES_NOT_EXIST = "Please, init your database";
const MSG_USER_NOT_PRESENT = "Could not find user information";
Expand Down Expand Up @@ -311,27 +311,38 @@ const actions: ActionTree<ProjectsState, RootState> = {
const user = context.rootGetters["UserModule/user"] as CouchUser;
// context.commit("ADD_DOC", generateNewProject(newGhg, user));
const value = generateNewProject(newGhg, user) as any;
const remoteDB = context.state.localCouch?.remoteDB;
if (remoteDB) {
let db = context.state.localCouch?.remoteDB;
if (user.roles?.includes(Roles.guest)) {
db = context.state.localCouch?.localDB;
}
if (db) {
delete value.isUNHCR;
value._id = value?._id ?? value?.id ?? uuidv4();
delete value.id; // just in case we forgot to remove it
delete value._rev; // just in case we forgot to remove it
return remoteDB.post(value).then((response) => {
return db.post(value).then((response) => {
context.commit("NEW_ASSESSEMENT", true);
return context.dispatch("getDoc", response.id);
});
}
},
removeDoc: (context: ActionContext<ProjectsState, RootState>, id) => {
const remoteDB = context.state.localCouch?.remoteDB;
if (!remoteDB) {
const user = context.rootGetters["UserModule/user"] as CouchUser;

let db = context.state.localCouch?.remoteDB;
if (user.roles?.includes(Roles.guest)) {
db = context.state.localCouch?.localDB;
}
if (!db) {
throw new Error(MSG_DB_DOES_NOT_EXIST);
}
return remoteDB
return db
.get(id)
.then(function (doc: PouchDB.Core.ExistingDocument<GreenHouseGaz>) {
return remoteDB.put({ ...doc, _deleted: true });
if (!db) {
throw new Error(MSG_DB_DOES_NOT_EXIST);
}
return db.put({ ...doc, _deleted: true });
})
.finally(() => {
context.commit("REMOVE_ASSESSEMENT", true);
Expand All @@ -348,24 +359,32 @@ const actions: ActionTree<ProjectsState, RootState> = {
const newValue = updateMetaFieldsForUpdate(value, user);
context.commit("SET_PROJECT", newValue);
},
getDoc: (context: ActionContext<ProjectsState, RootState>, id) => {
getDoc: async (context: ActionContext<ProjectsState, RootState>, id) => {
const db = context.state.localCouch?.remoteDB;
if (db) {
const localDB = context.state.localCouch?.localDB; // for guest user only
if (localDB) {
context.commit("SET_PROJECT_LOADING", true);
return db
.get(id)
.then(function (result) {
context.commit("SET_PROJECT", result);
return result;
})
.catch(function (err: Error) {
context.commit("SET_PROJECT_LOADING", false);
err.message = `${err?.message} ${id}`;
throw err;
})
.finally(() => {
context.commit("SET_PROJECT_LOADING", false);
});
try {
const result = await localDB.get(id);
context.commit("SET_PROJECT", result);
return result;
} catch (errL: unknown) {
try {
if (db) {
const result = await db.get(id);
context.commit("SET_PROJECT", result);
return result;
}
} catch (err: unknown) {
if (err instanceof Error) {
context.commit("SET_PROJECT_LOADING", false);
err.message = `${err?.message} ${id}`;
}
}
throw errL;
} finally {
context.commit("SET_PROJECT_LOADING", false);
}
} else {
context.commit("SET_PROJECT_LOADING", false);
throw new Error(MSG_DB_DOES_NOT_EXIST);
Expand All @@ -388,7 +407,11 @@ const actions: ActionTree<ProjectsState, RootState> = {
// no need for "SET_PROJECT" it should only be done by getDoc

context.commit("SET_PROJECT_LOADING", true);
const db = context.state.localCouch?.remoteDB;

let db = context.state.localCouch?.remoteDB;
if (user.roles?.includes(Roles.guest)) {
db = context.state.localCouch?.localDB;
}
if (db) {
// first retrieve latest rev
// const doc = await db.get(newValue.id);
Expand Down
51 changes: 42 additions & 9 deletions frontend/src/store/ShelterModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
Module,
MutationTree,
} from "vuex";
import { CouchUser } from "./UserModule";
import { CouchUser, Roles } from "./UserModule";

const DB_NAME = "shelter_projects_1698666594213623";
const MSG_DB_DOES_NOT_EXIST = "Please, init your database";
Expand Down Expand Up @@ -206,11 +206,15 @@ const actions: ActionTree<ShelterState, RootState> = {
const user = context.rootGetters["UserModule/user"] as CouchUser;
if (user.name) {
const value = generateNewShelter(name, user);
const remoteDB = context.state.localCouch?.remoteDB;
if (!remoteDB) {
let db = context.state.localCouch?.remoteDB;

if (user.roles?.includes(Roles.guest)) {
db = context.state.localCouch?.localDB;
}
if (!db) {
throw new Error(MSG_DB_DOES_NOT_EXIST);
}
return remoteDB.post(value).then(() => {
return db.post(value).then(() => {
context.commit("ADD_DOC", value);
});
}
Expand All @@ -220,6 +224,9 @@ const actions: ActionTree<ShelterState, RootState> = {
value: Shelter
) => {
const user = context.rootGetters["UserModule/user"] as CouchUser;
if (user.roles?.includes(Roles.guest)) {
return;
}
if (user.name) {
// retrieve real document first
let newValue = await context.dispatch("getDoc", value._id);
Expand All @@ -238,6 +245,10 @@ const actions: ActionTree<ShelterState, RootState> = {
}
},
removeDoc: (context: ActionContext<ShelterState, RootState>, id) => {
const user = context.rootGetters["UserModule/user"] as CouchUser;
if (user.roles?.includes(Roles.guest)) {
return;
}
context.commit("REMOVE_DOC", id);
},
updateDoc: async (context: ActionContext<ShelterState, RootState>, value) => {
Expand All @@ -246,8 +257,13 @@ const actions: ActionTree<ShelterState, RootState> = {
value.updated_at = new Date().toISOString();
value.updated_by = user.name;
const computedShelter = computeShelter(value);
const remoteDB = context.state.localCouch?.remoteDB;
if (!remoteDB) {
// const remoteDB = context.state.localCouch?.remoteDB;
let db = context.state.localCouch?.remoteDB;

if (user.roles?.includes(Roles.guest)) {
db = context.state.localCouch?.localDB;
}
if (!db) {
throw new Error(MSG_DB_DOES_NOT_EXIST);
}
if (!context.getters["shelterLoading"]) {
Expand All @@ -257,7 +273,7 @@ const actions: ActionTree<ShelterState, RootState> = {
}, 300);
timeoutIds.push(timeId);
try {
const response = await remoteDB.put(computedShelter);
const response = await db.put(computedShelter);
computedShelter._rev = response.rev;
// we were calling the get Doc too many times
// const newValue = await context.dispatch("getDoc", response.id);
Expand Down Expand Up @@ -321,8 +337,25 @@ const actions: ActionTree<ShelterState, RootState> = {
true,
{ root: true }
);
let result: Shelter | undefined =
await context.state.localCouch?.remoteDB.get(id);
const db = context.state.localCouch?.remoteDB;
const localDB = context.state.localCouch?.localDB; // for guest user only
let result: Shelter | undefined;
if (localDB) {
try {
result = await localDB.get(id);
} catch (errL: unknown) {
try {
if (db) {
result = await db.get(id);
}
} catch (err: unknown) {
if (err instanceof Error) {
err.message = `${err?.message} ${id}`;
}
throw err;
}
}
}
if (result) {
result = computeShelter(result);
context.commit("SET_SHELTER", result);
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/utils/couchdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export function createSyncDatabase<T>(name: string): SyncDatabase<T> {

export class SyncDatabase<T> {
public remoteDB: PouchDB.Database<T>;
public localDB: PouchDB.Database<T>;
private onChangeListener: PouchDB.Core.Changes<T> | undefined;

constructor(name: string) {
Expand All @@ -198,6 +199,7 @@ export class SyncDatabase<T> {
}
: undefined,
});
this.localDB = new PouchDB<T>(name);
this.remoteDB = remoteDB;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@
</v-btn>
</v-col>
<v-col class="country-list__actions d-flex justify-end align-center">
<v-btn
class="float-right"
color="primary"
:disabled="!$can('create')"
text
@click="addSurvey"
>
<v-btn class="float-right" color="primary" text @click="addSurvey">
<v-icon left>$mdiPlusBox</v-icon>
New assessment
</v-btn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
? ""
: "(This assessment is the Reference for this site so it cannot be modified.)"
}}
<span v-if="$userIs('Guest')">(Guest mode: read only)</span>
</span>
</h2>
</v-col>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
<v-btn
class="float-right"
color="primary"
:disabled="!$can('create')"
text
@click="addProject"
>
Expand Down

0 comments on commit 0054b5f

Please sign in to comment.