Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion src/pages/sign-up-index-page/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,64 @@
</head>
<body>
{{> ../../header/header pageRoute='anketa'}}
<main>sign up first page</main>
<main>
<div class="first-form">
<h2>{{ htmlWebpackPlugin.options.content.translations.ff_h }}</h2>
<form>
<div class="warning hidden">
{{ htmlWebpackPlugin.options.content.translations.ff_you_are_too_young }}
</div>
<div class="info-input">
<label for="name" class="question">
{{ htmlWebpackPlugin.options.content.translations.ff_name
}}<span class="important">*</span>
</label>
<input
type="text"
name="name"
id="name"
placeholder="{{ htmlWebpackPlugin.options.content.translations.ff_name_pl }}"
pattern="^[\w'&quot;а-яА-ЯёЁіІїЇєЄґҐ\-.,:]+"
required
/>
</div>

<div class="info-input">
<label for="surname" class="question">
{{ htmlWebpackPlugin.options.content.translations.ff_surname
}}<span class="important">*</span>
</label>
<input
type="text"
name="surname"
id="surname"
placeholder="{{ htmlWebpackPlugin.options.content.translations.ff_surname_pl }}"
pattern="^[\w'&quot;а-яА-ЯёЁіІїЇєЄґҐ\-.,:]+"
required
/>
</div>
<div class="info-input">
<label for="date" class="question"
>{{ htmlWebpackPlugin.options.content.translations.ff_birthday
}}<span class="important">*</span></label
>
<input
type="date"
id="date"
name="birthday"
min="1920-12-31"
value="2000-01-31"
required
/>
</div>
<div class="row pa center">
<button class="button-primary">
{{ htmlWebpackPlugin.options.content.translations.ff_submit }}
</button>
</div>
</form>
</div>
</main>
{{> ../../footer/footer}}
</body>
</html>
15 changes: 15 additions & 0 deletions src/pages/sign-up-index-page/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
import './index.scss';
import { StudentStorage } from './student-storage';
import { StudentForm } from './student-form';
import { PageProxy } from './page-proxy';

const minAdultsAge = 17;
const minTeensAge = 12;

StudentForm.data = StudentStorage.data;
StudentForm.onChange((data) => (StudentStorage.data = data));
StudentForm.onSubmit(() => {
if (StudentForm.age >= minAdultsAge) return PageProxy.redirectToAdults();
if (StudentForm.age >= minTeensAge) return PageProxy.redirectToTeens();

PageProxy.showWarning();
});
73 changes: 73 additions & 0 deletions src/pages/sign-up-index-page/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
.first-form {
background-color: white;
border: 2px solid rgba(black, 0.08);
max-width: 550px;
padding: 20px 30px;
margin: 45px auto 50px;
box-sizing: border-box;

h2 {
text-align: center;
}

.warning {
background-color: #fafad2;
color: #333;
font-size: 16px;
padding: 10px 15px;
margin-bottom: 16px;

&.hidden {
display: none;
}

&.visible {
display: block;
}
}

.info-input {
margin-bottom: 32px;

label {
display: block;
width: 100%;
color: rgba(black, 0.87);
text-align: left;

.important {
color: #e74c3c;
}
}

input {
display: block;
width: 100%;
border: 2px solid rgba(black, 0.08);
font-family: 'Open Sans', sans-serif;
margin: 10px 0;
min-height: 48px;
appearance: inherit !important;
color: rgba(black, 0.87);
font-size: inherit;
padding: 1px 5px;

&::placeholder {
color: #ccc;
}
}
}

.pa {
padding: 35px;
display: flex;
justify-content: center;
flex-direction: row;

button {
font-size: 18px;
line-height: 20px;
padding: 10px 20px;
}
}
}
14 changes: 14 additions & 0 deletions src/pages/sign-up-index-page/page-proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const PageProxy = {
redirectToAdults() {
location.href += '/adults';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here might be some problems if we want to reuse this class for form on adults landing page, which has url /courses/adults, so I'd suggest setting full path here, instead of adding.

},

redirectToTeens() {
location.href += '/teens';
},

showWarning() {
document.querySelector('.warning').classList.remove('hidden');
document.querySelector('.warning').classList.add('visible');
},
};
51 changes: 51 additions & 0 deletions src/pages/sign-up-index-page/student-form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const formRef = document.querySelector('form');
const getInputRef = (name) => formRef.querySelector(`[name="${name}"]`);

export const StudentForm = {
get formData() {
return new FormData(formRef);
},

get data() {
return {
name: this.formData.get('name'),
surname: this.formData.get('surname'),
birthday: this.formData.get('birthday'),
};
},

set data({ name, surname, birthday }) {
getInputRef('name').value = name;
getInputRef('surname').value = surname;
getInputRef('birthday').value = birthday;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line overrides the default value from input provided in HTML, can you please check here for the birthday variable not to have empty value?

},

get age() {
const birthDate = this.data.birthday;
const currentDate = new Date();
const [currentYear, currentMonth, currentDay] = [
currentDate.getFullYear(),
currentDate.getMonth(),
currentDate.getDate(),
];
const [birthYear, birthMonth, birthDay] = birthDate.split('-').map((item) => +item);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be risk at some moment for the format of this value to be changed, so I'd suggest using Date constructor instead and calling methods getFullYear etc.

let calculatedAge = currentYear - birthYear;

if (currentMonth < birthMonth - 1) calculatedAge--;

if (birthMonth - 1 === currentMonth && currentDay < birthDay) calculatedAge--;

return calculatedAge;
},

onChange(action) {
formRef.addEventListener('input', () => action(this.data));
},

onSubmit(action) {
formRef.addEventListener('submit', (event) => {
event.preventDefault();
action();
});
},
};
21 changes: 21 additions & 0 deletions src/pages/sign-up-index-page/student-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const key = 'anketa.student';
const storage = localStorage;
const initialData = { name: '', surname: '', birthday: '' };

export const StudentStorage = {
get data() {
const savedData = storage.getItem(key);

return JSON.parse(savedData) ?? initialData;
},

set data(data) {
const serializedData = JSON.stringify(data);

storage.setItem(key, serializedData);
},

reset() {
this.data = initialData;
},
};
2 changes: 2 additions & 0 deletions src/pages/sign-up-success-page/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './index.scss';
import { StudentStorage } from '../sign-up-index-page/student-storage';

const pageLocalStorageKey = 'anketa/last';
const pageVisitedKey = `${pageLocalStorageKey}.isVisited`;
Expand All @@ -15,6 +16,7 @@ function handleRedirects() {
}

document.querySelector('.content .email').innerHTML = `(${studentEmail})`;
StudentStorage.reset();

if (!localStorage.getItem(pageVisitedKey)) {
localStorage.setItem(pageVisitedKey, 'true');
Expand Down