|
| 1 | +import {Url} from '../url/url'; |
| 2 | + |
| 3 | +class AddUserPage { |
| 4 | + constructor(store) { |
| 5 | + this.setStateContact = () => { |
| 6 | + const {setState} = store; |
| 7 | + const initializeState = { |
| 8 | + stateName: 'ADD USER', |
| 9 | + activePage: this.render(), |
| 10 | + }; |
| 11 | + setState(initializeState); |
| 12 | + } |
| 13 | + |
| 14 | + this.serverSide = new Url(); |
| 15 | + } |
| 16 | + |
| 17 | + render() { |
| 18 | + return /*html*/` |
| 19 | + <div id="add-user-page"> |
| 20 | +
|
| 21 | + <header class="container"> |
| 22 | + <div class="row d-flex justify-content-center"> |
| 23 | + <span class="add-user-header">Add new user</span> |
| 24 | + </div> |
| 25 | + </header> |
| 26 | +
|
| 27 | + <main class="container add-user-block"> |
| 28 | + <form> |
| 29 | +
|
| 30 | + <div class="form-group"> |
| 31 | + <label for="fullName-input">Full name:</label> |
| 32 | + <input type="text" name="fullName" class="form-control" id="fullName-input" placeholder="Enter full name" required> |
| 33 | + </div> |
| 34 | +
|
| 35 | + <div class="form-group"> |
| 36 | + <label for="email-input">Email address:</label> |
| 37 | + <input type="email" name="email" class="form-control" id="email-input" placeholder="Enter email" required> |
| 38 | + </div> |
| 39 | +
|
| 40 | + <div class="form-group"> |
| 41 | + <label for="phoneNumber-input">Phone Number:</label> |
| 42 | + <input type="number" name="phone" class="form-control" id="phoneNumber-input" placeholder="Enter phone number" required> |
| 43 | + </div> |
| 44 | +
|
| 45 | + <div class="form-group"> |
| 46 | + <label for="birthDate-input">Birth date:</label> |
| 47 | + <input type="date" name="birthdate" class="form-control" id="birthDate-input"> |
| 48 | + </div> |
| 49 | +
|
| 50 | + <div class="form-group"> |
| 51 | + <label for="address-input">Address:</label> |
| 52 | + <input type="text" name="address" class="form-control" id="address-input" placeholder="Enter address"> |
| 53 | + </div> |
| 54 | +
|
| 55 | + <div class="form-group"> |
| 56 | + <label for="gender-selection">Gender:</label> |
| 57 | + <select name="gender" class="form-control" id="gender-selection"> |
| 58 | + <option>Male</option> |
| 59 | + <option>Female</option> |
| 60 | + </select> |
| 61 | + </div> |
| 62 | + |
| 63 | + <div class="form-group"> |
| 64 | + <label for="avatarUrl-input">Avatar url:</label> |
| 65 | + <input type="text" name="avatarUrl" class="form-control" id="avatarUrl-input" aria-describedby="avatarHelp" placeholder="Enter avatar url"> |
| 66 | + <small id="avatarHelp" class="form-text text-muted">ex: https://cloud-drive/photo123456.</small> |
| 67 | + </div> |
| 68 | +
|
| 69 | + <div class="row d-flex justify-content-center"> |
| 70 | + <button type="submit" class="btn btn-primary">Add user</button> |
| 71 | + </div> |
| 72 | + |
| 73 | + </form> |
| 74 | + </main> |
| 75 | +
|
| 76 | + </div> |
| 77 | + `; |
| 78 | + } |
| 79 | + |
| 80 | + applyListenersForAddUserPage() { |
| 81 | + // const fullNameInput = document.querySelector('#fullName-input'); |
| 82 | + // const emailInput = document.querySelector('#email-input'); |
| 83 | + // const phoneNumberInput = document.querySelector('#phoneNumber-input'); |
| 84 | + // const birthDateInput = document.querySelector('#birthDate-input'); |
| 85 | + // const addressInput = document.querySelector('#address-input'); |
| 86 | + // const genderInput = document.querySelector('#gender-selection'); |
| 87 | + // const avatarUrlInput = document.querySelector('#avatarUrl-input'); |
| 88 | + |
| 89 | + const addUserForm = document.querySelector('form'); |
| 90 | + const inputs = [...addUserForm.elements] |
| 91 | + .filter(elem => elem.tagName === 'INPUT' || elem.tagName === 'SELECT'); |
| 92 | + |
| 93 | + addUserForm.addEventListener('submit', (e) => { |
| 94 | + |
| 95 | + const user = inputs.reduce((newUser, input) => { |
| 96 | + const KEY = input.name; |
| 97 | + const VALUE = input.value; |
| 98 | + |
| 99 | + if(VALUE.lenght !== 0) { |
| 100 | + newUser[KEY] = VALUE; |
| 101 | + } |
| 102 | + if(VALUE === 'Male' || VALUE === 'Female') { |
| 103 | + const firstChar = input.value[0]; |
| 104 | + newUser[KEY] = firstChar; |
| 105 | + } |
| 106 | + return newUser; |
| 107 | + }, {}); |
| 108 | + |
| 109 | + this.serverSide.postUser(user); |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + isValid() { |
| 114 | + |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +export {AddUserPage}; |
0 commit comments