Files
asan-service/pages/admin/downloads/new.vue
T
2023-08-17 13:05:51 +03:30

187 lines
6.0 KiB
Vue

<template>
<div>
<CustomSubHeader>
<CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
<CButton size="sm" color="primary" class="mr-auto" :to="{ name: 'admin-downloads-page', params: { page: 1 } }"
>برگشت به صفحه قبل</CButton
>
<CButton size="sm" color="success" style="margin-right: 10px" :disabled="pending" @click="upload">ایجاد</CButton>
</CustomSubHeader>
<CRow>
<CCol xl="6">
<CCard>
<CCardHeader>
<strong class="alert-success d-block"
>ابتدا نام، توضیحات، دسته بندی و تصویر را افزوده سپس در مرحله بعد فایل ها رو اضافه کنید.</strong
>
</CCardHeader>
<CCardBody>
<CForm>
<CSelect
label="دسته بندی"
placeholder="دسته بندی را انتخاب کنید"
horizontal
:options="mappedCategories(dCategories)"
:value.sync="formData.category"
:description="validation.category ? validation.category.msg : null"
:class="validation.category ? 'err' : null"
>
</CSelect>
<CInput
v-model="formData.title"
label="عنوان"
horizontal
:description="validation.title ? validation.title.msg : null"
:class="validation.title ? 'err' : null"
/>
<CTextarea
v-model="formData.short_description"
label="توضیح کوتاه"
horizontal
rows="3"
:description="validation.short_description ? validation.short_description.msg : null"
:class="validation.short_description ? 'err' : null"
/>
<CRow form class="form-group" :class="validation.description ? 'err' : null">
<CCol tag="label" sm="3" class="col-form-label">توضیحات</CCol>
<CCol sm="9">
<client-only>
<ckeditor v-model="formData.description" :config="editorConfig"></ckeditor>
</client-only>
<small v-if="validation.description" class="form-text text-muted w-100">
{{ validation.description.msg }}
</small>
</CCol>
</CRow>
</CForm>
</CCardBody>
</CCard>
</CCol>
<CCol xl="6">
<CCard>
<CCardBody>
<h1>تصویر:</h1>
<el-divider />
<CRow>
<CCol col="12" class="mb-3">
<img :src="formData.image" alt="" style="width: 100%; max-width: 500px" />
</CCol>
<CCol col="1">
<span>کاور</span>
</CCol>
<CCol col="11">
<input ref="image" type="file" @change="imagePreview" />
</CCol>
<CCol col="11" class="mr-auto mt-3">
<small v-if="validation.image" class="form-text alert-danger w-100">
{{ validation.image.msg }}
</small>
<small v-else class="form-text text-muted w-100">
بهتر است تصویر با فرمت png و پسزمینه شفاف باشد.
</small>
</CCol>
</CRow>
</CCardBody>
</CCard>
</CCol>
</CRow>
</div>
</template>
<script>
import axiosUploadProcess from '@/mixins/axiosUploadProcess'
export default {
name: 'AddminAddNewDownload',
mixins: [axiosUploadProcess],
layout: 'admin',
async asyncData({ $axios, error }) {
try {
const dCategories = await $axios.get(`/api/public/dCategory`)
return {
dCategories: dCategories.data
}
} catch (e) {
error({ status: 404, message: 'There is a problem here' })
}
},
data() {
return {
title: 'افزودن فایل',
dCategories: null,
formData: {
title: '',
short_description: '',
description: '',
image: '',
category: ''
},
editorConfig: {
language: 'fa',
extraPlugins: ['bidi', 'justify']
},
validation: {},
pending: false
}
},
head() {
return {
title: this.title
}
},
methods: {
upload() {
this.pending = true
this.validation = {}
const data = new FormData()
data.append('title', this.formData.title)
data.append('short_description', this.formData.short_description)
data.append('description', this.formData.description)
data.append('category', this.formData.category)
data.append('image', this.$refs.image.files[0])
this.$axios
.post(`/api/admin/downloads`, data, this.axiosConfig)
.then(response => {
if (response.data) {
this.pending = false
this.$message({
message: 'آیتم با موفقیت ثبت شد،میتوانید فایل ها رو اضافه کنید.',
type: 'success'
})
this.$router.push({ name: 'admin-downloads-page-item', params: { page: 1, item: response.data._id } })
}
})
.catch(error => {
this.pending = false
if (error.response.status === 422) {
this.validation = error.response.data.validation
this.$message({
type: 'error',
message: 'اطلاعات رو بررسی و دوباره تلاش کنید'
})
} else {
this.$alert(error.response.data.message, 'خطا', {
confirmButtonText: 'باشه'
})
}
})
},
imagePreview(event) {
this.formData.image = URL.createObjectURL(event.target.files[0])
},
mappedCategories(arr) {
return arr.map(item => {
return {
label: item.name,
value: item._id
}
})
}
}
}
</script>