Skip to content

Commit

Permalink
wip: frontend - category update
Browse files Browse the repository at this point in the history
  • Loading branch information
rkshaon committed Jun 22, 2024
1 parent 9539b61 commit 94fd6ff
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 92 deletions.
69 changes: 0 additions & 69 deletions frontend/src/components/admin/category/AdminCategoryComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,75 +14,6 @@
</ul>
</div>
<AdminAddCategoryComponent ref="addComponentModal" />
<!-- Update category modal -->
<div class="modal fade" id="updateCategoryModal" tabindex="-1" aria-labelledby="updateCategoryModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="updateCategoryModalLabel">Update Category</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body text-start">
<form action="">
<div class="mb-3">
<label for="exampleInputText1" class="form-label">Title</label>
<input type="text" class="form-control" id="exampleInputText1"
aria-describedby="textHelp1" v-model="updateForm.title">
<div id="textHelp1" class="form-text">Category name must be unique.</div>
</div>
<div class="mb-3">
<label for="exampleInputText2" class="form-label">Short Title</label>
<input type="text" class="form-control" id="exampleInputText2"
v-model="updateForm.short_title">
</div>
<div class="mb-3">
<label for="exampleInputText3" class="form-label">Icon</label>
<img v-if="updateForm.icon" :src="`${API_BASE_URL}${updateForm.icon}`"
:alt="updateForm.title" height="50" />
<input type="file" class="form-control" id="exampleInputText3"
@change="handleFileChangeOnUpdate">
</div>
<div class="mb-3">
<label for="exampleTextarea" class="form-label">Description</label>
<textarea class="form-control" id="exampleTextarea" rows="3"
v-model="updateForm.description" maxlength="255">
</textarea>
<div class="ms-3 text-end">
<span class="text-muted small">{{ updateForm.description.length }} / 255</span>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" @click="updateCategory(updateForm.id)">Save
changes</button>
</div>
</div>
</div>
</div>
<!-- Delete category modal -->
<div class="modal fade" id="deleteCategoryModal" tabindex="-1" aria-labelledby="deleteCategoryModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteCategoryModalLabel">Delete Category</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body text-start">
<div class="mb-3">
<label for="exampleInputText1" class="form-label">Are you sure?</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-danger" @click="deleteCategory">Yes, delete</button>
</div>
</div>
</div>
</div>
<!-- Add category button -->
<!-- <button type="button" class="btn btn-primary btn-lg mb-2" data-bs-toggle="modal"
data-bs-target="#addCategoryModal">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<AdminDeleteCategoryComponent ref="deleteComponentModal" />
<AdminUpdateCategoryComponent ref="updateComponentModal" />
<div class="pt-5">
<table class="table caption-top table-hover table-bordered">
<caption>List of categories</caption>
Expand All @@ -22,12 +23,14 @@
<div v-else>Upload Icon</div>
</td>
<td>
<!-- <font-awesome-icon class="text-warning" :icon="['fas', 'file-pen']" style=""
data-bs-toggle="modal" data-bs-target="#updateCategoryModal" /> -->
<font-awesome-icon class="text-warning" :icon="['fas', 'file-pen']" style=""
data-bs-toggle="modal" data-bs-target="#updateCategoryModal" />
@click="showUpdateCategoryModal(cat.id)" />
<!-- <font-awesome-icon class="text-danger" :icon="['fas', 'trash']" style="padding-left: 10px;"
data-bs-toggle="modal" data-bs-target="#deleteCategoryModal" /> -->
<font-awesome-icon class="text-danger" :icon="['fas', 'trash']" style="padding-left: 10px;"
@click="showDeleteCategoryModal(cat.id)"/>
@click="showDeleteCategoryModal(cat.id)" />
</td>
</tr>
</tbody>
Expand All @@ -39,11 +42,13 @@
import { mapState, mapActions } from 'vuex';
import { API_BASE_URL } from '@/config';
import AdminDeleteCategoryComponent from '@/components/admin/category/AdminDeleteCategoryComponent.vue';
import AdminUpdateCategoryComponent from '@/components/admin/category/AdminUpdateCategoryComponent.vue';
export default {
name: "AdminCategoryListComponent",
components: {
AdminDeleteCategoryComponent,
AdminUpdateCategoryComponent
},
setup() { },
data() {
Expand All @@ -63,6 +68,10 @@ export default {
this.$refs.deleteComponentModal.showDeleteCategoryModal(id);
},
showUpdateCategoryModal(id) {
this.$refs.updateComponentModal.showUpdateCategoryModal(id);
}
},
created() {
this.fetchCategories();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default {
async confirmDelete() {
try {
await this.deleteCategory(this.deleteCategoryId);
this.$emit('categoryDeleted', this.deleteCategoryId);
} catch (error) {
console.log('Failed:', error);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<template>
<!-- Update category modal -->
<div class="modal fade" id="updateCategoryModal" tabindex="-1" aria-labelledby="updateCategoryModalLabel"
aria-hidden="true" ref="updateCategoryModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="updateCategoryModalLabel">Update Category</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body text-start">
<form action="">
<div class="mb-3">
<label for="exampleInputText1" class="form-label">Title</label>
<input type="text" class="form-control" id="exampleInputText1" aria-describedby="textHelp1"
v-model="updateForm.title">
<div id="textHelp1" class="form-text">Category name must be unique.</div>
</div>
<div class="mb-3">
<label for="exampleInputText2" class="form-label">Short Title</label>
<input type="text" class="form-control" id="exampleInputText2"
v-model="updateForm.short_title">
</div>
<div class="mb-3">
<label for="exampleInputText3" class="form-label">Icon</label>
<img v-if="updateForm.icon" :src="`${API_BASE_URL}${updateForm.icon}`"
:alt="updateForm.title" height="50" />
<input type="file" class="form-control" id="exampleInputText3"
@change="handleFileChangeOnUpdate">
</div>
<div class="mb-3">
<label for="exampleTextarea" class="form-label">Description</label>
<textarea class="form-control" id="exampleTextarea" rows="3"
v-model="updateForm.description" maxlength="255">
</textarea>
<div class="ms-3 text-end">
<span class="text-muted small">{{ updateForm.description.length }} / 255</span>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" @click="updateCategory(updateForm.id)">Save
changes</button>
</div>
</div>
</div>
</div>
</template>

<script>
import { mapActions } from 'vuex';
import { API_BASE_URL } from '@/config';
import { Modal } from 'bootstrap';
export default {
name: 'AdminUpdateCategoryComponent',
data() {
return {
API_BASE_URL: API_BASE_URL,
updateCategoryId: null,
updateForm: {
id: '',
title: '',
short_title: '',
icon: null,
description: '',
},
}
},
methods: {
...mapActions('category', ['fetchCategory', 'updateCategory']),
async showUpdateCategoryModal(id) {
this.updateCategoryId = id;
try {
this.updateForm = await this.fetchCategory(this.updateCategoryId);
} catch (error) {
console.log('Failed:', error);
}
const modalElement = this.$refs.updateCategoryModal;
if (modalElement) {
const modal = new Modal(modalElement);
modal.show();
} else {
console.log('404');
}
},
}
}
</script>
21 changes: 12 additions & 9 deletions frontend/src/services/adminCategoryAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ apiClient.interceptors.response.use(
}
);

export const getCategoryForAdmin = (category_id) => {
return apiClient.get(`/api/v1/categories/${category_id}`);
};

export const getCategoriesForAdmin = () => {
// return axios.get(`${API_URL}/categories`);
return apiClient.get("/api/v1/categories/");
};

Expand All @@ -60,13 +63,13 @@ export const deleteCategoryForAdmin = (category_id) => {
return apiClient.delete(`/api/v1/categories/${category_id}`);
};

export default {
updateCategoryForAdmin(category_id, categoryData) {
return apiClient.put(`/api/v1/categories/${category_id}`, categoryData, {
headers: {
'Content-Type': 'multipart/form-data',
}
});
},
export const updateCategoryForAdmin = (category_id, categoryData) => {
return apiClient.put(`/api/v1/categories/${category_id}`, categoryData, {
headers: {
'Content-Type': 'multipart/form-data',
}
});
};

export default { };

51 changes: 39 additions & 12 deletions frontend/src/store/modules/category.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,83 @@
import { getCategoriesForAdmin, createCategoryForAdmin, deleteCategoryForAdmin } from '@/services/adminCategoryAPI';
import {
getCategoryForAdmin,
getCategoriesForAdmin,
createCategoryForAdmin,
deleteCategoryForAdmin,
updateCategoryForAdmin,
} from "@/services/adminCategoryAPI";

const state = {
categories: [],
categories: [],
};

const mutations = {
setCategories(state, categories) {
state.categories = categories;
},

addCategory(state, category) {
state.categories.push(category);
},

removeCategory(state, id) {
state.categories = state.categories.filter(
(category) => category.id !== id
);
},

updateCategory(state, updatedCategory) {
const index = state.categories.findIndex(
(category) => category.id === updatedCategory.id
);

if (index !== -1) {
state.categories.splice(index, 1, updatedCategory);
}
},
};

const actions = {
async fetchCategory({ commit }, id) {
const response = await getCategoryForAdmin(id);
commit("addCategory", response.data);
return response.data;
},

async fetchCategories({ commit }) {
const response = await getCategoriesForAdmin();
commit("setCategories", response.data);
},

async addCategory({ commit }, category) {
try {
const response = await createCategoryForAdmin(category);
commit("addCategory", response.data);
return response.data;
} catch (error) {
console.error('Error adding category:', error);
console.error("Error adding category:", error);
throw error;
}
},

async deleteCategory({ commit }, id) {
await deleteCategoryForAdmin(id);
commit("removeCategory", id);
},

async updateCategory({ commit }, category) {
const response = await updateCategoryForAdmin(category);
commit("updateCategory", response.data);
},
};

const getters = {
categories: (state) => state.categories,
};

export default {
namespaced: true,
state,
mutations,
actions,
getters,
namespaced: true,
state,
mutations,
actions,
getters,
};

0 comments on commit 94fd6ff

Please sign in to comment.