-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
158 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
frontend/src/components/admin/category/AdminUpdateCategoryComponent.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |