Files
hamid.zarghami1@gmail.com e644edbd65 transfer project in github
2024-05-12 15:29:27 +03:30

187 lines
5.6 KiB
Vue

<template>
<div>
<CustomSubHeader>
<CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
<CButton size="sm" color="primary" class="mr-auto" :to="{name: 'admin-slider'}">برگشت به صفحه قبل</CButton>
<CButton size="sm" v-if="newPage" color="success" class="mr-1" @click="add">افزودن</CButton>
<CButton size="sm" v-else color="success" class="mr-1" @click="update">بروزرسانی</CButton>
</CustomSubHeader>
<CCard>
<CCardHeader>
<slot name="header">
<!-- <i class="fal fa-bars"></i>-->
<!-- <span>{{ list_title }}</span>-->
</slot>
</CCardHeader>
<CCardBody>
<CRow>
<CCol md="6">
<CForm>
<CRow>
<CCol sm="12">
<CTextarea
:class="validation.caption ? 'err' : null"
label="توضیحات"
v-model="slide.caption"
rows="10"
/>
<p class="form-err" v-if="validation.caption">{{ validation.caption.msg }}</p>
</CCol>
</CRow>
</CForm>
</CCol>
<CCol md="6">
<h4>تصویر:</h4>
<el-divider/>
<img :src="slide.image" alt="" style="width: 100%">
<input type="file" ref="image" @change="preview">
<p class="form-err" style="color: red" v-if="validation.image">{{ validation.image.msg }}</p>
</CCol>
</CRow>
</CCardBody>
</CCard>
</div>
</template>
<script>
import axiosUploadProcess from "@/mixins/axiosUploadProcess"
export default {
data() {
return {
slide: null,
validation: {}
}
},
mixins: [axiosUploadProcess],
computed: {
title() {
return this.$route.params.slide === 'new' ? 'افزودن اسلاید' : 'ویرایش اسلاید'
},
newPage() {
return this.$route.params.slide === 'new'
}
},
methods: {
add() {
this.validation = {}
const data = new FormData()
data.append('caption', this.slide.caption)
data.append('image', this.$refs.image.files[0])
this.$axios.post(`/api/admin/slider`, data, this.axiosConfig)
.then(response => {
this.$message({
message: 'اسلاید با موفقیت ثبت شد.',
type: 'success'
})
this.$router.push({name: 'admin-slider'})
})
.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 {
this.$alert(err.response.data.error.message, 'خطا', {
confirmButtonText: 'باشه'
})
}
})
},
update() {
this.validation = {}
const data = new FormData()
data.append('caption', this.slide.caption)
if (this.$refs.image.files[0]) data.append('image', this.$refs.image.files[0])
this.$axios.put(`/api/admin/slider/${this.slide._id}`, data, this.axiosConfig)
.then(response => {
this.$message({
message: 'تغییرات با موفقیت انجام شد.',
type: 'success'
})
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 {
this.$alert(err.response.data.error.message, 'خطا', {
confirmButtonText: 'باشه'
})
}
})
},
preview(event) {
this.slide.image = URL.createObjectURL(event.target.files[0])
}
},
head() {
return {
title: this.title
}
},
layout: 'admin',
async asyncData({$axios, params, error}) {
try {
if (params.slide !== 'new') {
const slide = await $axios.get(`/api/public/slider/${params.slide}`)
return {
slide: slide.data
}
} else {
return {
slide: {
caption: '',
image: ''
}
}
}
} catch (e) {
if (e?.response?.status === 401) {
error({
status: 401,
message: "لطفا دوباره وارد سیستم شوید.",
});
}
if (e?.response?.status === 403) {
error({
status: 403,
message: e?.response?.data?.message,
});
}
if (e?.response?.status === 500) {
error({
status: 500,
message: "مشکلی در گرفتن اطلاعات پیش آمده",
});
} else
error({
status: 404,
message: "اطلاعات مورد نظر پیدا نشد",
});
}
}
}
</script>