-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap-with-provider.tsx
69 lines (60 loc) · 1.98 KB
/
wrap-with-provider.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
import React, { ReactNode } from 'react'
import Helmet from 'react-helmet'
import { Provider } from 'react-redux'
import createStore from './src/state/store/createStore'
import { loadProducts } from './src/state/actions/productActions'
import { loginUserSuccess } from './src/state/actions/authActions'
import { loadUser, removeUserLocalStorage } from './src/utils/authUtils'
// Instantiating store in `wrapRootElement` handler ensures:
// - there is fresh store for each SSR page
// - it will be called only once in browser, when React mounts
export default ({ element }: { element: ReactNode }) => {
const store = createStore()
store.dispatch(loadProducts())
const PaypalKey = process.env.NODE_ENV === 'production'
? process.env.GATSBY_PAYPAL_KEY
: process.env.GATSBY_PAYPAL_TEST_KEY
const PaypalMerchantId = process.env.NODE_ENV === 'production'
? '39L6VSTBV6WFG'
: '6Q52LNH4ART22'
// USER CHECK
// check localstorage for user
// decode jwt and check if expired - if expired throw error and logout
// if no user found do nothing
loadUser()
.then((user) => {
// console.log('user', user)
if (user) {
store.dispatch(loginUserSuccess(user))
}
})
.catch((e) => {
// console.error('user token error', e)
// remove user from localstorage
removeUserLocalStorage()
// // que toaster
// const action = {
// type: '@ReduxToastr/toastr/ADD',
// payload: {
// type: 'error',
// position: 'bottom-right',
// options: {
// removeOnHover: false,
// showCloseButton: true
// },
// title: 'Invalid User',
// message: 'Please login again.'
// }
// }
// store.dispatch(action)
// // redirect
// navigate(`/page-2`)
})
return <Provider store={store}>
<Helmet>
<script id='paypal-js'
src={`https://www.paypal.com/sdk/js?client-id=${PaypalKey}&disable-funding=credit,card&commit=true&merchant-id=${PaypalMerchantId}&locale=en_US&integration-date=2019-11-07`}/>
</Helmet>
{element}
</Provider>
}