Files
dlearn-admin/src/stores/statics.js
T
2024-06-27 16:21:52 +03:30

117 lines
3.6 KiB
JavaScript

import axios from '../plugins/axios'
import { defineStore } from 'pinia'
export const useStaticsStore = defineStore('statics', {
state: () => ({
items: [],
fetching: true,
total: 0
}),
getters: {
// treeSelect: (state) => (id = null) => {
// if (id) {
// const foreach = (items) => {
// return items.filter((item) => {
// if (item.key != id) {
// if (item.children)
// item.children = foreach(item.children)
// return true
// } else return false
// })
// }
// return foreach(state.items)
// }
// return state.items
// },
find: (state) => (id) => {
if (id) {
const foreach = (items) => {
const item = items.find(({ key }) => key == id)
if (item) return item
for (let i = 0; i < items.length; i++) {
const item = items[i];
if (item.children) {
const temp = foreach(item.children)
if (temp) return temp
}
}
}
return foreach(state.items)
}
return '';
}
},
actions: {
async index() {
this.fetching = true
const { status, data } = await axios.get('/admin/settings')
this.fetching = false
if (status === 200) {
this.items = data
// this.total = data.total
}
},
async edit(data) {
this.fetching = true
// const json = {
// "about": "",
// "student": 100,
// "hours": 0,
// "title": 0,
// "expertise": 0,
// "nameCompany": "string",
// "mission": "string",
// "start": "string",
// "goals": "string"
// }
const result = await axios.patch('/admin/settings/edit', data)
this.fetching = false
return result
// if (status === 200) {
// this.items = data
// // this.total = data.total
// }
},
async create(id) {
const result = await axios.post('/courseCategory', {
name: id.value
})
//console.log('result', result);
return result;
},
async store(form) {
const result = await axios.post('/admin/categories', form)
if (result.status === 201)
this.items.unshift(result.data)
return result;
},
async update(id, form) {
const result = await axios.put(`/admin/categories/${id}`, form)
if (result.status === 200) {
const index = this.items.findIndex(({ _id }) => _id == id)
this.items[index] = result.data
}
return result;
},
async remove(id) {
const result = await axios.delete(`/courseCategory/${id}`)
if (result.status === 200) {
const index = this.items.findIndex(({ _id }) => _id == id)
this.items.splice(index, 1)
}
return result;
},
}
})