|
| 1 | +import Component from '../../../helpers/Component' |
| 2 | + |
| 3 | +import TextField from '../../../imports/materialdesign/components/TextField' |
| 4 | +import MaterialButton from '../../../imports/materialdesign/components/MaterialButton' |
| 5 | +import Preloader from '../../../imports/materialdesign/components/Preloader' |
| 6 | + |
| 7 | +export default class ManageAccountPage extends Component { |
| 8 | + beforeRender () { |
| 9 | + this.pageData = { |
| 10 | + title: 'Zarządzaj kontem', |
| 11 | + url: 'manageaccount', |
| 12 | + loaded: false |
| 13 | + } |
| 14 | + |
| 15 | + this.isEdited = false |
| 16 | + this.avatar = null |
| 17 | + this.canShowButtons = false |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Gets root. |
| 22 | + * @return {DOMElement} root |
| 23 | + */ |
| 24 | + getRoot () { |
| 25 | + return this.elements.root |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Loads categories. |
| 30 | + */ |
| 31 | + load () { |
| 32 | + const self = this |
| 33 | + |
| 34 | + const app = window.app |
| 35 | + |
| 36 | + setTimeout(function () { |
| 37 | + app.togglePreloader(false) |
| 38 | + app.pagesData.loading = false |
| 39 | + self.canShowButtons = true |
| 40 | + }, 10) |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * On user logs event. |
| 45 | + */ |
| 46 | + onUserLog () { |
| 47 | + this.setForm() |
| 48 | + } |
| 49 | + |
| 50 | + setForm () { |
| 51 | + const app = window.app |
| 52 | + const accountInfo = app.accountInfo |
| 53 | + |
| 54 | + this.avatar = accountInfo.avatar |
| 55 | + this.elements.avatar.style.backgroundImage = 'url(' + accountInfo.avatar + ')' |
| 56 | + this.elements.login.setValue(accountInfo.login) |
| 57 | + this.elements.name.setValue(accountInfo.userName) |
| 58 | + this.elements.email.setValue(accountInfo.email) |
| 59 | + this.elements.password.setValue('') |
| 60 | + this.elements.passwordVerify.setValue('') |
| 61 | + } |
| 62 | + |
| 63 | + onTextFieldInput = (e, textField) => { |
| 64 | + if (this.canShowButtons) { |
| 65 | + this.isEdited = true |
| 66 | + this.toggleButtons(true) |
| 67 | + |
| 68 | + if (textField.error) textField.toggleError(false) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Shows or hides action buttons. (save and cancel) |
| 74 | + * @param {Boolean} |
| 75 | + */ |
| 76 | + toggleButtons (flag) { |
| 77 | + const buttonsContainer = this.elements.buttonsContainer |
| 78 | + |
| 79 | + buttonsContainer.style.height = ((flag) ? buttonsContainer.scrollHeight : 0) + 'px' |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Shows or hides preloader. |
| 84 | + * @param {Boolean} |
| 85 | + */ |
| 86 | + togglePreloader (flag) { |
| 87 | + const container = this.elements.preloaderContainer |
| 88 | + |
| 89 | + container.style[(flag) ? 'display' : 'opacity'] = (flag) ? 'block' : '0' |
| 90 | + |
| 91 | + setTimeout(function () { |
| 92 | + container.style[(flag) ? 'opacity' : 'display'] = (flag) ? '1' : 'none' |
| 93 | + }, (flag) ? 20 : 300) |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * On save button click. |
| 98 | + * Saves settings. |
| 99 | + * @param {Event} |
| 100 | + */ |
| 101 | + onSaveButtonClick = (e) => { |
| 102 | + const self = this |
| 103 | + |
| 104 | + const app = window.app |
| 105 | + |
| 106 | + this.canShowButtons = false |
| 107 | + |
| 108 | + if (this.verifyData()) { |
| 109 | + this.toggleButtons(false) |
| 110 | + this.togglePreloader(true) |
| 111 | + |
| 112 | + app.accountInfo.avatar = this.avatar |
| 113 | + app.accountInfo.login = this.elements.login.getValue() |
| 114 | + app.accountInfo.userName = this.elements.name.getValue() |
| 115 | + app.accountInfo.email = this.elements.email.getValue() |
| 116 | + |
| 117 | + console.log(app.accountInfo) |
| 118 | + |
| 119 | + setTimeout(function () { |
| 120 | + self.togglePreloader(false) |
| 121 | + self.canShowButtons = true |
| 122 | + }, 1000) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * On cancel button click. |
| 128 | + * Backs settings. |
| 129 | + * @param {Event} |
| 130 | + */ |
| 131 | + onCancelButtonClick = (e) => { |
| 132 | + this.setForm() |
| 133 | + this.toggleButtons(false) |
| 134 | + } |
| 135 | + |
| 136 | + verifyData () { |
| 137 | + const textFields = [ |
| 138 | + this.elements.login, |
| 139 | + this.elements.name, |
| 140 | + this.elements.email |
| 141 | + ] |
| 142 | + |
| 143 | + let correct = true |
| 144 | + |
| 145 | + for (var i = 0; i < textFields.length; i++) { |
| 146 | + if (!this.verifyTextField(textFields[i])) correct = false |
| 147 | + } |
| 148 | + |
| 149 | + const password = this.elements.password.getValue() |
| 150 | + const passwordVerifyTextField = this.elements.passwordVerify |
| 151 | + const passwordVerify = passwordVerifyTextField.getValue() |
| 152 | + |
| 153 | + if (password.length >= 1) { |
| 154 | + if (password !== passwordVerify) { |
| 155 | + passwordVerifyTextField.toggleError(true) |
| 156 | + return false |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return correct |
| 161 | + } |
| 162 | + |
| 163 | + verifyTextField (textField) { |
| 164 | + if (textField.getValue().length < 1) { |
| 165 | + textField.toggleError(true) |
| 166 | + return false |
| 167 | + } |
| 168 | + |
| 169 | + return true |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * On avatar click. |
| 174 | + * Triggers file input dialog. |
| 175 | + * @param {Event} |
| 176 | + */ |
| 177 | + onAvatarClick = (e) => { |
| 178 | + const self = this |
| 179 | + |
| 180 | + const input = document.createElement('input') |
| 181 | + |
| 182 | + input.type = 'file' |
| 183 | + input.accept = 'image/*' |
| 184 | + |
| 185 | + input.addEventListener('change', function (e) { |
| 186 | + let reader = new FileReader() |
| 187 | + |
| 188 | + reader.onload = function (e) { |
| 189 | + const src = e.target.result |
| 190 | + |
| 191 | + self.avatar = src |
| 192 | + self.elements.avatar.style.backgroundImage = 'url(' + src + ')' |
| 193 | + self.toggleButtons(true) |
| 194 | + } |
| 195 | + |
| 196 | + reader.readAsDataURL(input.files[0]) |
| 197 | + }) |
| 198 | + |
| 199 | + setTimeout(function () { |
| 200 | + input.click() |
| 201 | + }, 20) |
| 202 | + } |
| 203 | + |
| 204 | + render () { |
| 205 | + return ( |
| 206 | + <div className='page page-manage-account' ref='root'> |
| 207 | + <div className='page-manage-account-container'> |
| 208 | + <div className='section avatar'> |
| 209 | + <div className='title'> |
| 210 | + Avatar |
| 211 | + </div> |
| 212 | + <div className='control'> |
| 213 | + <div className='avatar' ref='avatar' onClick={this.onAvatarClick} /> |
| 214 | + </div> |
| 215 | + </div> |
| 216 | + <div className='section login'> |
| 217 | + <div className='title'> |
| 218 | + Login |
| 219 | + </div> |
| 220 | + <div className='control'> |
| 221 | + <TextField ref='login' onInput={this.onTextFieldInput} /> |
| 222 | + </div> |
| 223 | + </div> |
| 224 | + <div className='section name'> |
| 225 | + <div className='title'> |
| 226 | + Imię i Nazwisko |
| 227 | + </div> |
| 228 | + <div className='control'> |
| 229 | + <TextField ref='name' onInput={this.onTextFieldInput} /> |
| 230 | + </div> |
| 231 | + </div> |
| 232 | + <div className='section email'> |
| 233 | + <div className='title'> |
| 234 | + E-Mail |
| 235 | + </div> |
| 236 | + <div className='control'> |
| 237 | + <TextField ref='email' onInput={this.onTextFieldInput} /> |
| 238 | + </div> |
| 239 | + </div> |
| 240 | + <div className='section password'> |
| 241 | + <div className='title'> |
| 242 | + Hasło |
| 243 | + </div> |
| 244 | + <div className='control'> |
| 245 | + <TextField ref='password' type='password' onInput={this.onTextFieldInput} /> |
| 246 | + </div> |
| 247 | + </div> |
| 248 | + <div className='section password-verify'> |
| 249 | + <div className='title'> |
| 250 | + Powtórz hasło |
| 251 | + </div> |
| 252 | + <div className='control'> |
| 253 | + <TextField ref='passwordVerify' type='password' onInput={this.onTextFieldInput} /> |
| 254 | + </div> |
| 255 | + </div> |
| 256 | + <div className='buttons-container' ref='buttonsContainer'> |
| 257 | + <MaterialButton text='ZAPISZ' onClick={this.onSaveButtonClick} shadow={false} rippleStyle={{ |
| 258 | + backgroundColor: '#3f51b5', |
| 259 | + opacity: 0.2 |
| 260 | + }} /> |
| 261 | + <MaterialButton className='cancel' onClick={this.onCancelButtonClick} text='ANULUJ' shadow={false} rippleStyle={{ |
| 262 | + backgroundColor: '#000', |
| 263 | + opacity: 0.2 |
| 264 | + }} /> |
| 265 | + </div> |
| 266 | + <div className='preloader-container' ref='preloaderContainer'> |
| 267 | + <Preloader /> |
| 268 | + </div> |
| 269 | + </div> |
| 270 | + </div> |
| 271 | + ) |
| 272 | + } |
| 273 | + |
| 274 | + afterRender () { |
| 275 | + window.app.elementsToCall.push(this) |
| 276 | + } |
| 277 | +} |
0 commit comments