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

Commit

Permalink
rewrote base module
Browse files Browse the repository at this point in the history
  • Loading branch information
velopert committed Mar 14, 2018
1 parent 278dba5 commit 2ec3edc
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 40 deletions.
2 changes: 1 addition & 1 deletion velog-backend/.eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node_modules/*
node_modules/*.js
3 changes: 2 additions & 1 deletion velog-backend/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module.exports = {
"no-unused-vars": 1,
"no-confusing-arrow": 0,
"no-continue": 0,
"no-await-in-loop": 0
"no-await-in-loop": 0,
"no-param-reassign": 0
},
"plugins": [
"flowtype"
Expand Down
22 changes: 11 additions & 11 deletions velog-backend/src/database/models/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,19 @@ Comment.listComments = async function (postId: string) {
limit: 20,
});
if (!data) return [];
// TODO: Pagination
const comments = data.map(c => c.toJSON());
for (let i = 0; i < comments.length; i++) {
if (!comments[i].has_replies) continue;
const c2 = (await Comment.getChildrenOf(comments[i].id))
.map(c => c.toJSON());
comments[i].children = c2.map(c => c.toJSON());
for (let j = 0; j < c2.length; j++) {
if (c2[j].has_replies) continue;
const c3 = (await Comment.getChildrenOf(c2[j].id))
.map(c => c.toJSON());
c2[j].children = c3;
const fetchChildren = async (list: any[], level = 0) => {
for (let i = 0; i < list.length; i++) {
if (!list[i].has_replies) continue;
const children = await Comment.getChildrenOf(list[i].id);
const childrenJSON = children.map(c => c.toJSON());
list[i].children = childrenJSON;
if (level === 2) return;
return fetchChildren(childrenJSON, level + 1);
}
}
};
await fetchChildren(comments);
return comments;
} catch (e) {
throw e;
Expand Down
21 changes: 21 additions & 0 deletions velog-frontend/src/containers/etc/SampleContainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @flow
import React from 'react';
import { connect } from 'react-redux';
import type { State } from 'store';
import { SampleActions } from 'store/actionCreators';

const SampleContainer = () => {
SampleActions.increase(10);
return (
<div>
Hello
</div>
);
};

export default connect(
({ sample }: State) => ({
value: sample.value,
}),
() => ({}),
)(SampleContainer);
2 changes: 1 addition & 1 deletion velog-frontend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ import registerServiceWorker from './registerServiceWorker';
window.socialAuth = socialAuth;

ReactDOM.render(<Root />, document.getElementById('root'));
registerServiceWorker();
registerServiceWorker();
2 changes: 2 additions & 0 deletions velog-frontend/src/store/actionCreators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { actionCreators as authActions, type AuthActionCreators } from './module
import { actionCreators as userActions, type UserActionCreators } from './modules/user';
import { actionCreators as baseActions, type BaseActionCreators } from './modules/base';
import { actionCreators as writeActions, type WriteActionCreators } from './modules/write';
import { actionCreators as sampleActions, type SampleActionCreators } from './modules/sample';

const { dispatch } = store;

export const AuthActions: AuthActionCreators = bindActionCreators(authActions, dispatch);
export const UserActions: UserActionCreators = bindActionCreators(userActions, dispatch);
export const BaseActions: BaseActionCreators = bindActionCreators(baseActions, dispatch);
export const WriteActions: WriteActionCreators = bindActionCreators(writeActions, dispatch);
export const SampleActions: SampleActionCreators = bindActionCreators(sampleActions, dispatch);
2 changes: 2 additions & 0 deletions velog-frontend/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Auth } from './modules/auth';
import type { User } from './modules/user';
import type { Base } from './modules/base';
import type { Write } from './modules/write';
import type { Sample } from './modules/sample';

const store = configure();

Expand All @@ -14,6 +15,7 @@ export type State = {
user: User,
base: Base,
write: Write,
sample: Sample,
pender: {
pending: any,
success: any,
Expand Down
38 changes: 38 additions & 0 deletions velog-frontend/src/store/modules/base.bak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// @flow
import { createAction, handleActions } from 'redux-actions';
import { Record, type Map } from 'immutable';

const SHOW_USER_MENU = 'base/SHOW_USER_MENU';
const HIDE_USER_MENU = 'base/HIDE_USER_MENU';
const SET_FULLSCREEN_LOADER = 'base/SET_FULLSCREEN_LOADER';

export type BaseActionCreators = {
showUserMenu(): any,
hideUserMenu(): any,
setFullscreenLoader(visibility: boolean): any,
};

export const actionCreators = {
showUserMenu: createAction(SHOW_USER_MENU),
hideUserMenu: createAction(HIDE_USER_MENU),
setFullscreenLoader: createAction(SET_FULLSCREEN_LOADER),
};

export type Base = {
userMenu: boolean,
fullscreenLoader: boolean
}

const BaseRecord = Record({
userMenu: false,
fullscreenLoader: false,
});


const initialState: Map<string, *> = BaseRecord();

export default handleActions({
[SHOW_USER_MENU]: state => state.set('userMenu', true),
[HIDE_USER_MENU]: state => state.set('userMenu', false),
[SET_FULLSCREEN_LOADER]: (state, { payload: visibility }) => state.set('fullscreenLoader', visibility),
}, initialState);
53 changes: 33 additions & 20 deletions velog-frontend/src/store/modules/base.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,51 @@
// @flow
import { createAction, handleActions } from 'redux-actions';
import { Record, type Map } from 'immutable';
import { createAction, handleActions, type ActionTypes } from 'redux-actions';
import produce from 'immer';

const SHOW_USER_MENU = 'base/SHOW_USER_MENU';
const HIDE_USER_MENU = 'base/HIDE_USER_MENU';
const SET_FULLSCREEN_LOADER = 'base/SET_FULLSCREEN_LOADER';

export type BaseActionCreators = {
showUserMenu(): any,
hideUserMenu(): any,
setFullscreenLoader(visibility: boolean): any,
};
const showUserMenu = createAction(SHOW_USER_MENU);
const hideUserMenu = createAction(HIDE_USER_MENU);
const setFullscreenLoader = createAction(
SET_FULLSCREEN_LOADER,
(visibility): boolean => visibility);

type ShowUserMenuAction = ActionType<typeof showUserMenu>;
type HideUserMenuAction = ActionType<typeof hideUserMenu>;
type SetFullscreenLoaderAction = ActionType<typeof setFullscreenLoader>;

export const actionCreators = {
showUserMenu: createAction(SHOW_USER_MENU),
hideUserMenu: createAction(HIDE_USER_MENU),
setFullscreenLoader: createAction(SET_FULLSCREEN_LOADER),
export interface BaseActionCreators {
showUserMenu(): ShowUserMenuAction,
hideUserMenu(): HideUserMenuAction,
setFullscreenLoader(): SetFullscreenLoaderAction
}

export const actionCreators: BaseActionCreators = {
showUserMenu, hideUserMenu, setFullscreenLoader,
};

export type Base = {
userMenu: boolean,
fullscreenLoader: boolean
}
};

const BaseRecord = Record({
const initialState: Base = {
userMenu: false,
fullscreenLoader: false,
});


const initialState: Map<string, *> = BaseRecord();
};

export default handleActions({
[SHOW_USER_MENU]: state => state.set('userMenu', true),
[HIDE_USER_MENU]: state => state.set('userMenu', false),
[SET_FULLSCREEN_LOADER]: (state, { payload: visibility }) => state.set('fullscreenLoader', visibility),
[SHOW_USER_MENU]: state => produce(state, (draft) => {
draft.userMenu = true;
}),
[HIDE_USER_MENU]: state => produce(state, (draft) => {
draft.userMenu = false;
}),
[SET_FULLSCREEN_LOADER]: (state, action: SetFullscreenLoaderAction) => {
return produce(state, (draft) => {
draft.fullscreenLoader = action.payload;
});
},
}, initialState);
12 changes: 6 additions & 6 deletions velog-frontend/src/store/modules/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ type InsertAction = ActionType<typeof insert>;
type ChangeAction = ActionType<typeof change>;

/* EXPORTING ACTION CREATORS / ACTION CREATORS TYPES */
export type SampleActionCreators = {
export interface SampleActionCreators {
plain(): PlainAction,
increase(value: number): IncreaseAction,
insert(payload: InsertPayload): InsertAction,
change(text: string): IncreaseAction
};
change(text: string): ChangeAction,
}

export const actionCreators = {
export const actionCreators: SampleActionCreators = {
plain, increase, insert, change,
};

Expand All @@ -45,13 +45,13 @@ type TodoItem = {
done: boolean
};

type State = {
export type Sample = {
value: number,
text: string,
todos: TodoItem[],
}

const initialState: State = {
const initialState: Sample = {
value: 0,
text: '',
todos: [],
Expand Down
12 changes: 12 additions & 0 deletions velog-frontend/src/store/modules/tests/base.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// // @flow
// import base, { actionCreators, type Base } from '../base';

// describe('base', () => {
// describe('reducer', () => {
// // let state: Base = base(undefined, {});
// // it('SHOW_USER_MENU works', () => {
// // state = base(state, actionCreators.showUserMenu());
// // expect(state.userMenu).tobe(true);
// // });
// });
// });
11 changes: 11 additions & 0 deletions velog.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"folders": [
{
"path": "velog-backend"
},
{
"path": "velog-frontend"
}
],
"settings": {}
}

0 comments on commit 2ec3edc

Please sign in to comment.