refactor group pages to composition API

This solves some errors from just reinstating the pages since the new
routing and tab implementation have a different API.
This commit is contained in:
Johann150 2022-08-10 16:58:18 +02:00
parent 7a80015225
commit d6fb9619f6
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1
2 changed files with 196 additions and 225 deletions

View file

@ -3,17 +3,17 @@
<transition :name="$store.state.animation ? 'zoom' : ''" mode="out-in"> <transition :name="$store.state.animation ? 'zoom' : ''" mode="out-in">
<div v-if="group" class="_section"> <div v-if="group" class="_section">
<div class="_content" style="display: flex; gap: var(--margin); flex-wrap: wrap;"> <div class="_content" style="display: flex; gap: var(--margin); flex-wrap: wrap;">
<MkButton inline @click="invite()">{{ $ts.invite }}</MkButton> <MkButton inline @click="invite()">{{ i18n.ts.invite }}</MkButton>
<MkButton inline @click="renameGroup()">{{ $ts.rename }}</MkButton> <MkButton inline @click="renameGroup()">{{ i18n.ts.rename }}</MkButton>
<MkButton inline @click="transfer()">{{ $ts.transfer }}</MkButton> <MkButton inline @click="transfer()">{{ i18n.ts.transfer }}</MkButton>
<MkButton inline @click="deleteGroup()">{{ $ts.delete }}</MkButton> <MkButton inline @click="deleteGroup()">{{ i18n.ts.delete }}</MkButton>
</div> </div>
</div> </div>
</transition> </transition>
<transition :name="$store.state.animation ? 'zoom' : ''" mode="out-in"> <transition :name="$store.state.animation ? 'zoom' : ''" mode="out-in">
<div v-if="group" class="_section members _gap"> <div v-if="group" class="_section members _gap">
<div class="_title">{{ $ts.members }}</div> <div class="_title">{{ i18n.ts.members }}</div>
<div class="_content"> <div class="_content">
<div class="users"> <div class="users">
<div v-for="user in users" :key="user.id" class="user _panel"> <div v-for="user in users" :key="user.id" class="user _panel">
@ -33,113 +33,91 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { computed, defineComponent } from 'vue'; import { computed } from 'vue';
import MkButton from '@/components/ui/button.vue'; import MkButton from '@/components/ui/button.vue';
import * as os from '@/os'; import * as os from '@/os';
import * as symbols from '@/symbols'; import { definePageMetadata } from '@/scripts/page-metadata';
import { i18n } from '@/i18n';
import { mainRouter } from '@/router';
export default defineComponent({ const props = defineProps<{
components: { groupId: string;
MkButton }>();
},
props: { let group = $ref(null);
groupId: { let users = $ref([]);
type: String,
required: true,
},
},
data() { watch(props.groupId, fetch, { immediate: true });
return {
[symbols.PAGE_INFO]: computed(() => this.group ? {
title: this.group.name,
icon: 'fas fa-users',
} : null),
group: null,
users: [],
};
},
watch: { definePageMetadata(computed(() => group ? {
groupId: 'fetch', title: group.name,
}, icon: 'fas fa-users',
} : null));
created() { function fetch(): void {
this.fetch(); os.api('users/groups/show', { groupId: props.groupId })
}, .then(fetchedGroup => {
group = fetchedGroup;
os.api('users/show', { userIds: group.userIds })
.then(fetchedUsers => users = fetchedUsers);
});
}
methods: { function invite(): void {
fetch() { os.selectUser().then(user => {
os.api('users/groups/show', { os.apiWithDialog('users/groups/invite', {
groupId: this.groupId groupId: group.id,
}).then(group => { userId: user.id,
this.group = group; });
os.api('users/show', { });
userIds: this.group.userIds }
}).then(users => {
this.users = users;
});
});
},
invite() { function removeUser(user): void {
os.selectUser().then(user => { os.api('users/groups/pull', {
os.apiWithDialog('users/groups/invite', { groupId: group.id,
groupId: this.group.id, userId: user.id,
userId: user.id }).then(() => {
}); users = users.filter(x => x.id !== user.id);
}); });
}, }
removeUser(user) { async function renameGroup(): void {
os.api('users/groups/pull', { const { canceled, result: name } = await os.inputText({
groupId: this.group.id, title: i18n.ts.groupName,
userId: user.id default: group.name,
}).then(() => { });
this.users = this.users.filter(x => x.id !== user.id); if (canceled) return;
});
},
async renameGroup() { await os.api('users/groups/update', {
const { canceled, result: name } = await os.inputText({ groupId: group.id,
title: this.$ts.groupName, name,
default: this.group.name });
});
if (canceled) return;
await os.api('users/groups/update', { group.name = name;
groupId: this.group.id, }
name: name
});
this.group.name = name; function transfer(): void {
}, os.selectUser().then(user => {
os.apiWithDialog('users/groups/transfer', {
groupId: group.id,
userId: user.id,
});
});
}
transfer() { async function deleteGroup() {
os.selectUser().then(user => { const { canceled } = await os.confirm({
os.apiWithDialog('users/groups/transfer', { type: 'warning',
groupId: this.group.id, text: i18n.t('removeAreYouSure', { x: group.name }),
userId: user.id });
}); if (canceled) return;
});
},
async deleteGroup() { await os.apiWithDialog('users/groups/delete', {
const { canceled } = await os.confirm({ groupId: group.id,
type: 'warning', });
text: this.$t('removeAreYouSure', { x: this.group.name }), mainRouter.push('/my/groups');
}); }
if (canceled) return;
await os.apiWithDialog('users/groups/delete', {
groupId: this.group.id
});
this.$router.push('/my/groups');
}
}
});
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View file

@ -1,146 +1,139 @@
<template> <template>
<MkSpacer :content-max="700"> <MkStickyContainer>
<div v-if="tab === 'owned'" class="_content"> <template #header><MkPageHeader v-model:tab="tab" :actions="headerActions" :tabs="headerTabs"/></template>
<MkButton primary style="margin: 0 auto var(--margin) auto;" @click="create"><i class="fas fa-plus"></i> {{ $ts.createGroup }}</MkButton> <MkSpacer :content-max="700">
<div v-if="tab === 'owned'" class="_content">
<MkPagination v-slot="{items}" ref="owned" :pagination="ownedPagination"> <MkPagination v-slot="{items}" ref="owned" :pagination="ownedPagination">
<div v-for="group in items" :key="group.id" class="_card"> <div v-for="group in items" :key="group.id" class="_card">
<div class="_title"><MkA :to="`/my/groups/${ group.id }`" class="_link">{{ group.name }}</MkA></div> <div class="_title"><MkA :to="`/my/groups/${ group.id }`" class="_link">{{ group.name }}</MkA></div>
<div class="_content"><MkAvatars :user-ids="group.userIds"/></div> <div class="_content"><MkAvatars :user-ids="group.userIds"/></div>
</div>
</MkPagination>
</div>
<div v-else-if="tab === 'joined'" class="_content">
<MkPagination v-slot="{items}" ref="joined" :pagination="joinedPagination">
<div v-for="group in items" :key="group.id" class="_card">
<div class="_title">{{ group.name }}</div>
<div class="_content"><MkAvatars :user-ids="group.userIds"/></div>
<div class="_footer">
<MkButton danger @click="leave(group)">{{ $ts.leaveGroup }}</MkButton>
</div> </div>
</div> </MkPagination>
</MkPagination> </div>
</div>
<div v-else-if="tab === 'invites'" class="_content"> <div v-else-if="tab === 'joined'" class="_content">
<MkPagination v-slot="{items}" ref="invitations" :pagination="invitationPagination"> <MkPagination v-slot="{items}" ref="joined" :pagination="joinedPagination">
<div v-for="invitation in items" :key="invitation.id" class="_card"> <div v-for="group in items" :key="group.id" class="_card">
<div class="_title">{{ invitation.group.name }}</div> <div class="_title">{{ group.name }}</div>
<div class="_content"><MkAvatars :user-ids="invitation.group.userIds"/></div> <div class="_content"><MkAvatars :user-ids="group.userIds"/></div>
<div class="_footer"> <div class="_footer">
<MkButton primary inline @click="acceptInvite(invitation)"><i class="fas fa-check"></i> {{ $ts.accept }}</MkButton> <MkButton danger @click="leave(group)">{{ i18n.ts.leaveGroup }}</MkButton>
<MkButton primary inline @click="rejectInvite(invitation)"><i class="fas fa-ban"></i> {{ $ts.reject }}</MkButton> </div>
</div> </div>
</div> </MkPagination>
</MkPagination> </div>
</div>
</MkSpacer> <div v-else-if="tab === 'invites'" class="_content">
<MkPagination v-slot="{items}" ref="invitations" :pagination="invitationPagination">
<div v-for="invitation in items" :key="invitation.id" class="_card">
<div class="_title">{{ invitation.group.name }}</div>
<div class="_content"><MkAvatars :user-ids="invitation.group.userIds"/></div>
<div class="_footer">
<MkButton primary inline @click="acceptInvite(invitation)"><i class="fas fa-check"></i> {{ i18n.ts.accept }}</MkButton>
<MkButton primary inline @click="rejectInvite(invitation)"><i class="fas fa-ban"></i> {{ i18n.ts.reject }}</MkButton>
</div>
</div>
</MkPagination>
</div>
</MkSpacer>
</MkStickyContainer>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent, computed } from 'vue'; import { computed } from 'vue';
import MkPagination from '@/components/ui/pagination.vue'; import MkPagination from '@/components/ui/pagination.vue';
import MkButton from '@/components/ui/button.vue'; import MkButton from '@/components/ui/button.vue';
import MkContainer from '@/components/ui/container.vue'; import MkContainer from '@/components/ui/container.vue';
import MkAvatars from '@/components/avatars.vue'; import MkAvatars from '@/components/avatars.vue';
import MkTab from '@/components/tab.vue'; import MkTab from '@/components/tab.vue';
import * as os from '@/os'; import * as os from '@/os';
import * as symbols from '@/symbols'; import { definePageMetadata } from '@/scripts/page-metadata';
import { i18n } from '@/i18n';
export default defineComponent({ const headerActions = [{
components: { icon: 'fas fa-plus',
MkPagination, text: i18n.ts.createGroup,
MkButton, handler: create,
MkContainer, }];
MkTab,
MkAvatars,
},
data() { const headerTabs = [{
return { key: 'owned',
[symbols.PAGE_INFO]: computed(() => ({ title: i18n.ts.ownedGroups,
title: this.$ts.groups, icon: 'fas fa-user-tie',
icon: 'fas fa-users', }, {
bg: 'var(--bg)', key: 'joined',
actions: [{ title: i18n.ts.joinedGroups,
icon: 'fas fa-plus', icon: 'fas fa-id-badge',
text: this.$ts.createGroup, }, {
handler: this.create, key: 'invites',
}], title: i18n.ts.invites,
tabs: [{ icon: 'fas fa-envelope-open-text',
active: this.tab === 'owned', }];
title: this.$ts.ownedGroups,
icon: 'fas fa-user-tie',
onClick: () => { this.tab = 'owned'; },
}, {
active: this.tab === 'joined',
title: this.$ts.joinedGroups,
icon: 'fas fa-id-badge',
onClick: () => { this.tab = 'joined'; },
}, {
active: this.tab === 'invites',
title: this.$ts.invites,
icon: 'fas fa-envelope-open-text',
onClick: () => { this.tab = 'invites'; },
},]
})),
tab: 'owned',
ownedPagination: {
endpoint: 'users/groups/owned' as const,
limit: 10,
},
joinedPagination: {
endpoint: 'users/groups/joined' as const,
limit: 10,
},
invitationPagination: {
endpoint: 'i/user-group-invites' as const,
limit: 10,
},
};
},
methods: { let tab: 'owned' | 'joined' | 'invites' = $ref('owned');
async create() { let owned = $ref<MkPagination>();
const { canceled, result: name } = await os.inputText({ let joined = $ref<MkPagination>();
title: this.$ts.groupName, let invitations = $ref<MkPagination>();
});
if (canceled) return; const ownedPagination = {
await os.api('users/groups/create', { name: name }); endpoint: 'users/groups/owned' as const,
this.$refs.owned.reload(); limit: 10,
os.success(); };
}, const joinedPagination = {
acceptInvite(invitation) { endpoint: 'users/groups/joined' as const,
os.api('users/groups/invitations/accept', { limit: 10,
invitationId: invitation.id };
}).then(() => { const invitationPagination = {
os.success(); endpoint: 'i/user-group-invites' as const,
this.$refs.invitations.reload(); limit: 10,
this.$refs.joined.reload(); };
});
}, definePageMetadata(computed(() => ({
rejectInvite(invitation) { title: i18n.ts.groups,
os.api('users/groups/invitations/reject', { icon: 'fas fa-users',
invitationId: invitation.id bg: 'var(--bg)',
}).then(() => { })));
this.$refs.invitations.reload();
}); async function create(): void {
}, const { canceled, result: name } = await os.inputText({
async leave(group) { title: i18n.ts.groupName,
const { canceled } = await os.confirm({ });
type: 'warning', if (canceled) return;
text: this.$t('leaveGroupConfirm', { name: group.name }), await os.api('users/groups/create', { name });
}); owned.reload();
if (canceled) return; os.success();
os.apiWithDialog('users/groups/leave', { }
groupId: group.id,
}).then(() => { function acceptInvite(invitation): void {
this.$refs.joined.reload(); os.api('users/groups/invitations/accept', {
}); invitationId: invitation.id,
} }).then(() => {
} invitations.reload();
}); joined.reload();
os.success();
});
}
function rejectInvite(invitation): void {
os.api('users/groups/invitations/reject', {
invitationId: invitation.id
}).then(() => {
invitations.reload();
});
}
async function leave(group): void {
const { canceled } = await os.confirm({
type: 'warning',
text: i18n.t('leaveGroupConfirm', { name: group.name }),
});
if (canceled) return;
os.apiWithDialog('users/groups/leave', {
groupId: group.id,
}).then(() => {
joined.reload();
});
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>