|
| 1 | +import type { |
| 2 | + CreatedFormInstance, |
| 3 | + FormInstanceConfig, |
| 4 | + LoadFormOptions, |
| 5 | + LoadFormResult, |
| 6 | + LoadFormSuccessResult, |
| 7 | + LoadFormWarningResult, |
| 8 | + RestoredFormInstance, |
| 9 | +} from '@getodk/xforms-engine'; |
| 10 | +import { loadForm } from '@getodk/xforms-engine'; |
| 11 | +import type { Ref } from 'vue'; |
| 12 | +import { computed, reactive, ref } from 'vue'; |
| 13 | +import type { AnyInstancePayload } from './instance-cache-state.ts'; |
| 14 | + |
| 15 | +// prettier-ignore |
| 16 | +export type InstantiableFormResult = |
| 17 | + | LoadFormSuccessResult |
| 18 | + | LoadFormWarningResult; |
| 19 | + |
| 20 | +export const formResultState: Ref<LoadFormResult | null> = ref(null); |
| 21 | + |
| 22 | +const isInstantiableFormResult = ( |
| 23 | + formResult: LoadFormResult | null |
| 24 | +): formResult is InstantiableFormResult => { |
| 25 | + return formResult != null && formResult.status !== 'failure'; |
| 26 | +}; |
| 27 | + |
| 28 | +export const instantiableFormResult = computed(() => { |
| 29 | + const formResult = formResultState.value; |
| 30 | + |
| 31 | + if (isInstantiableFormResult(formResult)) { |
| 32 | + return formResult; |
| 33 | + } |
| 34 | + |
| 35 | + return null; |
| 36 | +}); |
| 37 | + |
| 38 | +export const initializeFormResultState = async ( |
| 39 | + formXML: string, |
| 40 | + options: LoadFormOptions |
| 41 | +): Promise<LoadFormResult> => { |
| 42 | + const formResult = await loadForm(formXML, options); |
| 43 | + |
| 44 | + formResultState.value = formResult; |
| 45 | + |
| 46 | + return formResult; |
| 47 | +}; |
| 48 | + |
| 49 | +const instanceConfig: FormInstanceConfig = { |
| 50 | + stateFactory: reactive, |
| 51 | +}; |
| 52 | + |
| 53 | +// prettier-ignore |
| 54 | +export type AnyInstance = |
| 55 | + | CreatedFormInstance |
| 56 | + | RestoredFormInstance; |
| 57 | + |
| 58 | +export const instanceState: Ref<AnyInstance | null> = ref(null); |
| 59 | + |
| 60 | +export const initializeInstanceState = ( |
| 61 | + formResult: InstantiableFormResult |
| 62 | +): CreatedFormInstance => { |
| 63 | + const instance = formResult.createInstance(instanceConfig); |
| 64 | + |
| 65 | + instanceState.value = instance; |
| 66 | + |
| 67 | + return instanceState.value; |
| 68 | +}; |
| 69 | + |
| 70 | +interface FormInstanceState { |
| 71 | + readonly formResult: LoadFormResult; |
| 72 | + readonly instance: AnyInstance; |
| 73 | +} |
| 74 | + |
| 75 | +export const initializeFormInstanceState = async ( |
| 76 | + formXML: string, |
| 77 | + options: LoadFormOptions |
| 78 | +): Promise<FormInstanceState | null> => { |
| 79 | + const formResult = await initializeFormResultState(formXML, options); |
| 80 | + |
| 81 | + if (formResult.status === 'failure') { |
| 82 | + instanceState.value = null; |
| 83 | + |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + const instance = initializeInstanceState(formResult); |
| 88 | + |
| 89 | + return { |
| 90 | + formResult, |
| 91 | + instance, |
| 92 | + }; |
| 93 | +}; |
| 94 | + |
| 95 | +export const restoreInstanceState = async ( |
| 96 | + formResult: InstantiableFormResult, |
| 97 | + payload: AnyInstancePayload |
| 98 | +): Promise<AnyInstance> => { |
| 99 | + const instance = await formResult.restoreInstance(payload, instanceConfig); |
| 100 | + |
| 101 | + instanceState.value = instance; |
| 102 | + |
| 103 | + return instance; |
| 104 | +}; |
| 105 | + |
| 106 | +export const resetInstanceState = () => { |
| 107 | + const formResult = formResultState.value; |
| 108 | + |
| 109 | + if (isInstantiableFormResult(formResult)) { |
| 110 | + initializeInstanceState(formResult); |
| 111 | + } |
| 112 | +}; |
0 commit comments