index.tsx 10.1 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
陈帅's avatar
陈帅 committed
3
import { formatMessage, FormattedMessage } from 'umi-plugin-react/locale';
zinkey's avatar
zinkey committed
4
import Link from 'umi/link';
陈帅's avatar
陈帅 committed
5
import { Form, Input, message, Button, Select, Row, Col, Popover, Progress } from 'antd';
6
import styles from './style.less';
陈帅's avatar
陈帅 committed
7 8 9
import { Dispatch } from 'redux';
import { IStateType } from './model';
import { FormComponentProps } from 'antd/lib/form';
10 11

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

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

陈帅's avatar
陈帅 committed
33 34 35 36 37
const passwordProgressMap: {
  ok: 'success';
  pass: 'normal';
  poor: 'exception';
} = {
38 39
  ok: 'success',
  pass: 'normal',
jim's avatar
jim committed
40
  poor: 'exception',
41 42
};

陈帅's avatar
陈帅 committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
interface BLOCK_NAME_CAMEL_CASEProps extends FormComponentProps {
  dispatch: Dispatch;
  BLOCK_NAME_CAMEL_CASE: IStateType;
  submitting: boolean;
}
interface BLOCK_NAME_CAMEL_CASEState {
  count: number;
  confirmDirty: boolean;
  visible: boolean;
  help: string;
  prefix: string;
}

export interface IUserRegisterParams {
  mail: string;
  password: string;
  confirm: string;
  mobile: string;
  captcha: string;
  prefix: string;
}

@connect(
  ({
    BLOCK_NAME_CAMEL_CASE,
    loading,
  }: {
    BLOCK_NAME_CAMEL_CASE: IStateType;
    loading: {
      effects: {
        [key: string]: string;
      };
    };
  }) => ({
    BLOCK_NAME_CAMEL_CASE,
    submitting: loading.effects['BLOCK_NAME_CAMEL_CASE/submit'],
  })
)
class PAGE_NAME_UPPER_CAMEL_CASE extends Component<
  BLOCK_NAME_CAMEL_CASEProps,
  BLOCK_NAME_CAMEL_CASEState
> {
  state: BLOCK_NAME_CAMEL_CASEState = {
86 87 88 89
    count: 0,
    confirmDirty: false,
    visible: false,
    help: '',
ddcat1115's avatar
ddcat1115 committed
90 91
    prefix: '86',
  };
92

jim's avatar
jim committed
93
  componentDidUpdate() {
94
    const { form, BLOCK_NAME_CAMEL_CASE } = this.props;
陈帅's avatar
陈帅 committed
95
    const account = form.getFieldValue('mail');
96
    if (BLOCK_NAME_CAMEL_CASE.status === 'ok') {
陈帅's avatar
陈帅 committed
97
      message.success('注册成功!');
98 99
    }
  }
陈帅's avatar
陈帅 committed
100

101 102 103
  componentWillUnmount() {
    clearInterval(this.interval);
  }
陈帅's avatar
陈帅 committed
104
  interval: number | undefined;
105 106 107
  onGetCaptcha = () => {
    let count = 59;
    this.setState({ count });
陈帅's avatar
陈帅 committed
108
    this.interval = window.setInterval(() => {
109 110 111 112 113 114
      count -= 1;
      this.setState({ count });
      if (count === 0) {
        clearInterval(this.interval);
      }
    }, 1000);
ddcat1115's avatar
ddcat1115 committed
115
  };
116 117

  getPasswordStatus = () => {
afc163's avatar
afc163 committed
118
    const { form } = this.props;
119 120 121 122 123 124 125
    const value = form.getFieldValue('password');
    if (value && value.length > 9) {
      return 'ok';
    }
    if (value && value.length > 5) {
      return 'pass';
    }
jim's avatar
jim committed
126
    return 'poor';
ddcat1115's avatar
ddcat1115 committed
127
  };
128

陈帅's avatar
陈帅 committed
129
  handleSubmit = (e: React.FormEvent) => {
130
    e.preventDefault();
陈帅's avatar
陈帅 committed
131 132
    const { form, dispatch } = this.props;
    form.validateFields({ force: true }, (err, values) => {
ddcat1115's avatar
ddcat1115 committed
133
      if (!err) {
陈帅's avatar
陈帅 committed
134 135
        const { prefix } = this.state;
        dispatch({
136
          type: 'BLOCK_NAME_CAMEL_CASE/submit',
ddcat1115's avatar
ddcat1115 committed
137 138
          payload: {
            ...values,
陈帅's avatar
陈帅 committed
139
            prefix,
ddcat1115's avatar
ddcat1115 committed
140 141
          },
        });
142
      }
ddcat1115's avatar
ddcat1115 committed
143 144
    });
  };
145

陈帅's avatar
陈帅 committed
146
  checkConfirm = (rule: any, value: string, callback: (messgae?: string) => void) => {
afc163's avatar
afc163 committed
147
    const { form } = this.props;
148
    if (value && value !== form.getFieldValue('password')) {
149
      callback(formatMessage({ id: 'BLOCK_NAME.password.twice' }));
150 151 152
    } else {
      callback();
    }
ddcat1115's avatar
ddcat1115 committed
153
  };
154

陈帅's avatar
陈帅 committed
155
  checkPassword = (rule: any, value: string, callback: (messgae?: string) => void) => {
陈帅's avatar
陈帅 committed
156
    const { visible, confirmDirty } = this.state;
157 158
    if (!value) {
      this.setState({
159
        help: formatMessage({ id: 'BLOCK_NAME.password.required' }),
160 161 162 163 164 165 166
        visible: !!value,
      });
      callback('error');
    } else {
      this.setState({
        help: '',
      });
陈帅's avatar
陈帅 committed
167
      if (!visible) {
168 169 170 171 172 173 174
        this.setState({
          visible: !!value,
        });
      }
      if (value.length < 6) {
        callback('error');
      } else {
afc163's avatar
afc163 committed
175
        const { form } = this.props;
陈帅's avatar
陈帅 committed
176
        if (value && confirmDirty) {
177 178 179 180 181
          form.validateFields(['confirm'], { force: true });
        }
        callback();
      }
    }
ddcat1115's avatar
ddcat1115 committed
182 183
  };

陈帅's avatar
陈帅 committed
184
  changePrefix = (value: string) => {
ddcat1115's avatar
ddcat1115 committed
185 186 187 188
    this.setState({
      prefix: value,
    });
  };
189 190

  renderPasswordProgress = () => {
afc163's avatar
afc163 committed
191
    const { form } = this.props;
192 193
    const value = form.getFieldValue('password');
    const passwordStatus = this.getPasswordStatus();
ddcat1115's avatar
ddcat1115 committed
194
    return value && value.length ? (
195 196 197 198 199 200 201 202
      <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
203 204 205
      </div>
    ) : null;
  };
206 207

  render() {
Andreas Cederström's avatar
Andreas Cederström committed
208
    const { form, submitting } = this.props;
209
    const { getFieldDecorator } = form;
陈帅's avatar
陈帅 committed
210
    const { count, prefix, help, visible } = this.state;
211 212
    return (
      <div className={styles.main}>
213
        <h3>
214
          <FormattedMessage id="BLOCK_NAME.register.register" />
215
        </h3>
216 217 218
        <Form onSubmit={this.handleSubmit}>
          <FormItem>
            {getFieldDecorator('mail', {
ddcat1115's avatar
ddcat1115 committed
219 220 221
              rules: [
                {
                  required: true,
222
                  message: formatMessage({ id: 'BLOCK_NAME.email.required' }),
ddcat1115's avatar
ddcat1115 committed
223 224 225
                },
                {
                  type: 'email',
226
                  message: formatMessage({ id: 'BLOCK_NAME.email.wrong-format' }),
ddcat1115's avatar
ddcat1115 committed
227 228
                },
              ],
229
            })(
xiaohuoni's avatar
xiaohuoni committed
230 231 232 233
              <Input
                size="large"
                placeholder={formatMessage({ id: 'BLOCK_NAME.email.placeholder' })}
              />
234
            )}
235
          </FormItem>
陈帅's avatar
陈帅 committed
236
          <FormItem help={help}>
237
            <Popover
陈帅's avatar
陈帅 committed
238
              getPopupContainer={node => (node && node.parentNode ? node.parentNode : node)}
239
              content={
ddcat1115's avatar
ddcat1115 committed
240
                <div style={{ padding: '4px 0' }}>
241 242
                  {passwordStatusMap[this.getPasswordStatus()]}
                  {this.renderPasswordProgress()}
ddcat1115's avatar
ddcat1115 committed
243
                  <div style={{ marginTop: 10 }}>
244
                    <FormattedMessage id="BLOCK_NAME.strength.msg" />
ddcat1115's avatar
ddcat1115 committed
245
                  </div>
246 247 248 249
                </div>
              }
              overlayStyle={{ width: 240 }}
              placement="right"
陈帅's avatar
陈帅 committed
250
              visible={visible}
251 252
            >
              {getFieldDecorator('password', {
ddcat1115's avatar
ddcat1115 committed
253 254 255 256 257
                rules: [
                  {
                    validator: this.checkPassword,
                  },
                ],
258 259 260 261
              })(
                <Input
                  size="large"
                  type="password"
262
                  placeholder={formatMessage({ id: 'BLOCK_NAME.password.placeholder' })}
263 264
                />
              )}
265 266 267 268
            </Popover>
          </FormItem>
          <FormItem>
            {getFieldDecorator('confirm', {
ddcat1115's avatar
ddcat1115 committed
269 270 271
              rules: [
                {
                  required: true,
272
                  message: formatMessage({ id: 'BLOCK_NAME.confirm-password.required' }),
ddcat1115's avatar
ddcat1115 committed
273 274 275 276 277
                },
                {
                  validator: this.checkConfirm,
                },
              ],
278 279 280 281
            })(
              <Input
                size="large"
                type="password"
282
                placeholder={formatMessage({ id: 'BLOCK_NAME.confirm-password.placeholder' })}
283 284
              />
            )}
285 286
          </FormItem>
          <FormItem>
ddcat1115's avatar
ddcat1115 committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300
            <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,
301
                    message: formatMessage({ id: 'BLOCK_NAME.phone-number.required' }),
ddcat1115's avatar
ddcat1115 committed
302 303
                  },
                  {
304
                    pattern: /^\d{11}$/,
305
                    message: formatMessage({ id: 'BLOCK_NAME.phone-number.wrong-format' }),
ddcat1115's avatar
ddcat1115 committed
306 307
                  },
                ],
308 309 310 311
              })(
                <Input
                  size="large"
                  style={{ width: '80%' }}
312
                  placeholder={formatMessage({ id: 'BLOCK_NAME.phone-number.placeholder' })}
313 314
                />
              )}
315 316 317 318 319 320
            </InputGroup>
          </FormItem>
          <FormItem>
            <Row gutter={8}>
              <Col span={16}>
                {getFieldDecorator('captcha', {
ddcat1115's avatar
ddcat1115 committed
321 322 323
                  rules: [
                    {
                      required: true,
324
                      message: formatMessage({ id: 'BLOCK_NAME.verification-code.required' }),
ddcat1115's avatar
ddcat1115 committed
325 326
                    },
                  ],
327 328 329
                })(
                  <Input
                    size="large"
330
                    placeholder={formatMessage({ id: 'BLOCK_NAME.verification-code.placeholder' })}
331 332
                  />
                )}
333 334 335
              </Col>
              <Col span={8}>
                <Button
ddcat1115's avatar
ddcat1115 committed
336
                  size="large"
陈帅's avatar
陈帅 committed
337
                  disabled={!!count}
338 339 340
                  className={styles.getCaptcha}
                  onClick={this.onGetCaptcha}
                >
341 342
                  {count
                    ? `${count} s`
343
                    : formatMessage({ id: 'BLOCK_NAME.register.get-verification-code' })}
344 345 346 347 348
                </Button>
              </Col>
            </Row>
          </FormItem>
          <FormItem>
ddcat1115's avatar
ddcat1115 committed
349 350
            <Button
              size="large"
Andreas Cederström's avatar
Andreas Cederström committed
351
              loading={submitting}
ddcat1115's avatar
ddcat1115 committed
352 353 354 355
              className={styles.submit}
              type="primary"
              htmlType="submit"
            >
356
              <FormattedMessage id="BLOCK_NAME.register.register" />
357
            </Button>
xiaohu's avatar
xiaohu committed
358
            <Link className={styles.login} to="/User/Login">
359
              <FormattedMessage id="BLOCK_NAME.register.sign-in" />
ddcat1115's avatar
ddcat1115 committed
360
            </Link>
361 362 363 364 365 366
          </FormItem>
        </Form>
      </div>
    );
  }
}
lijiehua's avatar
lijiehua committed
367

陈帅's avatar
陈帅 committed
368
export default Form.create()(PAGE_NAME_UPPER_CAMEL_CASE);