File size: 4,508 Bytes
a0fda44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
import React from "react";
import * as Yup from "yup";
import { Formik, Form } from "formik";
import FormField from "../../globals/FormField";
import useFetch from "../../../hooks/useFetch";
import { useDispatch } from "react-redux";
import { authActions } from "../../../store/authSlice";
import Spinner from "../../globals/Spinner";
import useChatBot from "../../../hooks/useChatBot";

const schema = Yup.object().shape({
  name: Yup.string()
    .min(1, "Too short")
    .max(50, "Too long")
    .required("Field is required"),
  username: Yup.string()
    .min(1, "Too short")
    .max(50, "Too long")
    .required("Field is required"),
  password: Yup.string().required("Field is required"),
  confirmPassword: Yup.string()
    .required("Field is required")
    .oneOf([Yup.ref("password"), null], "Passwords must match"),
});

function Register({ setUserWantsToLogin }) {
  const dispatch = useDispatch();

  // Add bot as contact
  const { reqFn: addBotAsContact, reqState: addBotState } = useFetch(
    { url: "/contacts", method: "POST" },
    (data) => {
      dispatch(
        authActions.setUserIsNew({
          isNew: true,
          payload: { chatRoomId: data.data.contact.chatRoomId },
        })
      );

      dispatch(authActions.login());
    }
  );

  // Register user
  const { reqFn, reqState } = useFetch(
    { url: "/user/register", method: "POST" },
    () => {
      addBotAsContact({
        name: process.env.REACT_APP_BOT_NAME,
        username: process.env.REACT_APP_BOT_USERNAME,
      });
    }
  );

  return (
    <div className="basis-[35rem]">
      <h1 className="text-cta-icon font-semibold text-[2rem] uppercase mb-[2rem]">
        Join Telegram
      </h1>
      <Formik
        initialValues={{
          name: "",
          username: "",
          password: "",
          confirmPassword: "",
        }}
        validationSchema={schema}
        onSubmit={reqFn}
      >
        {({ errors, values }) => (
          <Form className="flex flex-col gap-[1.5rem]" autoComplete="off">
            <FormField
              labelName="Name"
              labelClassName={`bg-transparent group-focus-within:hidden ${
                values.name && "hidden"
              }`}
              name="name"
              required={true}
              value={values.name}
              error={errors.name}
            />

            <FormField
              labelName="Username"
              labelClassName={`bg-transparent group-focus-within:hidden ${
                values.username && "hidden"
              }`}
              name="username"
              required={true}
              value={values.username}
              error={errors.username}
            />

            <FormField
              labelName="Password"
              labelClassName={`bg-transparent group-focus-within:hidden ${
                values.password && "hidden"
              }`}
              name="password"
              required={true}
              value={values.password}
              error={errors.password}
              fieldType="password"
            />

            <FormField
              labelName="Confirm Password"
              labelClassName={`bg-transparent group-focus-within:hidden ${
                values.confirmPassword && "hidden"
              }`}
              name="confirmPassword"
              required={true}
              value={values.confirmPassword}
              error={errors.confirmPassword}
              fieldType="password"
            />
            <button
              className={`bg-cta-icon mt-[1rem] p-[1rem] rounded-xl uppercase text-white font-semibold opacity-80 flex items-center justify-center ${
                !errors.name &&
                !errors.username &&
                !errors.phoneNumber &&
                !errors.password &&
                !errors.confirmPassword &&
                "opacity-100"
              }`}
              type="submit"
            >
              {reqState !== "loading" && addBotState !== "loading" && "Join"}
              {(reqState === "loading" || addBotState === "loading") && (
                <Spinner className="w-[2.5rem] h-[2.5rem]" />
              )}
            </button>
          </Form>
        )}
      </Formik>
      <div
        onClick={() => setUserWantsToLogin(true)}
        className="mt-[2rem] text-right  text-secondary-text underline cursor-pointer hover:text-cta-icon"
      >
        Already on Telegram
      </div>
    </div>
  );
}

export default Register;