Files
2024-10-10 21:57:37 +03:30

183 lines
6.1 KiB
Vue

<template>
<div>
<CustomSubHeader>
<CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
<CButton size="sm" color="primary" class="mr-auto" :to="{name: 'admin-gallery'}">برگشت به صفحه قبل</CButton>
<CButton v-if="$route.params.image === 'new'" size="sm" color="success" class="mr-1" @click="post">افزودن</CButton>
<CButton v-else size="sm" color="success" class="mr-1" @click="update">بروزرسانی</CButton>
</CustomSubHeader>
<CRow>
<CCol lg="6">
<CCard>
<CCardBody>
<div>
<p>دسته بندی</p>
<el-select v-model="image.category" filterable :disabled="image.noCategory" :class="validation.category? 'err' : null">
<el-option v-for="item in galleryCategories" :label="item.locale.fa.title" :value="item._id" :key="item._id"/>
</el-select>
<p class="text-danger" v-if="validation.category">{{ validation.category.msg }}</p>
</div>
<div class="mt-3">
<el-checkbox v-model="image.noCategory" label="بدون دسته بندی"/>
</div>
<el-divider></el-divider>
<CInput
:class="validation.fa_caption ? 'err' : null"
label="کپشن فارسی"
:description="validation.fa_caption ? validation.fa_caption.msg : null"
v-model="image.locale.fa.caption"
/>
<CInput
:class="validation.en_caption ? 'err' : null"
label="کپشن انگلیسی"
:description="validation.en_caption ? validation.en_caption.msg : null"
v-model="image.locale.en.caption"
/>
</CCardBody>
</CCard>
</CCol>
<CCol lg="6">
<CCard>
<CCardBody>
<p>تصویر</p>
<img :src="image.image" alt="" style="width: 100%;max-width: 600px;">
<input type="file" ref="image" @change="preview">
<p class="text-danger" v-if="validation.image">{{ validation.image.msg }}</p>
</CCardBody>
</CCard>
</CCol>
</CRow>
</div>
</template>
<script>
import axiosUploadProcess from "@/mixins/axiosUploadProcess"
export default {
data() {
return {
galleryCategories: null,
image: null,
validation: {}
}
},
computed: {
title() {
return this.$route.params.image === 'new' ? 'افزودن تصویر جدید' : 'ویرایش تصویر'
}
},
mixins: [axiosUploadProcess],
methods: {
preview() {
this.image.image = URL.createObjectURL(event.target.files[0])
},
post() {
this.validation = {}
const data = new FormData()
data.append('fa_caption', this.image.locale.fa.caption)
data.append('en_caption', this.image.locale.en.caption)
data.append('category', this.image.category)
data.append('noCategory', this.image.noCategory)
data.append('image', this.$refs.image.files[0])
this.$axios.post(`/api/admin/gallery`, data, this.axiosConfig)
.then(response => {
this.$message({
type: 'success',
message: 'تصویر با موفقیت افزوده شد'
})
this.$router.push({name: 'admin-gallery'})
})
.catch(err => {
if (err.response.status === 401) {
return this.$message({
type: 'error',
message: 'لطفا دوباره وارد سیستم شوید.'
})
}
if (err.response.status === 422) {
this.validation = err.response.data.validation
this.$message({
type: 'error',
message: 'پارامتر هارو بررسی کنید'
})
} else console.log(err.response.data)
})
},
update() {
this.validation = {}
const data = new FormData()
data.append('fa_caption', this.image.locale.fa.caption)
data.append('en_caption', this.image.locale.en.caption)
data.append('category', this.image.category)
data.append('noCategory', this.image.noCategory)
if (this.$refs.image.files[0]) data.append('image', this.$refs.image.files[0])
this.$axios.put(`/api/admin/gallery/${this.image._id}`, data, this.axiosConfig)
.then(response => {
this.$message({
type: 'success',
message: 'اطلاعات بروزرسانی شد.'
})
this.$refs.image.value = null
this.$nuxt.refresh()
})
.catch(err => {
if (err.response.status === 401) {
return this.$message({
type: 'error',
message: 'لطفا دوباره وارد سیستم شوید.'
})
}
if (err.response.status === 422) {
this.validation = err.response.data.validation
this.$message({
type: 'error',
message: 'پارامتر هارو بررسی کنید'
})
} else console.log(err.response.data)
})
}
},
head() {
return {
title: this.title
}
},
layout: 'admin',
async asyncData({$axios, params, error}) {
try {
const galleryCategories = await $axios.get(`/api/public/galleryCategories`)
if (params.image !== 'new') {
const image = await $axios.get(`/api/public/galleryImage/${params.image}`)
return {
galleryCategories: galleryCategories.data,
image: image.data
}
} else {
return {
galleryCategories: galleryCategories.data,
image: {
locale: {
fa: {
caption: ''
},
en: {
caption: ''
}
},
image: '',
category: '',
noCategory: false
}
}
}
} catch (e) {
error({status: 404, message: 'page not found'})
}
}
}
</script>