refactor(client): refactor admin/announcements to use Composition API (#8650)

This commit is contained in:
Andy 2022-05-14 14:34:07 +02:00 committed by GitHub
parent cafd29888d
commit 9f07bd8f46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,112 +3,98 @@
<section v-for="announcement in announcements" class="_card _gap announcements"> <section v-for="announcement in announcements" class="_card _gap announcements">
<div class="_content announcement"> <div class="_content announcement">
<MkInput v-model="announcement.title"> <MkInput v-model="announcement.title">
<template #label>{{ $ts.title }}</template> <template #label>{{ i18n.ts.title }}</template>
</MkInput> </MkInput>
<MkTextarea v-model="announcement.text"> <MkTextarea v-model="announcement.text">
<template #label>{{ $ts.text }}</template> <template #label>{{ i18n.ts.text }}</template>
</MkTextarea> </MkTextarea>
<MkInput v-model="announcement.imageUrl"> <MkInput v-model="announcement.imageUrl">
<template #label>{{ $ts.imageUrl }}</template> <template #label>{{ i18n.ts.imageUrl }}</template>
</MkInput> </MkInput>
<p v-if="announcement.reads">{{ $t('nUsersRead', { n: announcement.reads }) }}</p> <p v-if="announcement.reads">{{ i18n.t('nUsersRead', { n: announcement.reads }) }}</p>
<div class="buttons"> <div class="buttons">
<MkButton class="button" inline primary @click="save(announcement)"><i class="fas fa-save"></i> {{ $ts.save }}</MkButton> <MkButton class="button" inline primary @click="save(announcement)"><i class="fas fa-save"></i> {{ i18n.ts.save }}</MkButton>
<MkButton class="button" inline @click="remove(announcement)"><i class="fas fa-trash-alt"></i> {{ $ts.remove }}</MkButton> <MkButton class="button" inline @click="remove(announcement)"><i class="fas fa-trash-alt"></i> {{ i18n.ts.remove }}</MkButton>
</div> </div>
</div> </div>
</section> </section>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from 'vue'; import { } from 'vue';
import MkButton from '@/components/ui/button.vue'; import MkButton from '@/components/ui/button.vue';
import MkInput from '@/components/form/input.vue'; import MkInput from '@/components/form/input.vue';
import MkTextarea from '@/components/form/textarea.vue'; import MkTextarea from '@/components/form/textarea.vue';
import * as os from '@/os'; import * as os from '@/os';
import * as symbols from '@/symbols'; import * as symbols from '@/symbols';
import { i18n } from '@/i18n';
export default defineComponent({ let announcements: any[] = $ref([]);
components: {
MkButton,
MkInput,
MkTextarea,
},
emits: ['info'], os.api('admin/announcements/list').then(announcementResponse => {
announcements = announcementResponse;
});
data() { function add() {
return { announcements.unshift({
[symbols.PAGE_INFO]: { id: null,
title: this.$ts.announcements, title: '',
icon: 'fas fa-broadcast-tower', text: '',
bg: 'var(--bg)', imageUrl: null
actions: [{ });
asFullButton: true, }
icon: 'fas fa-plus',
text: this.$ts.add,
handler: this.add,
}],
},
announcements: [],
}
},
created() { function remove(announcement) {
os.api('admin/announcements/list').then(announcements => { os.confirm({
this.announcements = announcements; type: 'warning',
text: i18n.t('removeAreYouSure', { x: announcement.title }),
}).then(({ canceled }) => {
if (canceled) return;
announcements = announcements.filter(x => x !== announcement);
os.api('admin/announcements/delete', announcement);
});
}
function save(announcement) {
if (announcement.id == null) {
os.api('admin/announcements/create', announcement).then(() => {
os.alert({
type: 'success',
text: i18n.ts.saved
});
}).catch(err => {
os.alert({
type: 'error',
text: err
});
}); });
}, } else {
os.api('admin/announcements/update', announcement).then(() => {
methods: { os.alert({
add() { type: 'success',
this.announcements.unshift({ text: i18n.ts.saved
id: null,
title: '',
text: '',
imageUrl: null
}); });
}, }).catch(err => {
os.alert({
remove(announcement) { type: 'error',
os.confirm({ text: err
type: 'warning',
text: this.$t('removeAreYouSure', { x: announcement.title }),
}).then(({ canceled }) => {
if (canceled) return;
this.announcements = this.announcements.filter(x => x != announcement);
os.api('admin/announcements/delete', announcement);
}); });
}, });
}
}
save(announcement) { defineExpose({
if (announcement.id == null) { [symbols.PAGE_INFO]: {
os.api('admin/announcements/create', announcement).then(() => { title: i18n.ts.announcements,
os.alert({ icon: 'fas fa-broadcast-tower',
type: 'success', bg: 'var(--bg)',
text: this.$ts.saved actions: [{
}); asFullButton: true,
}).catch(e => { icon: 'fas fa-plus',
os.alert({ text: i18n.ts.add,
type: 'error', handler: add,
text: e }],
});
});
} else {
os.api('admin/announcements/update', announcement).then(() => {
os.alert({
type: 'success',
text: this.$ts.saved
});
}).catch(e => {
os.alert({
type: 'error',
text: e
});
});
}
}
} }
}); });
</script> </script>