massive state refactor, mostly fixes #3

This commit is contained in:
Michael Dausmann
2023-04-12 00:05:32 +10:00
parent 4d288c7468
commit 028a7dda45
11 changed files with 160 additions and 171 deletions

View File

@@ -2,23 +2,15 @@
import { storeToRefs } from 'pinia';
import { ACCOUNT_ACCESS } from '@prisma/client';
const authStore = useAuthStore();
const { activeMembership } = storeToRefs(authStore);
const accountStore = useAccountStore();
const { activeAccountMembers } = storeToRefs(accountStore)
const { activeMembership, activeAccountMembers } = storeToRefs(accountStore)
const config = useRuntimeConfig();
const newAccountName = ref("");
onMounted(async () => {
await authStore.initUser();
await accountStore.init();
});
watchEffect(async () => {
if (activeMembership.value) {
await accountStore.getActiveAccountMembers();
}
})
function formatDate(date: Date | undefined){
if(!date){ return ""; }
@@ -50,8 +42,8 @@
[{{ accountMember.access }}]
<span v-if="accountMember.pending">(pending)</span>
<span v-if="accountMember.pending && activeMembership && (activeMembership.access === ACCOUNT_ACCESS.OWNER || activeMembership.access === ACCOUNT_ACCESS.ADMIN)"><button @click.prevent="accountStore.acceptPendingMembership(accountMember.id)">Accept Pending Membership</button></span>
<span v-if="activeMembership && (activeMembership.access === ACCOUNT_ACCESS.OWNER || activeMembership.access === ACCOUNT_ACCESS.ADMIN) && accountMember.access === ACCOUNT_ACCESS.READ_ONLY"><button @click.prevent="accountStore.changeUserAccessWithinAccount(accountMember.user.id, ACCOUNT_ACCESS.READ_WRITE)">Promote to Read/Write</button></span>
<span v-if="activeMembership && (activeMembership.access === ACCOUNT_ACCESS.OWNER || activeMembership.access === ACCOUNT_ACCESS.ADMIN) && accountMember.access === ACCOUNT_ACCESS.READ_WRITE"><button @click.prevent="accountStore.changeUserAccessWithinAccount(accountMember.user.id, ACCOUNT_ACCESS.ADMIN)">Promote to Admin</button></span>
<span v-if="activeMembership && (activeMembership.access === ACCOUNT_ACCESS.OWNER || activeMembership.access === ACCOUNT_ACCESS.ADMIN) && accountMember.access === ACCOUNT_ACCESS.READ_ONLY && !accountMember.pending"><button @click.prevent="accountStore.changeUserAccessWithinAccount(accountMember.user.id, ACCOUNT_ACCESS.READ_WRITE)">Promote to Read/Write</button></span>
<span v-if="activeMembership && (activeMembership.access === ACCOUNT_ACCESS.OWNER || activeMembership.access === ACCOUNT_ACCESS.ADMIN) && accountMember.access === ACCOUNT_ACCESS.READ_WRITE && !accountMember.pending"><button @click.prevent="accountStore.changeUserAccessWithinAccount(accountMember.user.id, ACCOUNT_ACCESS.ADMIN)">Promote to Admin</button></span>
</li>
</ul>

View File

@@ -5,21 +5,12 @@
middleware: ['auth'],
});
const authStore = useAuthStore();
const { activeMembership } = storeToRefs(authStore);
const notesStore = useNotesStore();
const { notes } = storeToRefs(notesStore); // ensure the notes list is reactive
onMounted(async () => {
await authStore.initUser();
});
watchEffect(async () => {
if (activeMembership.value) {
await notesStore.fetchNotesForCurrentUser();
}
})
await notesStore.fetchNotesForCurrentUser();
});
</script>
<template>
<div>

View File

@@ -4,8 +4,9 @@
const route = useRoute();
const {join_password} : {join_password?: string} = route.params;
const { $client } = useNuxtApp();
const accountStore = useAccountStore();
const { $client } = useNuxtApp();
// this could probably be an elegant destructure here but I lost patience
let account: AccountWithMembers | undefined;
if(join_password){
@@ -16,8 +17,8 @@
const { data: dbUser } = await $client.auth.getDBUser.useQuery();
async function doJoin(){
if(dbUser.value?.dbUser && account){
await $client.account.joinUserToAccountPending.useQuery({account_id: account.id, user_id: dbUser.value.dbUser.id});
if(account){
await accountStore.joinUserToAccountPending(account.id);
} else {
console.log(`Unable to Join`)
}