Commit 31242626 authored by Terrazzoni Jean-Baptiste's avatar Terrazzoni Jean-Baptiste 🚀

Redux persistance and push notifications

parent e50dea93
import Expo, { Notifications } from "expo";
import React from "react";
import { StyleSheet, Text, View } from "react-native";
import { StyleSheet, Text, View, Alert } from "react-native";
import { Provider } from "react-redux";
import { PersistGate } from "redux-persist/integration/react";
import {
createAppContainer,
createBottomTabNavigator,
......@@ -8,7 +10,8 @@ import {
createStackNavigator
} from "react-navigation";
import store from "./store";
import registerFromNotifications from "./services/push_notifications";
import configureStore from "./store";
import AuthScreen from "./screens/AuthScreen";
import WelcomeScreen from "./screens/WelcomeScreen";
import DeckScreen from "./screens/DeckScreen";
......@@ -16,6 +19,8 @@ import MapScreen from "./screens/MapScreen";
import SettingsScreen from "./screens/SettingsScreen";
import ReviewScreen from "./screens/ReviewScreen";
const { persistor, store } = configureStore();
const navigationOptions = {
tabBarVisible: false
};
......@@ -51,10 +56,25 @@ const MainNavigator = createBottomTabNavigator(
const AppContainer = createAppContainer(MainNavigator);
class App extends React.Component {
componentDidMount() {
registerFromNotifications();
Notifications.addListener(notification => {
const {
data: { text },
origin
} = notification;
if (origin === "received" && text)
Alert.alert("New push notification", text, [{ text: "Ok" }]);
});
}
render() {
return (
<Provider store={store}>
<AppContainer />
<PersistGate loading={null} persistor={persistor}>
<AppContainer />
</PersistGate>
</Provider>
);
}
......
......@@ -18,6 +18,7 @@
"react-navigation": "^3.5.1",
"react-redux": "^6.0.1",
"redux": "^4.0.1",
"redux-persist": "^5.10.0",
"redux-thunk": "^2.3.0"
},
"devDependencies": {
......
import { Permissions, Notifications } from "expo";
import { AsyncStorage } from "react-native";
import axios from "axios";
const PUSH_ENDPOINT = "http://rallycoding.herokuapp.com/api/tokens";
export default async () => {
let previousToken = await AsyncStorage.getItem("pushtoken");
if (previousToken) return;
else {
let { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
if (status !== "granted") return;
const token = await Notifications.getExpoPushTokenAsync();
await axios.post(PUSH_ENDPOINT, { token: { token } });
AsyncStorage.setItem("pushtoken", token);
}
};
import { createStore, compose, applyMiddleware } from "redux";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import thunk from "redux-thunk";
import reducers from "../reducers";
const store = createStore(reducers, {}, compose(applyMiddleware(thunk)));
import rootReducer from "../reducers";
export default store;
const middlewares = [thunk];
const persistConfig = {
key: "root",
storage,
whitelist: ["likedJobs"]
};
const persistedReducer = persistReducer(persistConfig, rootReducer);
export default () => {
const store = createStore(
persistedReducer,
{},
compose(applyMiddleware(...middlewares))
);
const persistor = persistStore(store);
return { store, persistor };
};
......@@ -5019,6 +5019,11 @@ readable-stream@^2.0.1, readable-stream@^2.0.6, readable-stream@^2.2.2, readable
string_decoder "~1.1.1"
util-deprecate "~1.0.1"
redux-persist@^5.10.0:
version "5.10.0"
resolved "https://registry.yarnpkg.com/redux-persist/-/redux-persist-5.10.0.tgz#5d8d802c5571e55924efc1c3a9b23575283be62b"
integrity sha512-sSJAzNq7zka3qVHKce1hbvqf0Vf5DuTVm7dr4GtsqQVOexnrvbV47RWFiPxQ8fscnyiuWyD2O92DOxPl0tGCRg==
redux-thunk@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.3.0.tgz#51c2c19a185ed5187aaa9a2d08b666d0d6467622"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment