|
| 1 | +const envList = helper.getElement('.envList') |
| 2 | +const environmentVariables: EnvironmentItem[] = [] |
| 3 | +const fullDomain = window.location.pathname.split('/').pop() |
| 4 | +document.getElementById('full-domain').innerText = fullDomain |
| 5 | + |
| 6 | +class EnvironmentItem { |
| 7 | + name: string |
| 8 | + value: string |
| 9 | + isValid: boolean |
| 10 | + |
| 11 | + private listItemElement: HTMLLIElement |
| 12 | + private nameInputElement: HTMLInputElement |
| 13 | + private valueInputElement: HTMLInputElement |
| 14 | + private removeButtonElement: HTMLButtonElement |
| 15 | + |
| 16 | + private readonly IS_INVALID = 'is-invalid' |
| 17 | + private readonly LETTER_NUMBER_REGEX = /^[A-Z0-9_]+$/g |
| 18 | + private readonly ELEMENT_HTML = ` |
| 19 | + <div class="input-group"> |
| 20 | + <input |
| 21 | + type="text" |
| 22 | + aria-label="Name" |
| 23 | + class="form-control text-uppercase text-monospace name-input" |
| 24 | + placeholder="NAME" |
| 25 | + > |
| 26 | + <div class="input-group-prepend input-group-append"> |
| 27 | + <span class="input-group-text">=</span> |
| 28 | + </div> |
| 29 | + <input |
| 30 | + type="text" |
| 31 | + aria-label="Value" |
| 32 | + class="form-control text-monospace value-input" |
| 33 | + placeholder="Value" |
| 34 | + > |
| 35 | + <div class="input-group-append"> |
| 36 | + <button |
| 37 | + class="btn btn-outline-danger remove-button" |
| 38 | + type="button" |
| 39 | + > |
| 40 | + Remove |
| 41 | + </button> |
| 42 | + </div> |
| 43 | + <div class="invalid-feedback"> |
| 44 | + Name must contain only letters, number, or underscore. |
| 45 | + </div> |
| 46 | + </div> |
| 47 | + ` |
| 48 | + |
| 49 | + constructor(name?: string, value?: string) { |
| 50 | + this.name = name |
| 51 | + this.value = value |
| 52 | + this.createElement() |
| 53 | + this.setupEventListeners() |
| 54 | + if (name && value) { |
| 55 | + this.nameInputElement.value = this.name |
| 56 | + this.valueInputElement.value = this.value |
| 57 | + this.setValid(true) |
| 58 | + } else { |
| 59 | + this.setValid(false) |
| 60 | + } |
| 61 | + this.nameInputElement.focus() |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Sets the valid status for the object and updates the input DOM element. |
| 66 | + * @param isValid |
| 67 | + */ |
| 68 | + private setValid(isValid: boolean): void { |
| 69 | + if (isValid === this.isValid) return |
| 70 | + this.isValid = isValid |
| 71 | + if (isValid) this.nameInputElement.classList.remove(this.IS_INVALID) |
| 72 | + else this.nameInputElement.classList.add(this.IS_INVALID) |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Create the list element and append to the DOM |
| 77 | + */ |
| 78 | + private createElement(): void { |
| 79 | + this.listItemElement = document.createElement('li') |
| 80 | + this.listItemElement.innerHTML = this.ELEMENT_HTML |
| 81 | + this.listItemElement.classList.add( |
| 82 | + 'list-group-item', |
| 83 | + 'd-flex', |
| 84 | + 'align-items-center' |
| 85 | + ) |
| 86 | + envList.appendChild(this.listItemElement) |
| 87 | + this.nameInputElement = this.listItemElement.querySelector('.name-input') |
| 88 | + this.valueInputElement = this.listItemElement.querySelector('.value-input') |
| 89 | + this.removeButtonElement = this.listItemElement.querySelector( |
| 90 | + '.remove-button' |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Setup the event listeners for inputs and button |
| 96 | + */ |
| 97 | + private setupEventListeners(): void { |
| 98 | + this.nameInputElement.addEventListener('input', () => |
| 99 | + this.validateAndSetName(this.nameInputElement.value) |
| 100 | + ) |
| 101 | + this.valueInputElement.addEventListener( |
| 102 | + 'input', |
| 103 | + () => (this.value = this.valueInputElement.value) |
| 104 | + ) |
| 105 | + this.removeButtonElement.addEventListener('click', () => |
| 106 | + this.removeElement() |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Validates the variable name with regex and sets the field if it's valid |
| 112 | + * @param value input value |
| 113 | + */ |
| 114 | + private validateAndSetName(value: string): void { |
| 115 | + const upperCaseValue = value.toUpperCase() |
| 116 | + if (upperCaseValue.match(this.LETTER_NUMBER_REGEX)) { |
| 117 | + this.name = upperCaseValue |
| 118 | + this.setValid(true) |
| 119 | + } else { |
| 120 | + this.setValid(false) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Removes the list item element from the DOM and the items list. |
| 126 | + * @param element list item element |
| 127 | + */ |
| 128 | + private removeElement(): void { |
| 129 | + this.listItemElement.remove() |
| 130 | + environmentVariables.splice(environmentVariables.indexOf(this), 1) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +document.getElementById('addEnvButton').onclick = (): void => { |
| 135 | + environmentVariables.push(new EnvironmentItem()) |
| 136 | +} |
| 137 | + |
| 138 | +document.getElementById('submitEnvButton').onclick = (): void => { |
| 139 | + // check if there's at least one variable to submit |
| 140 | + if (environmentVariables.length === 0) { |
| 141 | + return alert('Error: add at least one variable before submitting.') |
| 142 | + } |
| 143 | + // check if all variables are valid |
| 144 | + if (environmentVariables.some(env => !env.isValid)) { |
| 145 | + return alert( |
| 146 | + 'Error: some fields are not valid, please fix them before submitting.' |
| 147 | + ) |
| 148 | + } |
| 149 | + // confirm if it's OK to restart the app and send the request |
| 150 | + if ( |
| 151 | + confirm( |
| 152 | + 'Submitting will recreate the container for your app.' + |
| 153 | + 'All data not saved inside the main app folder will be lost. Is this OK?' |
| 154 | + ) |
| 155 | + ) { |
| 156 | + // convert to the request body format |
| 157 | + // { variables: { NAME: value, ... }} |
| 158 | + const body = environmentVariables.reduce((acc, { name, value }) => { |
| 159 | + acc[name] = value |
| 160 | + return acc |
| 161 | + }, {}) |
| 162 | + // send the PUT request |
| 163 | + // show alert with error if request is not successful |
| 164 | + fetch(`/api/mappings/${fullDomain}/environment`, { |
| 165 | + method: 'PUT', |
| 166 | + headers: { |
| 167 | + 'Content-Type': 'application/json' |
| 168 | + }, |
| 169 | + body: JSON.stringify({ variables: body }) |
| 170 | + }) |
| 171 | + .then(response => (response.ok ? {} : response.json())) |
| 172 | + .then((body: ContainerResponse) => |
| 173 | + body.message |
| 174 | + ? alert(`ERROR: ${body.message}`) |
| 175 | + : window.location.reload() |
| 176 | + ) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +// Populate the list with existing environment variables |
| 181 | +fetch(`/api/mappings/${fullDomain}/environment`) |
| 182 | + .then(r => r.json()) |
| 183 | + .then(({ variables }) => { |
| 184 | + Object.entries(variables).forEach(([name, value]) => { |
| 185 | + environmentVariables.push( |
| 186 | + new EnvironmentItem(name as string, value as string) |
| 187 | + ) |
| 188 | + }) |
| 189 | + }) |
0 commit comments