Skip to content

Commit

Permalink
add success screen on form submit OpenClassrooms-Student-Center#4
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingirorhaun committed Apr 22, 2024
1 parent ba38565 commit 6a4999e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
8 changes: 8 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ <h1 class="hero-headline">
value="C'est parti"
/>
</form>
<div id="success-screen">
<h2>Merci pour votre inscription</h2>
<input
class="btn-submit button btn-close"
type="button"
value="Fermer"
/>
</div>
</div>
</div>
</div>
Expand Down
19 changes: 19 additions & 0 deletions modal.css
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ main {
margin: 5% auto;
width: 100%;
max-width: 500px;
height: 767px;
animation-name: modalopen;
animation-duration: var(--modal-duration);
background: #232323;
Expand All @@ -191,6 +192,7 @@ main {
.modal-body {
padding: 15px 8%;
margin: 15px auto;
height: 100%;
}

label {
Expand All @@ -213,6 +215,23 @@ input {
font-size: 20px;
height: 48px;
}

#success-screen {
display: none;
flex-direction: column;
height: 100%;
padding: 30px 0;
background-color: #232323;
}

#success-screen h2 {
font-size: 36px;
font-weight: 400;
margin: auto;
padding: 0 73px;
text-align: center;
}

.formData[data-error]::after {
content: attr(data-error);
font-size: 0.4em;
Expand Down
43 changes: 41 additions & 2 deletions modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ const modalShowBtn = document.querySelectorAll(".modal-btn");
const form = document.getElementsByTagName("form")[0];
const formData = document.querySelectorAll(".formData");
const modalHideBtn = document.getElementsByClassName("close")[0];
const successScreenHideBtn = document.querySelector("#success-screen .btn-close");

// Register event listeners
//
// show/hide modal
modalShowBtn.forEach((btn) => btn.addEventListener("click", launchModal));
modalHideBtn?.addEventListener("click", hideModal);
successScreenHideBtn?.addEventListener("click", hideModal);
// form submit event
form.addEventListener("submit", validate);

Expand All @@ -30,6 +32,7 @@ function launchModal() {
// hide the modal
function hideModal() {
modalbg.style.display = "none";
hideSuccessScreen();
}


Expand Down Expand Up @@ -73,7 +76,6 @@ function validate(e) {

// birthday validation
let birthday = formData[3].getElementsByTagName("input")[0].value;
console.log(birthday);
if (birthday === "") {
showValidationError(formData[3], "Veuillez sélectionner une date");
} else {
Expand Down Expand Up @@ -111,7 +113,7 @@ function validate(e) {
// submit the form if there are no validation errors
let visibleErrors = document.querySelectorAll("[data-error-visible=true]");
if (visibleErrors.length === 0) {
form.submit();
submitForm(form);
}
}

Expand All @@ -133,3 +135,40 @@ function showValidationError(element, errorMessage) {
function hideValidationError(element) {
element.setAttribute("data-error-visible", false);
}

// submit the form
async function submitForm(form) {
const data = new FormData(form);
const url = ""; // to be updated

try {
const response = await fetch(url, {
method: "POST",
body: data
});

// The following code should be re-enabled once we have a proper endpoint to receive the POST request

// if (!response.ok) {
// throw new Error("Fetch error");
// } else {
showSuccessScreen();
// }

} catch (error) {
console.error(error.message);
}
}


// show success screen
function showSuccessScreen() {
form.style.display = "none";
document.getElementById("success-screen").style.display = "flex";
}

//hide success screen
function hideSuccessScreen() {
form.style.display = "block";
document.getElementById("success-screen").style.display = "none";
}

0 comments on commit 6a4999e

Please sign in to comment.