Skip to content

Commit

Permalink
Merge pull request #27 from rkshaon/shaon
Browse files Browse the repository at this point in the history
rework: frontend
  • Loading branch information
rkshaon authored Jun 22, 2024
2 parents 222e92f + f1bf870 commit 7534405
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 92 deletions.
101 changes: 101 additions & 0 deletions frontend/src/components/admin/category/AdminAddCategoryComponent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<template>
<div class="modal fade" id="addCategoryModal" tabindex="-1" aria-labelledby="addCategoryModalLabel"
aria-hidden="true" ref="addCategoryModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addCategoryModalLabel">Add Category</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form @submit.prevent="createCategory">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" v-model="categoryForm.title">
</div>
<div class="mb-3">
<label for="shortTitle" class="form-label">Short Title</label>
<input type="text" class="form-control" id="shortTitle" v-model="categoryForm.short_title">
</div>
<div class="mb-3">
<label for="icon" class="form-label">Icon</label>
<input type="file" class="form-control" id="icon" @change="handleFileChangeOnInsert">
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" rows="3" v-model="categoryForm.description"
maxlength="255"></textarea>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>
</div>
</div>
</div>
</template>

<script>
import { Modal } from 'bootstrap';
import adminCategoryAPI from "@/services/adminCategoryAPI";
export default {
name: 'AdminAddCategoryComponent',
data() {
return {
categoryForm: {
title: '',
short_title: '',
icon: null,
description: '',
}
}
},
methods: {
showAddCategoryModal() {
const modalElement = this.$refs.addCategoryModal;
console.log(modalElement);
if (modalElement) {
const modal = new Modal(modalElement);
modal.show();
} else {
console.log('404');
}
},
handleFileChangeOnInsert(event) {
this.categoryForm.icon = event.target.files[0];
},
async createCategory() {
const formData = new FormData();
formData.append('title', this.categoryForm.title);
formData.append('short_title', this.categoryForm.short_title);
formData.append('description', this.categoryForm.description);
formData.append('is_active', true);
if (this.categoryForm.icon) {
formData.append('icon', this.categoryForm.icon);
}
console.log(formData);
try {
const response = await adminCategoryAPI.createCategoryForAdmin(formData);
console.log('Create...', response.data);
this.successMessage = 'Category created successfully!';
} catch (error) {
console.error('Failed...', error);
this.errorMessages.push({
'title': 'Create Category',
'message': 'Failed to create a new category.',
});
}
const modalElement = this.$refs.addCategoryModal;
const modal = new Modal(modalElement);
modal._hideModal();
},
},
}
</script>
114 changes: 27 additions & 87 deletions frontend/src/components/admin/category/AdminCategoryListComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,7 @@
</li>
</ul>
</div>
<!-- Add category modal -->
<div class="modal fade" id="addCategoryModal" tabindex="-1" aria-labelledby="addCategoryModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addCategoryModalLabel">Add 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="categoryForm.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="categoryForm.short_title">
</div>
<div class="mb-3">
<label for="exampleInputText3" class="form-label">Icon</label>
<input type="file" class="form-control" id="exampleInputText3"
@change="handleFileChangeOnInsert">
</div>
<div class="mb-3">
<label for="exampleTextarea" class="form-label">Description</label>
<textarea class="form-control" id="exampleTextarea" rows="3"
v-model="categoryForm.description" maxlength="255">
</textarea>
<div class="ms-3 text-end">
<span class="text-muted small">{{ categoryForm.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="createCategory">Save</button>
</div>
</div>
</div>
</div>
<AdminAddCategoryComponent ref="addComponentModal" />
<!-- Update category modal -->
<div class="modal fade" id="updateCategoryModal" tabindex="-1" aria-labelledby="updateCategoryModalLabel"
aria-hidden="true">
Expand Down Expand Up @@ -128,11 +84,16 @@
</div>
</div>
<!-- Add category button -->
<button type="button" class="btn btn-primary btn-lg mb-2" data-bs-toggle="modal"
<!-- <button type="button" class="btn btn-primary btn-lg mb-2" data-bs-toggle="modal"
data-bs-target="#addCategoryModal">
<font-awesome-icon :icon="['fas', 'plus']" class="me-2" />
Add Category
</button>
</button> -->
<button type="button" class="btn btn-primary btn-lg mb-2" @click="showAddCategoryModal">
<font-awesome-icon :icon="['fas', 'plus']" class="me-2" />Add Category
</button>
<button type="button" class="btn btn-success" @click="showTestModal">Test me!</button>
<ModalComponent ref="modalComponent" />
<div class="row">
<div class="col-md-4">
<div class="shadow-lg">
Expand Down Expand Up @@ -231,21 +192,21 @@
<script>
import { API_BASE_URL } from '@/config';
import adminCategoryAPI from "@/services/adminCategoryAPI";
import ModalComponent from '@/components/admin/category/ModalComponent.vue';
import AdminAddCategoryComponent from '@/components/admin/category/AdminAddCategoryComponent.vue';
import { Modal } from 'bootstrap';
export default {
name: "AdminCategoryListComponent",
components: {
ModalComponent,
AdminAddCategoryComponent,
},
setup() {},
data() {
return {
API_BASE_URL: API_BASE_URL,
categoryForm: {
title: '',
short_title: '',
icon: null,
description: '',
},
updateForm: {
id: '',
title: '',
Expand All @@ -265,6 +226,18 @@ export default {
this.fetchCategories();
},
methods: {
showTestModal() {
// console.log('Hello...');
// let testModal = new bootstrap.Modal(document.getElementById('testModal'));
// // console.log(testModal);
// testModal.show();
this.$refs.modalComponent.showTestModal();
},
showAddCategoryModal() {
this.$refs.addComponentModal.showAddCategoryModal();
},
async fetchCategories() {
try {
const response = await adminCategoryAPI.getCategoriesForAdmin();
Expand All @@ -277,36 +250,7 @@ export default {
});
}
},
async createCategory() {
const formData = new FormData();
formData.append('title', this.categoryForm.title);
formData.append('short_title', this.categoryForm.short_title);
formData.append('description', this.categoryForm.description);
formData.append('is_active', true);
if (this.categoryForm.icon) {
formData.append('icon', this.categoryForm.icon);
}
try {
const response = await adminCategoryAPI.createCategoryForAdmin(formData);
console.log('Create...', response.data);
this.successMessage = 'Category created successfully!';
} catch(error) {
console.error('Failed...', error);
this.errorMessages.push({
'title': 'Create Category',
'message': 'Failed to create a new category.',
});
}
let createCategoryModalElement = document.getElementById('addCategoryModal');
let modal = Modal.getInstance(createCategoryModalElement);
modal.hide();
},
async deleteCategory() {
if (!this.deleteCategoryId) {
console.error('Category ID is not set');
Expand All @@ -330,10 +274,6 @@ export default {
modal.hide();
},
handleFileChangeOnInsert(event) {
this.categoryForm.icon = event.target.files[0];
},
handleFileChangeOnUpdate(event) {
this.updateForm.icon = event.target.files[0];
},
Expand Down
28 changes: 28 additions & 0 deletions frontend/src/components/admin/category/DeleteCategoryComponent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<template>
<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>
</template>

<script>
export default {
name: 'DeleteCategoryComponent',
}
</script>
46 changes: 46 additions & 0 deletions frontend/src/components/admin/category/ModalComponent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<template>
<div>
<div class="modal" tabindex="-1" id="testModal" ref="testModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal Title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</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="submit">Submit</button>
</div>
</div>
</div>
</div>
</div>
</template>

<script>
import { Modal } from 'bootstrap';
export default {
name: 'ModalComponent',
methods: {
showTestModal() {
const modalElement = this.$refs.testModal;
if (modalElement) {
const modal = new Modal(modalElement);
modal.show();
} else {
console.log('404');
}
},
submit() {
const modalElement = this.$refs.testModal;
const modal = new Modal(modalElement);
modal._hideModal();
},
},
}
</script>
6 changes: 4 additions & 2 deletions frontend/src/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createApp } from 'vue';
import App from './App.vue';
import router from '@/router';
import { BootstrapVueNext } from "bootstrap-vue-next";
// import { BootstrapVueNext } from "bootstrap-vue-next";
// import store from "@/store";

/* import the fontawesome core */
Expand All @@ -10,8 +10,10 @@ import { library } from '@fortawesome/fontawesome-svg-core'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'

import "bootstrap/dist/css/bootstrap.css";
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap-vue-next/dist/bootstrap-vue-next.css";
import "bootstrap/dist/js/bootstrap.js";
import "bootstrap/dist/js/bootstrap.bundle.min.js";

import {
faEnvelope, faPhone, faMapPin, faHouse, faRightLong, faPlus,
Expand Down Expand Up @@ -45,7 +47,7 @@ library.add(
);

const app = createApp(App);
app.use(BootstrapVueNext);
// app.use(BootstrapVueNext);
app.use(router);
// app.use(store);
app.component('font-awesome-icon', FontAwesomeIcon);
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import NotFoundComponent from '@/components/NotFoundComponent';
import AdminBaseComponent from "@/components/admin/AdminBaseComponent";
import AdminDashboardComponent from '@/components/admin/AdminDashboardComponent';
import AdminLoginComponent from '@/components/admin/AdminLoginComponent.vue';
import AdminCategoryListComponent from '@/components/admin/category/AdminCategoryListComponent';
import AdminCategoryListComponent from "@/components/admin/category/AdminCategoryListComponent.vue";



const routes = [
Expand Down Expand Up @@ -48,8 +49,8 @@ const routes = [
{
path: "category",
name: "admin-category",
component: AdminCategoryListComponent,
}
component: AdminCategoryListComponent,
},
],
},
{
Expand Down

0 comments on commit 7534405

Please sign in to comment.