Skip to content

Commit 77ed715

Browse files
committed
App restructure
1 parent 99f207d commit 77ed715

File tree

11 files changed

+141
-136
lines changed

11 files changed

+141
-136
lines changed

src/App.tsx

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,48 @@
11
import React from 'react'
22
import { Provider } from 'react-redux'
3-
import { gql } from 'apollo-boost'
43
import { ThemeProvider } from '@material-ui/styles'
5-
import { HashRouter, BrowserRouter, Route, RouteProps, Redirect } from 'react-router-dom' //
64
import { ApolloProvider } from '@apollo/react-hooks'
75

8-
import ApolloClient from 'apollo-boost'
96
import CssBaseline from '@material-ui/core/CssBaseline'
107

118
import config from './config'
12-
import store from './_storeRedux'
13-
import api from './_apiREST'
149
import authService from './_services/authService'
15-
import theme from './_theme'
16-
17-
import Auth from './Auth/Auth'
18-
import { DashboardContainer } from './Dashboard'
1910

20-
const client = new ApolloClient({
21-
uri: 'https://48p1r2roz4.sse.codesandbox.io',
22-
})
11+
import apiRest from './_api/rest'
12+
import apiApollo from './_api/apollo'
13+
import storeRedux from './_store/redux'
14+
import theme from './_theme'
2315

24-
api.init()
16+
import AppRouter from './AppRouter'
2517

2618
// Init the authentication service
2719
authService.init({
28-
useDemoToken: true,
20+
useSampleData: config.useSampleData,
21+
})
22+
23+
// Init rest API client
24+
apiRest.init({
25+
useSampleData: config.useSampleData,
2926
})
3027

31-
// Use different router type depending on configuration
32-
const AppRouter: React.ComponentType<any> =
33-
config.navigationType === 'history' ? BrowserRouter : HashRouter
28+
// Init apollo client
29+
const apiApolloClient = apiApollo.init({
30+
useSampleData: config.useSampleData,
31+
})
3432

3533
const App: React.FC = () => {
3634
return (
3735
<div className="App">
3836
<CssBaseline />
39-
<AppRouter>
40-
<Route path="/auth" component={Auth} />
41-
<PrivateRoute path="/" component={DashboardContainer} />
42-
</AppRouter>
37+
<AppRouter />
4338
</div>
4439
)
4540
}
4641

47-
// See https://reacttraining.com/react-router/web/example/auth-workflow
48-
const PrivateRoute: React.FC<RouteProps> = ({
49-
component: Component,
50-
...rest
51-
}: RouteProps) => {
52-
if (!Component) {
53-
return <Route {...rest} />
54-
}
55-
56-
return (
57-
<Route
58-
{...rest}
59-
render={props =>
60-
authService.isAuthenticated() ? (
61-
<Component {...props} />
62-
) : (
63-
<Redirect
64-
to={{
65-
pathname: '/auth/login',
66-
}}
67-
/>
68-
)
69-
}
70-
/>
71-
)
72-
}
73-
7442
export default () => (
7543
<ThemeProvider theme={theme}>
76-
<ApolloProvider client={client}>
77-
<Provider store={store}>
44+
<ApolloProvider client={apiApolloClient}>
45+
<Provider store={storeRedux}>
7846
<App />
7947
</Provider>
8048
</ApolloProvider>

src/AppRouter.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react'
2+
import { HashRouter, BrowserRouter, Route, RouteProps, Redirect } from 'react-router-dom' //
3+
4+
import config from './config'
5+
import authService from './_services/authService'
6+
7+
import Auth from './Auth/Auth'
8+
import { DashboardContainer } from './Dashboard'
9+
10+
// Use different router type depending on configuration
11+
const AppRouterComponent: React.ComponentType<any> =
12+
config.navigationType === 'history' ? BrowserRouter : HashRouter
13+
14+
const AppRouter: React.FC = () => (
15+
<AppRouterComponent>
16+
<Route path="/auth" component={Auth} />
17+
<PrivateRoute path="/" component={DashboardContainer} />
18+
</AppRouterComponent>
19+
)
20+
21+
// See https://reacttraining.com/react-router/web/example/auth-workflow
22+
const PrivateRoute: React.FC<RouteProps> = ({
23+
component: Component,
24+
...rest
25+
}: RouteProps) => {
26+
if (!Component) {
27+
return <Route {...rest} />
28+
}
29+
30+
return (
31+
<Route
32+
{...rest}
33+
render={props =>
34+
authService.isAuthenticated() ? (
35+
<Component {...props} />
36+
) : (
37+
<Redirect
38+
to={{
39+
pathname: '/auth/login',
40+
}}
41+
/>
42+
)
43+
}
44+
/>
45+
)
46+
}
47+
48+
export default AppRouter

src/Dashboard/DashboardContainer.tsx

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,19 @@
1-
import React, { useEffect } from 'react'
2-
import { useDispatch, useSelector } from 'react-redux'
1+
import React from 'react'
32
import LinearProgress from '@material-ui/core/LinearProgress'
4-
import { useQuery } from '@apollo/react-hooks'
5-
import { gql } from 'apollo-boost'
63

7-
import { RootState, RootDispatch } from '../_storeRedux'
8-
import Dashboard from './Dashboard'
9-
10-
function useDashboardData() {
11-
const { loading, error, data } = useQuery(gql`
12-
{
13-
rates(currency: "USD") {
14-
currency
15-
rate
16-
}
17-
}
18-
`)
4+
import { useStore } from './_store'
195

20-
return { loading, error, data }
21-
}
6+
import Dashboard from './Dashboard'
227

238
// Before showing the dashboard we need to be sure that the
249
// User data is propperly requested
2510
const DashboardContainer: React.FC = props => {
26-
const { loading, error, data } = useDashboardData()
27-
28-
// const user = useSelector((state: RootState) => state.user)
29-
// const dispatch = useDispatch<RootDispatch>()
30-
31-
// // Request user data
32-
// useEffect(() => {
33-
// dispatch.user.request()
34-
// })
11+
const { loading, error, data } = useStore()
3512

3613
if (loading) return <LinearProgress />
3714
if (error) return <p>Error :(</p>
3815

39-
return data.rates.map(({ currency, rate }: { currency: any; rate: any }) => (
40-
<div key={currency}>
41-
<p>
42-
{currency}: {rate}
43-
</p>
44-
</div>
45-
))
46-
47-
// if (!user) {
48-
// return <LinearProgress />
49-
// } else {
50-
// return <Dashboard />
51-
// }
16+
return <Dashboard />
5217
}
5318

5419
export default DashboardContainer

src/Dashboard/_store/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useEffect } from 'react'
2+
3+
import { useDispatch, useSelector } from 'react-redux'
4+
import { RootState, RootDispatch } from '_store/redux'
5+
import { useQuery } from '@apollo/react-hooks'
6+
import { gql } from 'apollo-boost'
7+
8+
export function useStore() {
9+
return useStoreApollo()
10+
}
11+
12+
function useStoreApollo() {
13+
const { loading, error, data } = useQuery(gql`
14+
{
15+
rates(currency: "USD") {
16+
currency
17+
rate
18+
}
19+
}
20+
`)
21+
22+
return { loading, error, data }
23+
}
24+
25+
function useStoreRedux() {
26+
const data = useSelector((state: RootState) => state.dashboard)
27+
const dispatch = useDispatch<RootDispatch>()
28+
29+
// Request user data
30+
useEffect(() => {
31+
dispatch.dashboard.request()
32+
})
33+
34+
return { data }
35+
}
36+
37+
export default {}

src/_api/apollo/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import ApolloClient from 'apollo-boost'
2+
// import config from './config'
3+
4+
export interface ApiApolloInitOptions {
5+
useSampleData?: boolean
6+
}
7+
8+
export default {
9+
init({ useSampleData }: ApiApolloInitOptions) {
10+
const client = new ApolloClient({
11+
uri: 'https://48p1r2roz4.sse.codesandbox.io',
12+
})
13+
14+
return client
15+
},
16+
}

src/_api/graphql/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/_api/rest/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import config from '../../config'
2-
31
import instance from './apiService'
42
import accounts from './accountsService'
53
import users from './usersService'
64

7-
const init = () => {
5+
export interface ApiRestInitOptions {
6+
useSampleData?: boolean
7+
}
8+
9+
const init = ({ useSampleData }: ApiRestInitOptions) => {
810
// Respond with a sample data
9-
if (config.api.useSampleData) {
11+
if (useSampleData) {
1012
console.log('should mock rest api!')
11-
12-
// apiSampleData.init(instance)
1313
}
14+
15+
return instance
1416
}
1517

1618
export default { instance, accounts, users, init }

src/_services/authService.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import store from 'store'
77

88
// import config from '../config'
99

10-
const demoToken =
10+
const sampleToken =
1111
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.Db8fjZU7MkBZoJDjmjuvv2EeDgG9RSaZ1xKm__qHelw'
1212

1313
export interface AuthService {
@@ -20,14 +20,14 @@ export interface AuthService {
2020
}
2121

2222
interface AuthServiceInitOptions {
23-
useDemoToken?: boolean
23+
useSampleData?: boolean
2424
}
2525

2626
const authService: AuthService = {
2727
token: null,
28-
init<AuthServiceInitOptions>({ useDemoToken = false } = {}) {
29-
if (useDemoToken) {
30-
this.token = demoToken
28+
init<AuthServiceInitOptions>({ useSampleData = false } = {}) {
29+
if (useSampleData) {
30+
this.token = sampleToken
3131
} else {
3232
this.token = store.get('token') || null
3333
}

src/_store/redux/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { init, RematchRootState, RematchDispatch } from '@rematch/core'
22

3-
import user from './userStore'
3+
import dashboard from 'Dashboard/_store/redux/dashboardStore'
44

55
const models = {
6-
user,
6+
dashboard,
77
}
88

99
export const store = init({

src/_store/redux/userStore.tsx

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)