Commit 6059db29 authored by 2breakegg's avatar 2breakegg

lint

parents 900bf08f 044b5655
import { Form, Input } from 'antd';
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import styles from './EmailForm.css';
import SubmitButton from './SubmitButton'; import {connect} from 'react-redux'
import { Form, Input, Icon, Button } from 'antd'
const FormItem = Form.Item; const FormItem = Form.Item;
import SubmitButton from './SubmitButton'
const formItemLayout = { const formItemLayout = {
labelCol: { span: 4 }, labelCol: { span: 4 },
wrapperCol: { span: 15 }, wrapperCol: { span: 15 },
}; }
class EmailForm extends React.Component { class EmailForm extends React.Component {
onUpdateEmail = (e) => { onSubmit = (e) => {
const { form, dispatch, data: { id } } = this.props; const { form, dispatch, data: {id} } = this.props
e && e.preventDefault(); e && e.preventDefault();
form.validateFieldsAndScroll((err, values) => { form.validateFieldsAndScroll((err, values) => {
if (!err) { if (!err) {
console.log('Received values of form: ', values); console.log('Received values of form: ', values);
const { email, password } = values
const { email, password } = values; dispatch({type: "user/updateAccount", payload: { email, password, user_id: id }})
dispatch({ type: 'user/updateEmail', payload: { email, password, user_id: id } });
} }
}); });
}; }
render() { render(){
const { form, dispatch, data, checkEmail, isEmailExists } = this.props; const {form, dispatch, data, checkEmail, isEmailExists} = this.props
const { getFieldDecorator } = form; const {getFieldDecorator} = form
const { id, email } = data; const {id, email} = data
const emailProps = { const emailProps = {
fromItem: { fromItem: {
label: 'email', label: "email",
hasFeedback: true, hasFeedback: true,
validateStatus: checkEmail, validateStatus: checkEmail,
help: isEmailExists ? 'email exists' : '', help: isEmailExists ? 'email exists' : '',
...formItemLayout ...formItemLayout
}, },
decorator: { decorator: {
initialValue: email, initialValue: email
}, },
input: { input: {
placeholder: 'email', placeholder: "email"
}, }
}; }
const passwordProps = { const passwordProps = {
fromItem: { fromItem: {
label: 'passwrod', label: "passwrod",
...formItemLayout, ...formItemLayout
}, },
decorator: { decorator: {
rules: [ rules: [
{ required: true, message: '密码至少为8-24位', pattern: /^.{8,24}$/ }, { required: true, message: '密码至少为8-24位', pattern: /^.{8,24}$/ }
], ]
}, },
input: { input: {
placeholder: 'password', placeholder: "password",
type: 'password', type: 'password'
}, }
}; }
return ( return (
<Form onSubmit={this.onUpdateEmail}> <Form onSubmit={this.onSubmit}>
<FormItem {...emailProps.fromItem}> <FormItem {...emailProps.fromItem}>
{getFieldDecorator(`email`, { ...emailProps.decorator })( {getFieldDecorator(`email`, {...emailProps.decorator})(
<Input <Input
{...emailProps.input} {...emailProps.input}
onBlur={() => dispatch({ type: 'auth/checkEmail', payload: { ...form.getFieldsValue(), id } })}/>, onBlur = {() => dispatch({type: 'auth/checkEmail', payload: { ...form.getFieldsValue(), id} })}/>
)} )}
</FormItem> </FormItem>
<FormItem {...passwordProps.fromItem}> <FormItem {...passwordProps.fromItem}>
{getFieldDecorator('password', { ...passwordProps.decorator })( {getFieldDecorator(`password`, {...passwordProps.decorator})(
<Input {...passwordProps.input} />, <Input {...passwordProps.input} />
)} )}
</FormItem> </FormItem>
...@@ -92,19 +92,20 @@ class EmailForm extends React.Component { ...@@ -92,19 +92,20 @@ class EmailForm extends React.Component {
} }
function mapStateToProps(state) {
function mapStateToProps(state, props) {
const { const {
user: { data }, user: {data},
auth: { isEmailExists, checkEmail }, auth: {isEmailExists, checkEmail}
} = state; } = state
return { return {
data, data,
checkEmail, checkEmail,
isEmailExists, isEmailExists
}; };
} }
const WrapperEmailForm = Form.create()(EmailForm); const WrapperEmailForm = Form.create()(EmailForm)
export default connect(mapStateToProps)(WrapperEmailForm); export default connect(mapStateToProps)(WrapperEmailForm);
import { Form, Input } from 'antd';
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import styles from './EmailForm.css';
import SubmitButton from './SubmitButton'; import {connect} from 'react-redux'
import { Form, Input, Icon, Button } from 'antd'
const FormItem = Form.Item; const FormItem = Form.Item;
import SubmitButton from './SubmitButton'
const formItemLayout = { const formItemLayout = {
labelCol: { span: 4 }, labelCol: { span: 4 },
wrapperCol: { span: 15 }, wrapperCol: { span: 15 },
}; }
class EmailForm extends React.Component { class EmailForm extends React.Component {
checkPassword = (rule, value, callback) => {
const form = this.props.form;
if (value && value !== form.getFieldValue('new_password')) {
callback('两次密码输入不符');
} else {
callback();
}
}
checkConfirm = (rule, value, callback) => {
const form = this.props.form;
if (value) {
form.validateFields(['confirm'], { force: true });
}
callback();
}
onSubmit = (e) => { onSubmit = (e) => {
const { form, dispatch, data: { id } } = this.props; const { form, dispatch, data: {id} } = this.props
e && e.preventDefault(); e && e.preventDefault();
form.validateFieldsAndScroll((err, values) => { form.validateFieldsAndScroll((err, values) => {
if (!err) { if (!err) {
console.log('Received values of form: ', values); console.log('Received values of form: ', values);
const { new_password, password } = values
const { email, password } = values; dispatch({type: "user/updateAccount", payload: { new_password, password, user_id: id }})
dispatch({ type: 'user/updateEmail', payload: { email, password, user_id: id } });
} }
}); });
}; }
checkConfirm = (rule, value, callback) => {
const form = this.props.form;
if (value) {
form.validateFields(['confirm'], { force: true });
}
callback();
};
checkPassword = (rule, value, callback) => {
const form = this.props.form;
if (value && value !== form.getFieldValue('new_password')) {
callback('两次密码输入不符');
} else {
callback();
}
};
render() { render(){
const { form } = this.props; const {form } = this.props
const { getFieldDecorator } = form; const {getFieldDecorator} = form
const passwordProps = { const passwordProps = {
fromItem: { fromItem: {
label: 'passwrod', label: "passwrod",
...formItemLayout ...formItemLayout
}, },
decorator: { decorator: {
rules: [ rules: [
{ required: true, message: '密码至少为8-24位', pattern: /^.{8,24}$/ }, { required: true, message: '密码至少为8-24位', pattern: /^.{8,24}$/ },
{ validator: this.checkConfirm }, { validator: this.checkConfirm }
], ]
}, },
input: { input: {
placeholder: 'password', placeholder: "password",
type: 'password', type: 'password'
}, }
}; }
const confirmProps = { const confirmProps = {
fromItem: { fromItem: {
label: 'comfirm', label: "comfirm",
...formItemLayout ...formItemLayout
}, },
decorator: { decorator: {
rules: [ rules: [
{ required: true, message: '密码至少为8-24位', pattern: /^.{8,24}$/ }, { required: true, message: '密码至少为8-24位', pattern: /^.{8,24}$/},
{ validator: this.checkPassword }, { validator: this.checkPassword}
], ]
}, },
input: { input: {
placeholder: 'confirm', placeholder: "confirm",
type: 'password', type: 'password'
}, }
}; }
return ( return (
<Form onSubmit={this.onSubmit}> <Form onSubmit={this.onSubmit}>
<FormItem {...passwordProps.fromItem} label="old password"> <FormItem {...passwordProps.fromItem} label="old password">
{getFieldDecorator(`old_password`, { ...passwordProps.decorator })( {getFieldDecorator(`password`, {...passwordProps.decorator})(
<Input {...passwordProps.input} />, <Input {...passwordProps.input} />
)} )}
</FormItem> </FormItem>
<FormItem {...passwordProps.fromItem} label="new password"> <FormItem {...passwordProps.fromItem} label="new password">
{getFieldDecorator(`new_password`, { ...passwordProps.decorator })( {getFieldDecorator(`new_password`, {...passwordProps.decorator})(
<Input {...passwordProps.input} />, <Input {...passwordProps.input} />
)} )}
</FormItem> </FormItem>
<FormItem {...confirmProps.fromItem}> <FormItem {...confirmProps.fromItem}>
{getFieldDecorator(`confirm`, { ...confirmProps.decorator })( {getFieldDecorator(`confirm`, {...confirmProps.decorator})(
<Input {...confirmProps.input} />, <Input {...confirmProps.input} />
)} )}
</FormItem> </FormItem>
...@@ -113,16 +113,18 @@ class EmailForm extends React.Component { ...@@ -113,16 +113,18 @@ class EmailForm extends React.Component {
} }
function mapStateToProps(state, props) { function mapStateToProps(state, props) {
const { const {
user: { data }, user: {data}
} = state; } = state
return { return {
data, data,
}; };
} }
const WrapperEmailForm = Form.create()(EmailForm); const WrapperEmailForm = Form.create()(EmailForm)
export default connect(mapStateToProps)(WrapperEmailForm); export default connect(mapStateToProps)(WrapperEmailForm);
import React from 'react';
import styles from './SendEmail.css';
function SendEmail() {
return (
<div className={styles.normal}>
Component: SendEmail
</div>
);
}
export default SendEmail;
import dva from 'dva'; import dva from 'dva';
import { browserHistory } from 'dva/router';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import { addLocaleData, IntlProvider } from 'react-intl'; import { browserHistory } from 'dva/router'
import en from 'react-intl/locale-data/en'; import { message } from 'antd'
import zh from 'react-intl/locale-data/zh';
import localeData from '../i18n.json';
import './index.css'; import './index.css';
import { IntlProvider, addLocaleData } from 'react-intl';
// 1. Initialize // 1. Initialize
const app = dva({ const app = dva({
history: browserHistory, onError: (error, dispatch) => {
message.destroy();
message.error(error.message);
},
history: browserHistory
}); });
...@@ -35,6 +37,9 @@ app.model(require('./models/common')); ...@@ -35,6 +37,9 @@ app.model(require('./models/common'));
app.router(require('./router')); app.router(require('./router'));
// 5. Start // 5. Start
import en from 'react-intl/locale-data/en'
import zh from 'react-intl/locale-data/zh'
import localeData from '../i18n.json'
addLocaleData([...en, ...zh]); addLocaleData([...en, ...zh]);
......
import { message } from 'antd'; import { login, forgot, register, reset, activate, checkUserExists } from '../services/auth'
import { activate, checkUserExists, forgot, login, register, reset } from '../services/auth'; import { message } from 'antd'
export default { export default {
...@@ -8,13 +8,15 @@ export default { ...@@ -8,13 +8,15 @@ export default {
activateState: false, activateState: false,
checkEmail: '', checkEmail: '',
checkUsername: '', checkUsername: '',
isSendEmailActive: false,
isEmailExists: false, isEmailExists: false,
isUserNameExists: false, isUserNameExists: false,
isRegisterSubmit: false, isRegisterSubmit: false,
isLoginSubmit: false, isLoginSubmit: false,
isForgotSubmit: false, isForgotSubmit: false,
isSpinSubmit: false, isSpinSubmit: false,
register: {}, isActivateSubmit: false,
register: {}
}, },
reducers: { reducers: {
change(state, action) { change(state, action) {
...@@ -100,111 +102,105 @@ export default { ...@@ -100,111 +102,105 @@ export default {
forgotSuccess(state, action) { forgotSuccess(state, action) {
return { return {
...state, ...{ ...state, ...{
isForgotSubmit: false, isForgotSubmit: false
} }
}; }
}, },
forgotFail(state, action) { forgotFail(state, action) {
return { return {
...state, ...{ ...state, ...{
isForgotSubmit: false, isForgotSubmit: false
} }
}; }
}, },
reset(state, action) { reset(state, action) {
return { return {
...state, ...{ ...state, ...{
isResetSubmit: true, isResetSubmit: true
} }
}; }
}, },
resetSuccess(state, action) { resetSuccess(state, action) {
return { return {
...state, ...{ ...state, ...{
isResetSubmit: false, isResetSubmit: false
} }
}; }
}, },
resetFail(state, action) { resetFail(state, action) {
return { return {
...state, ...{ ...state, ...{
isResetSubmit: false, isResetSubmit: false
} }
}; }
}, },
}, },
effects: { effects: {
*activate({ payload }, { call, put }) { *activate({ payload }, { call, put }) {
const { data } = yield call(activate, payload); const { data } = yield call(activate, payload)
if (data) {
yield put({ type: 'check', payload: { activateState: false } });
}
}, },
*checkEmail({ payload }, { call, put }) { *checkEmail({ payload }, { call, put }) {
const { data } = yield call(checkUserExists, payload); const { data } = yield call(checkUserExists, payload)
if (data) { if(data) {
yield put({ type: 'check', payload: { isEmailExists: true, checkEmail: 'warning' } }); yield put({ type: 'check', payload: { isEmailExists: true , checkEmail: 'warning'}})
} else { } else {
yield put({ type: 'check', payload: { isEmailExists: false, checkEmail: 'success' } }); yield put({ type: 'check', payload: { isEmailExists: false , checkEmail: 'success'}})
} }
}, },
*checkUsername({ payload }, { call, put }) { *checkUsername({ payload }, { call, put }) {
const { data } = yield call(checkUserExists, payload); const { data } = yield call(checkUserExists, payload)
if (data) { if(data) {
yield put({ type: 'check', payload: { isUserNameExists: true, checkUsername: 'warning' } }); yield put({ type: 'check', payload: { isUserNameExists: true , checkUsername: 'warning'}})
} else { } else {
yield put({ type: 'check', payload: { isUserNameExists: false, checkUsername: 'success' } }); yield put({ type: 'check', payload: { isUserNameExists: false , checkUsername: 'success'}})
} }
}, },
*login({ payload }, { call, put }) { *login({ payload }, { call, put }) {
const { data } = yield call(login, payload); const {data} = yield call(login, payload)
if (data) { if(data){
yield put({ type: 'loginSuccess' }); yield put({ type: 'loginSuccess' })
yield put({ type: 'user/loginSuccess', payload: { data } }); yield put({ type: 'user/loginSuccess', payload: { data } })
message.info('登录成功'); message.info("登录成功")
} else { } else {
yield put({ type: 'loginFail' }); yield put({ type: 'loginFail' })
message.error('登陆失败'); message.error("登陆失败")
} }
}, },
*forgot({ payload }, { call, put }) { *forgot({ payload }, { call, put }) {
const { data } = yield call(forgot, payload); const { data } = yield call(forgot, payload)
if (data) { if(data){
yield put({ type: 'forgotSuccess' }); yield put({ type: 'forgotSuccess' })
message.info('已发送密码重置邮件'); message.info("已发送密码重置邮件")
} else { }else {
yield put({ type: 'forgotFail' }); yield put({ type: 'forgotFail' })
message.error('密码重置邮件发送失败'); message.error("密码重置邮件发送失败")
} }
}, },
*register({ payload }, { call, put }) { *register({ payload }, { call, put }) {
const { data } = yield call(register, payload); const {data} = yield call(register, payload)
if (data) { if(data) {
yield put({ type: 'registerSuccess' }); yield put({ type: 'registerSuccess'})
message.info('注册成功'); message.info("注册成功")
} else { } else {
yield put({ type: 'registerFail' }); yield put({ type: 'registerFail' })
message.error('注册失败'); message.error("注册失败")
} }
}, },
*reset({ payload }, { call, put }) { *reset({ payload }, { call, put }) {
const { data } = yield call(reset, payload); const { data } = yield call(reset, payload)
if (data) { if(data){
yield put({ type: 'resetSuccess' }); yield put({ type: 'resetSuccess' })
message.info('重置成功'); message.info("重置成功")
} else { }else{
yield put({ type: 'resetFail' }); yield put({ type: 'resetFail' })
message.error('重置失败'); message.error("重置失败")
} }
}, },
}, },
subscriptions: {}, subscriptions: {},
......
import { message } from 'antd'; import { routerRedux } from 'dva/router'
import { routerRedux } from 'dva/router'; import { updateProfile, updateAccount } from '../services/user'
import { updateAccount, updateProfile } from '../services/user'; import { message } from 'antd'
export default { export default {
namespace: 'user', namespace: 'user',
state: { state: {
data: {}, data: {},
isUpdateSubmit: false, isUpdateSubmit: false
}, },
reducers: { reducers: {
loginFromStorage(state, action) { loginFromStorage(state, action) {
return { return {
...state, ...action.payload ...state, ...action.payload
}; }
}, },
loginSuccess(state, action) { loginSuccess(state, action) {
return { return {
...state, ...action.payload ...state, ...action.payload
}; }
}, },
updateProfile(state, action) { updateProfile(state, action) {
return { return {
...state, ...{ ...state, ...{
isUpdateSubmit: true, isUpdateSubmit: true
} }
}; }
}, },
updateProfileSuccess(state, action) { updateProfileSuccess(state, action) {
return { return {
...state, ...action.payload, ...{ ...state, ...action.payload, ...{
isUpdateSubmit: false, isUpdateSubmit: false
} }
}; }
}, },
updateProfileFail(state, action) { updateProfileFail(state, action) {
return { return {
...state, ...{ ...state, ...{
isUpdateSubmit: false, isUpdateSubmit: false
} }
}; }
}, },
updateAccount(state, action) { updateAccount(state, action) {
return { return {
...state, ...{ ...state, ...{
isUpdateSubmit: true, isUpdateSubmit: true
} }
}; }
}, },
updateAccountSuccess(state, action) { updateAccountSuccess(state, action) {
return { return {
...state, ...action.payload, ...{ ...state, ...action.payload, ...{
isUpdateSubmit: false, isUpdateSubmit: false
} }
}; }
}, },
updateAccountFail(state, action) { updateAccountFail(state, action) {
return { return {
...state, ...{ ...state, ...{
isUpdateSubmit: false, isUpdateSubmit: false
} }
}; }
}, },
}, },
effects: { effects: {
*loginSuccess({ payload }, { call, put }) { *loginSuccess({ payload }, { call, put }) {
localStorage.setItem('user', JSON.stringify(payload.data)); localStorage.setItem("user", JSON.stringify(payload.data))
yield put(routerRedux.replace('/profiles')); yield put(routerRedux.replace("/profiles"))
}, },
*preLogin({ payload }, { call, put }) { *preLogin({ payload }, { call, put }) {
let user = localStorage.getItem('user'); let user = localStorage.getItem("user")
if (!user) { if(!user){
yield put(routerRedux.replace('/login')); yield put(routerRedux.replace("/login"))
} }
yield put({ type: 'loginFromStorage', payload: { data: JSON.parse(user) } }); yield put({type: 'loginFromStorage', payload: { data: JSON.parse(user) }})
}, },
*updateProfile({ payload }, { call, put }) { *updateProfile({ payload }, { call, put }) {
let { data } = yield call(updateProfile, payload); let { data } = yield call(updateProfile, payload )
if (data) { if(data){
yield put({ type: 'updateProfileSuccess', payload: { data } }); yield put({ type: 'updateProfileSuccess', payload: { data } })
message.info('更新成功'); message.info("更新成功")
} else { }else {
yield put({ type: 'updateProfileFail' }); yield put({ type: 'updateProfileFail' })
message.error('更新失败'); message.error("更新失败")
} }
}, },
*updateAccount({ payload }, { call, put }) { *updateAccount({ payload }, { call, put }) {
try {
let { data } = yield call(updateAccount, payload )
let { data } = yield call(updateAccount, payload); if(data){
yield put({ type: 'updateAccountSuccess', payload: { data } })
if (data) { message.info("更新成功")
yield put({ type: 'updateAccountSuccess', payload: { data } }); }
message.info('更新成功'); } catch (error) {
} else { yield put({ type: 'updateAccountFail' })
yield put({ type: 'updateAccountFail' }); message.error(error.message)
message.error('更新失败');
} }
}, },
}, },
subscriptions: { subscriptions: {
setup({ dispatch, history }) { setup({ dispatch, history }) {
return history.listen(({ pathname, query }) => { return history.listen(({ pathname, query}) => {
if (pathname == '/profiles') { if(pathname == '/profiles') {
dispatch({ type: 'preLogin', payload: query });
dispatch({ type: 'preLogin', payload: query})
} }
}); })
}, }
}, },
}; };
...@@ -5,12 +5,19 @@ function parseJSON(response) { ...@@ -5,12 +5,19 @@ function parseJSON(response) {
return response.json(); return response.json();
} }
function checkStatus(response) { async function checkStatus(response) {
if (response.status >= 200 && response.status < 300) { if (response.status >= 200 && response.status < 300) {
return response; return response;
} }
const error = new Error(response.statusText); let message
try {
message = (await response.json())["message"]
} catch (error) {
message = response.statusText
}
const error = new Error(message);
error.response = response; error.response = response;
throw error; throw error;
} }
...@@ -23,16 +30,16 @@ function checkStatus(response) { ...@@ -23,16 +30,16 @@ function checkStatus(response) {
* @return {object} An object containing either "data" or "err" * @return {object} An object containing either "data" or "err"
*/ */
export default function request(url, options) { export default function request(url, options) {
url = `${config.apiRoot}${url}`; url = `${config.apiRoot}${url}`
if (options && !options.headers && (options.method == 'POST' || options.method == 'PATCH')) { if(options && !options.headers && (options.method == 'POST' || options.method == 'PATCH')) {
options.headers = { options.headers = {
'content-type': 'application/json', "content-type": "application/json"
}; }
} }
console.log(options); console.log(options)
return fetch(url, options) return fetch(url, options)
.then(checkStatus) .then(checkStatus)
.then(parseJSON) .then(parseJSON)
.then(data => ({ data })) .then(data => ({ data }))
.catch(err => ({ err })); // .catch(err => ({ err }));
}; }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment