|
1 |
| -import { useMachine } from "@xstate/react"; |
2 | 1 | import { useOrganizationId } from "hooks/useOrganizationId";
|
3 | 2 | import { FC } from "react";
|
4 | 3 | import { Helmet } from "react-helmet-async";
|
5 | 4 | import { useNavigate } from "react-router-dom";
|
6 | 5 | import { pageTitle } from "utils/page";
|
7 |
| -import { createGroupMachine } from "xServices/groups/createGroupXService"; |
8 | 6 | import CreateGroupPageView from "./CreateGroupPageView";
|
| 7 | +import { useMutation, useQueryClient } from "@tanstack/react-query"; |
| 8 | +import { createGroup } from "api/queries/groups"; |
9 | 9 |
|
10 | 10 | export const CreateGroupPage: FC = () => {
|
| 11 | + const queryClient = useQueryClient(); |
11 | 12 | const navigate = useNavigate();
|
12 | 13 | const organizationId = useOrganizationId();
|
13 |
| - const [createState, sendCreateEvent] = useMachine(createGroupMachine, { |
14 |
| - context: { |
15 |
| - organizationId, |
16 |
| - }, |
17 |
| - actions: { |
18 |
| - onCreate: (_, { data }) => { |
19 |
| - navigate(`/groups/${data.id}`); |
20 |
| - }, |
21 |
| - }, |
22 |
| - }); |
23 |
| - const { error } = createState.context; |
| 14 | + const createGroupMutation = useMutation(createGroup(queryClient)); |
24 | 15 |
|
25 | 16 | return (
|
26 | 17 | <>
|
27 | 18 | <Helmet>
|
28 | 19 | <title>{pageTitle("Create Group")}</title>
|
29 | 20 | </Helmet>
|
30 | 21 | <CreateGroupPageView
|
31 |
| - onSubmit={(data) => { |
32 |
| - sendCreateEvent({ |
33 |
| - type: "CREATE", |
34 |
| - data, |
| 22 | + onSubmit={async (data) => { |
| 23 | + const newGroup = await createGroupMutation.mutateAsync({ |
| 24 | + organizationId, |
| 25 | + ...data, |
35 | 26 | });
|
| 27 | + navigate(`/groups/${newGroup.id}`); |
36 | 28 | }}
|
37 |
| - formErrors={error} |
38 |
| - isLoading={createState.matches("creatingGroup")} |
| 29 | + formErrors={createGroupMutation.error} |
| 30 | + isLoading={createGroupMutation.isLoading} |
39 | 31 | />
|
40 | 32 | </>
|
41 | 33 | );
|
|
0 commit comments