Skip to content

Commit bbd6997

Browse files
committed
Remove apollo
Clean up Restructure Stick to Redux + Rematch
1 parent 882522d commit bbd6997

File tree

21 files changed

+133
-151
lines changed

21 files changed

+133
-151
lines changed

docs/drafts.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ The points below are still in progress
88
- Static typecheckign with TypeScript for large scale projects
99
- Modular and scalable architecture
1010
- Built in JWT authentication
11-
- Built in interchangable REST and GraphQL API interaction services, pick the prefered option
1211
- Typed data entities
1312
- Not just a dummy template
1413
- Uses react hooks
@@ -45,6 +44,15 @@ It improves the code quality, helps to avoid of lot's of runtime errors, during
4544

4645
Although I'm not a big fan of material design, but the MaterialUI gives a huge solid fundation for building user interfaces. I never enjoyed using any UI framework that much as I did with MaterialUI. Big appreciation and my credits to the MaterialUI team.
4746

47+
#### Why Redux + Rematch for the state management?
48+
49+
Redux has became de-facto standart solution for the React state management. Unfortunately you need to write lot's of boilerplate code by default. Rematch solves that issue
50+
51+
52+
-----
53+
54+
Not Valid
55+
4856
#### Why do you use two different approaches for state management?
4957

5058
Initially I was thinking about using Redux only as a state management with GraphQL Apollo Client as well, but it turns out that Appolo Client is also prefered way to manage the UI state.
@@ -58,7 +66,3 @@ https://www.apollographql.com/docs/react/essentials/local-state/
5866

5967
I'm trying to build the Modular Material Admin in a way that you would be able to use your desired approach. Each component that needs an ASYNC data, has it's own data usage custom hook, which can be Redux state or Appolo state.
6068
The custom hook of the component abstracts away the state management implementation under the hood, so we can use the preferred method for the state management: Apollo GraphQL or Redux + Rematch
61-
62-
#### Why Redux + Rematch for the state management?
63-
64-
Redux has became de-facto standart solution for the React state management. Unfortunately you need to write lot's of boilerplate code by default. Rematch solves that issue

package.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@
1313
},
1414
"homepage": "http://modular-material-admin-react.modularcode.io",
1515
"dependencies": {
16-
"@apollo/react-hooks": "^3.0.0",
17-
"@apollo/react-testing": "^3.0.0",
1816
"@material-ui/core": "^4.1.3",
1917
"@material-ui/icons": "^4.2.1",
20-
"@nivo/core": "^0.59.1",
21-
"@nivo/line": "^0.59.3",
2218
"@rematch/core": "^1.1.0",
2319
"@types/axios": "^0.14.0",
2420
"@types/chart.js": "^2.7.56",
@@ -30,9 +26,7 @@
3026
"@types/react-dom": "16.8.4",
3127
"@types/react-redux": "^7.1.1",
3228
"@types/react-router-dom": "^4.3.4",
33-
"@types/recharts": "^1.1.17",
3429
"@types/store": "^2.0.2",
35-
"apollo-boost": "^0.4.4",
3630
"axios": "^0.19.0",
3731
"axios-mock-adapter": "^1.17.0",
3832
"chart.js": "^2.8.0",

src/App.tsx

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
import React from 'react'
22
import { Provider } from 'react-redux'
33
import { ThemeProvider } from '@material-ui/styles'
4-
import { ApolloProvider } from '@apollo/react-hooks'
54

65
import CssBaseline from '@material-ui/core/CssBaseline'
76

87
import config from './config'
98
import authService from './_services/authService'
109

11-
import apiRest from './_api/rest'
12-
import apiApollo from './_api/apollo'
13-
import storeRedux from './_store/redux'
10+
import api from './_api'
11+
import apiMocks from './_api/_mocks'
12+
13+
import store from './_store'
1414
import theme from './_theme'
1515

1616
import AppRouter from './AppRouter'
1717

18-
// Init the authentication service
18+
// Init the API service
1919
authService.init({
2020
useSampleData: config.useSampleData,
2121
})
2222

2323
// Init rest API client
24-
apiRest.init({
25-
useSampleData: config.useSampleData,
26-
})
24+
const apiClient = api.init()
2725

28-
// Init apollo client
29-
const apiApolloClient = apiApollo.init({
30-
useSampleData: config.useSampleData,
31-
})
26+
if (config.useSampleData) {
27+
apiMocks.init(apiClient)
28+
}
3229

3330
const App: React.FC = () => {
3431
return (
@@ -38,13 +35,10 @@ const App: React.FC = () => {
3835
</div>
3936
)
4037
}
41-
4238
export default () => (
4339
<ThemeProvider theme={theme}>
44-
<ApolloProvider client={apiApolloClient}>
45-
<Provider store={storeRedux}>
46-
<App />
47-
</Provider>
48-
</ApolloProvider>
40+
<Provider store={store}>
41+
<App />
42+
</Provider>
4943
</ThemeProvider>
5044
)

src/Dashboard/DashboardContainer.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import React from 'react'
22
import LinearProgress from '@material-ui/core/LinearProgress'
33

4-
import { useStore } from './_store'
4+
import { useDashboardData } from './DashboardData'
55

66
import Dashboard from './Dashboard'
77

88
// Before showing the dashboard we need to be sure that the
99
// User data is propperly requested
1010
const DashboardContainer: React.FC = props => {
11-
const { loading, error, data } = useStore()
12-
13-
if (loading) return <LinearProgress />
14-
if (error) return <p>Error :(</p>
11+
const state = useDashboardData()
1512

13+
// if (loading) return <LinearProgress />
14+
// if (error) {
15+
// console.log(error)
16+
// return <p>Error :(</p>
17+
// }
1618
return <Dashboard />
1719
}
1820

src/Dashboard/DashboardData.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useEffect } from 'react'
2+
3+
import { useDispatch, useSelector } from 'react-redux'
4+
import { RootState, RootDispatch } from '_store'
5+
6+
export function useDashboardData() {
7+
const state = useSelector((state: RootState) => state.dashboard)
8+
const dispatch = useDispatch<RootDispatch>()
9+
10+
// Request user data
11+
useEffect(() => {
12+
dispatch.dashboard.request()
13+
}, [dispatch])
14+
15+
return state
16+
}
17+
18+
export default {}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { createModel } from '@rematch/core' // RematchDispatch
2+
import User from '_types/User'
3+
import users from '_api/users'
4+
5+
export interface DashboardStateStatus {
6+
loading?: boolean
7+
error?: Error
8+
}
9+
10+
export interface DashboardStateData {
11+
user?: User
12+
}
13+
export interface DashboardState extends DashboardStateStatus {
14+
data?: DashboardStateData
15+
}
16+
17+
const model = createModel({
18+
state: {
19+
loading: false,
20+
error: null,
21+
data: null,
22+
},
23+
reducers: {
24+
setStatus: (
25+
state: DashboardState,
26+
payload: { loading?: boolean; error?: any },
27+
): DashboardState => ({
28+
...state,
29+
...payload,
30+
}),
31+
setData: (state: DashboardState, payload: DashboardStateData): DashboardState => ({
32+
...state,
33+
data: {
34+
...state.data,
35+
...payload,
36+
},
37+
}),
38+
},
39+
// dispatch: RematchDispatch
40+
effects: () => ({
41+
async request() {
42+
this.setStatus({
43+
loading: true,
44+
error: null,
45+
})
46+
47+
try {
48+
const currentUser = await users.getProfile()
49+
50+
this.setData({
51+
user: currentUser,
52+
})
53+
} catch (e) {
54+
this.setStatus({
55+
error: e,
56+
})
57+
}
58+
59+
this.setStatus({
60+
loading: false,
61+
})
62+
},
63+
}),
64+
})
65+
66+
export default model

src/Dashboard/_store/index.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1 @@
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 {}
1+
export { default } from './dashboardStore'

src/Dashboard/_store/redux/dashboardStore.ts

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

src/_api/README.md

Whitespace-only changes.

src/_api/_data/index.ts

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

0 commit comments

Comments
 (0)