Skip to content

Commit a4e4fb1

Browse files
committed
refactor store
refactor about page
1 parent b1c9b97 commit a4e4fb1

File tree

6 files changed

+57
-86
lines changed

6 files changed

+57
-86
lines changed

frontend/pages/about.tsx

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { connect, useSelector } from "react-redux";
2-
32
import { NextPage } from "next";
43
import { bindActionCreators } from "redux";
5-
import { getUsers } from "../store/count/action";
4+
import { getUsers } from "../store/count/actions/countActions";
65
import { useEffect } from "react";
76
import { wrapper } from "../store/store";
87
import { client } from "../lib/apollo";
@@ -16,12 +15,11 @@ const About: NextPage = ({pageProps, getStaticProp}) => {
1615
// getUsers()
1716
// const aa = useSelector((state) => state);
1817
//console.log("IM AA", aa);
19-
const {app,users, page} = useSelector(state => state);
18+
const data = useSelector(state => state);
2019

21-
// console.log("im props", props);
2220
return (
2321
<ul>
24-
{users?.map((post) => (
22+
{data.count.users?.map((post) => (
2523
<>
2624
<li>{post.id}</li>
2725
<li>{post.email}</li>
@@ -34,35 +32,25 @@ const About: NextPage = ({pageProps, getStaticProp}) => {
3432
export const getStaticProps = wrapper.getStaticProps(async ({ store }) => {
3533

3634

37-
const User_Query = gql`
38-
query {
39-
getUsers {
40-
id
41-
email
42-
}
43-
}
44-
`;
45-
46-
await client().query({ query: User_Query }).then((res) => {
47-
48-
store.dispatch({type: 'GET_USERS', payload: res?.data?.getUsers});
49-
});
35+
// const User_Query = gql`
36+
// query {
37+
// getUsers {
38+
// id
39+
// email
40+
// }
41+
// }
42+
// `;
5043

44+
// await client().query({ query: User_Query }).then((res) => {
45+
// store.dispatch({type: 'GET_USERS', payload: res?.data?.getUsers});
46+
// });
5147

48+
await store.dispatch(getUsers());
5249
return {props: {getStaticProp: 'bar'}};
5350

54-
// store.dispatch(serverRenderClock(true))
55-
//store.dispatch(addCount())
56-
// await store.dispatch(getUsers());
5751

58-
// const data = await store.getState();
5952

60-
// return {
61-
// props: {
62-
// users: data.count.users,
63-
// },
64-
// revalidate: 1,
65-
// };
53+
6654
});
6755

6856
// const mapDispatchToProps = (dispatch) => {

frontend/store/count/action.ts renamed to frontend/store/count/actions/countActions.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import { useLazyQuery, useQuery } from "@apollo/react-hooks";
2-
32
import { gql } from "apollo-boost";
43
import { useApolloClient } from "@apollo/react-hooks";
4+
import { countActionTypes } from './types';
5+
import { client } from "../../../lib/apollo";
56

6-
export const countActionTypes = {
7-
ADD: "ADD",
8-
ADD_POST: "ADD_POSTS",
9-
GET_USERS: "GET_USERS",
10-
SET_USERS: "SET_USERS"
11-
};
127

138
export const addCount = () => (dispatch) => {
149
return dispatch({ type: countActionTypes.ADD });
@@ -18,8 +13,7 @@ export const addPosts = () => (dispatch) => {
1813
return dispatch({ type: countActionTypes.ADD_POST });
1914
};
2015

21-
export const getUsers = () => async (dispatch, getState, client) => {
22-
//console.log("chcekted");
16+
export const getUsers = () => async (dispatch, getState) => {
2317
const User_Query = gql`
2418
query {
2519
getUsers {
@@ -29,7 +23,7 @@ export const getUsers = () => async (dispatch, getState, client) => {
2923
}
3024
`;
3125

32-
await client.query({ query: User_Query }).then((res) => {
26+
await client().query({ query: User_Query }).then((res) => {
3327
return dispatch({
3428
type: countActionTypes.GET_USERS,
3529
payload: res?.data?.getUsers,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const countActionTypes = {
2+
ADD: "ADD",
3+
ADD_POST: "ADD_POSTS",
4+
GET_USERS: "GET_USERS",
5+
SET_USERS: "SET_USERS"
6+
};

frontend/store/count/reducer.ts renamed to frontend/store/count/reducers/countReducer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import {AnyAction} from 'redux';
22
import {HYDRATE} from 'next-redux-wrapper';
3-
import {client} from '../../lib/apollo';
4-
import { countActionTypes } from './action';
3+
import { countActionTypes } from '../actions/types';
4+
55
export interface State {
66
app: string;
77
page: string;
88
users: any
99
}
1010

11-
const reducer = (state: State = {app: 'init', page: 'init', users: {}}, action: AnyAction) => {
11+
export const countReducer = (state: State = {app: 'init', page: 'init', users: {}}, action: AnyAction) => {
1212
switch (action.type) {
1313
case HYDRATE:
1414
if (action.payload.app === 'init') delete action.payload.app;
@@ -18,14 +18,14 @@ const reducer = (state: State = {app: 'init', page: 'init', users: {}}, action:
1818
return {...state, app: action.payload};
1919
case 'PAGE':
2020
return {...state, page: action.payload};
21-
case 'GET_USERS':
21+
case countActionTypes.GET_USERS:
2222
return {...state, users: action.payload};
2323
default:
2424
return state;
2525
}
2626
};
2727

28-
export default reducer;
28+
///export default count;
2929

3030

3131

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {combineReducers} from 'redux';
2+
import {countReducer} from './countReducer'
3+
4+
export default combineReducers({
5+
count: countReducer
6+
});

frontend/store/store.ts

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import {createStore, applyMiddleware} from 'redux';
1+
import {createStore, applyMiddleware, combineReducers} from 'redux';
22
//import logger from 'redux-logger';
3-
import {MakeStore, createWrapper, Context} from 'next-redux-wrapper';
4-
import reducer, {State} from './count/reducer';
5-
3+
import {MakeStore, createWrapper, Context, HYDRATE} from 'next-redux-wrapper';
4+
import rootReducer from '../store/count/reducers';
5+
import {State} from '../store/count/reducers/countReducer';
6+
import thunkMiddleware from 'redux-thunk'
67

78
const bindMiddleware = (middleware) => {
89
if (process.env.NODE_ENV !== 'production') {
@@ -12,56 +13,32 @@ const bindMiddleware = (middleware) => {
1213
return applyMiddleware(...middleware)
1314
}
1415

16+
17+
const reducer = (state, action) => {
18+
if (action.type === HYDRATE) {
19+
const nextState = {
20+
...state, // use previous state
21+
...action.payload, // apply delta from hydration
22+
}
23+
//if (state.count) nextState.count = state.count // preserve count value on client side navigation
24+
return nextState
25+
} else {
26+
return rootReducer(state, action)
27+
}
28+
}
29+
1530
export const makeStore: MakeStore<State> = (context: Context) => {
16-
const store = createStore(reducer, bindMiddleware([]));
31+
const store = createStore(reducer, bindMiddleware([thunkMiddleware]));
1732

1833
// if (module.hot) {
1934
// module.hot.accept('./reducer', () => {
2035
// console.log('Replacing reducer');
2136
// store.replaceReducer(require('./reducer').default);
2237
// });
2338
// }
24-
2539
return store;
2640
};
2741

2842
export const wrapper = createWrapper<State>(makeStore, {debug: true});
2943

3044

31-
// import { HYDRATE, createWrapper } from 'next-redux-wrapper'
32-
// import { applyMiddleware, combineReducers, createStore } from 'redux'
33-
34-
// import { client } from '../lib/apollo';
35-
// import count from './count/reducer'
36-
// import thunkMiddleware from 'redux-thunk'
37-
38-
// const bindMiddleware = (middleware) => {
39-
// if (process.env.NODE_ENV !== 'production') {
40-
// const { composeWithDevTools } = require('redux-devtools-extension')
41-
// return composeWithDevTools(applyMiddleware(...middleware))
42-
// }
43-
// return applyMiddleware(...middleware)
44-
// }
45-
46-
// const combinedReducer = combineReducers({
47-
// count,
48-
// })
49-
50-
// const reducer = (state, action) => {
51-
// if (action.type === HYDRATE) {
52-
// const nextState = {
53-
// ...state, // use previous state
54-
// ...action.payload, // apply delta from hydration
55-
// }
56-
// if (state.count) nextState.count = state.count // preserve count value on client side navigation
57-
// return nextState
58-
// } else {
59-
// return combinedReducer(state, action)
60-
// }
61-
// }
62-
63-
// const initStore = () => {
64-
// return createStore(reducer, bindMiddleware([thunkMiddleware.withExtraArgument(client)]))
65-
// }
66-
67-
// export const wrapper = createWrapper(initStore)

0 commit comments

Comments
 (0)