refactor: ♻️ Use setup syntax for groups index

This commit is contained in:
ThatOneCalculator 2022-09-13 18:57:26 -07:00
parent 64a96b8f67
commit 47509e28b1
4 changed files with 170 additions and 141 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "calckey", "name": "calckey",
"version": "12.119.0-calc-rc.2.2", "version": "12.119.0-calc-rc.2.3",
"codename": "aqua", "codename": "aqua",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -1,6 +1,6 @@
<template> <template>
<MkStickyContainer> <MkStickyContainer>
<template #header><MkPageHeader /></template> <template #header><MkPageHeader/></template>
<div class="mk-group-page"> <div class="mk-group-page">
<div class="_section"> <div class="_section">
<div <div
@ -141,7 +141,7 @@ definePageMetadata(
computed(() => ({ computed(() => ({
title: i18n.ts.members, title: i18n.ts.members,
icon: "fas fa-users", icon: "fas fa-users",
})) })),
); );
</script> </script>

View File

@ -1,145 +1,174 @@
<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="1000">
<swiper
<MkPagination v-slot="{items}" ref="owned" :pagination="ownedPagination"> :modules="[Virtual]"
<div v-for="group in items" :key="group.id" class="_card"> :space-between="20"
<div class="_title"><MkA :to="`/my/groups/${ group.id }`" class="_link">{{ group.name }}</MkA></div> :virtual="true"
<div class="_content"><MkAvatars :user-ids="group.userIds"/></div> @swiper="setSwiperRef"
</div> @slide-change="onSlideChange"
</MkPagination> >
</div> <swiper-slide class="_content">
<MkButton primary style="margin: 0 auto var(--margin) auto;" @click="create"><i class="fas fa-plus"></i> {{ i18n.ts.createGroup }}</MkButton>
<div v-else-if="tab === 'joined'" class="_content"> <MkPagination v-slot="{items}" ref="owned" :pagination="ownedPagination">
<MkPagination v-slot="{items}" ref="joined" :pagination="joinedPagination"> <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">{{ group.name }}</div> <div class="_content">
<div class="_content"><MkAvatars :user-ids="group.userIds"/></div> <MkAvatars :user-ids="group.userIds"/>
<div class="_footer"> </div>
<MkButton danger @click="leave(group)">{{ $ts.leaveGroup }}</MkButton> </div>
</div> </MkPagination>
</div> </swiper-slide>
</MkPagination> <swiper-slide class="_content">
</div> <MkPagination v-slot="{items}" ref="joined" :pagination="joinedPagination">
<div v-for="group in items" :key="group.id" class="_card">
<div v-else-if="tab === 'invites'" class="_content"> <div class="_title">{{ group.name }}</div>
<MkPagination v-slot="{items}" ref="invitations" :pagination="invitationPagination"> <div class="_content">
<div v-for="invitation in items" :key="invitation.id" class="_card"> <MkAvatars :user-ids="group.userIds"/>
<div class="_title">{{ invitation.group.name }}</div> </div>
<div class="_content"><MkAvatars :user-ids="invitation.group.userIds"/></div> <div class="_footer">
<div class="_footer"> <MkButton danger @click="leave(group)">{{ i18n.ts.leaveGroup }}</MkButton>
<MkButton primary inline @click="acceptInvite(invitation)"><i class="fas fa-check"></i> {{ $ts.accept }}</MkButton> </div>
<MkButton primary inline @click="rejectInvite(invitation)"><i class="fas fa-ban"></i> {{ $ts.reject }}</MkButton> </div>
</div> </MkPagination>
</div> </swiper-slide>
</MkPagination> <swiper-slide class="_content">
</div> <MkPagination v-slot="{items}" ref="invitations" :pagination="invitationPagination">
</MkSpacer> <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>
</swiper-slide>
</swiper>
</MkSpacer>
</MkStickyContainer>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent, computed } from 'vue'; import { computed, ref } from 'vue';
import { Virtual } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/vue';
import MkPagination from '@/components/MkPagination.vue'; import MkPagination from '@/components/MkPagination.vue';
import MkButton from '@/components/MkButton.vue'; import MkButton from '@/components/MkButton.vue';
import MkContainer from '@/components/MkContainer.vue';
import MkAvatars from '@/components/MkAvatars.vue'; import MkAvatars from '@/components/MkAvatars.vue';
import MkTab from '@/components/MkTab.vue';
import * as os from '@/os'; import * as os from '@/os';
import { definePageMetadata } from "@/scripts/page-metadata";
import { i18n } from '@/i18n';
import MkStickyContainer from '@/components/global/MkStickyContainer.vue';
export default defineComponent({ const tabs = ['owned', 'joined', 'invites'];
components: { let tab = $ref('owned');
MkPagination, const owned = ref('owned');
MkButton, const joined = ref('joined');
MkContainer, const invitations = ref('invitations');
MkTab,
MkAvatars,
},
data() { const ownedPagination = {
return { endpoint: 'users/groups/owned' as const,
[Symbol('Page info')]: computed(() => ({ limit: 12,
title: this.$ts.groups, };
icon: 'fas fa-users',
bg: 'var(--bg)',
actions: [{
icon: 'fas fa-plus',
text: this.$ts.createGroup,
handler: this.create,
}],
tabs: [{
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: { const joinedPagination = {
async create() { endpoint: 'users/groups/joined' as const,
const { canceled, result: name } = await os.inputText({ limit: 12,
title: this.$ts.groupName, };
});
if (canceled) return; const invitationPagination = {
await os.api('users/groups/create', { name: name }); endpoint: 'i/user-group-invites' as const,
this.$refs.owned.reload(); limit: 12,
os.success(); };
},
acceptInvite(invitation) { const headerActions = $computed(() => [
os.api('users/groups/invitations/accept', { {
invitationId: invitation.id icon: 'fas fa-plus',
}).then(() => { text: i18n.ts.createGroup,
os.success(); handler: create,
this.$refs.invitations.reload();
this.$refs.joined.reload();
});
},
rejectInvite(invitation) {
os.api('users/groups/invitations/reject', {
invitationId: invitation.id
}).then(() => {
this.$refs.invitations.reload();
});
},
async leave(group) {
const { canceled } = await os.confirm({
type: 'warning',
text: this.$t('leaveGroupConfirm', { name: group.name }),
});
if (canceled) return;
os.apiWithDialog('users/groups/leave', {
groupId: group.id,
}).then(() => {
this.$refs.joined.reload();
});
}
} }
}); ]);
const headerTabs = $computed(() => [{
key: 'owned',
icon: 'fas fa-crown',
title: i18n.ts.ownedGroups,
}, {
key: 'joined',
icon: 'fas fa-hand',
title: i18n.ts.joinedGroups,
}, {
key: 'invites',
icon: 'fas fa-envelope-open-text',
title: i18n.ts.invites,
}]);
definePageMetadata(
computed(() => ({
title: i18n.ts.groups,
icon: "fas fa-users",
})),
);
async function create() {
const { canceled, result: name } = await os.inputText({
title: i18n.ts.groupName,
});
if (canceled) return;
await os.api('users/groups/create', { name: name });
owned.value.reload();
os.success();
}
function acceptInvite(invitation) {
os.api('users/groups/invitations/accept', {
invitationId: invitation.id,
}).then(() => {
os.success();
invitations.value.reload();
joined.value.reload();
});
}
function rejectInvite(invitation) {
os.api('users/groups/invitations/reject', {
invitationId: invitation.id,
}).then(() => {
invitations.value.reload();
});
}
async function leave(group) {
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.value.reload();
});
}
let swiperRef = null;
function setSwiperRef(swiper) {
swiperRef = swiper;
syncSlide(tabs.indexOf(tab));
}
function onSlideChange() {
tab = tabs[swiperRef.activeIndex];
}
function syncSlide(index) {
swiperRef.slideTo(index);
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -98,12 +98,12 @@ self.addEventListener('notificationclick', <K extends keyof pushNotificationData
if (_DEV_) { if (_DEV_) {
console.log('notificationclick', ev.action, ev.notification.data); console.log('notificationclick', ev.action, ev.notification.data);
} }
const { action, notification } = ev; const { action, notification } = ev;
const data: pushNotificationDataMap[K] = notification.data; const data: pushNotificationDataMap[K] = notification.data;
const { userId: id } = data; const { userId: id } = data;
let client: WindowClient | null = null; let client: WindowClient | null = null;
switch (data.type) { switch (data.type) {
case 'notification': case 'notification':
switch (action) { switch (action) {
@ -167,16 +167,16 @@ self.addEventListener('notificationclick', <K extends keyof pushNotificationData
client = await swos.openChat(data.body, id); client = await swos.openChat(data.body, id);
break; break;
} }
if (client) { if (client) {
client.focus(); client.focus();
} }
if (data.type === 'notification') { if (data.type === 'notification') {
swNotificationRead.then(that => that.read(data)); swNotificationRead.then(that => that.read(data));
} }
notification.close(); notification.close();
})()); })());
}); });
@ -199,11 +199,11 @@ self.addEventListener('message', (ev: ServiceWorkerGlobalScopeEventMap['message'
)); ));
return; // TODO return; // TODO
} }
if (typeof ev.data === 'object') { if (typeof ev.data === 'object') {
// E.g. '[object Array]' → 'array' // E.g. '[object Array]' → 'array'
const otype = Object.prototype.toString.call(ev.data).slice(8, -1).toLowerCase(); const otype = Object.prototype.toString.call(ev.data).slice(8, -1).toLowerCase();
if (otype === 'object') { if (otype === 'object') {
if (ev.data.msg === 'initialize') { if (ev.data.msg === 'initialize') {
swLang.setLang(ev.data.lang); swLang.setLang(ev.data.lang);