Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. import React, { useContext } from "react";
  2. import { Link as RouterLink, Redirect } from "react-router-dom";
  3. import { userService } from "Services/userService.js";
  4. import { UserContext } from "Services/UserContext.js";
  5. import LoginForm from "Forms/Authorization/Login.js";
  6. import EnterPhone from "Forms/Authorization/EnterPhone";
  7. import Grid from "@material-ui/core/Grid";
  8. import Typography from "@material-ui/core/Typography";
  9. import Link from "@material-ui/core/Link";
  10.  
  11. import {
  12. Facebook,
  13. Google,
  14. PhoneNumber as PhoneButton
  15. } from "Forms/Authorization/Buttons";
  16.  
  17. export default function Login() {
  18. const { user, setUser } = useContext(UserContext);
  19. let think;
  20. const [dialog, setDialog] = React.useState(false);
  21. const [agreed, setAgreed] = React.useState(true);
  22. const [socialFirst, setSocialFirst] = React.useState(false);
  23. const [tokenValid, setTokenValid] = React.useState(false);
  24. const [credits, setCredits] = React.useState({
  25. type: "",
  26. phoneNumber: "",
  27. password: "",
  28. firstName: "",
  29. lastName: "",
  30. country: "TJ",
  31. countryCode: "+992"
  32. });
  33.  
  34. const handleCreditChange = event => {
  35. setCredits({ ...credits, [event.target.name]: event.target.value });
  36. };
  37.  
  38. const handleClickAgreed = agr => {
  39. setAgreed(agr);
  40. };
  41.  
  42. const handleDialogAction = act => {
  43. setDialog(act);
  44. };
  45.  
  46. const handleSubmit = () => {
  47. if (credits.type == "phoneNumber") {
  48. let sendCredits = {
  49. "phoneNumber": credits.countryCode.slice(1) + credits.phoneNumber,
  50. "password": credits.password
  51. }
  52. userService.login(sendCredits).then(setUser);
  53. }
  54. else {
  55. if (agreed) {
  56. let sendCredits = credits;
  57. sendCredits["phoneNumber"] =
  58. credits.countryCode.slice(1) + credits.phoneNumber;
  59. userService.regin(sendCredits).then(setUser);
  60. } else {
  61. handleDialogAction(true);
  62. }
  63. }
  64. };
  65.  
  66. const phoneLogin = () => {
  67. setCredits({ ...credits, type: "phoneNumber" });
  68. };
  69.  
  70. const responseFacebook = response => {
  71. if (response) {
  72. console.log(response);
  73. const fb = {
  74. phoneNumber: "",
  75. name: response.name,
  76. firstName: response.first_name,
  77. lastName: response.last_name,
  78. facebook_id: response.userID,
  79. facebook_token: response.accessToken,
  80. type: "facebook",
  81. country: "TJ",
  82. countryCode: "+992"
  83. };
  84. setCredits(fb);
  85. userService.checkTokenId(fb.facebook_id, fb.facebook_token, "facebook").then(res => {setTokenValid(res);});
  86. } else {
  87. alert("Error");
  88. }
  89. };
  90.  
  91. const responseGoogle = response => {
  92. if (response) {
  93. const ggl = {
  94. phone_number: "",
  95. name: response.profileObj.name,
  96. first_name: response.profileObj.givenName,
  97. last_name: response.profileObj.familyName,
  98. auth_id: response.profileObj.googleId,
  99. auth_token: response.accessToken,
  100. type: "google"
  101. };
  102. setCredits(ggl);
  103. } else {
  104. alert("Error");
  105. }
  106. };
  107.  
  108. if (tokenValid) {
  109. userService.isUserOld(credits.facebook_id, credits.type).then(res => {
  110. res ? setUser(res) : setSocialFirst(true)
  111. });
  112.  
  113. setTokenValid(false);
  114. }
  115.  
  116. if (credits.type == "phoneNumber") {
  117. think = (
  118. <LoginForm
  119. credits={credits}
  120. dialog={dialog}
  121. agreed={agreed}
  122. onSubmit={handleSubmit}
  123. onCreditChange={handleCreditChange}
  124. onClickAgreed={handleClickAgreed}
  125. onDialogAction={handleDialogAction}
  126. />
  127. );
  128. }
  129. else if (socialFirst) {
  130. think = (
  131. <EnterPhone
  132. credits={credits}
  133. dialog={dialog}
  134. agreed={agreed}
  135. onSubmit={handleSubmit}
  136. onCreditChange={handleCreditChange}
  137. onClickAgreed={handleClickAgreed}
  138. onDialogAction={handleDialogAction}
  139. />
  140. )
  141. }
  142. else {
  143. think = <>
  144. <Typography component="h1" variant="h5">
  145. Вход
  146. </Typography>
  147.  
  148. <Grid container>
  149. <Grid item xs={12}>
  150. <Facebook responseFacebook={responseFacebook} type={'login'} />
  151. </Grid>
  152. <Grid item xs={12}>
  153. <Google responseGoogle={responseGoogle} type={'login'} />
  154. </Grid>
  155. <Grid item xs={12}>
  156. <PhoneButton phoneRegistration={phoneLogin} type={'login'} />
  157. </Grid>
  158. </Grid>
  159. <Grid container justify="flex-end">
  160. <Grid item>
  161. <Link component={RouterLink} to={"/login"} variant="body2">
  162. Уже зарегистрированы? Войти
  163. </Link>
  164. </Grid>
  165. </Grid>
  166. </>;
  167. }
  168.  
  169. return Boolean(user) ? <Redirect to="/" /> : think ;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement