|
| 1 | +<template> |
| 2 | + <form |
| 3 | + class="card mb-4" |
| 4 | + @submit.prevent="saveExternalAppConfiguration" |
| 5 | + > |
| 6 | + <div class="card-body border-bottom"> |
| 7 | + <h3 class="card-title mb-3"> |
| 8 | + External Apps |
| 9 | + </h3> |
| 10 | + |
| 11 | + <div class="form-check form-switch"> |
| 12 | + <label class="form-check-label"> |
| 13 | + <input |
| 14 | + v-model="workspace.externalAppAccess" |
| 15 | + type="checkbox" |
| 16 | + class="form-check-input" |
| 17 | + :true-value="1" |
| 18 | + :false-value="0" |
| 19 | + > |
| 20 | + Publish this workspace for external apps |
| 21 | + </label> |
| 22 | + </div> |
| 23 | + |
| 24 | + <hr> |
| 25 | + |
| 26 | + <h4 class="h5"> |
| 27 | + AVIV ScoutRoute Long Form Quest Definitions |
| 28 | + </h4> |
| 29 | + |
| 30 | + <div class="form-check"> |
| 31 | + <label class="form-check-label"> |
| 32 | + Define quests in Workspaces |
| 33 | + <input |
| 34 | + v-model="longFormQuestType" |
| 35 | + class="form-check-input" |
| 36 | + type="radio" |
| 37 | + name="longFormQuestType" |
| 38 | + value="JSON" |
| 39 | + > |
| 40 | + </label> |
| 41 | + </div> |
| 42 | + <div class="form-check"> |
| 43 | + <label class="form-check-label"> |
| 44 | + Load quest definitions from an external URL |
| 45 | + <input |
| 46 | + v-model="longFormQuestType" |
| 47 | + class="form-check-input" |
| 48 | + type="radio" |
| 49 | + name="longFormQuestType" |
| 50 | + value="URL" |
| 51 | + > |
| 52 | + </label> |
| 53 | + </div> |
| 54 | + |
| 55 | + <template v-if="longFormQuestType === 'JSON'"> |
| 56 | + <label class="d-block form-label mt-3"> |
| 57 | + JSON Quest Definition |
| 58 | + <textarea |
| 59 | + v-model.trim="longFormQuestDef" |
| 60 | + class="form-control" |
| 61 | + :class="{ 'drag-over': isDraggingQuest }" |
| 62 | + rows="5" |
| 63 | + placeholder="Optional" |
| 64 | + @dragover.prevent="isDraggingQuest = true" |
| 65 | + @dragleave.prevent="isDraggingQuest = false" |
| 66 | + @drop.prevent="onQuestFileDrop" |
| 67 | + /> |
| 68 | + </label> |
| 69 | + <div |
| 70 | + id="imagery-help" |
| 71 | + class="form-text" |
| 72 | + > |
| 73 | + Paste the JSON content directly or drag and drop a JSON file. |
| 74 | + See the |
| 75 | + <a |
| 76 | + :href="longFormQuestSchemaUrl" |
| 77 | + target="_blank" |
| 78 | + > |
| 79 | + JSON Schema |
| 80 | + </a> |
| 81 | + for the required format and an |
| 82 | + <a |
| 83 | + :href="longFormQuestExampleUrl" |
| 84 | + target="_blank" |
| 85 | + > |
| 86 | + example |
| 87 | + </a>. |
| 88 | + </div> |
| 89 | + </template> |
| 90 | + |
| 91 | + <template v-else-if="longFormQuestType === 'URL'"> |
| 92 | + <label class="d-block form-label mt-3"> |
| 93 | + Quest Definition URL |
| 94 | + <input |
| 95 | + v-model.trim="longFormQuestUrl" |
| 96 | + type="text" |
| 97 | + class="form-control" |
| 98 | + placeholder="https://..." |
| 99 | + > |
| 100 | + </label> |
| 101 | + <div |
| 102 | + id="imagery-help" |
| 103 | + class="form-text" |
| 104 | + > |
| 105 | + Enter the address of a quest definition JSON document |
| 106 | + See the |
| 107 | + <a |
| 108 | + :href="longFormQuestSchemaUrl" |
| 109 | + target="_blank" |
| 110 | + > |
| 111 | + JSON Schema |
| 112 | + </a> |
| 113 | + for the required format and an |
| 114 | + <a |
| 115 | + :href="longFormQuestExampleUrl" |
| 116 | + target="_blank" |
| 117 | + > |
| 118 | + example |
| 119 | + </a>. |
| 120 | + </div> |
| 121 | + </template> |
| 122 | + |
| 123 | + <div |
| 124 | + v-if="longFormQuestError" |
| 125 | + class="form-text text-danger" |
| 126 | + > |
| 127 | + {{ longFormQuestError }} |
| 128 | + </div> |
| 129 | + |
| 130 | + <hr> |
| 131 | + <button |
| 132 | + type="submit" |
| 133 | + class="btn btn-primary" |
| 134 | + > |
| 135 | + Save |
| 136 | + </button> |
| 137 | + <div |
| 138 | + v-if="externalAppSaveStatus" |
| 139 | + :class="`mt-2 form-text text-${ |
| 140 | + externalAppSaveStatus.type === 'success' ? 'success' : 'danger' |
| 141 | + }`" |
| 142 | + > |
| 143 | + {{ externalAppSaveStatus.message }} |
| 144 | + </div> |
| 145 | + </div><!-- .card-body --> |
| 146 | + </form><!-- .card --> |
| 147 | +</template> |
| 148 | + |
| 149 | +<script setup lang="ts"> |
| 150 | +import { workspacesClient } from '~/services/index'; |
| 151 | +import { handleFileDrop, validateJson } from '~/util/schema'; |
| 152 | +import { isHttpUrl, normalizeUrl } from '~/util/url'; |
| 153 | +
|
| 154 | +import type { Workspace } from '~/types/workspaces'; |
| 155 | +
|
| 156 | +const longFormQuestSchemaUrl = import.meta.env.VITE_LONG_FORM_QUEST_SCHEMA; |
| 157 | +const longFormQuestExampleUrl = import.meta.env.VITE_LONG_FORM_QUEST_EXAMPLE_URL; |
| 158 | +
|
| 159 | +const workspace = inject<Workspace>('workspace')!; |
| 160 | +
|
| 161 | +const [longFormQuestSettings] = await Promise.all([ |
| 162 | + workspacesClient.getLongFormQuestSettings(workspace.id), |
| 163 | +]); |
| 164 | +
|
| 165 | +const longFormQuestSchema = ref<object | undefined>(); |
| 166 | +const longFormQuestType = ref(longFormQuestSettings.type); |
| 167 | +const longFormQuestDef = ref(longFormQuestSettings.definition); |
| 168 | +const longFormQuestUrl = ref(longFormQuestSettings.url); |
| 169 | +const longFormQuestError = ref<string | null>(null); |
| 170 | +const externalAppSaveStatus = ref<{ type: 'success' | 'error'; message: string } | null>(null); |
| 171 | +const isDraggingQuest = ref(false); |
| 172 | +
|
| 173 | +watch( |
| 174 | + [ |
| 175 | + longFormQuestType, |
| 176 | + longFormQuestDef, |
| 177 | + longFormQuestUrl, |
| 178 | + () => workspace.externalAppAccess, |
| 179 | + ], |
| 180 | + () => { clearExternalAppMessages(); }, |
| 181 | +); |
| 182 | +
|
| 183 | +function clearExternalAppMessages() { |
| 184 | + longFormQuestError.value = null; |
| 185 | + externalAppSaveStatus.value = null; |
| 186 | +} |
| 187 | +
|
| 188 | +function onQuestFileDrop(event: DragEvent) { |
| 189 | + handleFileDrop(event, longFormQuestDef, isDraggingQuest); |
| 190 | +} |
| 191 | +
|
| 192 | +async function saveExternalAppConfiguration() { |
| 193 | + clearExternalAppMessages(); |
| 194 | +
|
| 195 | + let type = longFormQuestType.value; |
| 196 | + let definition = longFormQuestDef.value; |
| 197 | + let url = longFormQuestUrl.value; |
| 198 | +
|
| 199 | + if (type === 'JSON') { |
| 200 | + url = undefined; |
| 201 | +
|
| 202 | + if (!definition) { |
| 203 | + type = 'NONE'; |
| 204 | + } |
| 205 | + else { |
| 206 | + const validationResult = await validateJson( |
| 207 | + definition, |
| 208 | + longFormQuestSchemaUrl, |
| 209 | + longFormQuestSchema, |
| 210 | + 'Long form quest definition', |
| 211 | + ); |
| 212 | +
|
| 213 | + if (validationResult.error) { |
| 214 | + longFormQuestError.value = validationResult.error; |
| 215 | + return; |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + else if (type === 'URL') { |
| 220 | + definition = undefined; |
| 221 | +
|
| 222 | + if (!url) { |
| 223 | + type = 'NONE'; |
| 224 | + } |
| 225 | + else if (!isHttpUrl(url)) { |
| 226 | + longFormQuestError.value = 'The URL is not valid.'; |
| 227 | + return; |
| 228 | + } |
| 229 | + else { |
| 230 | + url = normalizeUrl(url); |
| 231 | + } |
| 232 | + } |
| 233 | +
|
| 234 | + try { |
| 235 | + await Promise.all([ |
| 236 | + workspacesClient.updateWorkspace(workspace.id, { |
| 237 | + externalAppAccess: workspace.externalAppAccess, |
| 238 | + }), |
| 239 | + workspacesClient.saveLongFormQuestSettings(workspace.id, { |
| 240 | + type, |
| 241 | + definition, |
| 242 | + url, |
| 243 | + }), |
| 244 | + ]); |
| 245 | +
|
| 246 | + externalAppSaveStatus.value = { type: 'success', message: 'Changes saved.' }; |
| 247 | + longFormQuestType.value = type; |
| 248 | + longFormQuestDef.value = definition; |
| 249 | + longFormQuestUrl.value = url; |
| 250 | + } |
| 251 | + catch (e) { |
| 252 | + const errorMessage = e instanceof Error ? e.message : 'unexpected error'; |
| 253 | + externalAppSaveStatus.value = { |
| 254 | + type: 'error', |
| 255 | + message: 'Failed to save changes: ' + errorMessage, |
| 256 | + }; |
| 257 | + } |
| 258 | +} |
| 259 | +</script> |
0 commit comments