Skip to content
This repository was archived by the owner on Aug 24, 2019. It is now read-only.

Commit 8385f87

Browse files
committed
add msgstack
1 parent 4d420de commit 8385f87

File tree

10 files changed

+198
-3
lines changed

10 files changed

+198
-3
lines changed

src/actions/actionTypes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,8 @@ export const POP_ADD_APP_INPUT = 'POP_ADD_APP_INPUT';
8282
export const REQUEST_ADD_PRODUCTS = 'REQUEST_ADD_PRODUCTS';
8383
export const RECEIVE_ADD_PRODUCTS = 'RECEIVE_ADD_PRODUCTS';
8484
/*========= end productsActions ===========*/
85+
86+
/*========= begin msgStackActions ===========*/
87+
export const MSG_STACK_SHOW_MSG = 'MSG_STACK_SHOW_MSG';
88+
export const MSG_STACK_CLOSE_MSG = 'MSG_STACK_CLOSE_MSG';
89+
/*========= end msgStackActions ===========*/

src/actions/msgStackActions.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {
2+
MSG_STACK_SHOW_MSG,
3+
MSG_STACK_CLOSE_MSG,
4+
} from './actionTypes';
5+
import _ from 'lodash';
6+
7+
export function addShowMsg(text, type="info", showTime=10) {
8+
return {
9+
type: MSG_STACK_SHOW_MSG,
10+
payload: {text,type,showTime},
11+
}
12+
}
13+
14+
export function closeMsg(id) {
15+
return {
16+
type: MSG_STACK_CLOSE_MSG,
17+
payload: id,
18+
}
19+
}

src/actions/productsActions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import _ from 'lodash';
33
import * as types from './actionTypes';
44
import restApi from '../network/RestApi';
55
import {checkResponseAuth} from './authActions';
6+
import {addShowMsg} from './msgStackActions';
67

78
export function requestProducts() {
89
return {
@@ -62,6 +63,11 @@ export function addProducts(appName) {
6263
return restApi.addProducts(appName)
6364
.then(data => {
6465
checkResponseAuth(dispatch, data);
66+
if (_.get(data, 'status') !== "OK") {
67+
dispatch(addShowMsg("创建应用: "+_.get(data,'errorMessage'), "danger"));
68+
} else {
69+
dispatch(addShowMsg("创建 "+_.get(data,'results.app.name')+" 应用成功", "success"));
70+
}
6571
dispatch(receiveAddProducts(data));
6672
dispatch(getProducts());
6773
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React, {Component, PropTypes} from 'react';
2+
import _ from 'lodash';
3+
import {Alert, Fade} from 'react-bootstrap';
4+
5+
class MsgStack extends Component {
6+
static propTypes = {
7+
items: PropTypes.array,
8+
close: PropTypes.func,
9+
};
10+
11+
static defaultProps = {
12+
items: [],
13+
close: ()=>{}
14+
};
15+
16+
intervalId = null;
17+
18+
checkItemsInterval() {
19+
const self = this;
20+
if (this.props.items && this.props.items.length > 0 ) {
21+
if (this.intervalId) {
22+
return;
23+
}
24+
this.intervalId = setInterval(function () {
25+
var time = parseInt((new Date()).getTime() /1000)
26+
_.map(self.props.items, function(item){
27+
console.log(item);
28+
if((parseInt(_.get(item, 'time')) + parseInt(_.get(item, 'showTime'))) < time) {
29+
self.props.close(_.get(item, 'id'));
30+
}
31+
})
32+
}, 1000);
33+
} else {
34+
if (this.intervalId) {
35+
clearInterval(this.intervalId);
36+
}
37+
}
38+
}
39+
40+
constructor() {
41+
super();
42+
this.checkItemsInterval = this.checkItemsInterval.bind(this);
43+
}
44+
45+
componentDidMount() {
46+
setTimeout(this.checkItemsInterval, 200);
47+
}
48+
49+
componentWillReceiveProps(nextProps) {
50+
this.checkItemsInterval();
51+
}
52+
53+
render() {
54+
const self = this;
55+
return (
56+
<div>
57+
{
58+
_.map(self.props.items, function (item) {
59+
var bsStyle = "info";
60+
if (_.indexOf(["info", "warning", "danger", "success"],item.type)!== -1){
61+
bsStyle = item.type;
62+
}
63+
return (
64+
<Alert key={item.id} bsStyle={bsStyle} onDismiss={()=>{self.props.close(item.id)}}>
65+
{item.text}
66+
</Alert>
67+
)
68+
})
69+
}
70+
</div>
71+
);
72+
}
73+
}
74+
75+
export default MsgStack;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
{
3+
"name": "MsgStack",
4+
"version": "0.0.0",
5+
"private": true,
6+
"main": "./MsgStack.js"
7+
}

src/containers/LayoutContainer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import _ from 'lodash';
66
import Header from '../components/Header';
77
import Footer from '../components/Footer';
88
import Layout from '../components/Layout';
9+
import MsgStackContainer from './MsgStackContainer';
910

1011
class LayoutContainer extends Component {
1112
render() {
@@ -17,6 +18,7 @@ class LayoutContainer extends Component {
1718
/>
1819
{this.props.children}
1920
<Footer/>
21+
<MsgStackContainer/>
2022
</Layout>
2123
);
2224
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React, {Component} from 'react';
2+
3+
import { connect } from 'react-redux';
4+
import { bindActionCreators } from 'redux';
5+
import _ from 'lodash';
6+
import MsgStack from '../components/MsgStack';
7+
import * as msgStackActions from '../actions/msgStackActions';
8+
9+
class MsgStackContainer extends Component {
10+
render() {
11+
const {msgStack, actions } = this.props;
12+
return (
13+
<div style={{position: 'fixed',top:60,right:20, width:'30%' }}>
14+
<MsgStack
15+
items={_.get(msgStack, 'rs', [])}
16+
close={(id)=>{actions.closeMsg(id)}}
17+
/>
18+
</div>
19+
);
20+
}
21+
}
22+
23+
function mapStateToProps(state, ownProps) {
24+
return {'msgStack': _.get(state, 'msgStack', {})};
25+
}
26+
27+
function mapDispatchToProps(dispatch, ownProps) {
28+
return {
29+
actions: bindActionCreators(Object.assign({}, msgStackActions), dispatch)
30+
}
31+
}
32+
33+
export default connect(
34+
mapStateToProps,
35+
mapDispatchToProps
36+
)(MsgStackContainer)

src/network/RestApi.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ class RestApi {
4242
}
4343

4444
addProducts(appName) {
45-
return this.post('/apps', {name:appName}, false)
45+
return this.post('/apps', {name:appName})
4646
.then(data=>{
4747
if (data.httpCode == 200) {
48-
return {status:"OK", httpCode: data.httpCode, results: this.jsonDecode(data.text)};
48+
var rs = this.jsonDecode(data);
49+
if (_.get(rs, 'status') != "ERROR") {
50+
return {status:"OK", httpCode: data.httpCode, results: rs};
51+
} else {
52+
return rs;
53+
}
4954
} else {
5055
return {status:"ERROR", httpCode: data.httpCode, errorCode: 0, errorMessage: data.text};
5156
}
@@ -161,7 +166,7 @@ class RestApi {
161166
});
162167
}
163168

164-
delete(uri, params={}, jsonDecode = true) {
169+
delete(uri, params={}) {
165170
return fetch(this.baseURI + uri, {
166171
method: 'DELETE',
167172
headers: this.headers,

src/reducers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {register} from './registers';
66
import {auth, accessKeys} from './auth';
77
import {routes} from './routes';
88
import {products, addProducts} from './products';
9+
import {msgStack} from './msgStack';
910

1011
const appReducer = combineReducers({
1112
users,
@@ -17,6 +18,7 @@ const appReducer = combineReducers({
1718
routes,
1819
products,
1920
addProducts,
21+
msgStack,
2022
});
2123

2224
const rootReducer = (state, action) => {

src/reducers/msgStack.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {
2+
MSG_STACK_SHOW_MSG,
3+
MSG_STACK_CLOSE_MSG,
4+
} from '../actions/actionTypes';
5+
import _ from 'lodash';
6+
import restApi from '../network/RestApi';
7+
import uuid from 'uuid';
8+
9+
export function msgStack(state = {rs:[]}, action) {
10+
let payload = action.payload;
11+
switch (action.type) {
12+
case MSG_STACK_SHOW_MSG:
13+
var id = uuid.v4();
14+
var data = Object.assign({}, state);
15+
var msg = {
16+
id,
17+
type: _.get(payload, 'type'),
18+
text: _.get(payload, 'text'),
19+
showTime: parseInt(_.get(payload, 'showTime')),
20+
time: parseInt((new Date()).getTime()/1000)
21+
};
22+
var rs = _.get(data, 'rs', []);
23+
rs.splice(0, 0, msg);
24+
_.set(data, 'rs', rs);
25+
return data;
26+
27+
case MSG_STACK_CLOSE_MSG:
28+
var id = payload;
29+
var data = Object.assign({}, state);
30+
_.remove(_.get(data, 'rs', []), (item)=>{
31+
return _.get(item, 'id') === id;
32+
});
33+
return data;
34+
35+
default:
36+
return state
37+
}
38+
}

0 commit comments

Comments
 (0)