1
1
mirror of https://github.com/kokonect-link/cherrypick synced 2024-12-05 02:08:56 +09:00

refactor(client): Migrate components to composition api

This commit is contained in:
ltlapy 2022-07-14 19:40:41 +09:00
parent eb2e977bb1
commit 2e485f9ddd
2 changed files with 234 additions and 245 deletions

View File

@ -1,151 +1,145 @@
<template>
<div class="mk-group-page">
<transition :name="$store.state.animation ? 'zoom' : ''" mode="out-in">
<div v-if="group" class="_section">
<div class="_content" style="display: flex; gap: var(--margin); flex-wrap: wrap;">
<MkButton inline @click="invite()">{{ $ts.invite }}</MkButton>
<MkButton inline @click="renameGroup()">{{ $ts.rename }}</MkButton>
<MkButton inline @click="transfer()">{{ $ts.transfer }}</MkButton>
<MkButton inline @click="deleteGroup()">{{ $ts.delete }}</MkButton>
<template><MkStickyContainer>
<template #header><MkPageHeader :actions="headerActions" :tabs="headerTabs"/></template>
<MkSpacer :content-max="700">
<div class="mk-group-page">
<transition :name="$store.state.animation ? 'zoom' : ''" mode="out-in">
<div v-if="group" class="_section">
<div class="_content" style="">
<MkButton inline @click="invite()">{{ $ts.invite }}</MkButton>
<MkButton inline @click="renameGroup()">{{ $ts.rename }}</MkButton>
<MkButton inline @click="transfer()">{{ $ts.transfer }}</MkButton>
<MkButton inline @click="deleteGroup()">{{ $ts.delete }}</MkButton>
</div>
</div>
</div>
</transition>
</transition>
<transition :name="$store.state.animation ? 'zoom' : ''" mode="out-in">
<div v-if="group" class="_section members _gap">
<div class="_title">{{ $ts.members }}</div>
<div class="_content">
<div class="users">
<div v-for="user in users" :key="user.id" class="user _panel">
<MkAvatar :user="user" class="avatar" :show-indicator="true"/>
<div class="body">
<MkUserName :user="user" class="name"/>
<MkAcct :user="user" class="acct"/>
</div>
<div class="action">
<button class="_button" @click="removeUser(user)"><i class="fas fa-times"></i></button>
<transition :name="$store.state.animation ? 'zoom' : ''" mode="out-in">
<div v-if="group" class="_section members _gap">
<div class="_title">{{ $ts.members }}</div>
<div class="_content">
<div class="users">
<div v-for="user in users" :key="user.id" class="user _panel">
<MkAvatar :user="user" class="avatar" :show-indicator="true"/>
<div class="body">
<MkUserName :user="user" class="name"/>
<MkAcct :user="user" class="acct"/>
</div>
<div class="action">
<button class="_button" @click="removeUser(user)"><i class="fas fa-times"></i></button>
</div>
</div>
</div>
</div>
</div>
</div>
</transition>
</div>
</transition>
</div>
</MkSpacer></MkStickyContainer>
</template>
<script lang="ts">
import { computed, defineComponent } from 'vue';
<script lang="ts" setup>
import { computed, watch } from 'vue';
import MkButton from '@/components/ui/button.vue';
import * as os from '@/os';
import * as symbols from '@/symbols';
import { mainRouter } from '@/router';
import { definePageMetadata } from '@/scripts/page-metadata';
import { i18n } from '@/i18n';
export default defineComponent({
components: {
MkButton
},
const props = defineProps<{
groupId: string;
}>();
props: {
groupId: {
type: String,
required: true,
},
},
let group = $ref(null);
let users = $ref([]);
data() {
return {
[symbols.PAGE_INFO]: computed(() => this.group ? {
title: this.group.name,
icon: 'fas fa-users',
} : null),
group: null,
users: [],
};
},
function fetchGroup() {
os.api('users/groups/show', {
groupId: props.groupId,
}).then(_group => {
group = _group;
os.api('users/show', {
userIds: group.userIds,
}).then(_users => {
users = _users;
});
});
}
watch: {
groupId: 'fetch',
},
function invite() {
os.selectUser().then(user => {
os.apiWithDialog('users/groups/invite', {
groupId: group.id,
userId: user.id,
});
});
}
created() {
this.fetch();
},
function removeUser(user) {
os.api('users/groups/pull', {
groupId: group.id,
userId: user.id,
}).then(() => {
users = users.filter(x => x.id !== user.id);
});
}
methods: {
fetch() {
os.api('users/groups/show', {
groupId: this.groupId
}).then(group => {
this.group = group;
os.api('users/show', {
userIds: this.group.userIds
}).then(users => {
this.users = users;
});
});
},
async function renameGroup() {
const { canceled, result: name } = await os.inputText({
title: i18n.ts.groupName,
default: group.name,
});
if (canceled) return;
invite() {
os.selectUser().then(user => {
os.apiWithDialog('users/groups/invite', {
groupId: this.group.id,
userId: user.id
});
});
},
await os.api('users/groups/update', {
groupId: group.id,
name: name,
});
removeUser(user) {
os.api('users/groups/pull', {
groupId: this.group.id,
userId: user.id
}).then(() => {
this.users = this.users.filter(x => x.id !== user.id);
});
},
group.name = name;
}
async renameGroup() {
const { canceled, result: name } = await os.inputText({
title: this.$ts.groupName,
default: this.group.name
});
if (canceled) return;
function transfer() {
os.selectUser().then(user => {
os.apiWithDialog('users/groups/transfer', {
groupId: group.id,
userId: user.id,
});
});
}
await os.api('users/groups/update', {
groupId: this.group.id,
name: name
});
async function deleteGroup() {
const { canceled } = await os.confirm({
type: 'warning',
text: i18n.t('removeAreYouSure', { x: group.name }),
});
if (canceled) return;
this.group.name = name;
},
await os.apiWithDialog('users/groups/delete', {
groupId: group.id,
});
mainRouter.push('/my/groups');
}
transfer() {
os.selectUser().then(user => {
os.apiWithDialog('users/groups/transfer', {
groupId: this.group.id,
userId: user.id
});
});
},
watch(() => props.listId, fetchGroup, { immediate: true });
async deleteGroup() {
const { canceled } = await os.confirm({
type: 'warning',
text: this.$t('removeAreYouSure', { x: this.group.name }),
});
if (canceled) return;
const headerActions = $computed(() => []);
const headerTabs = $computed(() => []);
definePageMetadata(computed(() => group ? {
title: group.name,
icon: 'fas fa-users',
} : null));
await os.apiWithDialog('users/groups/delete', {
groupId: this.group.id
});
this.$router.push('/my/groups');
}
}
});
</script>
<style lang="scss" scoped>
.mk-group-page {
> .members {
> ._content {
display: flex;
gap: var(--margin);
flex-wrap: wrap;
> .users {
> .user {
display: flex;

View File

@ -1,146 +1,141 @@
<template>
<MkSpacer :content-max="700">
<div v-if="tab === 'owned'" class="_content">
<MkButton primary style="margin: 0 auto var(--margin) auto;" @click="create"><i class="fas fa-plus"></i> {{ $ts.createGroup }}</MkButton>
<MkStickyContainer>
<template #header><MkPageHeader v-model:tab="tab" :actions="headerActions" :tabs="headerTabs"/></template>
<MkSpacer :content-max="700">
<div v-if="tab === 'owned'" class="_content">
<MkButton primary style="margin: 0 auto var(--margin) auto;" @click="create"><i class="fas fa-plus"></i> {{ $ts.createGroup }}</MkButton>
<MkPagination v-slot="{items}" ref="owned" :pagination="ownedPagination">
<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="_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>
<MkPagination v-slot="{items}" ref="pagingComponent" :pagination="ownedPagination">
<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="_content"><MkAvatars :user-ids="group.userIds"/></div>
</div>
</div>
</MkPagination>
</div>
</MkPagination>
</div>
<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> {{ $ts.accept }}</MkButton>
<MkButton primary inline @click="rejectInvite(invitation)"><i class="fas fa-ban"></i> {{ $ts.reject }}</MkButton>
<div v-else-if="tab === 'joined'" class="_content">
<MkPagination v-slot="{items}" ref="pagingComponent" :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>
</div>
</MkSpacer>
</MkPagination>
</div>
<div v-else-if="tab === 'invites'" class="_content">
<MkPagination v-slot="{items}" ref="pagingComponent" :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> {{ $ts.accept }}</MkButton>
<MkButton primary inline @click="rejectInvite(invitation)"><i class="fas fa-ban"></i> {{ $ts.reject }}</MkButton>
</div>
</div>
</MkPagination>
</div>
</MkSpacer>
</MkStickyContainer>
</template>
<script lang="ts">
import { defineComponent, computed } from 'vue';
<script lang="ts" setup>
import { computed } from 'vue';
import MkPagination from '@/components/ui/pagination.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 MkTab from '@/components/tab.vue';
// import MkTab from '@/components/tab.vue';
import * as os from '@/os';
import * as symbols from '@/symbols';
import { i18n } from '@/i18n';
import { definePageMetadata } from '@/scripts/page-metadata';
export default defineComponent({
components: {
MkPagination,
MkButton,
MkContainer,
MkTab,
MkAvatars,
},
const pagingComponent = $ref<InstanceType<typeof MkPagination>>();
data() {
return {
[symbols.PAGE_INFO]: computed(() => ({
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,
},
};
},
const ownedPagination = {
endpoint: 'users/groups/owned' as const,
limit: 10,
// sort: '+createdAt/updatedAt',
};
const joinedPagination = {
endpoint: 'users/groups/joined' as const,
limit: 10,
};
const invitationPagination = {
endpoint: 'i/user-group-invites' as const,
limit: 10,
};
methods: {
async create() {
const { canceled, result: name } = await os.inputText({
title: this.$ts.groupName,
});
if (canceled) return;
await os.api('users/groups/create', { name: name });
this.$refs.owned.reload();
os.success();
},
acceptInvite(invitation) {
os.api('users/groups/invitations/accept', {
invitationId: invitation.id
}).then(() => {
os.success();
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();
});
}
}
let tab = $ref('owned');
async function create() {
const { canceled, result: name } = await os.inputText({
title: i18n.ts.groupName,
});
if (canceled) return;
await os.apiWithDialog('users/groups/create', { name: name });
pagingComponent.reload();
}
async function acceptInvite(invitation) {
os.apiWithDialog('users/groups/invitations/accept', {
invitationId: invitation.id,
}).then(() => {
os.success();
pagingComponent.reload();
});
}
function rejectInvite(invitation) {
os.apiWithDialog('users/groups/invitations/reject', {
invitationId: invitation.id,
}).then(() => {
pagingComponent.reload();
// this.$refs.invitations.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(() => {
pagingComponent.reload();
});
}
const headerActions = $computed(() => []);
const headerTabs = $computed(() => [{
key: 'owned',
title: i18n.ts.ownedGroups,
icon: 'fas fa-user-tie',
}, {
key: 'joined',
title: i18n.ts.joinedGroups,
icon: 'fas fa-id-badge',
}, {
key: 'invites',
title: i18n.ts.invites,
icon: 'fas fa-envelope-open-text',
}]);
definePageMetadata({
title: i18n.ts.groups,
icon: 'fas fa-users',
actions: [{
icon: 'fas fa-plus',
text: i18n.ts.createGroup,
handler: create,
}],
});
</script>
<style lang="scss" scoped>