|
1 |
| -import { useMachine } from "@xstate/react" |
2 |
| -import React from "react" |
3 |
| -import { useNavigate } from "react-router" |
4 |
| -import { useParams } from "react-router-dom" |
5 |
| -import { createWorkspace } from "../../api/api" |
6 |
| -import { templateMachine } from "../../xServices/template/templateXService" |
| 1 | +import { useActor, useMachine } from "@xstate/react" |
| 2 | +import React, { useContext } from "react" |
| 3 | +import { useNavigate, useSearchParams } from "react-router-dom" |
| 4 | +import { Template } from "../../api/typesGenerated" |
| 5 | +import { createWorkspaceMachine } from "../../xServices/createWorkspace/createWorkspaceXService" |
| 6 | +import { XServiceContext } from "../../xServices/StateContext" |
7 | 7 | import { CreateWorkspacePageView } from "./CreateWorkspacePageView"
|
8 | 8 |
|
| 9 | +const useOrganizationId = () => { |
| 10 | + const xServices = useContext(XServiceContext) |
| 11 | + const [authState] = useActor(xServices.authXService) |
| 12 | + const organizationId = authState.context.me?.organization_ids[0] |
| 13 | + |
| 14 | + if (!organizationId) { |
| 15 | + throw new Error("No organization ID found") |
| 16 | + } |
| 17 | + |
| 18 | + return organizationId |
| 19 | +} |
| 20 | + |
9 | 21 | const CreateWorkspacePage: React.FC = () => {
|
10 |
| - const { template } = useParams() |
11 |
| - const [templateState] = useMachine(templateMachine, { |
12 |
| - context: { |
13 |
| - name: template, |
| 22 | + const organizationId = useOrganizationId() |
| 23 | + const [searchParams] = useSearchParams() |
| 24 | + const preSelectedTemplateName = searchParams.get("template") |
| 25 | + const navigate = useNavigate() |
| 26 | + const [createWorkspaceState, send] = useMachine(createWorkspaceMachine, { |
| 27 | + context: { organizationId, preSelectedTemplateName }, |
| 28 | + actions: { |
| 29 | + onCreateWorkspace: (_, event) => { |
| 30 | + navigate("/workspaces/" + event.data.id) |
| 31 | + }, |
14 | 32 | },
|
15 | 33 | })
|
16 |
| - const navigate = useNavigate() |
17 |
| - const loading = templateState.hasTag("loading") |
18 |
| - if (!templateState.context.template || !templateState.context.templateSchema) { |
19 |
| - return null |
20 |
| - } |
21 | 34 |
|
22 | 35 | return (
|
23 | 36 | <CreateWorkspacePageView
|
24 |
| - template={templateState.context.template} |
25 |
| - templateSchema={templateState.context.templateSchema} |
26 |
| - loading={loading} |
27 |
| - onCancel={() => navigate("/templates")} |
28 |
| - onSubmit={async (req) => { |
29 |
| - if (!templateState.context.template) { |
30 |
| - throw new Error("template isn't valid") |
31 |
| - } |
32 |
| - const workspace = await createWorkspace(templateState.context.template.organization_id, req) |
33 |
| - navigate("/workspaces/" + workspace.id) |
| 37 | + loadingTemplates={createWorkspaceState.matches("gettingTemplates")} |
| 38 | + loadingTemplateSchema={createWorkspaceState.matches("gettingTemplateSchema")} |
| 39 | + creatingWorkspace={createWorkspaceState.matches("creatingWorkspace")} |
| 40 | + templates={createWorkspaceState.context.templates} |
| 41 | + selectedTemplate={createWorkspaceState.context.selectedTemplate} |
| 42 | + templateSchema={createWorkspaceState.context.templateSchema} |
| 43 | + onCancel={() => { |
| 44 | + navigate(preSelectedTemplateName ? "/templates" : "/workspaces") |
| 45 | + }} |
| 46 | + onSubmit={(request) => { |
| 47 | + send({ |
| 48 | + type: "CREATE_WORKSPACE", |
| 49 | + request, |
| 50 | + }) |
| 51 | + }} |
| 52 | + onSelectTemplate={(template: Template) => { |
| 53 | + send({ |
| 54 | + type: "SELECT_TEMPLATE", |
| 55 | + template, |
| 56 | + }) |
34 | 57 | }}
|
35 | 58 | />
|
36 | 59 | )
|
|
0 commit comments