File size: 3,745 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
  Divider,
  Flex,
  Heading,
  Text,
  useColorModeValue,
  VStack,
} from "@chakra-ui/react";
import React, { useEffect } from "react";
import { Navigate, useNavigate } from "react-router-dom";
import SignUpButton from "../utils/SignUpButton";
import { signUpBtnDesigns } from "../utils/signUpBtnDesign";
import { signInWithPopup } from "firebase/auth";
import { useAuth } from "../context/auth";
import { auth } from "../config/firebase";
import { createUser } from "../lib/api";
import { useState } from "react";
import useGenerateUserName from "../hooks/useGenerateUserName";
import { useSelector } from "react-redux";

const SignUp = ({ type }) => {
  //scroll top
  useEffect(() => window.scrollTo(0, 0), []);

  const navigate = useNavigate();
  const getUsername = useGenerateUserName();

  const [signingIn, setSigningIn] = useState(false);

  const { profileData } = useSelector((state) => state.profileData);

  const user = useAuth();

  const cardBg = useColorModeValue("light.cardBg", "dark.cardBg");
  const color = useColorModeValue("light.primary", "dark.primary");
  const dividerColor = useColorModeValue("light.cardBorder", "dark.cardBorder");

  if (!signingIn && user) {
    return <Navigate to="/" />;
  }

  const signin = (provider) => {
    setSigningIn(true);
    signInWithPopup(auth, provider)
      .then((res) => {
        const userId = res.user.uid;
        const authenticatedUser = profileData?.find(
          (userData) => userData.id === userId
        );

        const username = authenticatedUser
          ? authenticatedUser.username
          : getUsername(res.user.displayName, userId);

        const userData = {
          name: res.user.displayName,
          username,
          profile: res.user.photoURL,
          createdAt: res.user.metadata.createdAt,
        };

        createUser(userId, userData)
          .then((_) => {
            navigate(-1);

            console.log("created user successfully");
          })
          .catch((err) => {
            console.log("failed to create user");
          });
      })
      .catch((err) => {
        setSigningIn(false);
        console.log(err.message);
      });
  };

  return (
    <VStack mx="auto" justify="center" maxW="640px" flex="1">
      <VStack
        bg={cardBg}
        className="shadow"
        p={{ base: "2rem 1rem", sm: "3rem" }}
        textAlign="center"
        borderRadius="5px"
      >
        <Heading fontSize="1.7rem">Welcome to DEV Community</Heading>

        <Text mb="1rem !important">
          <Text
            as="span"
            color={color}
            cursor="pointer"
            onClick={() => navigate("/")}
          >
            DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»
          </Text>{" "}
          is a community of 890,178 amazing developers
        </Text>

        {signUpBtnDesigns.map((obj) => (
          <SignUpButton
            {...obj}
            onClick={() => signin(obj.signInMethod)}
            key={obj.logo}
            type={type === "login" ? "Continue with" : "Sign up with"}
          />
        ))}

        {type !== "login" && (
          <Flex w="100%" align="center" mt="1.5rem !important">
            <Divider flex={1} bg={dividerColor} h="1.5px" />
            <Text fontSize="14px" mx="1rem">
              Already have an account?{" "}
              <Text
                as="span"
                color={color}
                cursor="pointer"
                onClick={() => navigate("/login")}
              >
                Log in
              </Text>
              .
            </Text>
            <Divider flex={1} bg={dividerColor} h="1.5px" />
          </Flex>
        )}
      </VStack>
    </VStack>
  );
};

export default SignUp;