Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. import * as SecureStore from "expo-secure-store";
  2. import {
  3. Alert,
  4. ScrollView,
  5. StatusBar,
  6. StyleSheet,
  7. Text,
  8. View,
  9. } from "react-native";
  10. import { Notifications } from "expo";
  11. import * as Permissions from "expo-permissions";
  12. import CodeInput from "react-native-confirmation-code-input";
  13. import CountDown from "react-native-countdown-component";
  14. import React from "react";
  15. import { dh, dw } from "../../Components/Scaler";
  16. import { storeData, retrieveData } from "../../Components/Helpers";
  17. import LoadingScreen from "../../Components/LoadingScreen";
  18. import Styler from "../../Components/Styler";
  19. import Touchable from "../../Components/Touchable";
  20. import api from "../../Constants/Api";
  21. import colors from "../../Constants/Colors";
  22.  
  23. const styles = StyleSheet.create({
  24. ...Styler,
  25. });
  26. export default class CredentialsScreen extends React.Component {
  27. constructor(props) {
  28. super(props);
  29. this.state = {
  30. loading: false,
  31. };
  32. this.codeInputRef2 = React.createRef();
  33. }
  34.  
  35. userCreating = (user) => {
  36. retrieveData("lastUser").then((lastUser) => {
  37. let req;
  38. if (lastUser) {
  39. req = { user, lastUser };
  40. } else {
  41. req = { user, lastUser: null };
  42. }
  43. fetch(`${api}api/user`, {
  44. body: JSON.stringify(req),
  45. headers: {
  46. Accept: "application/json",
  47. "Content-Type": "application/json",
  48. },
  49. method: "POST",
  50. })
  51. .catch((error) => {
  52. console.log(error);
  53. Alert.alert("Проверьте подключение к интернету");
  54. })
  55. .then((response) => response.json())
  56. .then((responseJson) => {
  57. if (responseJson.status === "success") {
  58. if (responseJson.token) {
  59. SecureStore.setItemAsync("access_token", responseJson.token.access);
  60. SecureStore.setItemAsync(
  61. "refresh_token",
  62. responseJson.token.refresh,
  63. );
  64. }
  65. storeData("user", responseJson.user);
  66.  
  67. if (responseJson.user.type === "barista") {
  68. this.props.navigation.navigate("Staff");
  69. } else {
  70. this.props.navigation.navigate("Main", {
  71. username: this.props.navigation.state.params.name,
  72. });
  73. }
  74. } else {
  75. this.setState({ loading: false });
  76. Alert.alert("Неверный код");
  77. }
  78. });
  79. });
  80. }
  81.  
  82. onFinishCheckingCode2 = async (isValid, code) => {
  83. this.setState({ loading: true });
  84. let token;
  85. const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
  86. if (status === "granted") {
  87. try {
  88. token = await Notifications.getExpoPushTokenAsync();
  89. } catch (err) {
  90. this.setState({ loading: false });
  91. console.log(err);
  92. }
  93. }
  94. if (token) {
  95. user.pushToken = token;
  96. }
  97. retrieveData("place").then((place) => {
  98. if (place) {
  99. const user = {
  100. code,
  101. name: this.props.navigation.state.params.name,
  102. phone: this.props.navigation.state.params.phone,
  103. place: place._id,
  104. };
  105. this.userCreating(user);
  106. } else {
  107. const user = {
  108. code,
  109. name: this.props.navigation.state.params.name,
  110. phone: this.props.navigation.state.params.phone,
  111. };
  112. this.userCreating(user);
  113. }
  114. });
  115. };
  116.  
  117. render() {
  118. if (this.state.loading) {
  119. return <LoadingScreen />;
  120. }
  121. return (
  122. <ScrollView
  123. scrollEnabled={false}
  124. keyboardShouldPersistTaps="handled"
  125. style={styles.containerWhite}
  126. contentContainerStyle={styles.containerBlock}
  127. >
  128. <StatusBar barStyle="dark-content" hidden={false} />
  129. <View style={styles.fieldsContainer}>
  130. <View
  131. style={{
  132. ...styles.EndColumn,
  133. flex: 1,
  134. }}
  135. >
  136. <Text style={styles.verificationText}>Введите код из смс:</Text>
  137. </View>
  138. <CodeInput
  139. containerStyle={styles.codeInputContainer}
  140. size={50 * dw}
  141. ref={this.codeInputRef2}
  142. keyboardType="numeric"
  143. codeLength={6}
  144. inactiveColor={colors.gray}
  145. activeColor={colors.gray}
  146. className="border-b"
  147. cellBorderWidth={2 * dw}
  148. compareWithCode="123456"
  149. space={8 * dw}
  150. codeInputStyle={styles.verificationText}
  151. onFulfill={(isValid, code) => this.onFinishCheckingCode2(isValid, code)}
  152. />
  153. </View>
  154. <View style={{ flexDirection: "row" }}>
  155. <Touchable
  156. style={{
  157. ...styles.CenterEndColumn,
  158. flex: 2,
  159. marginHorizontal: 15 * dw,
  160. }}
  161. >
  162. <Text style={{ color: colors.gray, fontSize: 15 * dh }}>
  163. Отправить код повторно
  164. </Text>
  165. </Touchable>
  166. <CountDown
  167. timeToShow={["S"]}
  168. digitBgColor={colors.gray}
  169. timeTxtColor={colors.white}
  170. digitTxtColor={colors.white}
  171. style={{ alignItems: "flex-start", flex: 1 }}
  172. labelS=""
  173. until={59}
  174. onFinish={() => Alert.alert("Период действия кода закончился")}
  175. size={20 * dh}
  176. />
  177. </View>
  178. <View style={styles.lastBlock}>
  179. <Touchable
  180. onPress={() => this.props.navigation.navigate("CredentialsScreen")}
  181. style={styles.button}
  182. >
  183. <Text style={styles.Bold21White}>Назад</Text>
  184. </Touchable>
  185. </View>
  186. </ScrollView>
  187. );
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement