index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <v-container>
  3. <AdminNestedList
  4. :depth="0"
  5. @edit="openEditor"
  6. @delete="deleteCategory"
  7. :categories="parentCategories"
  8. ></AdminNestedList>
  9. <modal height="auto" name="category-editor">
  10. <h2>Редактирование категории</h2>
  11. <v-text-field
  12. v-model="editingCategory.alias"
  13. label="Алиас"
  14. ></v-text-field>
  15. <v-text-field
  16. v-model="editingCategory.title"
  17. label="Название"
  18. ></v-text-field>
  19. <v-btn color="primary" @click="editCategory">Сохранить</v-btn>
  20. <v-btn color="error" @click="$modal.hide('category-editor')"
  21. >Отмена</v-btn
  22. >
  23. </modal>
  24. </v-container>
  25. </template>
  26. <script>
  27. export default {
  28. layout: "admin",
  29. data() {
  30. return {
  31. categories: [],
  32. editingCategory: false,
  33. };
  34. },
  35. async mounted() {
  36. await this.loadCategories();
  37. },
  38. methods: {
  39. async loadCategories() {
  40. await this.$axios.$get("/admin/categories").then((res) => {
  41. this.categories = res.data;
  42. });
  43. },
  44. getChildCaregories(parent) {
  45. return this.childCategories.filter(
  46. (child) => child.parent_id == parent.id
  47. );
  48. },
  49. openEditor(category) {
  50. this.editingCategory = Object.assign({}, category);
  51. this.$modal.show("category-editor");
  52. },
  53. async editCategory(category) {
  54. const result = await this.$swal.fire({
  55. title: "Вы уверены, что хотите отредактировать категорию?",
  56. showCancelButton: true,
  57. confirmButtonText: "Принять",
  58. icon: "question",
  59. cancelButtonText: `Отменить`,
  60. confirmButtonColor: `#1976d2`,
  61. denyButtonColor: `#2196f3`,
  62. });
  63. if (result.isConfirmed) {
  64. let { id, title, alias, parent_id } = this.editingCategory;
  65. await this.$axios.post("admin/categories/update", {
  66. id, title, alias, parent_id,
  67. });
  68. await this.loadCategories();
  69. this.$swal.fire("Готово!", "", "success");
  70. this.$modal.hide("category-editor");
  71. }
  72. },
  73. async deleteCategory(category) {
  74. console.log(123)
  75. const result = await this.$swal.fire({
  76. title: `Вы уверены, что хотите удалить категорию?`,
  77. showCancelButton: true,
  78. confirmButtonText: "Удалить",
  79. icon: "error",
  80. cancelButtonText: `Не удалять`,
  81. confirmButtonColor: `#ab003c`,
  82. denyButtonColor: `#2196f3`,
  83. });
  84. if (result.isConfirmed) {
  85. let { id } = category;
  86. await this.$axios.post("admin/categories/remove", { id });
  87. await this.loadCategories();
  88. this.$swal.fire("Готово!", "", "success");
  89. }
  90. },
  91. },
  92. computed: {
  93. parentCategories() {
  94. return this.categories.filter((category) => !category.parent_id);
  95. },
  96. },
  97. };
  98. </script>
  99. <style>
  100. </style>