summaryrefslogtreecommitdiff
path: root/src/components/Login2.tsx
blob: 6c26efc8799ee294da1bb6520098346d0b50942d (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"use client";

import { FormState, postLogin, postRegister } from "@/actions/login";

import { useActionState, useEffect, useState } from "react";
import {
  Card,
  CardHeader,
  CardDescription,
  CardContent,
  CardFooter,
  CardTitle,
} from "@/components/ui/card";
import { Label } from "@/components/ui/label";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";

export default function AuthScreen() {
  const [isReg, setReg] = useState(false);
  return <OOldform isReg={isReg} toggle={() => setReg((b) => !b)} />;
}

function OOldform({ isReg, toggle }: { isReg: boolean; toggle: () => void }) {
  const regstrings = {
    title: "Welcome to Sorlang",
    desc: "Sign up",
    button: "Sign up",
    toggle: "Have an account?",
    toggle2: "Login",
  };
  const logstrings = {
    title: "Welcome back",
    desc: "Login to Sorlang",
    button: "Login",
    toggle: "Don't have an account?",
    toggle2: "Sign up",
  };
  const [strings, setStrings] = useState(logstrings);

  useEffect(() => {
    if (isReg) setStrings(regstrings);
    else setStrings(logstrings);
  }, [isReg]);

  // const [state, formAction, isPending] = useActionState<FormState, FormData>(
  //   isReg ? postRegister : postLogin,
  //   { error: "" },
  //   "/login",
  // );
  const [state, formAction, isPending] = useActionState<FormState, FormData>(
    postLogin,
    { error: "" },
    "/login",
  );
  console.log({ state });
  return (
    <form action={formAction}>
      <div className="flex flex-col gap-6">
        <Card>
          <CardHeader className="text-center">
            <CardTitle className="text-xl">{strings.title}</CardTitle>
            <CardDescription>{strings.desc}</CardDescription>
          </CardHeader>
          <CardContent>
            <div className="grid gap-6">
              <div className="grid gap-6">
                <Label>
                  Username
                  <Input placeholder="shadcn" name="username" />
                  {state.name && <p>{state.name}</p>}
                </Label>
                <Label>
                  {isReg ? (
                    "Password"
                  ) : (
                    <Label className="flex justify-between">
                      <span>Password</span>
                      <a
                        href="#"
                        className="ml-auto text-sm underline-offset-4 hover:underline"
                      >
                        Forgot your password?
                      </a>
                    </Label>
                  )}
                  <Input type="password" placeholder="..." name="password" />
                  {state.password && <p>{state.password}</p>}
                </Label>
              </div>
              <Button type="submit" className="w-full">
                {strings.button}
              </Button>
              <div className="text-center text-sm">
                {strings.toggle}
                <a
                  onClick={toggle}
                  href="#"
                  className="underline underline-offset-4"
                >
                  {strings.toggle2}
                </a>
              </div>
            </div>
            {state.error && <p className="text-red">{state.error}</p>}
          </CardContent>
        </Card>
        <div className="text-balance text-center text-xs text-muted-foreground [&_a]:underline [&_a]:underline-offset-4 [&_a]:hover:text-primary  ">
          By clicking continue, you agree to our{" "}
          <a href="#">Terms of Service</a> and <a href="#">Privacy Policy</a>.
        </div>
      </div>
    </form>
    // <form action={formAction} aria-disabled={isPending}>
    //   {state.msg}
    //   <label>
    //     Username
    //     <input type="text" placeholder="shadcn" name="username" />
    //   </label>
    //   <label className="flex justify-between">
    //     <span>Password</span>
    //     <a
    //       href="#"
    //       className="ml-auto text-sm underline-offset-4 hover:underline"
    //     >
    //       Forgot your password?
    //     </a>
    //     <input type="password" placeholder="..." name="password" />
    //   </label>
    //   <button type="submit" className="w-full">
    //     Login
    //   </button>
    //   <div className="text-center text-sm">
    //     Don&apos;t have an account?{" "}
    //     <a href="#" className="underline underline-offset-4">
    //       Sign up
    //     </a>
    //   </div>
    // </form>
  );
}