Files
ostandari/pages/admin/links/index.vue
T
2024-10-21 10:22:26 +03:30

204 lines
6.2 KiB
Vue

<template>
<div>
<CustomSubHeader>
<CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
<CButton size="sm" color="success" :to="{ name: 'admin-links-details', params: { details: 'new' } }"
class="mr-auto">افزودن</CButton>
</CustomSubHeader>
<CCard>
<CCardHeader>
<slot name="header">
<i class="fal fa-filter"></i>
<span style="color:#000">فیلتر</span>
</slot>
</CCardHeader>
<CCardBody>
<CRow>
<CCol class="col">
<CRow>
<el-input v-model="search" size="medium" placeholder="جستجو" />
</CRow>
</CCol>
</CRow>
</CCardBody>
</CCard>
<CCard>
<CCardHeader>
<slot name="header">
<i class="fal fa-bars"></i>
<span>{{ list_title }}</span>
</slot>
<slot> </slot>
</CCardHeader>
<CCardBody>
<el-table :data="filteredNewsFile" :default-sort="{ prop: 'date', order: 'descending' }" style="width: 100%">
<el-table-column type="index" label="#"> </el-table-column>
<el-table-column
label="آیکون پیوند"
width="">
<template slot-scope="scope">
<img :src="scope.row.icon || '/img/avatar.png'" :alt="scope.row.icon">
</template>
</el-table-column>
<el-table-column label="عنوان">
<template slot-scope="scope">
<a :href="scope.row.url" target="_blank">
<span style="height: 0px; display: ruby-base;">
{{ scope.row.title }}
</span>
</a>
</template>
</el-table-column>
<el-table-column label="نمایش در صفحه اصلی" width="">
<template slot-scope="scope">
<el-tag :type="scope.row.showInHome ? 'primary' : 'danger'">{{
scope.row.showInHome ? "بله" : "خیر"
}}</el-tag>
</template>
</el-table-column>
<el-table-column label="تاریخ ایجاد" width="">
<template slot-scope="scope">
<span>{{ jDate(scope.row.created_at) }}</span>
</template>
</el-table-column>
<el-table-column label="ویرایش" width="110" align="center">
<template slot-scope="scope">
<CButton color="danger" variant="outline" :key="scope.row._id + Math.random()"
@click="deleteNewsFile(scope.row._id)">
<i class="far fa-trash-alt"></i>
</CButton>
<CButton color="success" variant="outline" :key="scope.row._id" :to="{
name: 'admin-links-details',
params: { details: scope.row._id },
}">
<i class="far fa-eye"></i>
</CButton>
</template>
</el-table-column>
</el-table>
</CCardBody>
</CCard>
<CCard class="col" v-if="!search">
<CRow alignHorizontal="center" style="padding : 10px">
<el-pagination :hide-on-single-page="true" layout="prev, pager, next" :page-size="5"
:current-page.sync="curentPage" :total="links.length">
</el-pagination>
</CRow>
</CCard>
</div>
</template>
<script>
import moment from "moment-jalaali";
export default {
data() {
return {
title: "لیست پیوندها",
list_title: "لیست",
links: null,
search: "",
curentPage: 1,
skip: 0
};
},
computed: {
filteredNewsFile() {
if (this.search) {
return this.links.filter(
(data) =>
data.title.toLowerCase().includes(this.search.toLowerCase())
);
} else {
return this.links.slice((this.curentPage - 1) * 5, this.curentPage * 5);
}
},
},
methods: {
jDate(date) {
return moment(date).format("jYYYY/jMM/jDD");
},
deleteNewsFile(id) {
this.$confirm("پیوند حذف شود؟", "هشدار", {
confirmButtonText: "بله",
cancelButtonText: "لغو",
type: "warning",
})
.then(async () => {
this.$axios
.delete(`/api/admin/links/${id}`)
.then((res) => {
this.$message({
type: "success",
message: "پیوند با موفقیت حذف شد",
});
this.links = this.links.filter((item) => item._id !== id);
})
.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 === 422) {
return this.$message({
type: "error",
message: err.response.data.message,
});
}
console.log(err.response.data);
});
})
.catch(() => {
this.$message({
type: "warning",
message: "عملیات لغو شد",
});
});
},
},
head() {
return {
title: this.title,
};
},
layout: "admin",
async asyncData({
$axios,
error
}) {
try {
const links = await $axios.get(`/api/admin/links`);
return {
links: links.data,
};
} catch (e) {
if(e?.response?.status === 401){
error({
status: 401,
message: 'شما اجازه دسترسی به این صفحه را ندارید'
})
}else{
error({
status: 500,
message: "مشکلی در گرفتن اطلاعات بوجود آمده است",
});
}
}
},
};
</script>