Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 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.  
  95. retrieveData("place").then((place) => {
  96. if (place) {
  97. const user = {
  98. code,
  99. name: this.props.navigation.state.params.name,
  100. phone: this.props.navigation.state.params.phone,
  101. place: place._id,
  102. };
  103. if (token) {
  104. user.pushToken = token;
  105. }
  106. this.userCreating(user);
  107. } else {
  108. const user = {
  109. code,
  110. name: this.props.navigation.state.params.name,
  111. phone: this.props.navigation.state.params.phone,
  112. };
  113. if (token) {
  114. user.pushToken = token;
  115. }
  116. this.userCreating(user);
  117. }
  118. });
  119. };
  120.  
  121. render() {
  122. if (this.state.loading) {
  123. return <LoadingScreen />;
  124. }
  125. return (
  126. <ScrollView
  127. scrollEnabled={false}
  128. keyboardShouldPersistTaps="handled"
  129. style={styles.containerWhite}
  130. contentContainerStyle={styles.containerBlock}
  131. >
  132. <StatusBar barStyle="dark-content" hidden={false} />
  133. <View style={styles.fieldsContainer}>
  134. <View
  135. style={{
  136. ...styles.EndColumn,
  137. flex: 1,
  138. }}
  139. >
  140. <Text style={styles.verificationText}>Введите код из смс:</Text>
  141. </View>
  142. <CodeInput
  143. containerStyle={styles.codeInputContainer}
  144. size={50 * dw}
  145. ref={this.codeInputRef2}
  146. keyboardType="numeric"
  147. codeLength={6}
  148. inactiveColor={colors.gray}
  149. activeColor={colors.gray}
  150. className="border-b"
  151. cellBorderWidth={2 * dw}
  152. compareWithCode="123456"
  153. space={8 * dw}
  154. codeInputStyle={styles.verificationText}
  155. onFulfill={(isValid, code) => this.onFinishCheckingCode2(isValid, code)}
  156. />
  157. </View>
  158. <View style={{ flexDirection: "row" }}>
  159. <Touchable
  160. style={{
  161. ...styles.CenterEndColumn,
  162. flex: 2,
  163. marginHorizontal: 15 * dw,
  164. }}
  165. >
  166. <Text style={{ color: colors.gray, fontSize: 15 * dh }}>
  167. Отправить код повторно
  168. </Text>
  169. </Touchable>
  170. <CountDown
  171. timeToShow={["S"]}
  172. digitBgColor={colors.gray}
  173. timeTxtColor={colors.white}
  174. digitTxtColor={colors.white}
  175. style={{ alignItems: "flex-start", flex: 1 }}
  176. labelS=""
  177. until={59}
  178. onFinish={() => Alert.alert("Период действия кода закончился")}
  179. size={20 * dh}
  180. />
  181. </View>
  182. <View style={styles.lastBlock}>
  183. <Touchable
  184. onPress={() => this.props.navigation.navigate("CredentialsScreen")}
  185. style={styles.button}
  186. >
  187. <Text style={styles.Bold21White}>Назад</Text>
  188. </Touchable>
  189. </View>
  190. </ScrollView>
  191. );
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement