Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit 48781ac

Browse files
authored
Added a new way to view the all fields form (#254)
* Added a new way to view the all fields form * Added a form test * Moved stuff to SURF specific * Cleaned up * Made create form promise immutable
1 parent 9313c52 commit 48781ac

File tree

5 files changed

+140
-1
lines changed

5 files changed

+140
-1
lines changed

src/custom-surf/api/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ abstract class CustomApiClientInterface extends BaseApiClient {
6767
abstract free_subnets: (subnet: string, netmask: number, prefixlen: number) => Promise<string[]>;
6868
abstract contacts: (organisationId: string) => Promise<ContactPerson[]>;
6969
abstract dienstafnameBySubscription: (subscriptionId: string) => Promise<Dienstafname | undefined>;
70+
abstract showForms: () => Promise<string[]>;
71+
abstract startForm: (formKey: string, userInputs: {}[]) => Promise<any>;
7072
abstract cimShowForms: () => Promise<string[]>;
7173
abstract cimStartForm: (formKey: string, userInputs: {}[]) => Promise<any>;
7274
abstract cimTickets: () => Promise<ServiceTicket[]>;
@@ -228,6 +230,14 @@ export class CustomApiClient extends CustomApiClientInterface {
228230
return this.postPutJson(prefix_cim_dev_uri("surf/cim/forms/" + formKey), userInputs, "post", false, true);
229231
};
230232

233+
showForms = (): Promise<string[]> => {
234+
return this.fetchJson("surf/forms");
235+
};
236+
237+
startForm = (formKey: string, userInputs: {}[]): Promise<{ id: string }> => {
238+
return this.postPutJson("surf/forms/" + formKey, userInputs, "post", false, true);
239+
};
240+
231241
cimCreateTicket = (ticket: CreateServiceTicketPayload): Promise<{ id: string }> => {
232242
return this.postPutJson(prefix_cim_dev_uri("surf/cim/tickets"), ticket, "post", false, true);
233243
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2019-2023 SURF.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
*/
15+
16+
import UserInputFormWizard from "components/inputForms/UserInputFormWizard";
17+
import React, { useCallback, useContext, useEffect, useState } from "react";
18+
import { useIntl } from "react-intl";
19+
import ApplicationContext from "utils/ApplicationContext";
20+
import { setFlash } from "utils/Flash";
21+
import { Form, FormNotCompleteResponse } from "utils/types";
22+
23+
interface IProps {
24+
preselectedInput?: any;
25+
formKey: string;
26+
handleSubmit: (userInputs: any) => void;
27+
}
28+
29+
export default function CreateTestForm(props: IProps) {
30+
const intl = useIntl();
31+
const { preselectedInput, formKey, handleSubmit } = props;
32+
const { redirect, customApiClient } = useContext(ApplicationContext);
33+
const [form, setForm] = useState<Form>({});
34+
const { stepUserInput, hasNext } = form;
35+
36+
const submit = useCallback(
37+
(userInputs: {}[]) => {
38+
// if (preselectedInput.product) {
39+
// Todo: decide if we want to implement pre-selections and design a way to do it generic
40+
// userInputs = [{ preselectedInput }, ...userInputs];
41+
// }
42+
const promise = customApiClient.startForm(formKey, userInputs).then(
43+
(form) => {
44+
handleSubmit(form);
45+
setFlash(intl.formatMessage({ id: `cim.flash.${formKey}` }));
46+
},
47+
(e) => {
48+
throw e;
49+
}
50+
);
51+
52+
return customApiClient.catchErrorStatus<any>(promise, 503, (json) => {
53+
setFlash(intl.formatMessage({ id: `cim.backendProblem` }), "error");
54+
redirect("/tickets");
55+
});
56+
},
57+
[redirect, intl, customApiClient, formKey, handleSubmit]
58+
);
59+
60+
useEffect(() => {
61+
if (formKey) {
62+
customApiClient.catchErrorStatus<FormNotCompleteResponse>(submit([]), 510, (json) => {
63+
setForm({
64+
stepUserInput: json.form,
65+
hasNext: json.hasNext ?? false,
66+
});
67+
});
68+
}
69+
}, [formKey, submit, preselectedInput, intl, customApiClient]);
70+
71+
return (
72+
<div>
73+
{stepUserInput && (
74+
<UserInputFormWizard
75+
stepUserInput={stepUserInput}
76+
validSubmit={submit}
77+
cancel={() => redirect("/tickets")}
78+
hasNext={hasNext ?? false}
79+
/>
80+
)}
81+
</div>
82+
);
83+
}

src/custom-surf/components/cim/CreateForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function CreateForm(props: IProps) {
3939
// Todo: decide if we want to implement pre-selections and design a way to do it generic
4040
// userInputs = [{ preselectedInput }, ...userInputs];
4141
// }
42-
let promise = customApiClient.cimStartForm(formKey, userInputs).then(
42+
const promise = customApiClient.cimStartForm(formKey, userInputs).then(
4343
(form) => {
4444
handleSubmit(form);
4545
setFlash(intl.formatMessage({ id: `cim.flash.${formKey}` }));

src/custom-surf/manifest.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@
6363
"file": "CloseServiceTicket",
6464
"component": "CloseServiceTicket",
6565
"showInMenu": false
66+
},
67+
{
68+
"name": "form-test",
69+
"path": "pages",
70+
"file": "FormTest",
71+
"component": "FormTest",
72+
"showInMenu": false
6673
}
6774
],
6875
"disabledRoutes": ["/YOUR_DISABLED_ROUTE_HERE"],

src/custom-surf/pages/FormTest.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2019-2023 SURF.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
*/
15+
16+
import { EuiPage, EuiPageBody } from "@elastic/eui";
17+
import CreateTestForm from "custom/components/CreateTestForm";
18+
import FormHeader from "custom/components/FormHeader";
19+
import { formStyling } from "custom/pages/FormStyling";
20+
import React from "react";
21+
22+
export default function FormTest() {
23+
const handleSubmit = (userInputs: any) => {
24+
console.log("Form succes! Payload: ", userInputs);
25+
};
26+
27+
return (
28+
<EuiPage css={formStyling}>
29+
<EuiPageBody component="div">
30+
<FormHeader
31+
title="Form test"
32+
explainTitle="A form that uses all available form elements"
33+
explainDescription={<p>Nothing will happen when you submit. This is a test form after all.</p>}
34+
/>
35+
<CreateTestForm formKey="all_form_fields" handleSubmit={handleSubmit} />
36+
</EuiPageBody>
37+
</EuiPage>
38+
);
39+
}

0 commit comments

Comments
 (0)