Files
orisoxin/pages/admin/blog-posts/new.vue
T
2024-10-10 21:57:37 +03:30

115 lines
3.3 KiB
Vue

<template>
<div>
<CustomSubHeader>
<CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
<CButton size="sm" color="primary" class="mr-auto" :to="{name: 'admin-blog-posts'}">برگشت به صفحه قبل</CButton>
<CButton size="sm" color="success" class="mr-1" @click="post">افزودن</CButton>
</CustomSubHeader>
<CRow>
<CCol lg="6">
<CCard>
<CCardBody>
<CForm>
<CRow>
<CCol sm="12">
<CInput
:class="validation.fa_title ? 'err' : null"
label="عنوان فارسی"
:description="validation.fa_title ? validation.fa_title.msg : null"
v-model="fa_title"
/>
<CInput
:class="validation.en_title ? 'err' : null"
label="عنوان انگلیسی"
:description="validation.en_title ? validation.en_title.msg : null"
v-model="en_title"
/>
</CCol>
</CRow>
</CForm>
</CCardBody>
</CCard>
</CCol>
<CCol lg="6">
<CCard>
<CCardBody>
<p>کاور</p>
<img :src="cover" alt="" style="width: 100%;max-width: 600px;">
<input type="file" ref="cover" @change="preview">
<p class="text-danger" v-if="validation.cover">{{ validation.cover.msg }}</p>
</CCardBody>
</CCard>
</CCol>
</CRow>
</div>
</template>
<script>
import axiosUploadProcess from "@/mixins/axiosUploadProcess"
export default {
data() {
return {
title: 'افزودن پست',
fa_title: '',
en_title: '',
cover: '',
blogCategories: null,
editorConfig: {
language: 'en',
extraPlugins: ['bidi', 'justify']
},
validation: {}
}
},
mixins: [axiosUploadProcess],
methods: {
preview() {
this.cover = URL.createObjectURL(event.target.files[0])
},
post() {
this.validation = {}
const data = new FormData()
data.append('fa_title', this.fa_title)
data.append('en_title', this.en_title)
data.append('cover', this.$refs.cover.files[0])
this.$axios.post(`/api/admin/blogPost`, data, this.axiosConfig)
.then(response => {
this.$message({
type: 'success',
message: 'پست با موفقیت ایجاد شد.'
})
this.$router.push({name: 'admin-blog-posts-post', params: {post: response.data._id}})
})
.catch(err => {
if (err.response.status === 401) {
return this.$message({
type: 'error',
message: 'لطفا دوباره وارد سیستم شوید.'
})
}
if (err.response.status === 422) {
this.validation = err.response.data.validation
return this.$message({
type: 'warning',
message: 'پارامتر ها رو بررسی و دوباره تلاش کنید'
})
} else console.log(err.response.data)
})
}
},
head() {
return {
title: this.title
}
},
layout: 'admin'
}
</script>