mooving 30s request into navigation

master
David D'ALMEIDA 2 years ago
parent 2ae912d64c
commit 2faef25e44

@ -6,31 +6,14 @@ import { useDispatch, useSelector } from 'react-redux';
import { useEffect, useState } from 'react';
import * as SplashScreen from 'expo-splash-screen';
import { ChangeMode, getRefreshToken } from '../redux/thunk/authThunk';
import * as Location from 'expo-location';
import SpotifyService from '../services/spotify/spotify.service';
import { getCurrentUserMusic, getSpotList } from '../redux/thunk/spotThunk';
import Music from '../Model/Music';
import axios from 'axios';
import qs from 'qs';
import * as SecureStore from 'expo-secure-store';
import { MY_SECURE_AUTH_STATE_KEY } from '../screens/Register';
export default function AuthNavigation() {
//@ts-ignore
const tokenProcesed: boolean = useSelector(state => state.userReducer.loading);
//@ts-ignore
const isLogin: boolean = useSelector(state => state.userReducer.isLogedIn);
//@ts-ignore
const currentMusic: Music = useSelector(state => state.appReducer.userCurrentMusic);
//@ts-ignore
const tokenSend: string = useSelector(state => state.userReducer.userFladToken);
const isLogin: boolean = useSelector(state => state.userReducer.isLogedIn);
const [appIsReady, setAppIsReady] = useState(false);
const dispatch = useDispatch();
const [location, setLocation] = useState<Location.LocationObject>();
const [setErrorMsg] = useState('');
async function prepare() {
//@ts-ignore
await dispatch(getRefreshToken())
@ -51,72 +34,11 @@ export default function AuthNavigation() {
console.log(`Une erreur s'est produite lors de la mise à jour de la valeur booléenne pour la clé 'dark': `, error);
}
}
const requestLocationPermission = async () => {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
console.log('Permission to access location was denied');
} else {
console.log('Permission to access location was granted');
}
}
useEffect(() => {
ChangeDarkMode();
prepare();
requestLocationPermission();
const sendLocationUpdate = async () => {
try {
let tmpKey: string = await SecureStore.getItemAsync(MY_SECURE_AUTH_STATE_KEY) ;
//@ts-ignore
await dispatch(getCurrentUserMusic(new SpotifyService(tmpKey)))
let { status } = await Location.requestForegroundPermissionsAsync();
if (status == 'granted') {
// should app is ready
const locationresp = await Location.getCurrentPositionAsync({});
// send location to server
if(currentMusic){
const body: Record<string, string | boolean | number | (string | boolean | number)[]> = {
longitude: locationresp.coords.longitude,
latitude: locationresp.coords.latitude,
currentMusic: currentMusic.id
}
const resp = await axios({
url: 'https://flad-api-production.up.railway.app/api/users/nextTo?' + qs.stringify(body),
method: 'GET',
headers: {
Authorization: `Bearer ${tokenSend}`,
},
});
const datat: Record<string, string> = resp.data.listUser2;
//@ts-ignore
dispatch(getSpotList(datat, new SpotifyService(tmpKey)))
}
else{
return;
}
}
else {
//@ts-ignore
let {status} = Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
return;
}
} catch (error) {
console.log(error);
}
};
const interval = setInterval(sendLocationUpdate, 5000);
return () => {
clearInterval(interval);
};
}, [appIsReady, tokenProcesed, currentMusic]);
}, [appIsReady, tokenProcesed]);
if (tokenProcesed == false) {

@ -1,4 +1,4 @@
import React, { useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import { View, StyleSheet, Platform } from 'react-native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { NavigationContainer } from '@react-navigation/native';
@ -13,10 +13,91 @@ import MessagingNavigation from './MessagingNavigation';
import { useDispatch, useSelector } from 'react-redux';
import { GraphicalCharterDark } from '../assets/GraphicalCharterDark';
import { GraphicalCharterLight } from '../assets/GraphicalCharterLight';
import { getCurrentUserMusic } from '../redux/thunk/spotThunk';
import { getCurrentUserMusic, getSpotList } from '../redux/thunk/spotThunk';
import SpotifyService from '../services/spotify/spotify.service';
import * as SecureStore from 'expo-secure-store';
import { MY_SECURE_AUTH_STATE_KEY } from '../screens/Register';
import * as Location from 'expo-location';
import axios from 'axios';
import qs from 'qs';
export default function Navigation() {
const [setErrorMsg] = useState('');
const [location, setLocation] = useState<Location.LocationObject>();
//@ts-ignore
const tokenSend: string = useSelector(state => state.userReducer.userFladToken);
//@ts-ignore
const currentMusic: Music = useSelector(state => state.appReducer.userCurrentMusic);
const dispatch = useDispatch();
const requestLocationPermission = async () => {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
console.log('Permission to access location was denied');
} else {
console.log('Permission to access location was granted');
}
}
useEffect(() => {
requestLocationPermission();
const sendLocationUpdate = async () => {
try {
let tmpKey: string = await SecureStore.getItemAsync(MY_SECURE_AUTH_STATE_KEY) ;
//@ts-ignore
dispatch(getCurrentUserMusic(new SpotifyService(tmpKey)))
let { status } = await Location.requestForegroundPermissionsAsync();
if (status == 'granted') {
// should app is ready
const locationresp = await Location.getCurrentPositionAsync({});
// send location to server
if(currentMusic){
const body: Record<string, string | boolean | number | (string | boolean | number)[]> = {
longitude: locationresp.coords.longitude,
latitude: locationresp.coords.latitude,
currentMusic: currentMusic.id
}
const resp = await axios({
url: 'https://flad-api-production.up.railway.app/api/users/nextTo?' + qs.stringify(body),
method: 'GET',
headers: {
Authorization: `Bearer ${tokenSend}`,
},
});
const datat: Record<string, string> = resp.data.listUser2;
//@ts-ignore
dispatch(getSpotList(datat, new SpotifyService(tmpKey)))
}
else{
return;
}
}
else {
//@ts-ignore
let {status} = Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
return;
}
} catch (error) {
console.log(error);
}
};
const interval = setInterval(sendLocationUpdate, 5000);
return () => {
clearInterval(interval);
};
}, [currentMusic]);
// @ts-ignore
const isDark = useSelector(state => state.userReducer.dark);
const style = isDark ? GraphicalCharterDark : GraphicalCharterLight;
const BottomTabNavigator = createBottomTabNavigator();
@ -31,16 +112,6 @@ export default function Navigation() {
};
//@ts-ignore
const favoritesMusicLength: number = useSelector(state => state.appReducer.favoriteMusic.length);
const dispatch = useDispatch();
const token = "BQBNdaYRkD3GAOFASk8uc-l72zVwQeQ0sFB4GJnkBGudsJHnuAXd4eIWb78gbFLKZeBoHrWpHxMeSmqvHk75Utg9fsOJp7XyJfm-tAlgGhUQ-xiUM8rXTpa9k3M40BMSnujPDrap_O1ChCyGhBWYVHDd2t67qY0NVDvCJ4Qz7LucJJdgu1BN838qXTScQV90zriO8lp6Rjx6SsWov_fMZTyadzxebYIiQ-VDQDs63gUordThas-jFlAHLgJlqPhVOHJ1WaZt-_oLhgY3fk4bhORkyeAFZVRTnjw38A70b0eZU3ziQkOYW6w7kN__tzgP5gis0Y8mEIiUyTnyuQ"
const sheet = async () => {
const service = new SpotifyService(token)
dispatch(getCurrentUserMusic(service))
}
useEffect(() => {
sheet()
}, []);
return (
// @ts-ignore
<NavigationContainer theme={MyTheme}>

Loading…
Cancel
Save