refactor(client): refactor admin/ads to use Composition API (#8649)

This commit is contained in:
Andreas Nedbal 2022-05-14 14:33:41 +02:00 committed by GitHub
parent 88307327e6
commit cafd29888d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@
<template #label>URL</template> <template #label>URL</template>
</MkInput> </MkInput>
<MkInput v-model="ad.imageUrl" class="_formBlock"> <MkInput v-model="ad.imageUrl" class="_formBlock">
<template #label>{{ $ts.imageUrl }}</template> <template #label>{{ i18n.ts.imageUrl }}</template>
</MkInput> </MkInput>
<FormRadios v-model="ad.place" class="_formBlock"> <FormRadios v-model="ad.place" class="_formBlock">
<template #label>Form</template> <template #label>Form</template>
@ -17,34 +17,34 @@
</FormRadios> </FormRadios>
<!-- <!--
<div style="margin: 32px 0;"> <div style="margin: 32px 0;">
{{ $ts.priority }} {{ i18n.ts.priority }}
<MkRadio v-model="ad.priority" value="high">{{ $ts.high }}</MkRadio> <MkRadio v-model="ad.priority" value="high">{{ i18n.ts.high }}</MkRadio>
<MkRadio v-model="ad.priority" value="middle">{{ $ts.middle }}</MkRadio> <MkRadio v-model="ad.priority" value="middle">{{ i18n.ts.middle }}</MkRadio>
<MkRadio v-model="ad.priority" value="low">{{ $ts.low }}</MkRadio> <MkRadio v-model="ad.priority" value="low">{{ i18n.ts.low }}</MkRadio>
</div> </div>
--> -->
<FormSplit> <FormSplit>
<MkInput v-model="ad.ratio" type="number"> <MkInput v-model="ad.ratio" type="number">
<template #label>{{ $ts.ratio }}</template> <template #label>{{ i18n.ts.ratio }}</template>
</MkInput> </MkInput>
<MkInput v-model="ad.expiresAt" type="date"> <MkInput v-model="ad.expiresAt" type="date">
<template #label>{{ $ts.expiration }}</template> <template #label>{{ i18n.ts.expiration }}</template>
</MkInput> </MkInput>
</FormSplit> </FormSplit>
<MkTextarea v-model="ad.memo" class="_formBlock"> <MkTextarea v-model="ad.memo" class="_formBlock">
<template #label>{{ $ts.memo }}</template> <template #label>{{ i18n.ts.memo }}</template>
</MkTextarea> </MkTextarea>
<div class="buttons _formBlock"> <div class="buttons _formBlock">
<MkButton class="button" inline primary style="margin-right: 12px;" @click="save(ad)"><i class="fas fa-save"></i> {{ $ts.save }}</MkButton> <MkButton class="button" inline primary style="margin-right: 12px;" @click="save(ad)"><i class="fas fa-save"></i> {{ i18n.ts.save }}</MkButton>
<MkButton class="button" inline danger @click="remove(ad)"><i class="fas fa-trash-alt"></i> {{ $ts.remove }}</MkButton> <MkButton class="button" inline danger @click="remove(ad)"><i class="fas fa-trash-alt"></i> {{ i18n.ts.remove }}</MkButton>
</div> </div>
</div> </div>
</div> </div>
</MkSpacer> </MkSpacer>
</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';
@ -52,81 +52,65 @@ import FormRadios from '@/components/form/radios.vue';
import FormSplit from '@/components/form/split.vue'; import FormSplit from '@/components/form/split.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 ads: any[] = $ref([]);
components: {
MkButton,
MkInput,
MkTextarea,
FormRadios,
FormSplit,
},
emits: ['info'], os.api('admin/ad/list').then(adsResponse => {
ads = adsResponse;
});
data() { function add() {
return { ads.unshift({
[symbols.PAGE_INFO]: { id: null,
title: this.$ts.ads, memo: '',
icon: 'fas fa-audio-description', place: 'square',
bg: 'var(--bg)', priority: 'middle',
actions: [{ ratio: 1,
asFullButton: true, url: '',
icon: 'fas fa-plus', imageUrl: null,
text: this.$ts.add, expiresAt: null,
handler: this.add, });
}], }
},
ads: [],
}
},
created() { function remove(ad) {
os.api('admin/ad/list').then(ads => { os.confirm({
this.ads = ads; type: 'warning',
text: i18n.t('removeAreYouSure', { x: ad.url }),
}).then(({ canceled }) => {
if (canceled) return;
ads = ads.filter(x => x !== ad);
os.apiWithDialog('admin/ad/delete', {
id: ad.id
}); });
}, });
}
methods: { function save(ad) {
add() { if (ad.id == null) {
this.ads.unshift({ os.apiWithDialog('admin/ad/create', {
id: null, ...ad,
memo: '', expiresAt: new Date(ad.expiresAt).getTime()
place: 'square', });
priority: 'middle', } else {
ratio: 1, os.apiWithDialog('admin/ad/update', {
url: '', ...ad,
imageUrl: null, expiresAt: new Date(ad.expiresAt).getTime()
expiresAt: null, });
}); }
}, }
remove(ad) { defineExpose({
os.confirm({ [symbols.PAGE_INFO]: {
type: 'warning', title: i18n.ts.ads,
text: this.$t('removeAreYouSure', { x: ad.url }), icon: 'fas fa-audio-description',
}).then(({ canceled }) => { bg: 'var(--bg)',
if (canceled) return; actions: [{
this.ads = this.ads.filter(x => x != ad); asFullButton: true,
os.apiWithDialog('admin/ad/delete', { icon: 'fas fa-plus',
id: ad.id text: i18n.ts.add,
}); handler: add,
}); }],
},
save(ad) {
if (ad.id == null) {
os.apiWithDialog('admin/ad/create', {
...ad,
expiresAt: new Date(ad.expiresAt).getTime()
});
} else {
os.apiWithDialog('admin/ad/update', {
...ad,
expiresAt: new Date(ad.expiresAt).getTime()
});
}
}
} }
}); });
</script> </script>