|
| 1 | +import { action } from '@ember/object'; |
| 2 | +import { debounce } from '@ember/runloop'; |
| 3 | +import Component from '@glimmer/component'; |
| 4 | +import { tracked } from '@glimmer/tracking'; |
| 5 | +import { JOIN_DEBOUNCE_TIME } from '../../constants/join'; |
| 6 | +import { validateWordCount } from '../../utils/validator'; |
| 7 | +import { scheduleOnce } from '@ember/runloop'; |
| 8 | +import { getLocalStorageItem, setLocalStorageItem } from '../../utils/storage'; |
| 9 | + |
| 10 | +export default class BaseStepComponent extends Component { |
| 11 | + stepValidation = {}; |
| 12 | + |
| 13 | + @tracked data = {}; |
| 14 | + @tracked errorMessage = {}; |
| 15 | + @tracked wordCount = {}; |
| 16 | + |
| 17 | + get storageKey() { |
| 18 | + return ''; |
| 19 | + } |
| 20 | + |
| 21 | + postLoadInitialize() {} |
| 22 | + |
| 23 | + constructor(...args) { |
| 24 | + super(...args); |
| 25 | + scheduleOnce('afterRender', this, this.initializeFormState); |
| 26 | + } |
| 27 | + |
| 28 | + initializeFormState() { |
| 29 | + let saved = {}; |
| 30 | + try { |
| 31 | + const stored = getLocalStorageItem(this.storageKey, '{}'); |
| 32 | + saved = stored ? JSON.parse(stored) : {}; |
| 33 | + } catch (e) { |
| 34 | + console.warn('Failed to parse stored form data:', e); |
| 35 | + saved = {}; |
| 36 | + } |
| 37 | + this.data = saved; |
| 38 | + |
| 39 | + this.errorMessage = Object.fromEntries( |
| 40 | + Object.keys(this.stepValidation).map((k) => [k, '']), |
| 41 | + ); |
| 42 | + |
| 43 | + this.wordCount = Object.fromEntries( |
| 44 | + Object.keys(this.stepValidation).map((k) => { |
| 45 | + let val = String(this.data[k] || ''); |
| 46 | + return [k, val.trim().split(/\s+/).filter(Boolean).length || 0]; |
| 47 | + }), |
| 48 | + ); |
| 49 | + |
| 50 | + this.postLoadInitialize(); |
| 51 | + |
| 52 | + const valid = this.isDataValid(); |
| 53 | + this.args.setIsPreValid(valid); |
| 54 | + setLocalStorageItem('isValid', String(valid)); |
| 55 | + } |
| 56 | + |
| 57 | + @action inputHandler(e) { |
| 58 | + if (!e?.target) return; |
| 59 | + this.args.setIsPreValid(false); |
| 60 | + const field = e.target.name; |
| 61 | + const value = e.target.value; |
| 62 | + debounce(this, this.handleFieldUpdate, field, value, JOIN_DEBOUNCE_TIME); |
| 63 | + } |
| 64 | + |
| 65 | + validateField(field, value) { |
| 66 | + const limits = this.stepValidation[field]; |
| 67 | + const fieldType = limits?.type || 'text'; |
| 68 | + |
| 69 | + if (fieldType === 'select' || fieldType === 'dropdown') { |
| 70 | + const hasValue = value && String(value).trim().length > 0; |
| 71 | + return { isValid: hasValue }; |
| 72 | + } |
| 73 | + return validateWordCount(value, limits); |
| 74 | + } |
| 75 | + |
| 76 | + isDataValid() { |
| 77 | + for (const field of Object.keys(this.stepValidation)) { |
| 78 | + const result = this.validateField(field, this.data[field]); |
| 79 | + if (!result.isValid) return false; |
| 80 | + } |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + handleFieldUpdate(field, value) { |
| 85 | + this.updateFieldValue(field, value); |
| 86 | + const result = this.validateField(field, value); |
| 87 | + this.updateWordCount(field, result); |
| 88 | + this.updateErrorMessage(field, result); |
| 89 | + this.syncFormValidity(); |
| 90 | + } |
| 91 | + |
| 92 | + updateFieldValue(field, value) { |
| 93 | + this.data = { ...this.data, [field]: value }; |
| 94 | + setLocalStorageItem(this.storageKey, JSON.stringify(this.data)); |
| 95 | + } |
| 96 | + |
| 97 | + updateWordCount(field, result) { |
| 98 | + const wordCount = result.wordCount ?? 0; |
| 99 | + this.wordCount = { ...this.wordCount, [field]: wordCount }; |
| 100 | + } |
| 101 | + |
| 102 | + updateErrorMessage(field, result) { |
| 103 | + this.errorMessage = { |
| 104 | + ...this.errorMessage, |
| 105 | + [field]: this.formatError(field, result), |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + formatError(field, result) { |
| 110 | + const limits = this.stepValidation[field]; |
| 111 | + if (result.isValid) return ''; |
| 112 | + |
| 113 | + const fieldType = limits?.type || 'text'; |
| 114 | + if (fieldType === 'select' || fieldType === 'dropdown') { |
| 115 | + return 'Please choose an option'; |
| 116 | + } |
| 117 | + if (result.remainingToMin) { |
| 118 | + return `At least ${result.remainingToMin} more word(s) required`; |
| 119 | + } |
| 120 | + return `Maximum ${limits?.max ?? 'N/A'} words allowed`; |
| 121 | + } |
| 122 | + |
| 123 | + syncFormValidity() { |
| 124 | + const allValid = this.isDataValid(); |
| 125 | + this.args.setIsValid(allValid); |
| 126 | + setLocalStorageItem('isValid', String(allValid)); |
| 127 | + } |
| 128 | +} |
0 commit comments