Skip to content

Commit f2cfe84

Browse files
committed
feat(StatusModal): add optional action handler/prop for status modal
1 parent 426992b commit f2cfe84

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

lib/common/actions/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function secureFetch (url, method = 'get', payload, raw) {
55
return function (dispatch, getState) {
66
const {user} = getState()
77
var opts = {
8-
method: method,
8+
method,
99
headers: {
1010
'Authorization': `Bearer ${user.token}`,
1111
'Accept': 'application/json',
@@ -20,18 +20,18 @@ export function secureFetch (url, method = 'get', payload, raw) {
2020
.then(res => {
2121
// if raw response is requested
2222
if (raw) return res
23-
23+
let action, message
2424
// check for errors
2525
if (res.status >= 500) {
26-
dispatch(setErrorMessage(`Network error!\n\n(${method} request on ${url})`))
26+
action = 'RELOAD'
27+
message = `Network error!\n\n(${method} request on ${url})`
28+
dispatch(setErrorMessage(message, action))
2729
return null
2830
} else if (res.status >= 400) {
29-
// res.text().then(text => {
30-
// dispatch(setErrorMessage(text))
31-
// })
31+
action = res.status === 401 ? 'LOG_IN' : null
3232
res.json().then(json => {
3333
const message = json.message || `Unknown (${res.status}) error occurred while making request`
34-
dispatch(setErrorMessage(message))
34+
dispatch(setErrorMessage(message, action))
3535
})
3636
return null
3737
} else {

lib/common/components/StatusModal.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import React from 'react'
2-
import { Modal, Button } from 'react-bootstrap'
1+
import React, {Component, PropTypes} from 'react'
2+
import {Modal, Button} from 'react-bootstrap'
3+
4+
export default class StatusModal extends Component {
5+
static propTypes = {
6+
title: PropTypes.string,
7+
body: PropTypes.string,
8+
action: PropTypes.string,
9+
onConfirm: PropTypes.func
10+
}
311

4-
export default class StatusModal extends React.Component {
512
state = {
613
showModal: false
714
}
@@ -10,8 +17,7 @@ export default class StatusModal extends React.Component {
1017
if (newProps.title) {
1118
this.setState({
1219
showModal: true,
13-
title: newProps.title,
14-
body: newProps.body
20+
...newProps
1521
})
1622
}
1723
}
@@ -23,12 +29,21 @@ export default class StatusModal extends React.Component {
2329
this.props.clearStatusModal()
2430
}
2531

32+
handleLogin = () => {
33+
this.props.login()
34+
.then((success) => {
35+
if (success) {
36+
this.close()
37+
}
38+
}, failure => {
39+
console.log(failure)
40+
})
41+
}
42+
2643
open (props) {
2744
this.setState({
2845
showModal: true,
29-
title: props.title,
30-
body: props.body,
31-
onConfirm: props.onConfirm
46+
...props
3247
})
3348
}
3449

@@ -37,19 +52,30 @@ export default class StatusModal extends React.Component {
3752
this.close()
3853
}
3954

55+
_renderAction = (action) => {
56+
switch (action) {
57+
case 'LOG_IN':
58+
return <Button bsStyle='primary' onClick={this.handleLogin}>Log in</Button>
59+
default:
60+
return null
61+
}
62+
}
63+
4064
render () {
4165
const {Body, Footer, Header, Title} = Modal
66+
const {title, body, action} = this.state
4267
return (
4368
<Modal
4469
show={this.state.showModal}
4570
onHide={this.close}>
4671
<Header>
47-
<Title>{this.state.title}</Title>
72+
<Title>{title}</Title>
4873
</Header>
4974
<Body>
50-
<p style={{whiteSpace: 'pre-wrap'}}>{this.state.body}</p>
75+
<p style={{whiteSpace: 'pre-wrap'}}>{body}</p>
5176
</Body>
5277
<Footer>
78+
{this._renderAction(action)}
5379
<Button onClick={this.close}>Close</Button>
5480
</Footer>
5581
</Modal>

lib/common/containers/CurrentStatusModal.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import { connect } from 'react-redux'
1+
import {connect} from 'react-redux'
22

33
import StatusModal from '../components/StatusModal'
4-
import { clearStatusModal } from '../../manager/actions/status'
4+
import {clearStatusModal} from '../../manager/actions/status'
5+
import {login} from '../../manager/actions/user'
56

67
const mapStateToProps = (state, ownProps) => {
8+
const {action, body, title} = state.status.modal ? state.status.modal : {}
79
return {
8-
title: state.status.modal ? state.status.modal.title : null,
9-
body: state.status.modal ? state.status.modal.body : null
10+
title,
11+
action,
12+
body
1013
}
1114
}
1215
const mapDispatchToProps = {
13-
clearStatusModal
16+
clearStatusModal,
17+
login
1418
}
1519
var CurrentStatusModal = connect(
1620
mapStateToProps,

0 commit comments

Comments
 (0)