197 lines
8.3 KiB
Vue
197 lines
8.3 KiB
Vue
<template>
|
||
<div>
|
||
<ul class="flex space-x-2 rtl:space-x-reverse">
|
||
<li>
|
||
<a href="javascript:;" class="text-primary hover:underline">Entertainment</a>
|
||
</li>
|
||
<li class="before:mr-2 before:content-['/'] rtl:before:ml-2">
|
||
<NuxtLink to="/entertainment/manga/list" class="text-primary hover:underline">
|
||
Manga
|
||
</NuxtLink>
|
||
</li>
|
||
<li class="before:mr-2 before:content-['/'] rtl:before:ml-2">
|
||
<NuxtLink :to="`/entertainment/manga/${route.params.mangaid}/chapters/list`" class="text-primary hover:underline">Daftar Chapter</NuxtLink>
|
||
</li>
|
||
<li class="before:mr-2 before:content-['/'] rtl:before:ml-2">
|
||
<NuxtLink :to="`/entertainment/manga/${route.params.mangaid}/chapters/${route.params.chapterid}/pages/list`" class="text-primary hover:underline">Daftar Pages</NuxtLink>
|
||
</li>
|
||
<li class="before:mr-2 before:content-['/'] rtl:before:ml-2">
|
||
<span>Edit Pages</span>
|
||
</li>
|
||
</ul>
|
||
|
||
<div class="grid grid-cols-1 gap-6 pt-5">
|
||
<div class="panel">
|
||
<div class="mb-5 flex items-center justify-between">
|
||
<h5 class="text-lg font-semibold dark:text-white-light">Edit Pages</h5>
|
||
<NuxtLink :to="`/entertainment/manga/${route.params.mangaid}/chapters/${route.params.chapterid}/pages/list`" class="dark:text-white-light btn btn-dark !py-1">
|
||
<icon-arrow-backward class="me-1" />
|
||
Daftar
|
||
</NuxtLink>
|
||
</div>
|
||
<div class="mb-5">
|
||
<form v-if="form" class="space-y-5" @submit.prevent="submitForm()">
|
||
<div class="grid grid-cols-1 gap-4 md:grid-cols-2 gap-2">
|
||
<div :class="{ 'has-error': $validate.form.order.$error, 'has-success': isSubmitted && !$validate.form.order.$error }">
|
||
<label for="order">Order</label>
|
||
<input id="order" type="number" class="form-input" v-model="form.order" />
|
||
<template v-if="isSubmitted && $validate.form.order.$error">
|
||
<p class="text-danger mt-1">Order harus diisi</p>
|
||
</template>
|
||
</div>
|
||
</div>
|
||
<div class="grid grid-cols-2 gap-4">
|
||
<div class="custom-file-container" data-upload-id="media"
|
||
:class="{ 'has-error': $validate.form.media.$error, 'has-success': isSubmitted && !$validate.form.media.$error }">
|
||
<div class="label-container">
|
||
<label for="media">Media </label> <a href="javascript:;" class="custom-file-container__image-clear" title="Clear Image">×</a>
|
||
</div>
|
||
<label class="custom-file-container__custom-file" >
|
||
<input id="media" type="file" class="custom-file-container__custom-file__custom-file-input"
|
||
accept="image/png" @change="handleMediaChange" />
|
||
<input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
|
||
<span class="custom-file-container__custom-file__custom-file-control ltr:pr-20 rtl:pl-20"></span>
|
||
</label>
|
||
<template v-if="isSubmitted && $validate.form.media.$error">
|
||
<p class="text-danger mt-1">Gambar karakter harus diupload dengan file berjenis PNG atau JPEG dengan ukuran maksimal 1 MB</p>
|
||
</template>
|
||
<div class="custom-file-container__image-preview"></div>
|
||
</div>
|
||
</div>
|
||
<div class="flex items-center ltr:ml-auto mt-8">
|
||
<button type="submit" class="btn btn-success !py-1" :disabled="isLoading"><icon-save class="me-1" /> Simpan</button>
|
||
<button type="reset" class="btn btn-dark !py-1 ml-1" @click="resetForm"><icon-restore class="me-1" />Reset</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script lang="ts" setup>
|
||
import { useVuelidate } from '@vuelidate/core';
|
||
import { required, helpers } from '@vuelidate/validators';
|
||
import Multiselect from '@suadelabs/vue3-multiselect';
|
||
import '@suadelabs/vue3-multiselect/dist/vue3-multiselect.css';
|
||
import '@/assets/css/file-upload-preview.css';
|
||
|
||
useHead({ title: 'Edit Pages' });
|
||
|
||
const isSubmitted = ref(false);
|
||
|
||
const imageType = helpers.withMessage('Format gambar harus PNG atau JPG', (value) => {
|
||
if (!value) return true;
|
||
if (typeof value === 'string') return true;
|
||
return ['image/png', 'image/jpeg', 'image/jpg'].includes(value.type);
|
||
});
|
||
const maxFileSize = helpers.withMessage('Ukuran gambar tidak boleh lebih dari 1MB', (value) => {
|
||
if (!value) return true;
|
||
if (typeof value === 'string') return true;
|
||
return value.size <= 1048576; // 1MB dalam byte
|
||
});
|
||
|
||
const rules = {
|
||
form: {
|
||
order: { required },
|
||
media: { imageType, maxFileSize },
|
||
}
|
||
};
|
||
const router = useRouter();
|
||
const route = useRoute();
|
||
const config = useRuntimeConfig();
|
||
const { $api } = useNuxtApp();
|
||
|
||
const isLoading = ref(false);
|
||
|
||
const featuredImageUploader = ref(null);
|
||
|
||
const { data: form, refresh } = await useAsyncData('page-get',
|
||
() => $api(`/entertainment/manga/${route.params.mangaid}/chapters/${route.params.chapterid}/pages/${route.params.id}`, {}),
|
||
);
|
||
|
||
const $validate = useVuelidate(rules, { form });
|
||
|
||
|
||
onMounted(async () => {
|
||
if (!form.value) return;
|
||
|
||
const fileupload = await import('file-upload-with-preview');
|
||
let FileUploadWithPreview = fileupload.default;
|
||
|
||
mediaUploader.value =new FileUploadWithPreview('media', {
|
||
images: {
|
||
baseImage: form.value.media || '/assets/images/file-preview.svg',
|
||
backgroundImage: '',
|
||
},
|
||
});
|
||
});
|
||
|
||
const submitForm = async () => {
|
||
isSubmitted.value = true;
|
||
$validate.value.form.$touch();
|
||
if ($validate.value.form.$invalid) {
|
||
return false;
|
||
}
|
||
|
||
const formData = new FormData();
|
||
|
||
formData.append('order', form.value.order);
|
||
|
||
if (form.value.media && typeof form.value.media !== 'string') {
|
||
formData.append('media', form.value.media);
|
||
}
|
||
|
||
isLoading.value = true;
|
||
await $api(`/entertainment/manga/${route.params.mangaid}/chapters/${route.params.chapterid}/pages/${route.params.id}`, {
|
||
method: 'PUT',
|
||
body: formData
|
||
}).then(() => {
|
||
router.push({ path: "/entertainment/manga/" + route.params.mangaid + "/chapters/" + route.params.chapterid + "/pages/list"});
|
||
})
|
||
.catch((error) => {
|
||
console.log(error);
|
||
})
|
||
.finally(() => {
|
||
isLoading.value = false;
|
||
});
|
||
}
|
||
|
||
function handleImageChange(event: { target: { files: any[]; }; }) {
|
||
const file = event.target.files[0]
|
||
|
||
if (!file) return
|
||
|
||
const allowed = ['image/png', 'image/jpeg', 'image/jpg']
|
||
if (!allowed.includes(file.type)) {
|
||
alert('Hanya PNG atau JPG yang diizinkan')
|
||
return
|
||
}
|
||
|
||
form.value.media = file
|
||
}
|
||
|
||
function handleMediaChange(event: { target: { files: any[]; }; }) {
|
||
const file = event.target.files[0]
|
||
|
||
if (!file) return
|
||
|
||
const allowed = ['image/png', 'image/jpeg', 'image/jpg']
|
||
if (!allowed.includes(file.type)) {
|
||
alert('Hanya PNG atau JPG yang diizinkan')
|
||
return
|
||
}
|
||
|
||
form.value.media = file
|
||
}
|
||
|
||
const resetForm = () => {
|
||
refresh()
|
||
|
||
isSubmitted.value = false;
|
||
$validate.value.form.$reset();
|
||
|
||
mediaUploader?.value?.clearPreviewPanel();
|
||
|
||
};
|
||
</script> |