Compare commits

...

3 Commits

10 changed files with 245 additions and 229 deletions

View File

@ -29,6 +29,7 @@ func routes(app *config.AppConfig) http.Handler {
mux.Post("/make-reservation", handlers.Repo.PostMakeReservation)
mux.Get("/reservation-summary", handlers.Repo.ReservationSummary)
mux.Get("/choose-room/{id}", handlers.Repo.ChooseRoom)
mux.Get("/book-room", handlers.Repo.BookRoom)
fileServer := http.FileServer(http.Dir("./static/"))
mux.Handle("/static/*", http.StripPrefix("/static", fileServer))

View File

@ -301,6 +301,7 @@ func (m *Repository) AvailabilityJSON(w http.ResponseWriter, r *http.Request) {
w.Write(out)
}
// ChooseRoom displays list of available rooms
func (m *Repository) ChooseRoom(w http.ResponseWriter, r *http.Request) {
roomID, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
@ -320,3 +321,36 @@ func (m *Repository) ChooseRoom(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/make-reservation", http.StatusSeeOther)
}
// BookRoom takes URL parameters, builds a sessional variable, and takes user to make reservation
func (m *Repository) BookRoom(w http.ResponseWriter, r *http.Request) {
roomID, _ := strconv.Atoi(r.URL.Query().Get("id"))
sd := r.URL.Query().Get("s")
ed := r.URL.Query().Get("e")
var res models.Reservation
layout := "2006-01-02"
startDate, err := time.Parse(layout, sd)
if err != nil {
helpers.ServerError(w, err)
return
}
endDate, err := time.Parse(layout, ed)
if err != nil {
helpers.ServerError(w, err)
return
}
room, err := m.DB.GetRoomById(roomID)
if err != nil {
helpers.ServerError(w, err)
return
}
res.RoomID = roomID
res.StartDate = startDate
res.EndDate = endDate
res.Room.RoomName = room.RoomName
m.App.Session.Put(r.Context(), "reservation", res)
http.Redirect(w, r, "/make-reservation", http.StatusSeeOther)
}

View File

@ -0,0 +1 @@
delete from rooms

View File

@ -0,0 +1,3 @@
INSERT INTO public.rooms(room_name, created_at, updated_at) VALUES
("General's Quarters","2024-07-08 00:00:00","2024-07-08 00:00:00"),
("Major's Suite","2024-07-08 00:00:00","2024-07-08 00:00:00");

View File

@ -0,0 +1 @@
delete from restrictions;

View File

@ -0,0 +1,4 @@
INSERT INTO public.restrictions(restriction_name, created_at, updated_at) VALUES
("Reservation","2024-07-08 00:00:00","2024-07-08 00:00:00"),
("Owner block","2024-07-08 00:00:00","2024-07-08 00:00:00");

197
static/js/app.js Normal file
View File

@ -0,0 +1,197 @@
// notie
function notify(msg, msgType) {
notie.alert({
type: msgType,
text: msg,
});
}
// sweetalert2
function notifyModal(title, text, icon, confirmButtonText) {
Swal.fire({
title: title,
text: text,
icon: icon,
confirmButtonText: confirmButtonText
})
}
// Prompt is out Javascript module for all alerts, notifications, and custom popup dialogs
function Prompt() {
let toast = function (c) {
const {
msg = "",
icon = "success",
position = "top-end",
} = c
const Toast = Swal.mixin({
toast: true,
title: msg,
position: position,
icon: icon,
showConfirmButton: false,
timer: 3000,
timerProgressBar: true,
didOpen: (toast) => {
toast.addEventListener('mouseenter', Swal.stopTimer)
toast.addEventListener('mouseleave', Swal.resumeTimer)
}
})
Toast.fire({})
}
let success = function (c) {
const {
msg = "",
title = "",
footer = "",
} = c
Swal.fire({
icon: 'success',
title: title,
text: msg,
footer: footer,
})
}
let error = function (c) {
const {
msg = "",
title = "",
footer = "",
} = c
Swal.fire({
icon: 'error',
title: title,
text: msg,
footer: footer,
})
}
async function custom(c) {
const {
icon = "",
msg = "",
title = "",
showConfirmButton = true,
} = c;
const {value: result} = await Swal.fire({
icon: icon,
title: title,
html: msg,
backdrop: false,
focusConfirm: false,
showCancelButton: true,
showConfirmButton: showConfirmButton,
willOpen: () => {
if (c.willOpen !== undefined) {
c.willOpen();
}
},
didOpen: () => {
if (c.didOpen !== undefined) {
c.didOpen();
}
},
preConfirm: () => {
return [
document.getElementById('start').value,
document.getElementById('end').value,
]
},
})
if (c.callback !== undefined) {
if (result &&
result.dismiss !== Swal.DismissReason.cancel &&
result !== "") {
c.callback(result);
} else {
c.callback(false);
}
}
}
return {
toast: toast,
success: success,
error: error,
custom: custom,
}
}
function roomReservation(id) {
document.getElementById("check-availability").addEventListener('click', () => {
let html = `
<form id="check-availability-form" action="/availability-json" method="post" novalidate class="needs-validation">
<div id="reservation-dates-modal" class="row">
<div class="col mb-3">
<input disabled required type="text" class="form-control" name="start" id="start" placeholder="Arrival">
</div>
<div class="col mb-3">
<input disabled required type="text" class="form-control" name="end" id="end" placeholder="Departure">
</div>
</div>
</form>
`;
attention.custom({
title: "Choose your dates",
msg: html,
willOpen: () => {
const elem = document.getElementById('reservation-dates-modal')
const rp = new DateRangePicker(elem, {
"format": "yyyy-mm-dd",
showOnFocus: true,
minDate: new Date(),
});
},
didOpen: () => {
return [
document.getElementById('start').removeAttribute("disabled"),
document.getElementById('end').removeAttribute("disabled"),
]
},
callback: (result) => {
const formElem = document.getElementById("check-availability-form");
let formData = new FormData(formElem);
formData.append("csrf_token", "{{.CSRFToken}}");
formData.append("room_id", id)
fetch('/availability-json', {
method: "post",
body: formData,
})
.then(response => response.json())
.then(data => {
if (data.ok) {
console.log("room is available")
attention.custom({
icon: "success",
// TODO: use the default ok button instead
msg: '<p>Room is available</p>' +
'<p><a href="/book-room?id=' +
data.room_id +
'&s=' +
data.start_date +
'&e=' +
data.end_date +
'" class="btn btn-primary">' +
'Book now!</a></p>',
showConfirmButton: false,
});
} else {
console.log("room is not available")
attention.error({
msg: "No availability"
});
}
})
},
});
});
}

View File

@ -77,44 +77,10 @@
<script src="https://cdn.jsdelivr.net/npm/vanillajs-datepicker@1.3.4/dist/js/datepicker-full.min.js"></script>
<script src="https://unpkg.com/notie"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="/static/js/app.js"></script>
<script>
(() => {
'use strict'
// Fetch all the forms we want to apply custom Bootstrap validation styles to
const forms = document.querySelectorAll('.needs-validation')
// Loop over them and prevent submission
Array.from(forms).forEach(form => {
form.addEventListener('submit', event => {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false)
})
})()
// notie
function notify(msg, msgType) {
notie.alert({
type: msgType,
text: msg,
});
}
// sweetalert2
function notifyModal(title, text, icon, confirmButtonText) {
Swal.fire({
title: title,
text: text,
icon: icon,
confirmButtonText: confirmButtonText
})
}
let attention = Prompt()
{{with .Error}}
notify("{{.}}", "error")
@ -126,114 +92,6 @@
notify("{{.}}", "flash")
{{end}}
// Prompt is out Javascript module for all alerts, notifications, and custom popup dialogs
function Prompt() {
let toast = function (c) {
const {
msg = "",
icon = "success",
position = "top-end",
} = c
const Toast = Swal.mixin({
toast: true,
title: msg,
position: position,
icon: icon,
showConfirmButton: false,
timer: 3000,
timerProgressBar: true,
didOpen: (toast) => {
toast.addEventListener('mouseenter', Swal.stopTimer)
toast.addEventListener('mouseleave', Swal.resumeTimer)
}
})
Toast.fire({})
}
let success = function (c) {
const {
msg = "",
title = "",
footer = "",
} = c
Swal.fire({
icon: 'success',
title: title,
text: msg,
footer: footer,
})
}
let error = function (c) {
const {
msg = "",
title = "",
footer = "",
} = c
Swal.fire({
icon: 'error',
title: title,
text: msg,
footer: footer,
})
}
async function custom(c) {
const {
icon = "",
msg = "",
title = "",
showConfirmButton = true,
} = c;
const {value: result} = await Swal.fire({
icon: icon,
title: title,
html: msg,
backdrop: false,
focusConfirm: false,
showCancelButton: true,
showConfirmButton: showConfirmButton,
willOpen: () => {
if (c.willOpen !== undefined) {
c.willOpen();
}
},
didOpen: () => {
if (c.didOpen !== undefined) {
c.didOpen();
}
},
preConfirm: () => {
return [
document.getElementById('start').value,
document.getElementById('end').value,
]
},
})
if (c.callback !== undefined) {
if (result &&
result.dismiss !== Swal.DismissReason.cancel &&
result !== "") {
c.callback(result);
} else {
c.callback(false);
}
}
}
return {
toast: toast,
success: success,
error: error,
custom: custom,
}
}
</script>
{{block "js" .}}

View File

@ -30,75 +30,6 @@
{{end}}
{{define "js"}}
<script>
document.getElementById("check-availability").addEventListener('click', () => {
let html = `
<form id="check-availability-form" action="/availability-json" method="post" novalidate class="needs-validation">
<div id="reservation-dates-modal" class="row">
<div class="col mb-3">
<input disabled required type="text" class="form-control" name="start" id="start" placeholder="Arrival">
</div>
<div class="col mb-3">
<input disabled required type="text" class="form-control" name="end" id="end" placeholder="Departure">
</div>
</div>
</form>
`;
let attention = Prompt()
attention.custom({
title: "Choose your dates",
msg: html,
willOpen: () => {
const elem = document.getElementById('reservation-dates-modal')
const rp = new DateRangePicker(elem, {
"format": "yyyy-mm-dd",
showOnFocus: true,
minDate: new Date(),
});
},
didOpen: () => {
return [
document.getElementById('start').removeAttribute("disabled"),
document.getElementById('end').removeAttribute("disabled"),
]
},
callback: (result) => {
const formElem = document.getElementById("check-availability-form");
let formData = new FormData(formElem);
formData.append("csrf_token", "{{.CSRFToken}}");
formData.append("room_id", "1")
fetch('/availability-json', {
method: "post",
body: formData,
})
.then(response => response.json())
.then(data => {
if (data.ok) {
console.log("room is available")
attention.custom({
icon: "success",
// TODO: use the default ok button instead
msg: '<p>Room is available</p>' +
'<p><a href="/book-room?id=' +
data.room_id +
'&s=' +
data.start_date +
'&e=' +
data.end_date +
'" class="btn btn-primary">' +
'Book now!</a></p>',
showConfirmButton: false,
});
} else {
console.log("room is not available")
attention.error({
msg: "No availability"
});
}
})
},
});
});
roomReservation("1");
</script>
{{end}}

View File

@ -30,20 +30,6 @@
{{end}}
{{define "js"}}
<script>
document.getElementById("check-availability").addEventListener('click', () => {
let html = `
<form action="reservation.html" method="get" novalidate class="needs-validation">
<div id="reservation-dates-modal" class="row">
<div class="col mb-3">
<input disabled required type="text" class="form-control" name="start" id="start" placeholder="Arrival">
</div>
<div class="col mb-3">
<input disabled required type="text" class="form-control" name="end" id="end" placeholder="Departure">
</div>
</div>
</form>
`;
Prompt().custom({title: "Choose your dates", msg: html})
});
roomReservation("2");
</script>
{{end}}