Something went wrong. Try again.
A comprehensive digital solution dedicated to bridging the gap between those offering services and those seeking them, providing a centrali… gigwork.co.in
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264<template> <DashboardLayout>
<div class="flex items-center mb-4"> <button @click="goBack" class="px-4 py-2 bg-gray-300 text-gray-700 rounded-md hover:bg-gray-400 mr-4">Back</button> </div>
<div class="flex justify-between items-center mb-4"> <h2 class="text-xl font-semibold text-gray-800 dark:text-white">Sub-Categories</h2> <h2 class="text-xl font-semibold text-gray-800 dark:text-white">{{ category.name }}</h2> <button @click="openAddModal" class="px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600">Add Category</button>
</div>
<div class="relative overflow-x-auto shadow-md sm:rounded-lg"> <table class="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400"> <thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400"> <tr> <th class="px-6 py-3 w-full"> Category </th> <th class="px-6 py-3 whitespace-nowrap"> Action </th> <th class="px-6 py-3 whitespace-nowrap"> Status </th> </tr> </thead> <tbody> <template v-for="subCategory in paginatedSubCategories" :key="subCategory.id"> <tr class="odd:bg-white odd:dark:bg-gray-900 even:bg-gray-50 even:dark:bg-gray-800 border-b dark:border-gray-700 border-gray-200"> <td class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white w-full"> <a @click="redirectToSubcategory(subCategory.id)" class="cursor-pointer hover:underline">{{ subCategory.name }}</a> </td> <td class="px-6 py-4 whitespace-nowrap"> <button @click="openEditModal(subCategory)" class="font-medium text-blue-600 dark:text-blue-500 hover:underline">Edit</button> </td> <td class="px-6 py-4 whitespace-nowrap"> <button v-if="subCategory.status" @click="openDisableModal(subCategory)" class="font-medium text-red-600 dark:text-red-500 hover:underline">Disable</button> <button v-else @click="openEnableModal(subCategory)" class="font-medium text-green-600 dark:text-green-500 hover:underline">Enable</button> </td> </tr> </template> </tbody></table>
</div>
<div class="flex justify-between items-center mt-4"> <button @click="changePage(currentPage - 1)" :disabled="currentPage === 1" class="px-4 py-2 bg-gray-300 text-gray-700 rounded-md hover:bg-gray-400">Previous</button> <span class="text-white">Page {{ currentPage }} of {{ totalPages }}</span> <button @click="changePage(currentPage + 1)" :disabled="currentPage === totalPages" class="px-4 py-2 bg-gray-300 text-gray-700 rounded-md hover:bg-gray-400">Next</button> </div>
<div v-if="showModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center z-40 justify-center"> <div class="bg-white p-6 rounded-lg w-full max-w-2xl max-h-[90vh] overflow-y-auto"> <div class="flex justify-between items-center mb-4"> <h2 class="text-xl font-bold">{{ isEdit ? 'Edit Sub Category' : 'Add New Sub Category' }}</h2> <button @click="closeModal" class="text-gray-500 hover:text-gray-700">×</button> </div> <form @submit.prevent="submitForm"> <label for="category">Category</label> <input type="text" id="category" v-model="subCategoryForm.name" class="w-full border border-gray-300 rounded-md p-2 mt-2 mb-4" placeholder="Enter the category"> <button type="submit" class="px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600">Save</button> </form> </div> </div>
</DashboardLayout></template>
<script>import { fetchSubCategories, addSubCategories, updateSubCategories ,updateSubCategoryStatus } from '@/api';import DashboardLayout from '@/layouts/DashboardLayout.vue';import Swal from 'sweetalert2';
export default { name: 'SubCategory', components: { DashboardLayout }, data() { return { pageId: this.$route.params.id, subCategories: [], category: {}, showModal: false, isAdd: false, isEdit: false, subCategoryForm: { name: '' }, currentPage: 1, itemsPerPage: 10, } }, computed: { paginatedSubCategories() { const start = (this.currentPage - 1) * this.itemsPerPage; const end = start + this.itemsPerPage; return this.subCategories.slice(start, end); }, totalPages() { return Math.ceil(this.subCategories.length / this.itemsPerPage); } }, mounted() { console.log(this.pageId); this.fetchSubCategory(); }, methods: { async fetchSubCategory() { try { const response = await fetchSubCategories(this.pageId); console.log(response.data); this.category = response.data.category; this.subCategories = response.data.subCategory; } catch (error) { console.error(error); } }, openAddModal() { this.showModal = true; this.isAdd = true; this.isEdit = false; console.log('Add Modal');
}, openEditModal(subCategory) { this.showModal = true; this.isEdit = true; this.isAdd = false; this.subCategoryForm.name = subCategory.name; this.editSubCategoryId = subCategory.id; }, closeModal() { this.showModal = false; }, submitForm() { if (this.isAdd) { this.addSubCategory(); } else if (this.isEdit) { this.updateSubCategory(); } }, async addSubCategory() {
const data = { name: this.subCategoryForm.name, category_id: this.pageId }
try { await addSubCategories(data); this.fetchSubCategory(); this.showModal = false; } catch (error) { console.error(error); } }, async updateSubCategory() { const data = { name: this.subCategoryForm.name, category_id: this.editSubCategoryId } try { await updateSubCategories(data); this.fetchSubCategory(); this.showModal = false; } catch (error) { console.error(error); } }, redirectToSubcategory(id) { this.$router.push(`/subcategoryoptions/${id}`); }, changePage(page) { if (page > 0 && page <= this.totalPages) { this.currentPage = page; } }, goBack() { this.$router.go(-1); }, openDisableModal(subCategory) { Swal.fire({ title: 'Are you sure?', // text: "You won't be able to revert this!", icon: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'Yes, disable it!' }).then((result) => { if (result.isConfirmed) { this.disablesubCategory(subCategory.id); Swal.fire( 'Disabled!', 'Your license has been disabled.', 'success' ); } }); }, async disablesubCategory(subCategoryId) { try { const data = { status: 0, sub_category_id: subCategoryId } await updateSubCategoryStatus(data); this.fetchSubCategory(); } catch (error) { console.error(error); } }, openEnableModal(subCategory) { Swal.fire({ title: 'Are you sure?', // text: "You won't be able to revert this!", icon: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'Yes, enable it!' }).then((result) => { if (result.isConfirmed) { this.enableSubCategory(subCategory.id); Swal.fire( 'Enabled!', 'Your category has been enabled.', 'success' ); } }); }, async enableSubCategory(subCategoryId) { try { const data = { status: 1, sub_category_id: subCategoryId } await updateSubCategoryStatus(data); this.fetchSubCategory(); } catch (error) { console.error(error); } },
}}</script>