Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.

Commit 2ec3edc

Browse files
committedMar 14, 2018
rewrote base module
1 parent 278dba5 commit 2ec3edc

File tree

12 files changed

+140
-40
lines changed

12 files changed

+140
-40
lines changed
 

‎velog-backend/.eslintignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node_modules/*
1+
node_modules/*.js

‎velog-backend/.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ module.exports = {
1919
"no-unused-vars": 1,
2020
"no-confusing-arrow": 0,
2121
"no-continue": 0,
22-
"no-await-in-loop": 0
22+
"no-await-in-loop": 0,
23+
"no-param-reassign": 0
2324
},
2425
"plugins": [
2526
"flowtype"

‎velog-backend/src/database/models/Comment.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ Comment.listComments = async function (postId: string) {
8686
limit: 20,
8787
});
8888
if (!data) return [];
89+
// TODO: Pagination
8990
const comments = data.map(c => c.toJSON());
90-
for (let i = 0; i < comments.length; i++) {
91-
if (!comments[i].has_replies) continue;
92-
const c2 = (await Comment.getChildrenOf(comments[i].id))
93-
.map(c => c.toJSON());
94-
comments[i].children = c2.map(c => c.toJSON());
95-
for (let j = 0; j < c2.length; j++) {
96-
if (c2[j].has_replies) continue;
97-
const c3 = (await Comment.getChildrenOf(c2[j].id))
98-
.map(c => c.toJSON());
99-
c2[j].children = c3;
91+
const fetchChildren = async (list: any[], level = 0) => {
92+
for (let i = 0; i < list.length; i++) {
93+
if (!list[i].has_replies) continue;
94+
const children = await Comment.getChildrenOf(list[i].id);
95+
const childrenJSON = children.map(c => c.toJSON());
96+
list[i].children = childrenJSON;
97+
if (level === 2) return;
98+
return fetchChildren(childrenJSON, level + 1);
10099
}
101-
}
100+
};
101+
await fetchChildren(comments);
102102
return comments;
103103
} catch (e) {
104104
throw e;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @flow
2+
import React from 'react';
3+
import { connect } from 'react-redux';
4+
import type { State } from 'store';
5+
import { SampleActions } from 'store/actionCreators';
6+
7+
const SampleContainer = () => {
8+
SampleActions.increase(10);
9+
return (
10+
<div>
11+
Hello
12+
</div>
13+
);
14+
};
15+
16+
export default connect(
17+
({ sample }: State) => ({
18+
value: sample.value,
19+
}),
20+
() => ({}),
21+
)(SampleContainer);

‎velog-frontend/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ import registerServiceWorker from './registerServiceWorker';
88
window.socialAuth = socialAuth;
99

1010
ReactDOM.render(<Root />, document.getElementById('root'));
11-
registerServiceWorker();
11+
registerServiceWorker();

‎velog-frontend/src/store/actionCreators.js

+2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { actionCreators as authActions, type AuthActionCreators } from './module
55
import { actionCreators as userActions, type UserActionCreators } from './modules/user';
66
import { actionCreators as baseActions, type BaseActionCreators } from './modules/base';
77
import { actionCreators as writeActions, type WriteActionCreators } from './modules/write';
8+
import { actionCreators as sampleActions, type SampleActionCreators } from './modules/sample';
89

910
const { dispatch } = store;
1011

1112
export const AuthActions: AuthActionCreators = bindActionCreators(authActions, dispatch);
1213
export const UserActions: UserActionCreators = bindActionCreators(userActions, dispatch);
1314
export const BaseActions: BaseActionCreators = bindActionCreators(baseActions, dispatch);
1415
export const WriteActions: WriteActionCreators = bindActionCreators(writeActions, dispatch);
16+
export const SampleActions: SampleActionCreators = bindActionCreators(sampleActions, dispatch);

‎velog-frontend/src/store/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Auth } from './modules/auth';
44
import type { User } from './modules/user';
55
import type { Base } from './modules/base';
66
import type { Write } from './modules/write';
7+
import type { Sample } from './modules/sample';
78

89
const store = configure();
910

@@ -14,6 +15,7 @@ export type State = {
1415
user: User,
1516
base: Base,
1617
write: Write,
18+
sample: Sample,
1719
pender: {
1820
pending: any,
1921
success: any,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// @flow
2+
import { createAction, handleActions } from 'redux-actions';
3+
import { Record, type Map } from 'immutable';
4+
5+
const SHOW_USER_MENU = 'base/SHOW_USER_MENU';
6+
const HIDE_USER_MENU = 'base/HIDE_USER_MENU';
7+
const SET_FULLSCREEN_LOADER = 'base/SET_FULLSCREEN_LOADER';
8+
9+
export type BaseActionCreators = {
10+
showUserMenu(): any,
11+
hideUserMenu(): any,
12+
setFullscreenLoader(visibility: boolean): any,
13+
};
14+
15+
export const actionCreators = {
16+
showUserMenu: createAction(SHOW_USER_MENU),
17+
hideUserMenu: createAction(HIDE_USER_MENU),
18+
setFullscreenLoader: createAction(SET_FULLSCREEN_LOADER),
19+
};
20+
21+
export type Base = {
22+
userMenu: boolean,
23+
fullscreenLoader: boolean
24+
}
25+
26+
const BaseRecord = Record({
27+
userMenu: false,
28+
fullscreenLoader: false,
29+
});
30+
31+
32+
const initialState: Map<string, *> = BaseRecord();
33+
34+
export default handleActions({
35+
[SHOW_USER_MENU]: state => state.set('userMenu', true),
36+
[HIDE_USER_MENU]: state => state.set('userMenu', false),
37+
[SET_FULLSCREEN_LOADER]: (state, { payload: visibility }) => state.set('fullscreenLoader', visibility),
38+
}, initialState);
+33-20
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
11
// @flow
2-
import { createAction, handleActions } from 'redux-actions';
3-
import { Record, type Map } from 'immutable';
2+
import { createAction, handleActions, type ActionTypes } from 'redux-actions';
3+
import produce from 'immer';
44

55
const SHOW_USER_MENU = 'base/SHOW_USER_MENU';
66
const HIDE_USER_MENU = 'base/HIDE_USER_MENU';
77
const SET_FULLSCREEN_LOADER = 'base/SET_FULLSCREEN_LOADER';
88

9-
export type BaseActionCreators = {
10-
showUserMenu(): any,
11-
hideUserMenu(): any,
12-
setFullscreenLoader(visibility: boolean): any,
13-
};
9+
const showUserMenu = createAction(SHOW_USER_MENU);
10+
const hideUserMenu = createAction(HIDE_USER_MENU);
11+
const setFullscreenLoader = createAction(
12+
SET_FULLSCREEN_LOADER,
13+
(visibility): boolean => visibility);
14+
15+
type ShowUserMenuAction = ActionType<typeof showUserMenu>;
16+
type HideUserMenuAction = ActionType<typeof hideUserMenu>;
17+
type SetFullscreenLoaderAction = ActionType<typeof setFullscreenLoader>;
1418

15-
export const actionCreators = {
16-
showUserMenu: createAction(SHOW_USER_MENU),
17-
hideUserMenu: createAction(HIDE_USER_MENU),
18-
setFullscreenLoader: createAction(SET_FULLSCREEN_LOADER),
19+
export interface BaseActionCreators {
20+
showUserMenu(): ShowUserMenuAction,
21+
hideUserMenu(): HideUserMenuAction,
22+
setFullscreenLoader(): SetFullscreenLoaderAction
23+
}
24+
25+
export const actionCreators: BaseActionCreators = {
26+
showUserMenu, hideUserMenu, setFullscreenLoader,
1927
};
2028

2129
export type Base = {
2230
userMenu: boolean,
2331
fullscreenLoader: boolean
24-
}
32+
};
2533

26-
const BaseRecord = Record({
34+
const initialState: Base = {
2735
userMenu: false,
2836
fullscreenLoader: false,
29-
});
30-
31-
32-
const initialState: Map<string, *> = BaseRecord();
37+
};
3338

3439
export default handleActions({
35-
[SHOW_USER_MENU]: state => state.set('userMenu', true),
36-
[HIDE_USER_MENU]: state => state.set('userMenu', false),
37-
[SET_FULLSCREEN_LOADER]: (state, { payload: visibility }) => state.set('fullscreenLoader', visibility),
40+
[SHOW_USER_MENU]: state => produce(state, (draft) => {
41+
draft.userMenu = true;
42+
}),
43+
[HIDE_USER_MENU]: state => produce(state, (draft) => {
44+
draft.userMenu = false;
45+
}),
46+
[SET_FULLSCREEN_LOADER]: (state, action: SetFullscreenLoaderAction) => {
47+
return produce(state, (draft) => {
48+
draft.fullscreenLoader = action.payload;
49+
});
50+
},
3851
}, initialState);

‎velog-frontend/src/store/modules/sample.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ type InsertAction = ActionType<typeof insert>;
2727
type ChangeAction = ActionType<typeof change>;
2828

2929
/* EXPORTING ACTION CREATORS / ACTION CREATORS TYPES */
30-
export type SampleActionCreators = {
30+
export interface SampleActionCreators {
3131
plain(): PlainAction,
3232
increase(value: number): IncreaseAction,
3333
insert(payload: InsertPayload): InsertAction,
34-
change(text: string): IncreaseAction
35-
};
34+
change(text: string): ChangeAction,
35+
}
3636

37-
export const actionCreators = {
37+
export const actionCreators: SampleActionCreators = {
3838
plain, increase, insert, change,
3939
};
4040

@@ -45,13 +45,13 @@ type TodoItem = {
4545
done: boolean
4646
};
4747

48-
type State = {
48+
export type Sample = {
4949
value: number,
5050
text: string,
5151
todos: TodoItem[],
5252
}
5353

54-
const initialState: State = {
54+
const initialState: Sample = {
5555
value: 0,
5656
text: '',
5757
todos: [],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// // @flow
2+
// import base, { actionCreators, type Base } from '../base';
3+
4+
// describe('base', () => {
5+
// describe('reducer', () => {
6+
// // let state: Base = base(undefined, {});
7+
// // it('SHOW_USER_MENU works', () => {
8+
// // state = base(state, actionCreators.showUserMenu());
9+
// // expect(state.userMenu).tobe(true);
10+
// // });
11+
// });
12+
// });

‎velog.code-workspace

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "velog-backend"
5+
},
6+
{
7+
"path": "velog-frontend"
8+
}
9+
],
10+
"settings": {}
11+
}

0 commit comments

Comments
 (0)
This repository has been archived.