Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit f0d5514
Author: nasubi916 <[email protected]>
Date:   Sat Nov 9 22:44:29 2024 +0900

    ok

commit cf26f4b
Author: nasubi916 <[email protected]>
Date:   Fri Nov 8 00:34:02 2024 +0900

    スポンサーログイン

commit 9af88a5
Author: nasubi916 <[email protected]>
Date:   Fri Nov 8 00:08:06 2024 +0900

    年齢入力

commit 7bba08c
Author: nasubi916 <[email protected]>
Date:   Thu Nov 7 23:04:23 2024 +0900

    仮アカウントログインからgoogleに変更

commit dd23d39
Author: nasubi916 <[email protected]>
Date:   Tue Nov 5 23:05:48 2024 +0900

    💄 ユーザー認証画面にアカウントタイプ選択ボタンを追加
  • Loading branch information
nasubi-dev committed Nov 9, 2024
1 parent 30d2e9e commit 8dc2a90
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 13 deletions.
39 changes: 35 additions & 4 deletions 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 @@ -36,9 +36,11 @@ export class User {

static async signIn(): Promise<void> {
match(
await supabase.auth.signInWithPassword({
email: "[email protected]",
password: "password",
await supabase.auth.signInWithOAuth({
provider: "google",
options: {
redirectTo: `${document.location.origin}/user/auth`,
},
}),
).with(S.Error, ({ error }) => {
notifyErrorInToast("User.signIn", error, "サインインに失敗しました");
Expand Down Expand Up @@ -105,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);
});
}
}
46 changes: 37 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,24 @@ 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 +159,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 +197,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 8dc2a90

Please sign in to comment.