forked from Alovoa/alovoa-expo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobal.tsx
141 lines (125 loc) · 3.93 KB
/
Global.tsx
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
import React from "react";
import { Platform, ToastAndroid } from 'react-native';
import axios, { AxiosResponse } from 'axios';
import * as SecureStore from 'expo-secure-store';
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as URL from "./URL";
import { createNavigationContainerRef, CommonActions } from '@react-navigation/native';
import Toast from 'react-native-root-toast';
import { ConversationDto, UserDto } from "./types";
export const FLAG_FDROID = true;
export const navigationRef = createNavigationContainerRef()
export const INDEX_LOGIN = "0"
export const INDEX_REGISTER = "1"
export const INDEX_ONBOARDING = "2"
export const INDEX_MAIN = "3"
export const STORAGE_FIRSTNAME = "firstName";
export const STORAGE_PAGE = "page";
export const STORAGE_SCREEN = "screen";
export const STORAGE_YOUR_PROFILE = "your-profile"
export const STORAGE_YOUR_CHAT = "chat"
export const STORAGE_YOUR_CHAT_DETAIL = "chat/%s"
export const STORAGE_LIKES = "likes"
export const STORAGE_DONATE = "donate"
export const STORAGE_LATITUDE = "latitude"
export const STORAGE_LONGITUDE = "longitude"
export const SCREEN_YOURPROFILE = "YourProfile"
export const SCREEN_CHAT = "Chat"
export const SCREEN_SEARCH = "Search"
export const SCREEN_LIKES = "Likes"
export const SCREEN_DONATE = "Donate"
export async function Fetch(url: string = "", method: string = "get", data: any = undefined,
contentType: string = "application/json"): Promise<AxiosResponse<any, any>> {
try {
let res = await axios({
withCredentials: true,
method: method,
url: url,
headers: {
'Content-Type': contentType
},
data: data,
})
if (res.request.responseURL == URL.AUTH_LOGIN) {
SetStorage(STORAGE_PAGE, INDEX_LOGIN);
navigate("Login");
throw new Error("Not authenticated")
}
return res;
} catch (e) {
throw e;
}
}
export function nagivateProfile(user?: UserDto, idEnc?: string) {
navigate("Profile", false, {
user: user,
idEnc: idEnc
});
}
export function nagivateChatDetails(conversation: ConversationDto) {
navigate("MessageDetail", false, {
conversation: conversation
});
}
export function navigate(name: string, reset: boolean = false, params?: any) {
if (navigationRef.isReady()) {
if (!reset) {
navigationRef.navigate(name, params);
} else {
navigationRef.dispatch(
CommonActions.reset({
index: 1,
routes: [{ name: name }],
})
);
}
}
}
export async function GetStorage(key: string): Promise<string | null> {
if (Platform.OS === 'web') {
return await AsyncStorage.getItem(key);
} else {
return await SecureStore.getItemAsync(key);
}
}
export async function SetStorage(key: string, value: string) {
if (Platform.OS === 'web') {
await AsyncStorage.setItem(key, value);
} else {
await SecureStore.setItemAsync(key, value);
}
}
export function loadPage(page: string = INDEX_REGISTER) {
if (INDEX_ONBOARDING == page) {
navigate("Onboarding");
} else if (INDEX_MAIN == page) {
navigate("Main", true);
} else {
navigate("Register");
}
}
export function ShowToast(text: string) {
if (Platform.OS === 'android') {
ToastAndroid.show(text, ToastAndroid.LONG);
} else {
Toast.show(text, {
duration: Toast.durations.LONG,
backgroundColor: "#424242"
});
}
}
export function isEmailValid(text: string) {
let reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return reg.test(text);
}
export function isPasswordSecure(password: string) {
const minPasswordLength = 7;
if (password.length < minPasswordLength) {
return false;
} else if (password.match(/[a-z]/i) && password.match(/[0-9]+/)) {
return true;
} else {
return false;
}
}
export const format = (str: string, ...args: any[]) => args.reduce((s, v) => s.replace('%s', v), str);