Skip to content

Zhenjieruan/modal stack #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
build
**/*.md
.idea/
37 changes: 10 additions & 27 deletions app/components/Modal/actions.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
/* @flow weak */
const createAction = (action) => {
var resolve, reject
var promise = new Promise((rs, rj) => { resolve = rs; reject = rj })
var baseAction = {
meta: { promise, resolve, reject },
then: promise.then.bind(promise),
catch: promise.catch.bind(promise)
}
import { createAction } from 'redux-actions'
import { promiseActionMixin } from '../../utils'

return Object.assign(baseAction, action)
}
let modal_id = 0;

export const MODAL_SHOW = 'MODAL_SHOW'
export function showModal (modalType, content) {
return createAction({
type: MODAL_SHOW,
payload: {modalType, content}
})
}
export const showModal = promiseActionMixin(
createAction(MODAL_SHOW, (modalType, content) => ({_id: modal_id++, modalType, content}))
)

export const MODAL_DISMISS = 'MODAL_DISMISS'
export function dismissModal () {
return {
type: MODAL_DISMISS
}
}
export const dismissModal = createAction(MODAL_DISMISS)

export const MODAL_UPDATE = 'MODAL_UPDATE'
export function updateModal (content) {
return createAction({
type: MODAL_UPDATE,
payload: {content}
})
}
export const updateModal = promiseActionMixin(
createAction(MODAL_UPDATE, content => ({content}))
)
26 changes: 16 additions & 10 deletions app/components/Modal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ import {
} from './modals'

var ModalContainer = (props) => {
const {isActive, showBackdrop, position, dispatch} = props
return isActive ? (
<div className={cx('modal-container', position,
{'show-backdrop': showBackdrop}
)}>
<Modal {...props}/>
<div className='backdrop'
onClick={e=>dispatch({type:'MODAL_DISMISS'})}></div>
</div>
) : null
const {dispatch} = props;
const hasModal = props.stack.length > 0;
return hasModal ? <div className={cx('modals-container')}>
{props.stack.map((config) => {
const {_id, isActive, showBackdrop, position} = config;
return isActive ? (
<div key={_id} className={cx('modal-container', position,
{'show-backdrop': showBackdrop}
)}>
<Modal {...config}/>
<div className='backdrop'
onClick={e=>dispatch({type:'MODAL_DISMISS'})}></div>
</div>
) : null
})}
</div> :null;
}
ModalContainer = connect(state => state.ModalState, null)(ModalContainer)

Expand Down
52 changes: 42 additions & 10 deletions app/components/Modal/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,71 @@ import {
} from './actions'

const _state = {
_id: 0,
isActive: false,
showBackdrop: false,
position: 'top'
}
};

export default function ModalReducer (state = _state, action) {
function modal(state = _state, action) {
switch (action.type) {

case MODAL_SHOW:
var newState = {
return Object.assign({}, state, {
_id: action.payload._id,
isActive: true,
modalType: action.payload.modalType,
meta: action.meta,
content: action.payload.content
}
return { ...state, ...newState }
});


case MODAL_DISMISS:
var newState = {
return Object.assign({}, state, {
isActive: false,
content: null,
modalType: null
}
return { ...state, ...newState }
});

case MODAL_UPDATE:
if (state._id != action._id) {
return state;
}

return {
...state,
meta: action.meta,
content: Object.assign({}, state.content, action.payload.content)
}
};

default:
return state;
}
}

export default function ModalsReducer(state = {stack: []}, action) {
switch (action.type) {

case MODAL_SHOW:
return Object.assign({}, state, {
stack: [
...state.stack,
modal(undefined, action)
]
});

case MODAL_DISMISS:
modal(state[state.length - 1], action);
return Object.assign({}, state, {stack: state.stack.slice(0, -1)});

case MODAL_UPDATE:
return Object.assign({}, state, {
stack: state.map(modal =>
modal(modal, action)
)
});

default:
return state
return state;
}
}
4 changes: 2 additions & 2 deletions app/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PaneReducer, { PaneCrossReducer } from './components/Pane/reducer'
import TabReducer from './components/Tab/reducer'
import EditorReducer from './components/AceEditor/reducer'
import FileTreeReducer from './components/FileTree/reducer'
import ModalReducer from './components/Modal/reducer'
import ModalsReducer from './components/Modal/reducer'
import NotificationReducer from './components/Notification/reducer'
import TerminalReducer from './components/Terminal/reducer'
import GitReducer from './components/Git/reducer'
Expand All @@ -21,7 +21,7 @@ const combinedReducers = combineReducers({
PaneState: PaneReducer,
TabState: TabReducer,
EditorState: EditorReducer,
ModalState: ModalReducer,
ModalState: ModalsReducer,
TerminalState: TerminalReducer,
GitState: GitReducer,
NotificationState: NotificationReducer,
Expand Down
3 changes: 3 additions & 0 deletions app/styles/components/Modal.styl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.modals-container {
}

.modal-container {
fixed(0);
display: flex;
Expand Down
1 change: 1 addition & 0 deletions app/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export path from './path'
export request from './request'
export setSelectionRange from './setSelectionRange'
export composeReducers from './composeReducers'
export promiseActionMixin from './promiseActionMixin'
18 changes: 18 additions & 0 deletions app/utils/promiseActionMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const promiseActionMixin = (actionCreator) => {
return function decoratedActionCreator () {
const action = actionCreator.apply(null, arguments)

let resolve, reject
let promise = new Promise((rs, rj) => { resolve = rs; reject = rj })
let meta = { promise, resolve, reject }

return {
...action,
meta: action.meta ? {...action.meta, ...meta} : meta,
then: promise.then.bind(promise),
catch: promise.catch.bind(promise)
}
}
}

export default promiseActionMixin