-
Notifications
You must be signed in to change notification settings - Fork 4
Implement sign-up page #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
| }); |
| 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; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| export const PageProxy = { | ||
| redirectToAdults() { | ||
| location.href += '/adults'; | ||
| }, | ||
|
|
||
| redirectToTeens() { | ||
| location.href += '/teens'; | ||
| }, | ||
|
|
||
| showWarning() { | ||
| document.querySelector('.warning').classList.remove('hidden'); | ||
| document.querySelector('.warning').classList.add('visible'); | ||
| }, | ||
| }; | ||
| 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| }); | ||
| }, | ||
| }; | ||
| 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; | ||
| }, | ||
| }; |
There was a problem hiding this comment.
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.