Register.js 9.14 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
3
import { formatMessage, FormattedMessage } from 'umi/locale';
zinkey's avatar
zinkey committed
4 5
import Link from 'umi/link';
import router from 'umi/router';
6 7 8 9
import { Form, Input, Button, Select, Row, Col, Popover, Progress } from 'antd';
import styles from './Register.less';

const FormItem = Form.Item;
afc163's avatar
afc163 committed
10
const { Option } = Select;
11 12 13
const InputGroup = Input.Group;

const passwordStatusMap = {
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  ok: (
    <div className={styles.success}>
      <FormattedMessage id="validation.password.strength.strong" />
    </div>
  ),
  pass: (
    <div className={styles.warning}>
      <FormattedMessage id="validation.password.strength.medium" />
    </div>
  ),
  poor: (
    <div className={styles.error}>
      <FormattedMessage id="validation.password.strength.short" />
    </div>
  ),
29 30 31 32 33
};

const passwordProgressMap = {
  ok: 'success',
  pass: 'normal',
jim's avatar
jim committed
34
  poor: 'exception',
35 36
};

Andreas Cederström's avatar
Andreas Cederström committed
37 38 39
@connect(({ register, loading }) => ({
  register,
  submitting: loading.effects['register/submit'],
40 41
}))
@Form.create()
afc163's avatar
afc163 committed
42
class Register extends Component {
43 44 45 46 47
  state = {
    count: 0,
    confirmDirty: false,
    visible: false,
    help: '',
ddcat1115's avatar
ddcat1115 committed
48 49
    prefix: '86',
  };
50

jim's avatar
jim committed
51
  componentDidUpdate() {
52
    const { form, register } = this.props;
陈帅's avatar
陈帅 committed
53 54
    const account = form.getFieldValue('mail');
    if (register.status === 'ok') {
55 56 57 58 59 60
      router.push({
        pathname: '/user/register-result',
        state: {
          account,
        },
      });
61 62
    }
  }
陈帅's avatar
陈帅 committed
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77
  componentWillUnmount() {
    clearInterval(this.interval);
  }

  onGetCaptcha = () => {
    let count = 59;
    this.setState({ count });
    this.interval = setInterval(() => {
      count -= 1;
      this.setState({ count });
      if (count === 0) {
        clearInterval(this.interval);
      }
    }, 1000);
ddcat1115's avatar
ddcat1115 committed
78
  };
79 80

  getPasswordStatus = () => {
afc163's avatar
afc163 committed
81
    const { form } = this.props;
82 83 84 85 86 87 88
    const value = form.getFieldValue('password');
    if (value && value.length > 9) {
      return 'ok';
    }
    if (value && value.length > 5) {
      return 'pass';
    }
jim's avatar
jim committed
89
    return 'poor';
ddcat1115's avatar
ddcat1115 committed
90
  };
91

jim's avatar
jim committed
92
  handleSubmit = e => {
93
    e.preventDefault();
陈帅's avatar
陈帅 committed
94 95
    const { form, dispatch } = this.props;
    form.validateFields({ force: true }, (err, values) => {
ddcat1115's avatar
ddcat1115 committed
96
      if (!err) {
陈帅's avatar
陈帅 committed
97 98
        const { prefix } = this.state;
        dispatch({
ddcat1115's avatar
ddcat1115 committed
99 100 101
          type: 'register/submit',
          payload: {
            ...values,
陈帅's avatar
陈帅 committed
102
            prefix,
ddcat1115's avatar
ddcat1115 committed
103 104
          },
        });
105
      }
ddcat1115's avatar
ddcat1115 committed
106 107
    });
  };
108

jim's avatar
jim committed
109
  handleConfirmBlur = e => {
afc163's avatar
afc163 committed
110
    const { value } = e.target;
陈帅's avatar
陈帅 committed
111 112
    const { confirmDirty } = this.state;
    this.setState({ confirmDirty: confirmDirty || !!value });
ddcat1115's avatar
ddcat1115 committed
113
  };
114 115

  checkConfirm = (rule, value, callback) => {
afc163's avatar
afc163 committed
116
    const { form } = this.props;
117
    if (value && value !== form.getFieldValue('password')) {
118
      callback(formatMessage({ id: 'validation.password.twice' }));
119 120 121
    } else {
      callback();
    }
ddcat1115's avatar
ddcat1115 committed
122
  };
123 124

  checkPassword = (rule, value, callback) => {
陈帅's avatar
陈帅 committed
125
    const { visible, confirmDirty } = this.state;
126 127
    if (!value) {
      this.setState({
128
        help: formatMessage({ id: 'validation.password.required' }),
129 130 131 132 133 134 135
        visible: !!value,
      });
      callback('error');
    } else {
      this.setState({
        help: '',
      });
陈帅's avatar
陈帅 committed
136
      if (!visible) {
137 138 139 140 141 142 143
        this.setState({
          visible: !!value,
        });
      }
      if (value.length < 6) {
        callback('error');
      } else {
afc163's avatar
afc163 committed
144
        const { form } = this.props;
陈帅's avatar
陈帅 committed
145
        if (value && confirmDirty) {
146 147 148 149 150
          form.validateFields(['confirm'], { force: true });
        }
        callback();
      }
    }
ddcat1115's avatar
ddcat1115 committed
151 152
  };

jim's avatar
jim committed
153
  changePrefix = value => {
ddcat1115's avatar
ddcat1115 committed
154 155 156 157
    this.setState({
      prefix: value,
    });
  };
158 159

  renderPasswordProgress = () => {
afc163's avatar
afc163 committed
160
    const { form } = this.props;
161 162
    const value = form.getFieldValue('password');
    const passwordStatus = this.getPasswordStatus();
ddcat1115's avatar
ddcat1115 committed
163
    return value && value.length ? (
164 165 166 167 168 169 170 171
      <div className={styles[`progress-${passwordStatus}`]}>
        <Progress
          status={passwordProgressMap[passwordStatus]}
          className={styles.progress}
          strokeWidth={6}
          percent={value.length * 10 > 100 ? 100 : value.length * 10}
          showInfo={false}
        />
ddcat1115's avatar
ddcat1115 committed
172 173 174
      </div>
    ) : null;
  };
175 176

  render() {
Andreas Cederström's avatar
Andreas Cederström committed
177
    const { form, submitting } = this.props;
178
    const { getFieldDecorator } = form;
陈帅's avatar
陈帅 committed
179
    const { count, prefix, help, visible } = this.state;
180 181
    return (
      <div className={styles.main}>
182 183 184
        <h3>
          <FormattedMessage id="app.register.register" />
        </h3>
185 186 187
        <Form onSubmit={this.handleSubmit}>
          <FormItem>
            {getFieldDecorator('mail', {
ddcat1115's avatar
ddcat1115 committed
188 189 190
              rules: [
                {
                  required: true,
191
                  message: formatMessage({ id: 'validation.email.required' }),
ddcat1115's avatar
ddcat1115 committed
192 193 194
                },
                {
                  type: 'email',
195
                  message: formatMessage({ id: 'validation.email.wrong-format' }),
ddcat1115's avatar
ddcat1115 committed
196 197
                },
              ],
198 199 200
            })(
              <Input size="large" placeholder={formatMessage({ id: 'form.email.placeholder' })} />
            )}
201
          </FormItem>
陈帅's avatar
陈帅 committed
202
          <FormItem help={help}>
203
            <Popover
Ilan's avatar
Ilan committed
204
              getPopupContainer={node => node.parentNode}
205
              content={
ddcat1115's avatar
ddcat1115 committed
206
                <div style={{ padding: '4px 0' }}>
207 208
                  {passwordStatusMap[this.getPasswordStatus()]}
                  {this.renderPasswordProgress()}
ddcat1115's avatar
ddcat1115 committed
209
                  <div style={{ marginTop: 10 }}>
210
                    <FormattedMessage id="validation.password.strength.msg" />
ddcat1115's avatar
ddcat1115 committed
211
                  </div>
212 213 214 215
                </div>
              }
              overlayStyle={{ width: 240 }}
              placement="right"
陈帅's avatar
陈帅 committed
216
              visible={visible}
217 218
            >
              {getFieldDecorator('password', {
ddcat1115's avatar
ddcat1115 committed
219 220 221 222 223
                rules: [
                  {
                    validator: this.checkPassword,
                  },
                ],
224 225 226 227 228 229 230
              })(
                <Input
                  size="large"
                  type="password"
                  placeholder={formatMessage({ id: 'form.password.placeholder' })}
                />
              )}
231 232 233 234
            </Popover>
          </FormItem>
          <FormItem>
            {getFieldDecorator('confirm', {
ddcat1115's avatar
ddcat1115 committed
235 236 237
              rules: [
                {
                  required: true,
238
                  message: formatMessage({ id: 'validation.confirm-password.required' }),
ddcat1115's avatar
ddcat1115 committed
239 240 241 242 243
                },
                {
                  validator: this.checkConfirm,
                },
              ],
244 245 246 247 248 249 250
            })(
              <Input
                size="large"
                type="password"
                placeholder={formatMessage({ id: 'form.confirm-password.placeholder' })}
              />
            )}
251 252
          </FormItem>
          <FormItem>
ddcat1115's avatar
ddcat1115 committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266
            <InputGroup compact>
              <Select
                size="large"
                value={prefix}
                onChange={this.changePrefix}
                style={{ width: '20%' }}
              >
                <Option value="86">+86</Option>
                <Option value="87">+87</Option>
              </Select>
              {getFieldDecorator('mobile', {
                rules: [
                  {
                    required: true,
267
                    message: formatMessage({ id: 'validation.phone-number.required' }),
ddcat1115's avatar
ddcat1115 committed
268 269
                  },
                  {
270
                    pattern: /^\d{11}$/,
271
                    message: formatMessage({ id: 'validation.phone-number.wrong-format' }),
ddcat1115's avatar
ddcat1115 committed
272 273
                  },
                ],
274 275 276 277 278 279 280
              })(
                <Input
                  size="large"
                  style={{ width: '80%' }}
                  placeholder={formatMessage({ id: 'form.phone-number.placeholder' })}
                />
              )}
281 282 283 284 285 286
            </InputGroup>
          </FormItem>
          <FormItem>
            <Row gutter={8}>
              <Col span={16}>
                {getFieldDecorator('captcha', {
ddcat1115's avatar
ddcat1115 committed
287 288 289
                  rules: [
                    {
                      required: true,
290
                      message: formatMessage({ id: 'validation.verification-code.required' }),
ddcat1115's avatar
ddcat1115 committed
291 292
                    },
                  ],
293 294 295 296 297 298
                })(
                  <Input
                    size="large"
                    placeholder={formatMessage({ id: 'form.verification-code.placeholder' })}
                  />
                )}
299 300 301
              </Col>
              <Col span={8}>
                <Button
ddcat1115's avatar
ddcat1115 committed
302
                  size="large"
303 304 305 306
                  disabled={count}
                  className={styles.getCaptcha}
                  onClick={this.onGetCaptcha}
                >
307 308 309
                  {count
                    ? `${count} s`
                    : formatMessage({ id: 'app.register.get-verification-code' })}
310 311 312 313 314
                </Button>
              </Col>
            </Row>
          </FormItem>
          <FormItem>
ddcat1115's avatar
ddcat1115 committed
315 316
            <Button
              size="large"
Andreas Cederström's avatar
Andreas Cederström committed
317
              loading={submitting}
ddcat1115's avatar
ddcat1115 committed
318 319 320 321
              className={styles.submit}
              type="primary"
              htmlType="submit"
            >
322
              <FormattedMessage id="app.register.register" />
323
            </Button>
xiaohu's avatar
xiaohu committed
324
            <Link className={styles.login} to="/User/Login">
325
              <FormattedMessage id="app.register.sing-in" />
ddcat1115's avatar
ddcat1115 committed
326
            </Link>
327 328 329 330 331 332
          </FormItem>
        </Form>
      </div>
    );
  }
}
lijiehua's avatar
lijiehua committed
333 334

export default Register;