diff options
author | Ivan Chen <ivan@tagg.id> | 2021-02-09 11:25:10 -0500 |
---|---|---|
committer | Ivan Chen <ivan@tagg.id> | 2021-02-09 11:25:10 -0500 |
commit | a25ec0b95017a9b6e0c3392f8fe1ad4c604de520 (patch) | |
tree | 370d6144466ffb82937adfb3a7737374656e67ed | |
parent | f28ef2d4ac61475d7bd9728634b80f7c0760ff58 (diff) |
added newVersionAvailable to store, added logic to request newest version
-rw-r--r-- | src/constants/api.ts | 2 | ||||
-rw-r--r-- | src/routes/Routes.tsx | 34 | ||||
-rw-r--r-- | src/screens/onboarding/Login.tsx | 12 | ||||
-rw-r--r-- | src/services/CommonService.ts | 11 | ||||
-rw-r--r-- | src/store/actions/user.ts | 32 | ||||
-rw-r--r-- | src/store/initialStates.ts | 1 | ||||
-rw-r--r-- | src/store/reducers/userReducer.ts | 5 |
7 files changed, 71 insertions, 26 deletions
diff --git a/src/constants/api.ts b/src/constants/api.ts index 32631be0..165bd550 100644 --- a/src/constants/api.ts +++ b/src/constants/api.ts @@ -3,7 +3,7 @@ const BASE_URL: string = 'http://127.0.0.1:8000/'; // local server const API_URL: string = BASE_URL + 'api/'; export const LOGIN_ENDPOINT: string = API_URL + 'login/'; -export const LOGOUT_ENDPOINT: string = API_URL + 'logout/'; +export const VERSION_ENDPOINT: string = API_URL + 'version/'; export const REGISTER_ENDPOINT: string = API_URL + 'register/'; export const EDIT_PROFILE_ENDPOINT: string = API_URL + 'edit-profile/'; export const SEND_OTP_ENDPOINT: string = API_URL + 'send-otp/'; diff --git a/src/routes/Routes.tsx b/src/routes/Routes.tsx index a5383a47..1cbc9bc5 100644 --- a/src/routes/Routes.tsx +++ b/src/routes/Routes.tsx @@ -1,18 +1,23 @@ -import React, {useEffect} from 'react'; -import NavigationBar from './tabs'; -import Onboarding from './onboarding'; -import {useSelector, useDispatch} from 'react-redux'; +import messaging from '@react-native-firebase/messaging'; +import React, {useEffect, useState} from 'react'; +import DeviceInfo from 'react-native-device-info'; +import SplashScreen from 'react-native-splash-screen'; +import {useDispatch, useSelector} from 'react-redux'; +import {fcmService, getLiveVersion} from '../services'; +import { + updateNewNotificationReceived, + updateNewVersionAvailable, +} from '../store/actions'; import {RootState} from '../store/rootReducer'; import {userLogin} from '../utils'; -import SplashScreen from 'react-native-splash-screen'; -import messaging from '@react-native-firebase/messaging'; -import {updateNewNotificationReceived} from '../store/actions'; -import {fcmService} from '../services'; +import Onboarding from './onboarding'; +import NavigationBar from './tabs'; const Routes: React.FC = () => { const { user: {userId}, } = useSelector((state: RootState) => state.user); + const [newVersionAvailable, setNewVersionAvailable] = useState(false); const dispatch = useDispatch(); /** @@ -47,7 +52,18 @@ const Routes: React.FC = () => { } }); - return userId ? <NavigationBar /> : <Onboarding />; + useEffect(() => { + const checkVersion = async () => { + const liveVersion = await getLiveVersion(); + if (liveVersion && liveVersion !== DeviceInfo.getVersion()) { + setNewVersionAvailable(true); + dispatch(updateNewVersionAvailable(true)); + } + }; + checkVersion(); + }); + + return userId && !newVersionAvailable ? <NavigationBar /> : <Onboarding />; }; export default Routes; diff --git a/src/screens/onboarding/Login.tsx b/src/screens/onboarding/Login.tsx index 2db039c1..4d1fb09a 100644 --- a/src/screens/onboarding/Login.tsx +++ b/src/screens/onboarding/Login.tsx @@ -13,7 +13,7 @@ import { TouchableOpacity, } from 'react-native'; import SplashScreen from 'react-native-splash-screen'; -import {useDispatch} from 'react-redux'; +import {useDispatch, useSelector} from 'react-redux'; import {Background, TaggInput, TaggSquareButton} from '../../components'; import {LOGIN_ENDPOINT, usernameRegex} from '../../constants'; import { @@ -25,6 +25,7 @@ import { } from '../../constants/strings'; import {OnboardingStackParams} from '../../routes/onboarding'; import {fcmService} from '../../services'; +import {RootState} from '../../store/rootReducer'; import {BackgroundGradientType, UserType} from '../../types'; import {normalize, userLogin} from '../../utils'; @@ -45,11 +46,6 @@ const Login: React.FC<LoginProps> = ({navigation}: LoginProps) => { // ref for focusing on input fields const inputRef = useRef(); - const NO_USER: UserType = { - userId: '', - username: '', - }; - // login form state const [form, setForm] = React.useState({ username: '', @@ -59,7 +55,7 @@ const Login: React.FC<LoginProps> = ({navigation}: LoginProps) => { attemptedSubmit: false, token: '', }); - const [user, setUser] = useState<UserType>(NO_USER); + const {newVersionAvailable} = useSelector((state: RootState) => state.user); /** * Redux Store stuff @@ -167,7 +163,6 @@ const Login: React.FC<LoginProps> = ({navigation}: LoginProps) => { userLogin(dispatch, {userId: data.UserID, username}); fcmService.sendFcmTokenToServer(); } catch (err) { - setUser(NO_USER); console.log(data); Alert.alert(ERROR_INVALID_LOGIN); } @@ -216,6 +211,7 @@ const Login: React.FC<LoginProps> = ({navigation}: LoginProps) => { style={styles.container} gradientType={BackgroundGradientType.Light}> <StatusBar barStyle="light-content" /> + {/* <Modal visible={newVersionAvailable} /> */} <KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : 'height'} style={styles.keyboardAvoidingView}> diff --git a/src/services/CommonService.ts b/src/services/CommonService.ts index dfbbf70e..9fa7417f 100644 --- a/src/services/CommonService.ts +++ b/src/services/CommonService.ts @@ -1,4 +1,5 @@ import RNFetchBlob from 'rn-fetch-blob'; +import {VERSION_ENDPOINT} from '../constants'; export const loadImageFromURL = async (url: string) => { try { @@ -20,3 +21,13 @@ export const loadImageFromURL = async (url: string) => { return undefined; } }; + +export const getLiveVersion = async () => { + try { + const response = await fetch(VERSION_ENDPOINT, {method: 'GET'}); + return response.status === 200 ? await response.json() : undefined; + } catch (error) { + console.log(error); + return undefined; + } +}; diff --git a/src/store/actions/user.ts b/src/store/actions/user.ts index 5f49a103..589e6f0d 100644 --- a/src/store/actions/user.ts +++ b/src/store/actions/user.ts @@ -1,18 +1,19 @@ -import { CommentThreadType } from './../../types/types'; -import {RootState} from '../rootReducer'; -import {UserType} from '../../types/types'; -import {loadProfileInfo, loadAvatar, loadCover} from '../../services'; import {Action, ThunkAction} from '@reduxjs/toolkit'; +import {loadAvatar, loadCover, loadProfileInfo} from '../../services'; +import {UserType} from '../../types/types'; +import {getTokenOrLogout} from '../../utils'; import { - userLoggedIn, - userDetailsFetched, - socialEdited, profileCompletionStageUpdated, setIsOnboardedUser, setNewNotificationReceived, + setNewVersionAvailable, setReplyPosted, + socialEdited, + userDetailsFetched, + userLoggedIn, } from '../reducers'; -import {getTokenOrLogout} from '../../utils'; +import {RootState} from '../rootReducer'; +import {CommentThreadType} from './../../types/types'; /** * Entry point to our store. @@ -98,6 +99,21 @@ export const updateIsOnboardedUser = ( } }; +export const updateNewVersionAvailable = ( + newVersionAvailable: boolean, +): ThunkAction<Promise<void>, RootState, unknown, Action<string>> => async ( + dispatch, +) => { + try { + dispatch({ + type: setNewVersionAvailable.type, + payload: {newVersionAvailable}, + }); + } catch (error) { + console.log(error); + } +}; + export const updateNewNotificationReceived = ( newNotificationReceived: boolean, ): ThunkAction<Promise<void>, RootState, unknown, Action<string>> => async ( diff --git a/src/store/initialStates.ts b/src/store/initialStates.ts index 8d137a5d..6ca133e0 100644 --- a/src/store/initialStates.ts +++ b/src/store/initialStates.ts @@ -44,6 +44,7 @@ export const NO_USER_DATA = { avatar: <string | null>'', cover: <string | null>'', isOnboardedUser: false, + newVersionAvailable: false, newNotificationReceived: false, replyPosted: <CommentThreadType | undefined>undefined, }; diff --git a/src/store/reducers/userReducer.ts b/src/store/reducers/userReducer.ts index 1e575339..29ec38cc 100644 --- a/src/store/reducers/userReducer.ts +++ b/src/store/reducers/userReducer.ts @@ -57,6 +57,10 @@ const userDataSlice = createSlice({ setReplyPosted: (state, action) => { state.replyPosted = action.payload.replyPosted; }, + + setNewVersionAvailable: (state, action) => { + state.newVersionAvailable = action.payload.newVersionAvailable; + }, }, }); @@ -66,6 +70,7 @@ export const { socialEdited, profileCompletionStageUpdated, setIsOnboardedUser, + setNewVersionAvailable, setNewNotificationReceived, setReplyPosted, } = userDataSlice.actions; |