Skip to content

Commit

Permalink
Merge pull request #64 from Tamandutech/fix/busca_e_atualizacao_de_se…
Browse files Browse the repository at this point in the history
…ssoes

Corrigir busca e sincronização de sessões no Firestore
  • Loading branch information
Matheus-de-Sousa authored Feb 7, 2024
2 parents 5943311 + cd2d394 commit 63a79bf
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/stores/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,18 @@ export const useSessionStore = defineStore('session', {

const userDocRef = doc(this.firestore, 'users', this.userId);
const userDocSnapshot = await getDoc(userDocRef);
const data = userDocSnapshot.data();
if (!userDocSnapshot.exists() || data.settings == undefined) {
await setDoc(userDocRef, {
userId: this.userId,
settings: defaultSettings,
});
return this.$patch({ settings: defaultSettings });
if (userDocSnapshot.exists()) {
const data = userDocSnapshot.data();
return this.$patch({ settings: data.settings, competitionId: data.competition?.id });
}

/**
* TODO: fazer com que o plugin ignore esse patch (talvez adicionando um campo
* protegido no estado para monitorar mudanças que precisam ser sincronizadas)
*/
return this.$patch({
settings: data.settings,
competitionId: data.competition.id,
});
const session: Omit<Dashboard.Session, "robot"> = {
userId: this.userId,
settings: defaultSettings,
competitionId: null,
}
setDoc(userDocRef, session);
return this.$patch(session);
},
async handleAuthStateChange(user: User | null) {
if (user) {
Expand All @@ -57,6 +52,7 @@ export const useSessionStore = defineStore('session', {
collection: 'users',
doc: 'userId',
fields: new Map([
['userId', { field: 'userId' }],
['settings', { field: 'settings' }],
['competitionId', { field: 'competition', ref: 'competitions' }],
]),
Expand Down

0 comments on commit 63a79bf

Please sign in to comment.