Files
2024-10-21 10:22:26 +03:30

335 lines
10 KiB
Vue

<template>
<div>
<CustomSubHeader>
<CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
<CButton
size="sm"
color="primary"
class="mr-auto"
:to="{ name: 'admin-links' }"
>برگشت به صفحه قبل</CButton
>
<CButton
v-if="$route.params.details === '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 style="display : flex ; flex-direction: column">
<CRow>
<CCol lg="6">
<CCard>
<CCardBody>
<CCol sm="12">
<h6>
قسمت انتخابی
<i
v-c-tooltip="
'شما باید تعیین کنید که این خبر برای کدام قسمت پرتال می باشد'
"
class="fas fa-question-circle"
></i>
</h6>
<el-select
:disabled="$auth.user.portals.length === 1 ? true: false"
v-model="links.modelType"
placeholder="انتخاب کنید"
style="width: 100%;"
>
<el-option
v-for="item in portals"
:key="item.value"
:label="item.name"
:value="item.value"
>
</el-option>
</el-select>
<p
style="margin-top : 5px !important ; color : red"
class="form-err"
v-if="validation.modelType"
>
{{ validation.modelType.msg }}
</p>
</CCol>
<el-divider></el-divider>
<CCol sm="12">
<CInput
:class="validation.title ? 'err' : null"
label="عنوان"
:description="
validation.title ? validation.title.msg : null
"
v-model="links.title"
/>
</CCol>
<el-divider></el-divider>
<CCol sm="12">
<CInput
:class="validation.url ? 'err' : null"
label="لینک"
:description="
validation.url ? validation.url.msg : null
"
v-model="links.url"
/>
</CCol>
<el-divider></el-divider>
<CCol sm="12">
<el-checkbox
v-model="links.showInHome"
label="نمایش در صفحه اصلی"
/>
</CCol>
<el-divider></el-divider>
<CCol sm="12">
<h6 class="mt-2">ترتیب نمایش</h6>
<!-- <el-input-number style="width:100%" class="mt-2" size="mini" v-model="links.index" controls-position="right" :min="0"></el-input-number>-->
<el-select style="width:100%" class="mt-2" v-model="links.index" placeholder="">
<el-option
v-for="item in allLinks.length + 10"
v-if="!allLinks.find((item2) => item2.index === item)"
:key="item"
:label="item"
:value="item">
</el-option>
</el-select>
</CCol>
</CCardBody>
</CCard>
</CCol>
<CCol lg="6">
<CCard>
<CCardBody>
<CCol sm="12">
<h5>آیکون</h5>
<el-divider />
<CCol>
<img
:src="links.icon"
style="width: 150px;"
alt=""
/>
</CCol>
<input type="file" ref="icon" @change="imagePreview" />
<p
style="margin-top : 5px !important ;"
class="form-err"
v-if="validation.icon"
>
{{ validation.icon.msg }}
</p>
<p class="mt-3 text-muted">
عکس 50*50 باشد. در غیر این صورت اتوماتیک بریده می شود.
</p>
</CCol>
</CCardBody>
</CCard>
</CCol>
</CRow>
</CRow>
</div>
</template>
<script>
import axiosUploadProcess from "@/mixins/axiosUploadProcess";
export default {
data() {
return {
links: null,
typePage: true,
validation: {},
editorConfig: {
language: "en",
extraPlugins: ["bidi", "justify"],
},
portals: null,
allLinks : null
};
},
mixins: [axiosUploadProcess],
computed: {
title() {
return this.$route.params.details === "new"
? "افزودن پیوند جدید"
: "مشاهده جزئیات پیوند";
},
},
methods: {
imagePreview(e) {
this.links.cover = URL.createObjectURL(e.target.files[0]);
},
post() {
this.validation = {};
const data = new FormData();
data.append("title", this.links.title.trim());
data.append("url", this.links.url);
data.append("index", this.links.index);
data.append("showInHome", this.links.showInHome);
data.append("modelType", this.links.modelType);
if (this.$refs.icon.files[0])
data.append("icon", this.$refs.icon.files[0]);
this.$axios
.post(`/api/admin/links`, data, this.axiosConfig)
.then((response) => {
this.$message({
type: "success",
message: "پیوند با موفقیت ایجاد شد.",
});
this.$router.push({
name: "admin-links",
});
})
.catch((err) => {
if (err.response.status === 401) {
return this.$message({
type: "error",
message: "لطفا دوباره وارد سیستم شوید.",
});
}
if (err.response.status === 403) {
return this.$message({
type: "error",
message: err.response.data.message,
});
}
if (err.response.status === 500) {
return this.$message({
type: "error",
message: "مشکلی در ارسال درخواست پیش آمده",
});
}
if (err.response.status === 422)
this.validation = err.response.data.validation;
else console.log(err.response.data);
});
},
update() {
this.validation = {};
const data = new FormData();
data.append("title", this.links.title.trim());
data.append("url", this.links.url);
data.append("index", this.links.index);
data.append("showInHome", this.links.showInHome);
data.append("modelType", this.links.modelType);
if (this.$refs.icon.files[0])
data.append("icon", this.$refs.icon.files[0]);
this.$axios
.put(`/api/admin/links/${this.links._id}`, data, this.axiosConfig)
.then((response) => {
this.$message({
type: "success",
message: "اطلاعات بروزرسانی شد.",
});
this.$nuxt.refresh();
})
.catch((err) => {
if (err.response.status === 401) {
return this.$message({
type: "error",
message: "لطفا دوباره وارد سیستم شوید.",
});
}
if (err.response.status === 403) {
return this.$message({
type: "error",
message: err.response.data.message,
});
}
if (err.response.status === 500) {
return this.$message({
type: "error",
message: "مشکلی در ارسال درخواست پیش آمده",
});
}
if (err.response.status === 422)
this.validation = err.response.data.validation;
else console.log(err.response.data);
});
},
},
head() {
return {
title: this.title,
};
},
layout: "admin",
async asyncData({ $axios, $auth, params, query, error }) {
try {
// const portals = await $axios.get("/api/admin/portals" , { progress: false });
// const allLinks = await $axios.get("/api/admin/links" , { progress: false });
const requests = [
$axios.get("/api/admin/links" , { progress: false }),
$axios.get(`/api/admin/portals` ,{ progress: false })
];
const fetch = await Promise.all(requests);
let [allLinks, portals] = fetch;
const setModelType = $auth.user.portals.length !== 1
? $auth.user.portals
: $auth.user.portals[0];
if (params.details !== "new") {
const links = await $axios.get(`/api/admin/links/${params.details}`);
return {
portals: portals.data,
allLinks : allLinks.data,
links: links.data
};
} else {
return {
portals: portals.data,
allLinks : allLinks.data,
links: {
title: '',
url: '',
index: null,
showInHome: true,
siteMap: true,
modelType: setModelType,
},
};
}
} 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>