Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
nasubi-dev committed Nov 9, 2024
1 parent cf26f4b commit f0d5514
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
31 changes: 30 additions & 1 deletion src/lib/classes/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Session } from "@supabase/supabase-js";
import { type UseNavigateResult } from "@tanstack/react-router";
import { ResultAsync } from "neverthrow";
import { ok, ResultAsync } from "neverthrow";
import { match } from "ts-pattern";
import { Sower } from "./sower";
import { Sponsor } from "./sponsor";
Expand Down Expand Up @@ -107,4 +107,33 @@ export class User {
Table.transformError(config, "fetchOwnComments", error),
);
}

public fetchKindOf(): TableResult<
| {
type: "SOWER";
data: Sower;
}
| {
type: "SPONSOR";
data: Sponsor;
}
| {
type: "UNKNOWN";
}
> {
const data = ResultAsync.combine([
Sower.factories.fromUser(this.id).orElse(() => ok(null)),
Sponsor.factories.fromUser(this.id).orElse(() => ok(null)),
]);

return data.andThen(([sower, sponsor]) => {
if (sower != null) {
return ok({ type: "SOWER", data: sower } as const);
}
if (sponsor != null) {
return ok({ type: "SPONSOR", data: sponsor } as const);
}
return ok({ type: "UNKNOWN" } as const);
});
}
}
45 changes: 36 additions & 9 deletions src/routes/user/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Field, NumberInput } from "@ark-ui/react";
import { Icon } from "@iconify/react";
import { createFileRoute } from "@tanstack/react-router";
import { createFileRoute, useNavigate } from "@tanstack/react-router";
import { useSetAtom } from "jotai";
import { Grid, styled as p, VStack } from "panda/jsx";
import { useState, type ReactElement } from "react";
import useSWRImmutable from "swr/immutable";
import { z } from "zod";
import { IconText } from "@/components/IconText";
import { Button } from "@/components/cva/Button";
Expand All @@ -13,6 +14,7 @@ import { svaTextArea } from "@/components/sva/textArea";
import { User } from "@/lib/classes/user";
import { $redirectTo } from "@/lib/stores/redirect";
import { notifyTableErrorInToast } from "@/lib/utils/table";
import { toaster } from "@/lib/utils/toast";

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

function Authenticated({ user }: { user: User }): ReactElement {
const [selected, setSelected] = useState<"sower" | "sponsor" | null>();
const [age, setAge] = useState(0);
const [selected, setSelected] = useState<"sower" | "sponsor" | null>(null);
const [description, setDescription] = useState("");
const navigate = useNavigate();
const numberInput = svaNumberInput();
const textArea = svaTextArea();

const swrUserKindOf = useSWRImmutable("userKindOf", async () =>
(
await user.fetchKindOf().mapErr(notifyTableErrorInToast("swrUserKindOf"))
)._unsafeUnwrap(),
);

return (
<Expanded items="center">
<VStack gap="10" p="10">
{selected == null && (
{swrUserKindOf.data?.type === "UNKNOWN" && (
<>
<p.span fontSize="xl" fontWeight="bold">
アカウントタイプを選択して下さい
Expand Down Expand Up @@ -149,7 +158,15 @@ function Authenticated({ user }: { user: User }): ReactElement {
name: user.metadata.name ?? "名無し",
birthday: `${new Date().getFullYear() - age}-10-05T14:48:00.000Z`,
})
.mapErr(notifyTableErrorInToast("User.registerAsASower"));
.mapErr(notifyTableErrorInToast("User.registerAsASower"))
.andTee(() => {
void navigate({ to: "/" });
});
toaster.success({
id: "register-as-sower",
title: "市民としてログインしました",
description: "ようこそ!",
});
}}
variant="outlined"
w="300px"
Expand Down Expand Up @@ -179,11 +196,21 @@ function Authenticated({ user }: { user: User }): ReactElement {
<Button
h="100px"
onClick={() => {
void user.registerAsASponsor({
user_id: user.id,
name: user.metadata.name,
icon: user.metadata.picture,
description,
void user
.registerAsASponsor({
user_id: user.id,
name: user.metadata.name,
icon: user.metadata.picture,
description,
})
.mapErr(notifyTableErrorInToast("Button.企業としてログイン"))
.andTee(() => {
void navigate({ to: "/" });
});
toaster.success({
id: "register-as-sponsor",
title: "企業としてログインしました",
description: "ようこそ!",
});
}}
variant="outlined"
Expand Down

0 comments on commit f0d5514

Please sign in to comment.