Skip to content
This repository was archived by the owner on Dec 1, 2025. It is now read-only.

Commit 0502d5b

Browse files
authored
Merge pull request #133 from thepensionsregulator/feature-insurer-placement-updates
insurer ref number input update and validation
2 parents 7b3327e + b21e568 commit 0502d5b

8 files changed

Lines changed: 68 additions & 23 deletions

File tree

packages/layout/src/components/__tests__/insurer.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('Insurer Preview', () => {
2828
test('is accessible', async () => {
2929
const { container } = render(
3030
<Insurer
31-
onSaveType={noop}
31+
onSaveRef={noop}
3232
onRemove={noop}
3333
onCorrect={(_value) => {}}
3434
complete={true}

packages/layout/src/components/cards/insurer/context.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const InsurerContext = createContext<InsurerContextProps>({
1111
send: (_, __) => ({}),
1212
onCorrect: () => {},
1313
onRemove: Promise.resolve,
14-
onSaveType: Promise.resolve,
14+
onSaveRef: Promise.resolve,
1515
i18n: i18nDefaults,
1616
});
1717

@@ -50,7 +50,7 @@ export interface InsurerProviderProps {
5050
complete?: boolean;
5151
onCorrect?: (...args: any[]) => void;
5252
onRemove?: (...args: any[]) => Promise<any>;
53-
onSaveType?: (...args: any[]) => Promise<any>;
53+
onSaveRef?: (...args: any[]) => Promise<any>;
5454
testId?: string;
5555
/** insurer props from the API */
5656
insurer: Partial<InsurerProps>;

packages/layout/src/components/cards/insurer/i18n.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export type InsurerI18nProps = {
1515
fields: {
1616
insurerCompanyReference: {
1717
label: string;
18-
error: string;
18+
errorIfEmpty: string;
19+
errorIfTooShort: string;
20+
errorIfTooLong: string;
1921
};
2022
};
2123
};
@@ -73,7 +75,9 @@ export const i18n: InsurerI18nProps = {
7375
fields: {
7476
insurerCompanyReference: {
7577
label: 'Reference Number',
76-
error: 'Please provide reference number for the insurer',
78+
errorIfEmpty: 'Please provide reference number for the insurer',
79+
errorIfTooShort: 'We require at least one letter',
80+
errorIfTooLong: 'Maximum length is 100 letters',
7781
},
7882
},
7983
},

packages/layout/src/components/cards/insurer/insurer.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { Insurer } from '@tpr/layout';
3535
schemeRoleId: 123,
3636
effectiveDate: '1997-04-01T00:00:00',
3737
organisationReference: 123,
38-
organisationName: 'Some Organisation Name',
38+
organisationName: 'Nike Inc.',
3939
insurerCompanyReference: '12345678',
4040
addressLine1: 'Napier House',
4141
addressLine2: 'Trafalgar Pl',
@@ -50,7 +50,7 @@ import { Insurer } from '@tpr/layout';
5050
};
5151
return (
5252
<Insurer
53-
onSaveType={callbackFn}
53+
onSaveRef={callbackFn}
5454
onRemove={callbackFn}
5555
onCorrect={(value) => setComplete(value)}
5656
complete={complete}

packages/layout/src/components/cards/insurer/insurer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
InsurerProviderProps,
55
useInsurerContext,
66
} from './context';
7-
import { Flex } from '@tpr/core';
7+
import { Flex, H4 } from '@tpr/core';
88
import { Toolbar } from '../components/toolbar';
99
import { UnderlinedButton } from '../components/button';
1010
import { Preview } from './views/preview/preview';
@@ -65,6 +65,7 @@ export const Insurer: React.FC<InsurerProviderProps> = ({
6565
<Flex cfg={cfg} data-testid={testId} className={styles.card}>
6666
<Toolbar
6767
complete={context.complete}
68+
subtitle={() => <H4>{context.insurer.organisationName}</H4>}
6869
buttonLeft={() => (
6970
<UnderlinedButton>{i18n.preview.buttons.one}</UnderlinedButton>
7071
)}

packages/layout/src/components/cards/insurer/insurerMachine.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,17 @@ const insurerMachine = Machine<InsurerContext, InsurerStates, InsurerEvents>({
6262
reference: {
6363
id: 'reference',
6464
on: {
65-
SAVE: '#preview',
65+
SAVE: {
66+
target: '#preview',
67+
actions: assign((context, event) => ({
68+
insurer: {
69+
...context.insurer,
70+
...event.values,
71+
},
72+
})),
73+
},
74+
CANCEL: '#preview',
75+
REMOVE: '#remove',
6676
},
6777
},
6878
},

packages/layout/src/components/cards/insurer/views/preview/preview.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export const Preview: React.FC<any> = () => {
1919
>
2020
<UnderlinedButton>{i18n.preview.buttons.three}</UnderlinedButton>
2121
<Flex cfg={{ my: 2, flexDirection: 'column' }}>
22-
<H4 cfg={{ lineHeight: 3 }}>{insurer.organisationName}</H4>
2322
<P>{insurer.addressLine1}</P>
2423
{insurer.addressLine2 && <P>{insurer.addressLine2}</P>}
2524
{insurer.addressLine3 && <P>{insurer.addressLine3}</P>}

packages/layout/src/components/cards/insurer/views/reference/index.tsx

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import { Form, validate, FieldProps, renderFields } from '@tpr/forms';
3+
import { Link, Flex } from '@tpr/core';
34
import { useInsurerContext } from '../../context';
45
import { Footer } from '../../../components/card';
56
import { Content } from '../../../components/content';
@@ -15,18 +16,37 @@ const getFields = (
1516
name: 'insurerCompanyReference',
1617
label: fields.insurerCompanyReference.label,
1718
inputWidth: 5,
18-
error: fields.insurerCompanyReference.error,
19+
error: (value: string) => {
20+
if (!value) {
21+
return fields.insurerCompanyReference.errorIfEmpty;
22+
} else if (value.length < 1) {
23+
return fields.insurerCompanyReference.errorIfTooShort;
24+
} else if (value.length > 100) {
25+
return fields.insurerCompanyReference.errorIfTooLong;
26+
}
27+
return undefined;
28+
},
1929
cfg: { mb: 3 },
2030
},
2131
];
2232

2333
export const Reference: React.FC = () => {
24-
const { current, send, i18n } = useInsurerContext();
34+
const [loading, setLoading] = useState(false);
35+
const { current, send, i18n, onSaveRef } = useInsurerContext();
2536
const { insurer } = current.context;
2637
const fields = getFields(i18n?.reference?.fields);
2738

28-
const onSubmit = (values) => {
29-
send('SAVE', { values });
39+
const onSubmit = async (values) => {
40+
setLoading(true);
41+
try {
42+
await onSaveRef(values, insurer);
43+
send('SAVE', { values });
44+
setLoading(false);
45+
} catch (error) {
46+
// todo: maybe an error message from the server?
47+
console.log(error);
48+
setLoading(false);
49+
}
3050
};
3151

3252
return (
@@ -47,14 +67,25 @@ export const Reference: React.FC = () => {
4767
<form onSubmit={handleSubmit}>
4868
{renderFields(fields)}
4969
<Footer>
50-
<ArrowButton
51-
intent="special"
52-
pointsTo="up"
53-
iconSide="right"
54-
type="submit"
55-
title="Save and close"
56-
disabled={false}
57-
/>
70+
<Flex>
71+
<ArrowButton
72+
intent="special"
73+
pointsTo="up"
74+
iconSide="right"
75+
type="submit"
76+
title="Save and close"
77+
disabled={loading}
78+
/>
79+
80+
<Link
81+
cfg={{ m: 3 }}
82+
disabled={loading}
83+
underline
84+
onClick={() => send('CANCEL')}
85+
>
86+
Cancel
87+
</Link>
88+
</Flex>
5889
</Footer>
5990
</form>
6091
)}

0 commit comments

Comments
 (0)