Skip to content

Commit

Permalink
feat: discount | init discount feature
Browse files Browse the repository at this point in the history
  • Loading branch information
MRoyhanF committed Dec 19, 2024
1 parent efeb725 commit b452ee8
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/modules/discount/discount.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import axios from 'axios';

export const index = async (req, res, next) => {
try {
const api = process.env.API_URL;
const { token } = req;

const discountsResponse = await axios.get(`${api}/api/v1/discounts`, {
headers: {
Authorization: `Bearer ${token}`,
},
});

const data = {
token : token,
api : api,
discounts : discountsResponse.data.data || [],
}

res.edge('pages/discount/index', data);
} catch (error) {
next(error)
}
};
7 changes: 7 additions & 0 deletions src/modules/discount/discount.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as discountController from "./discount.controller.js";
import {validateCookies} from '../../middlewares/auth.js';

export default (router) => {
const prefix = "/discount";
router.get(prefix + "/", validateCookies, discountController.index);
};
2 changes: 2 additions & 0 deletions src/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import airlineRoutes from "./airline/airline.routes.js";
import flightRoutes from "./flight/flight.routes.js";
import ticketRoutes from "./tikcet/ticket.routes.js";
import userRoutes from "./user/user.routes.js";
import discountRoutes from "./discount/discount.routes.js";

export default (app) => {
const router = Router();
Expand All @@ -29,4 +30,5 @@ export default (app) => {
flightRoutes(router);
ticketRoutes(router);
userRoutes(router);
discountRoutes(router);
};
6 changes: 6 additions & 0 deletions src/resources/views/components/ui/sidebar.edge
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 48 48"><g fill="none" stroke="#282828" stroke-linecap="round" stroke-width="4"><path stroke-linejoin="round" d="M9 16L34 6l4 10M4 16h40v6c-3 0-6 2-6 5.5s3 6.5 6 6.5v6H4v-6c3 0 6-2 6-6s-3-6-6-6z"/><path d="M17 25.385h6m-6 6h14"/></g></svg>
</div>Tikets
</a>
<a href="/admin/discount"
class="flex items-center w-full p-3 rounded-lg text-start leading-tight transition-all hover:bg-blue-50 hover:bg-opacity-80 focus:bg-blue-50 focus:bg-opacity-80 active:bg-blue-50 active:bg-opacity-80 hover:text-blue-900 focus:text-blue-900 active:text-blue-900 outline-none">
<div class="grid place-items-center mr-4">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><g fill="none" stroke="#282828"><path stroke-width="2.5" d="M10.51 3.665a2 2 0 0 1 2.98 0l.7.782a2 2 0 0 0 1.601.663l1.05-.058a2 2 0 0 1 2.107 2.108l-.058 1.049a2 2 0 0 0 .663 1.6l.782.7a2 2 0 0 1 0 2.981l-.782.7a2 2 0 0 0-.663 1.601l.058 1.05a2 2 0 0 1-2.108 2.107l-1.049-.058a2 2 0 0 0-1.6.663l-.7.782a2 2 0 0 1-2.981 0l-.7-.782a2 2 0 0 0-1.601-.663l-1.05.058a2 2 0 0 1-2.107-2.108l.058-1.049a2 2 0 0 0-.663-1.6l-.782-.7a2 2 0 0 1 0-2.981l.782-.7a2 2 0 0 0 .663-1.601l-.058-1.05A2 2 0 0 1 7.16 5.053l1.049.058a2 2 0 0 0 1.6-.663z"/><path stroke-linejoin="round" stroke-width="3.75" d="M9.5 9.5h.01v.01H9.5zm5 5h.01v.01h-.01z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="m15 9l-6 6"/></g></svg>
</div>Discount
</a>
<a href="/admin/users"
class="flex items-center w-full p-3 rounded-lg text-start leading-tight transition-all hover:bg-blue-50 hover:bg-opacity-80 focus:bg-blue-50 focus:bg-opacity-80 active:bg-blue-50 active:bg-opacity-80 hover:text-blue-900 focus:text-blue-900 active:text-blue-900 outline-none">
<div class="grid place-items-center mr-4">
Expand Down
185 changes: 185 additions & 0 deletions src/resources/views/pages/discount/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
@layout.app({ title: "User List" })
@slot('main')

<div class="px-6 py-2 w-full bg-white rounded-lg">
<!-- Content Head -->
<div class="flex justify-end items-center mb-2">
@component('components/ui/modal', {
title: 'Add Discount',
buttonClass: 'shadow-md py-2 px-4 inline-flex items-center gap-x-2 text-sm font-medium rounded-lg border border-transparent bg-blue-300 text-blue-800 hover:bg-blue-400 focus:outline-none focus:bg-blue-200 disabled:opacity-50 disabled:pointer-events-none',
})
@slot('header')
Add Discount
@end
@slot('content')

<form id="adminAddForm" class="max-w-sm mx-auto">
<div class="mb-5">
<label for="percentage" class="block mb-2 text-sm font-medium text-gray-900">Percentage</label>
<input type="number" id="percentage" name="percentage" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5" placeholder="75" required />
</div>
<button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center">Add Dscount</button>
</form>

@end
@end
</div>

<div class="border-2 rounded-lg p-4 overflow-x-auto text-sm">
<table class="myTable display" style="width:100%">
<thead>
<tr>
<th>No</th>
<th>Percentage</th>
<th>Action</th>
</tr>
</thead>
<tbody class="text-center">
@each((data, index) in discounts)
<tr>
<td>{{ index + 1 }}</td>
<td>{{ data.percentage }} %</td>
<td>
<div class="flex justify-center items-center gap-2">
@component('components/ui/modal', {
title: 'Edit Discount',
buttonClass: 'bg-blue-500 text-white px-2 py-1 rounded-md',
})
@slot('header')
Edit
@end
@slot('content')

<form id="adminEditForm" class="max-w-sm mx-auto">
<div class="mb-5">
<label for="percentageEdit" class="block mb-2 text-sm font-medium text-gray-900">Percentage</label>
<input type="number" id="percentageEdit-{{ data.id }}" name="percentageEdit" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5" value="{{ data.percentage }}" required />
</div>
<button type="button" onclick="editDiscount({{ data.id }})" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center">Edit Dscount</button>
</form>

@end
@end
<button type="button" onclick="deleteDiscount({{ data.id }})" class="bg-red-500 text-white px-2 py-1 rounded-md">Delete</button>
</div>
</td>
</tr>
@else
<tr>
<td colspan="3" class="text-center">No data available</td>
</tr>
@end
</tbody>

</table>
</div>
</div>

<script>
const token = '{{ token }}'
function editDiscount(id) {
const percentageEdit = $(`#percentageEdit-${id}`).val();
$.ajax({
url: '{{ api }}/api/v1/discounts/' + id,
method: 'PUT',
contentType: 'application/json',
data: JSON.stringify({
percentage: parseInt(percentageEdit),
}),
headers: {
'Authorization': `Bearer ${token}`
},
success: function (response) {
showToast("success", "Success", "Discount edited successfully!");
setTimeout(() => {
window.location.reload();
}, 2000);
},
error: function (xhr) {
if (xhr.responseJSON && xhr.responseJSON.errors) {
const errors = xhr.responseJSON.errors;
let errorMessages = '';
errors.forEach(error => {
errorMessages += `${error.field}: ${error.message}\n`;
});
showToast("error", "Error", errorMessages);
} else {
showToast("error", "Error", "An error occurred. Please try again.");
}
},
});
}
function deleteDiscount(id) {
$.ajax({
url: '{{ api }}/api/v1/discounts/' + id,
method: 'DELETE',
headers: {
'Authorization': `Bearer ${token}`
},
success: function (response) {
showToast("success", "Success", "Discount deleted successfully!");
setTimeout(() => {
window.location.reload();
}, 1000);
},
error: function (xhr) {
if (xhr.responseJSON && xhr.responseJSON.errors) {
const errors = xhr.responseJSON.errors;
let errorMessages = '';
errors.forEach(error => {
errorMessages += `${error.field}: ${error.message}\n`;
});
showToast("error", "Error", errorMessages);
} else {
showToast("error", "Error", "An error occurred. Please try again.");
}
},
});
}
$(document).ready(function () {
$('#adminAddForm').on('submit', function (e) {
e.preventDefault();
const formData = {
percentage: parseInt($('#percentage').val()),
};
$.ajax({
url: '{{ api }}/api/v1/discounts',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(formData),
headers: {
'Authorization': `Bearer ${token}`
},
success: function (response) {
showToast("success", "Success", "Discount created successfully!");
setTimeout(() => {
window.location.reload();
}, 1000);
},
error: function (xhr) {
if (xhr.responseJSON && xhr.responseJSON.errors) {
const errors = xhr.responseJSON.errors;
let errorMessages = '';
errors.forEach(error => {
errorMessages += `${error.field}: ${error.message}\n`;
});
showToast("error", "Error", errorMessages);
} else {
showToast("error", "Error", "An error occurred. Please try again.");
}
},
});
});
});
</script>

@endslot
@end
2 changes: 1 addition & 1 deletion src/resources/views/pages/user/index.edge
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@end
</div>

<div class="border-2 rounded-lg p-4 overflow-x-auto text-xs">
<div class="border-2 rounded-lg p-4 overflow-x-auto text-sm">
<table class="myTable display" style="width:100%">
<thead>
<tr>
Expand Down

0 comments on commit b452ee8

Please sign in to comment.