-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathnew.tsx
More file actions
87 lines (81 loc) · 3.03 KB
/
new.tsx
File metadata and controls
87 lines (81 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React, { useContext } from "react";
import type { FieldValues } from "react-hook-form";
import { useForm } from "react-hook-form";
import { redirect, useNavigate } from "react-router-dom";
import type { AppContextType } from "../../../app-context.jsx";
import { AppContext } from "../../../app-context.jsx";
import { Button } from "../../../components/Button.jsx";
export function newCloudAction(ctx: AppContextType) {
return async ({ request }: { request: Request }) => {
const tenantName = ((await request.json()) as { tenantName?: string }).tenantName;
if (!tenantName || tenantName.trim() === "") {
return new Response("Tenant name is required", { status: 400 });
}
const rTenant = await ctx.cloud.api.createTenant({
tenant: {
name: tenantName,
},
});
console.log("created", rTenant);
if (rTenant.isErr()) {
return new Response(rTenant.Err().message, { status: 400 });
}
// const { refresh } = ctx.cloud.useListTenantsByUser();
// refresh();
return redirect(`/fp/cloud/tenants/${rTenant.Ok().tenant.tenantId}`);
};
}
export function CloudNew() {
const ctx = useContext(AppContext);
const navigate = useNavigate();
const {
register,
handleSubmit,
formState: { errors },
} = useForm();
const createTenant = ctx.cloud.createTenantMutation();
async function onSubmit(data: FieldValues): Promise<void> {
try {
const rTenant = await createTenant.mutateAsync({ name: data.tenantName });
navigate(`/fp/cloud/tenants/${rTenant.tenant.tenantId}`);
} catch (error) {
console.error("Failed to create tenant:", error);
}
}
return (
<div className="max-w-2xl">
<h3 className="text-fp-p text-20">New Tenant Name:</h3>
<form onSubmit={handleSubmit(onSubmit)} className="mt-5 sm:flex">
<div className="w-full sm:max-w-xs">
<label htmlFor="tenantName" className="sr-only">
Tenant Name
</label>
<input
id="tenantName"
type="text"
{...register("tenantName", {
required: "Name is required",
})}
className="w-full py-2 px-3 bg-fp-bg-00 border border-fp-dec-00 rounded-fp-s text-14 text-fp-p placeholder-fp-dec-02 focus:placeholder-transparent focus:outline-none focus:ring-1 focus:ring-fp-dec-02 focus:border-transparent"
disabled={createTenant.isPending}
autoComplete="off"
data-1p-ignore
placeholder="Enter tenant name"
/>
</div>
<Button
variation="primary"
style="w-full mt-[14px] sm:ml-3 sm:mt-0 sm:w-auto"
type="submit"
disabled={createTenant.isPending}
>
Create
</Button>
<Button variation="secondary" style="w-full mt-[14px] sm:ml-3 sm:mt-0 sm:w-auto" type="button" onClick={() => navigate(-1)}>
Cancel
</Button>
</form>
{errors.tenantName && <p className="mt-1 text-sm text-red-500">{errors.tenantName.message as string}</p>}
</div>
);
}