|
1 |
| -import { Button } from '@entur/button' |
| 1 | +'use client' |
2 | 2 | import { TextField } from '@entur/form'
|
3 | 3 | import { AddIcon } from '@entur/icons'
|
4 |
| -import { useToggle } from 'hooks/useToggle' |
5 |
| -import { SyntheticEvent } from 'react' |
6 |
| -import { TOrganizationID, TUser } from 'types/settings' |
| 4 | +import { TOrganizationID } from 'types/settings' |
7 | 5 | import classes from './styles.module.css'
|
8 |
| -import { FeedbackCode, useFormFeedback } from 'hooks/useFormFeedback' |
9 |
| -import { fetchInviteUserToOrganizationByEmail } from 'Admin/utils/fetch' |
| 6 | +import { inviteUserAction } from 'Admin/utils/formActions' |
| 7 | +import { useFormState } from 'react-dom' |
| 8 | +import { HiddenInput } from 'components/Form/HiddenInput' |
| 9 | +import { getFormStateProps } from 'utils/formStatuses' |
| 10 | +import { SubmitButton } from 'components/Form/SubmitButton' |
10 | 11 |
|
11 |
| -function InviteUser({ |
12 |
| - oid, |
13 |
| - addMember, |
14 |
| -}: { |
15 |
| - oid?: TOrganizationID |
16 |
| - addMember: (member: TUser) => void |
17 |
| -}) { |
18 |
| - const [isLoading, enableLoading, disableLoading] = useToggle() |
19 |
| - const { setFeedback, clearFeedback, getTextFieldProps } = useFormFeedback() |
20 |
| - |
21 |
| - const submitHandler = (event: SyntheticEvent) => { |
22 |
| - event.preventDefault() |
23 |
| - clearFeedback() |
24 |
| - |
25 |
| - const { email } = event.currentTarget as unknown as { |
26 |
| - email: HTMLInputElement |
27 |
| - } |
28 |
| - |
29 |
| - enableLoading() |
30 |
| - |
31 |
| - fetchInviteUserToOrganizationByEmail(email.value, oid) |
32 |
| - .then((response) => { |
33 |
| - disableLoading() |
34 |
| - addMember({ email: email.value }) |
35 |
| - response.json().then((data: { feedbackCode: FeedbackCode }) => { |
36 |
| - setFeedback(data.feedbackCode) |
37 |
| - }) |
38 |
| - }) |
39 |
| - .catch(() => { |
40 |
| - disableLoading() |
41 |
| - setFeedback('error') |
42 |
| - }) |
43 |
| - } |
| 12 | +function InviteUser({ oid }: { oid?: TOrganizationID }) { |
| 13 | + const [formState, formAction] = useFormState(inviteUserAction, undefined) |
44 | 14 |
|
45 | 15 | return (
|
46 |
| - <form className={classes.inviteForm} onSubmit={submitHandler}> |
| 16 | + <form className={classes.inviteForm} action={formAction}> |
| 17 | + <HiddenInput id="oid" value={oid} /> |
47 | 18 | <div className="flexColumn g-1 w-100">
|
48 | 19 | <TextField
|
49 | 20 | name="email"
|
50 | 21 | label="E-post"
|
51 | 22 | type="email"
|
52 |
| - {...getTextFieldProps()} |
| 23 | + {...getFormStateProps(formState)} |
53 | 24 | />
|
54 | 25 | </div>
|
55 |
| - <Button |
| 26 | + <SubmitButton |
56 | 27 | variant="primary"
|
57 |
| - loading={isLoading} |
58 |
| - onClick={clearFeedback} |
59 | 28 | width="fluid"
|
60 | 29 | className={classes.addMemberButton}
|
61 | 30 | >
|
62 | 31 | Legg til medlem
|
63 | 32 | <AddIcon />
|
64 |
| - </Button> |
| 33 | + </SubmitButton> |
65 | 34 | </form>
|
66 | 35 | )
|
67 | 36 | }
|
|
0 commit comments