You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
3.9 KiB
147 lines
3.9 KiB
import RNRestart from 'react-native-restart'; |
|
import { toastMessage } from "../../utils/message" |
|
import { asyncAlert, asyncAwesomeAlert, asyncApiAwesomeAlert } from "../../utils/AsyncWrappers"; |
|
import AsyncStorage from '@react-native-community/async-storage' |
|
import DeviceInfo from 'react-native-device-info'; |
|
import { |
|
ImageBackground, |
|
Text, |
|
Image, |
|
View, |
|
SafeAreaView, |
|
StyleSheet, |
|
BackHandler, |
|
Platform |
|
} from 'react-native'; |
|
import apiConfig from './config'; |
|
import FetchRetry from './fetch-retry' |
|
import { nextFetchApi } from '.'; |
|
|
|
const fetchRetry = FetchRetry(fetch, {}) |
|
|
|
export const restartApp = () => { |
|
RNRestart.Restart() |
|
} |
|
export const resetApp = () => { |
|
AsyncStorage.setItem('UID_USER', JSON.stringify({}), () => { |
|
RNRestart.Restart() |
|
}); |
|
} |
|
export const networkAlert = async () => { |
|
return await asyncApiAwesomeAlert("خطا", "خطا در اتصال به اینترنت", { |
|
confirmText: "تلاش مجدد" |
|
}) |
|
} |
|
export const unexpectedAlert = async () => { |
|
return await asyncApiAwesomeAlert("خطا", "خطای داخلی سرور!", { |
|
confirmText: "تلاش مجدد" |
|
}) |
|
// return await asyncAlert("خطا","خطای داخلی سرور!", [ |
|
// { text: "تلاش مجدد", value: 'retry' }, |
|
// // { text: "بیخیال", value: 'ignore' } |
|
// ]) |
|
} |
|
export const getToken = async () => { |
|
let user = await AsyncStorage.getItem('UID_USER') |
|
if (!user) return '' |
|
let { token } = JSON.parse(user) |
|
if (!token) { |
|
return '' |
|
} |
|
return 'Bearer ' + token |
|
} |
|
export const deviceHeaders = async () => { |
|
return { |
|
'dev-os': Platform.OS, |
|
'dev-app-version': '1.6.0', |
|
'dev-os-version': Platform.Version + '', |
|
'dev-phone-manufacturer': DeviceInfo.getBrand(), |
|
'dev-phone-model': await DeviceInfo.getDeviceName(), |
|
'dev-phone-hash': DeviceInfo.getUniqueId(), |
|
} |
|
} |
|
export const getUrl = async (endpoint, dev) => { |
|
if (endpoint.includes('splash')) { |
|
if (dev === 'dev') { |
|
return apiConfig.dev + endpoint |
|
} |
|
return apiConfig.splash + endpoint |
|
} |
|
let base_urls = await getBaseUrls() |
|
return `${base_urls.backend}/v1.1${endpoint}` |
|
} |
|
|
|
export const isUnexpected = (statusCode) => { |
|
if (![200, 201, 400, 401, 403].includes(statusCode)) return true |
|
return false |
|
} |
|
export const renewTokenHandler = async () => { |
|
try { |
|
let splashData = JSON.parse(await AsyncStorage.getItem('UID_USER')) |
|
if (!splashData || !splashData.refresh_token) { |
|
return { |
|
ok: false, |
|
restart: true |
|
} |
|
} |
|
const options = { |
|
method: 'POST', |
|
body: JSON.stringify({ |
|
refresh_token: splashData.refresh_token |
|
}), |
|
headers: { |
|
Accept: 'application/json;charset=utf-8', |
|
'Content-Type': 'application/json;charset=utf-8' |
|
} |
|
} |
|
let res = await fetchRetry(await getUrl('/auth/renew-token'), options) |
|
let { data } = await res.json() |
|
const statusCode = res.status |
|
if (statusCode == 200) { |
|
splashData.token = data.token |
|
await AsyncStorage.setItem('UID_USER', JSON.stringify(splashData)) |
|
return { |
|
ok: true |
|
} |
|
} |
|
if (statusCode == 400) { |
|
return { |
|
ok: false, |
|
restart: true |
|
} |
|
} |
|
return { |
|
ok: false |
|
} |
|
} catch (error) { |
|
return { |
|
ok: false |
|
} |
|
} |
|
} |
|
export const getBaseUrls = async () => { |
|
// let { base_urls } = JSON.parse((await AsyncStorage.getItem('SPLASH_DATA'))) |
|
return apiConfig.dev |
|
} |
|
export const getCachedProfile = async () => { |
|
return JSON.parse(await AsyncStorage.getItem('UID_USER')) |
|
} |
|
|
|
export const cacheProfile = async () => { |
|
let data = await getProfile() |
|
let oldData = await getCachedProfile() |
|
if (data) { |
|
await AsyncStorage.removeItem('UID_USER') |
|
|
|
await AsyncStorage.setItem('UID_USER', JSON.stringify({ |
|
...oldData, |
|
...data |
|
})) |
|
} |
|
|
|
} |
|
export const getProfile = async () => { |
|
let { ok, data } = await nextFetchApi('/profile?with_details=1&no_cache=1') |
|
if (!ok) return null |
|
return data |
|
}
|
|
|