Skip to content

Commit f0d5514

Browse files
committed
ok
1 parent cf26f4b commit f0d5514

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

src/lib/classes/user.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type Session } from "@supabase/supabase-js";
22
import { type UseNavigateResult } from "@tanstack/react-router";
3-
import { ResultAsync } from "neverthrow";
3+
import { ok, ResultAsync } from "neverthrow";
44
import { match } from "ts-pattern";
55
import { Sower } from "./sower";
66
import { Sponsor } from "./sponsor";
@@ -107,4 +107,33 @@ export class User {
107107
Table.transformError(config, "fetchOwnComments", error),
108108
);
109109
}
110+
111+
public fetchKindOf(): TableResult<
112+
| {
113+
type: "SOWER";
114+
data: Sower;
115+
}
116+
| {
117+
type: "SPONSOR";
118+
data: Sponsor;
119+
}
120+
| {
121+
type: "UNKNOWN";
122+
}
123+
> {
124+
const data = ResultAsync.combine([
125+
Sower.factories.fromUser(this.id).orElse(() => ok(null)),
126+
Sponsor.factories.fromUser(this.id).orElse(() => ok(null)),
127+
]);
128+
129+
return data.andThen(([sower, sponsor]) => {
130+
if (sower != null) {
131+
return ok({ type: "SOWER", data: sower } as const);
132+
}
133+
if (sponsor != null) {
134+
return ok({ type: "SPONSOR", data: sponsor } as const);
135+
}
136+
return ok({ type: "UNKNOWN" } as const);
137+
});
138+
}
110139
}

src/routes/user/index.tsx

+36-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Field, NumberInput } from "@ark-ui/react";
22
import { Icon } from "@iconify/react";
3-
import { createFileRoute } from "@tanstack/react-router";
3+
import { createFileRoute, useNavigate } from "@tanstack/react-router";
44
import { useSetAtom } from "jotai";
55
import { Grid, styled as p, VStack } from "panda/jsx";
66
import { useState, type ReactElement } from "react";
7+
import useSWRImmutable from "swr/immutable";
78
import { z } from "zod";
89
import { IconText } from "@/components/IconText";
910
import { Button } from "@/components/cva/Button";
@@ -13,6 +14,7 @@ import { svaTextArea } from "@/components/sva/textArea";
1314
import { User } from "@/lib/classes/user";
1415
import { $redirectTo } from "@/lib/stores/redirect";
1516
import { notifyTableErrorInToast } from "@/lib/utils/table";
17+
import { toaster } from "@/lib/utils/toast";
1618

1719
export const Route = createFileRoute("/user/")({
1820
validateSearch: (s) =>
@@ -36,16 +38,23 @@ export const Route = createFileRoute("/user/")({
3638
});
3739

3840
function Authenticated({ user }: { user: User }): ReactElement {
39-
const [selected, setSelected] = useState<"sower" | "sponsor" | null>();
4041
const [age, setAge] = useState(0);
42+
const [selected, setSelected] = useState<"sower" | "sponsor" | null>(null);
4143
const [description, setDescription] = useState("");
44+
const navigate = useNavigate();
4245
const numberInput = svaNumberInput();
4346
const textArea = svaTextArea();
4447

48+
const swrUserKindOf = useSWRImmutable("userKindOf", async () =>
49+
(
50+
await user.fetchKindOf().mapErr(notifyTableErrorInToast("swrUserKindOf"))
51+
)._unsafeUnwrap(),
52+
);
53+
4554
return (
4655
<Expanded items="center">
4756
<VStack gap="10" p="10">
48-
{selected == null && (
57+
{swrUserKindOf.data?.type === "UNKNOWN" && (
4958
<>
5059
<p.span fontSize="xl" fontWeight="bold">
5160
アカウントタイプを選択して下さい
@@ -149,7 +158,15 @@ function Authenticated({ user }: { user: User }): ReactElement {
149158
name: user.metadata.name ?? "名無し",
150159
birthday: `${new Date().getFullYear() - age}-10-05T14:48:00.000Z`,
151160
})
152-
.mapErr(notifyTableErrorInToast("User.registerAsASower"));
161+
.mapErr(notifyTableErrorInToast("User.registerAsASower"))
162+
.andTee(() => {
163+
void navigate({ to: "/" });
164+
});
165+
toaster.success({
166+
id: "register-as-sower",
167+
title: "市民としてログインしました",
168+
description: "ようこそ!",
169+
});
153170
}}
154171
variant="outlined"
155172
w="300px"
@@ -179,11 +196,21 @@ function Authenticated({ user }: { user: User }): ReactElement {
179196
<Button
180197
h="100px"
181198
onClick={() => {
182-
void user.registerAsASponsor({
183-
user_id: user.id,
184-
name: user.metadata.name,
185-
icon: user.metadata.picture,
186-
description,
199+
void user
200+
.registerAsASponsor({
201+
user_id: user.id,
202+
name: user.metadata.name,
203+
icon: user.metadata.picture,
204+
description,
205+
})
206+
.mapErr(notifyTableErrorInToast("Button.企業としてログイン"))
207+
.andTee(() => {
208+
void navigate({ to: "/" });
209+
});
210+
toaster.success({
211+
id: "register-as-sponsor",
212+
title: "企業としてログインしました",
213+
description: "ようこそ!",
187214
});
188215
}}
189216
variant="outlined"

0 commit comments

Comments
 (0)