Handle back button
This commit is contained in:
@@ -1,12 +1,15 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { onMount, setContext } from "svelte";
|
||||||
import { Client, setContextClient, queryStore } from "@urql/svelte";
|
import { Client, setContextClient, queryStore } from "@urql/svelte";
|
||||||
import { graphql } from "./gql";
|
|
||||||
import ApplicationFormStep from "./components/ApplicationFormStep.svelte";
|
|
||||||
import { step, applicationId, application } from "./lib/stores";
|
|
||||||
import { createHttpClient } from "./lib/oap/oapApiV1Client";
|
|
||||||
import { onMount } from "svelte";
|
|
||||||
import type { IServiceError, IServiceResult } from "facility-core";
|
import type { IServiceError, IServiceResult } from "facility-core";
|
||||||
|
|
||||||
|
import { graphql } from "./gql";
|
||||||
|
import { applicationFormState } from "./lib/stores";
|
||||||
|
import { createHttpClient } from "./lib/oap/oapApiV1Client";
|
||||||
|
|
||||||
|
import ApplicationFormStep from "./components/ApplicationFormStep.svelte";
|
||||||
|
import type { FormContext } from "./lib/contextModels";
|
||||||
|
|
||||||
export let antiForgeryToken: string | null;
|
export let antiForgeryToken: string | null;
|
||||||
export let contentItemId: string;
|
export let contentItemId: string;
|
||||||
export let client: Client;
|
export let client: Client;
|
||||||
@@ -44,19 +47,19 @@
|
|||||||
let serviceError: IServiceError | null = null;
|
let serviceError: IServiceError | null = null;
|
||||||
let serviceMsg: string | null;
|
let serviceMsg: string | null;
|
||||||
|
|
||||||
function setResultOrError<T>(
|
function setResultOrError<T>(response: IServiceResult<T>) {
|
||||||
response: IServiceResult<T>,
|
|
||||||
) {
|
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
serviceError = response.error;
|
serviceError = response.error;
|
||||||
} else {
|
} else {
|
||||||
serviceMsg = "Did a thing!";
|
serviceMsg = "Did a thing!";
|
||||||
step.update((x) => x + 1);
|
$applicationFormState.currentStep += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleSubmit(event: SubmitEvent) {
|
async function handleSubmit(event: SubmitEvent) {
|
||||||
if (!$applicationId) {
|
event.preventDefault();
|
||||||
|
|
||||||
|
if (!$applicationFormState.application?.id) {
|
||||||
const response = await oapApi.createApplication({
|
const response = await oapApi.createApplication({
|
||||||
applicationCreateSet: {
|
applicationCreateSet: {
|
||||||
terms: {
|
terms: {
|
||||||
@@ -66,10 +69,14 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
setResultOrError(response);
|
setResultOrError(response);
|
||||||
applicationId.set(response.value?.application?.id ?? null);
|
|
||||||
|
applicationFormState.update((a) => {
|
||||||
|
a.application = response.value?.application ?? null;
|
||||||
|
return a;
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
const response = await oapApi.updateApplication({
|
const response = await oapApi.updateApplication({
|
||||||
id: $applicationId,
|
id: $applicationFormState.application?.id,
|
||||||
applicationChangeSet: {
|
applicationChangeSet: {
|
||||||
terms: {
|
terms: {
|
||||||
hasReadInstructions: true,
|
hasReadInstructions: true,
|
||||||
@@ -78,17 +85,38 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
setResultOrError(response);
|
setResultOrError(response);
|
||||||
|
|
||||||
|
applicationFormState.update((a) => {
|
||||||
|
a.application = response.value?.application ?? null;
|
||||||
|
return a;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleBack(event: Event) {
|
||||||
|
event.preventDefault();
|
||||||
|
applicationFormState.update((a) => {
|
||||||
|
a.currentStep -= 1;
|
||||||
|
return a;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setContext<FormContext>("formContext", {
|
||||||
|
handleSubmit,
|
||||||
|
handleBack,
|
||||||
|
});
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
if ($applicationId) {
|
if ($applicationFormState.application?.id) {
|
||||||
const response = await oapApi.getApplication({
|
const response = await oapApi.getApplication({
|
||||||
id: $applicationId,
|
id: $applicationFormState.application.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
setResultOrError(response);
|
setResultOrError(response);
|
||||||
application.set(response.value?.application ?? null);
|
applicationFormState.update((a) => {
|
||||||
|
a.application = response.value?.application ?? null;
|
||||||
|
return a;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
@@ -101,7 +129,7 @@
|
|||||||
<p>No content data??</p>
|
<p>No content data??</p>
|
||||||
{:else if $content.data?.oapApplicationForm}
|
{:else if $content.data?.oapApplicationForm}
|
||||||
<div class="flex flex-col gap-x-4">
|
<div class="flex flex-col gap-x-4">
|
||||||
{#each $content.data?.oapApplicationForm as oapApplicationForm}
|
{#each $content.data.oapApplicationForm as oapApplicationForm}
|
||||||
{#if oapApplicationForm}
|
{#if oapApplicationForm}
|
||||||
<h1
|
<h1
|
||||||
class="mb-6 border-b-2 border-solid border-black pb-1 text-center text-2xl font-bold"
|
class="mb-6 border-b-2 border-solid border-black pb-1 text-center text-2xl font-bold"
|
||||||
@@ -120,9 +148,8 @@
|
|||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
{#each oapApplicationForm?.list?.contentItems ?? [] as contentItem, index}
|
{#each oapApplicationForm?.list?.contentItems ?? [] as contentItem, index}
|
||||||
{#if contentItem?.__typename === "OapApplicationFormStep" && $step === index}
|
{#if contentItem?.__typename === "OapApplicationFormStep" && $applicationFormState.currentStep === index}
|
||||||
<ApplicationFormStep
|
<ApplicationFormStep
|
||||||
onSubmit={handleSubmit}
|
|
||||||
{antiForgeryToken}
|
{antiForgeryToken}
|
||||||
fragment={contentItem}
|
fragment={contentItem}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -24,10 +24,6 @@
|
|||||||
import Form from "./Form.svelte";
|
import Form from "./Form.svelte";
|
||||||
import type { EventHandler } from "svelte/elements";
|
import type { EventHandler } from "svelte/elements";
|
||||||
|
|
||||||
export let onSubmit:
|
|
||||||
| EventHandler<SubmitEvent, HTMLFormElement>
|
|
||||||
| undefined
|
|
||||||
| null;
|
|
||||||
export let antiForgeryToken: string | null;
|
export let antiForgeryToken: string | null;
|
||||||
export let fragment: FragmentType<typeof ApplicationFormStepFragment>;
|
export let fragment: FragmentType<typeof ApplicationFormStepFragment>;
|
||||||
|
|
||||||
@@ -44,6 +40,6 @@
|
|||||||
|
|
||||||
{#each content.flow?.widgets ?? [] as widget}
|
{#each content.flow?.widgets ?? [] as widget}
|
||||||
{#if widget?.__typename === "Form"}
|
{#if widget?.__typename === "Form"}
|
||||||
<Form {onSubmit} {antiForgeryToken} fragment={widget} />
|
<Form {antiForgeryToken} fragment={widget} />
|
||||||
{/if}
|
{/if}
|
||||||
{/each}
|
{/each}
|
||||||
|
|||||||
@@ -7,10 +7,14 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import type { EventHandler } from "svelte/elements";
|
||||||
|
|
||||||
export let type: HTMLButtonElement["type"] | null | undefined;
|
export let type: HTMLButtonElement["type"] | null | undefined;
|
||||||
export let name: string | null | undefined;
|
export let name: string | null | undefined;
|
||||||
export let id: string | null | undefined;
|
export let id: string | null | undefined;
|
||||||
export let text: string | null | undefined;
|
export let text: string | null | undefined;
|
||||||
|
export let onClick: EventHandler<Event, HTMLButtonElement> | undefined =
|
||||||
|
undefined;
|
||||||
|
|
||||||
const cssClasses =
|
const cssClasses =
|
||||||
type === "submit"
|
type === "submit"
|
||||||
@@ -18,6 +22,6 @@
|
|||||||
: "cursor-pointer px-4 py-2 text-gray-800 hover:text-gray-400";
|
: "cursor-pointer px-4 py-2 text-gray-800 hover:text-gray-400";
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<button class={cssClasses} {type} {name} {id}>
|
<button class={cssClasses} {type} {name} {id} on:click={onClick}>
|
||||||
{@html text}
|
{@html text}
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -26,7 +26,6 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { EventHandler } from "svelte/elements";
|
|
||||||
import { useFragment, type FragmentType } from "../gql";
|
import { useFragment, type FragmentType } from "../gql";
|
||||||
import ButtonWidget from "./Widgets/ButtonWidget.svelte";
|
import ButtonWidget from "./Widgets/ButtonWidget.svelte";
|
||||||
import InputWidget from "./Widgets/InputWidget.svelte";
|
import InputWidget from "./Widgets/InputWidget.svelte";
|
||||||
@@ -34,14 +33,14 @@
|
|||||||
import TextAreaWidget from "./Widgets/TextAreaWidget.svelte";
|
import TextAreaWidget from "./Widgets/TextAreaWidget.svelte";
|
||||||
import LabelWidget from "./Widgets/LabelWidget.svelte";
|
import LabelWidget from "./Widgets/LabelWidget.svelte";
|
||||||
import ApplicationFormButtonsWidget from "./Widgets/ApplicationFormButtonsWidget.svelte";
|
import ApplicationFormButtonsWidget from "./Widgets/ApplicationFormButtonsWidget.svelte";
|
||||||
|
import { getContext } from "svelte";
|
||||||
|
import { type FormContext } from "../lib/contextModels";
|
||||||
|
|
||||||
export let onSubmit:
|
|
||||||
| EventHandler<SubmitEvent, HTMLFormElement>
|
|
||||||
| undefined
|
|
||||||
| null;
|
|
||||||
export let antiForgeryToken: string | null;
|
export let antiForgeryToken: string | null;
|
||||||
export let fragment: FragmentType<typeof FormFragment>;
|
export let fragment: FragmentType<typeof FormFragment>;
|
||||||
|
|
||||||
|
const formContext = getContext<FormContext>("formContext");
|
||||||
|
|
||||||
const content = useFragment(FormFragment, fragment);
|
const content = useFragment(FormFragment, fragment);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -49,7 +48,7 @@
|
|||||||
id={content.formElement?.id}
|
id={content.formElement?.id}
|
||||||
action={content.form?.action}
|
action={content.form?.action}
|
||||||
method={content.form?.method}
|
method={content.form?.method}
|
||||||
on:submit|preventDefault={onSubmit}
|
on:submit|preventDefault={formContext.handleSubmit}
|
||||||
>
|
>
|
||||||
<input
|
<input
|
||||||
type="hidden"
|
type="hidden"
|
||||||
|
|||||||
@@ -26,6 +26,8 @@
|
|||||||
import { useFragment, type FragmentType } from "../../gql";
|
import { useFragment, type FragmentType } from "../../gql";
|
||||||
import Widget from "./Widget.svelte";
|
import Widget from "./Widget.svelte";
|
||||||
import Button, { isButtonType } from "../Fields/Button.svelte";
|
import Button, { isButtonType } from "../Fields/Button.svelte";
|
||||||
|
import { getContext } from "svelte";
|
||||||
|
import type { FormContext } from "../../lib/contextModels";
|
||||||
export let fragment: FragmentType<
|
export let fragment: FragmentType<
|
||||||
typeof OapApplicationFormButtonsWidgetFragment
|
typeof OapApplicationFormButtonsWidgetFragment
|
||||||
>;
|
>;
|
||||||
@@ -33,6 +35,8 @@
|
|||||||
OapApplicationFormButtonsWidgetFragment,
|
OapApplicationFormButtonsWidgetFragment,
|
||||||
fragment,
|
fragment,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const formContext = getContext<FormContext>("formContext");
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Widget metadata={content.metadata}>
|
<Widget metadata={content.metadata}>
|
||||||
@@ -45,6 +49,7 @@
|
|||||||
type={isButtonType(content.back?.type)
|
type={isButtonType(content.back?.type)
|
||||||
? content.back?.type
|
? content.back?.type
|
||||||
: "button"}
|
: "button"}
|
||||||
|
onClick={formContext.handleBack}
|
||||||
/>
|
/>
|
||||||
{:else}
|
{:else}
|
||||||
<span> </span>
|
<span> </span>
|
||||||
|
|||||||
6
src/OAP/Oap.Theme.Svelte/src/lib/contextModels.ts
Normal file
6
src/OAP/Oap.Theme.Svelte/src/lib/contextModels.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import type { EventHandler } from "svelte/elements";
|
||||||
|
|
||||||
|
export interface FormContext {
|
||||||
|
handleSubmit: EventHandler<SubmitEvent, HTMLFormElement>;
|
||||||
|
handleBack: EventHandler<Event, HTMLButtonElement>;
|
||||||
|
}
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
import applicationId from "./stores/applicationId";
|
import applicationFormState from "./stores/applicationFormState";
|
||||||
import step from "./stores/step";
|
|
||||||
import application from './stores/application';
|
|
||||||
|
|
||||||
export { step, applicationId, application };
|
export { applicationFormState };
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
import { writable } from "svelte/store";
|
|
||||||
import { type IApplication } from "../oap/oapApiV1ClientTypes";
|
|
||||||
|
|
||||||
const application = writable<IApplication | null>(null);
|
|
||||||
|
|
||||||
export default application;
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { writable } from "svelte/store";
|
||||||
|
import type { ApplicationFormState } from "../viewModels";
|
||||||
|
|
||||||
|
const applicationFormState = writable<ApplicationFormState>({
|
||||||
|
application: null,
|
||||||
|
currentStep: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default applicationFormState;
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
import { writable } from 'svelte/store';
|
|
||||||
const applicationId = writable<string | null>(null);
|
|
||||||
|
|
||||||
export default applicationId;
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
import { writable } from 'svelte/store';
|
|
||||||
const step = writable(0);
|
|
||||||
|
|
||||||
export default step;
|
|
||||||
6
src/OAP/Oap.Theme.Svelte/src/lib/viewModels.ts
Normal file
6
src/OAP/Oap.Theme.Svelte/src/lib/viewModels.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import type { IApplication } from "./oap/oapApiV1ClientTypes";
|
||||||
|
|
||||||
|
export interface ApplicationFormState {
|
||||||
|
application: IApplication | null;
|
||||||
|
currentStep: number;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user