index.tsx 10.7 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import React, { Component } from 'react';
valleykid's avatar
valleykid committed
2
import { findDOMNode } from 'react-dom';
3 4
import moment from 'moment';
import { connect } from 'dva';
jim's avatar
jim committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
import {
  List,
  Card,
  Row,
  Col,
  Radio,
  Input,
  Progress,
  Button,
  Icon,
  Dropdown,
  Menu,
  Avatar,
  Modal,
  Form,
  DatePicker,
  Select,
} from 'antd';
陈帅's avatar
陈帅 committed
23
import { FormComponentProps } from 'antd/es/form';
陈帅's avatar
陈帅 committed
24 25 26 27
import { IStateType } from './model';
import { Dispatch } from 'redux';
import { BasicListItemDataType } from './data';
import Result from './Result';
28
import { PageHeaderWrapper } from '@ant-design/pro-layout';
29

30
import styles from './style.less';
31

valleykid's avatar
valleykid committed
32
const FormItem = Form.Item;
33 34
const RadioButton = Radio.Button;
const RadioGroup = Radio.Group;
valleykid's avatar
valleykid committed
35 36
const SelectOption = Select.Option;
const { Search, TextArea } = Input;
37

陈帅's avatar
陈帅 committed
38 39
interface PAGE_NAME_UPPER_CAMEL_CASEProps extends FormComponentProps {
  BLOCK_NAME_CAMEL_CASE: IStateType;
陈帅's avatar
陈帅 committed
40
  dispatch: Dispatch<any>;
陈帅's avatar
陈帅 committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  loading: boolean;
}
interface PAGE_NAME_UPPER_CAMEL_CASEState {
  visible: boolean;
  done: boolean;
  current?: Partial<BasicListItemDataType>;
}
@connect(
  ({
    BLOCK_NAME_CAMEL_CASE,
    loading,
  }: {
    BLOCK_NAME_CAMEL_CASE: IStateType;
    loading: {
      models: { [key: string]: boolean };
    };
  }) => ({
    BLOCK_NAME_CAMEL_CASE,
    loading: loading.models.BLOCK_NAME_CAMEL_CASE,
陈帅's avatar
陈帅 committed
60
  }),
陈帅's avatar
陈帅 committed
61 62 63 64 65 66
)
class PAGE_NAME_UPPER_CAMEL_CASE extends Component<
  PAGE_NAME_UPPER_CAMEL_CASEProps,
  PAGE_NAME_UPPER_CAMEL_CASEState
> {
  state: PAGE_NAME_UPPER_CAMEL_CASEState = { visible: false, done: false, current: undefined };
valleykid's avatar
valleykid committed
67

68 69 70 71
  formLayout = {
    labelCol: { span: 7 },
    wrapperCol: { span: 13 },
  };
陈帅's avatar
陈帅 committed
72
  addBtn: HTMLButtonElement | undefined | null;
73

74
  componentDidMount() {
陈帅's avatar
陈帅 committed
75 76
    const { dispatch } = this.props;
    dispatch({
77
      type: 'BLOCK_NAME_CAMEL_CASE/fetch',
78 79 80 81 82
      payload: {
        count: 5,
      },
    });
  }
陈帅's avatar
陈帅 committed
83

valleykid's avatar
valleykid committed
84 85 86 87 88
  showModal = () => {
    this.setState({
      visible: true,
      current: undefined,
    });
jim's avatar
jim committed
89
  };
陈帅's avatar
陈帅 committed
90

陈帅's avatar
陈帅 committed
91
  showEditModal = (item: BasicListItemDataType) => {
valleykid's avatar
valleykid committed
92 93 94 95
    this.setState({
      visible: true,
      current: item,
    });
jim's avatar
jim committed
96
  };
陈帅's avatar
陈帅 committed
97

valleykid's avatar
valleykid committed
98
  handleDone = () => {
陈帅's avatar
陈帅 committed
99
    setTimeout(() => this.addBtn && this.addBtn.blur(), 0);
valleykid's avatar
valleykid committed
100 101 102 103
    this.setState({
      done: false,
      visible: false,
    });
jim's avatar
jim committed
104
  };
陈帅's avatar
陈帅 committed
105

valleykid's avatar
valleykid committed
106
  handleCancel = () => {
陈帅's avatar
陈帅 committed
107
    setTimeout(() => this.addBtn && this.addBtn.blur(), 0);
valleykid's avatar
valleykid committed
108 109 110
    this.setState({
      visible: false,
    });
jim's avatar
jim committed
111
  };
陈帅's avatar
陈帅 committed
112

陈帅's avatar
陈帅 committed
113
  handleSubmit = (e: React.FormEvent) => {
valleykid's avatar
valleykid committed
114 115
    e.preventDefault();
    const { dispatch, form } = this.props;
陈帅's avatar
陈帅 committed
116 117
    const { current } = this.state;
    const id = current ? current.id : '';
valleykid's avatar
valleykid committed
118

陈帅's avatar
陈帅 committed
119 120
    setTimeout(() => this.addBtn && this.addBtn.blur(), 0);
    form.validateFields((err: string | undefined, fieldsValue: BasicListItemDataType) => {
valleykid's avatar
valleykid committed
121 122 123 124 125
      if (err) return;
      this.setState({
        done: true,
      });
      dispatch({
126
        type: 'BLOCK_NAME_CAMEL_CASE/submit',
valleykid's avatar
valleykid committed
127 128 129
        payload: { id, ...fieldsValue },
      });
    });
jim's avatar
jim committed
130
  };
陈帅's avatar
陈帅 committed
131

陈帅's avatar
陈帅 committed
132
  deleteItem = (id: string) => {
陈帅's avatar
陈帅 committed
133 134
    const { dispatch } = this.props;
    dispatch({
135
      type: 'BLOCK_NAME_CAMEL_CASE/submit',
valleykid's avatar
valleykid committed
136 137
      payload: { id },
    });
jim's avatar
jim committed
138
  };
139 140

  render() {
陈帅's avatar
陈帅 committed
141
    const {
142
      BLOCK_NAME_CAMEL_CASE: { list },
陈帅's avatar
陈帅 committed
143 144 145 146 147
      loading,
    } = this.props;
    const {
      form: { getFieldDecorator },
    } = this.props;
陈帅's avatar
陈帅 committed
148

valleykid's avatar
valleykid committed
149 150
    const { visible, done, current = {} } = this.state;

陈帅's avatar
陈帅 committed
151
    const editAndDelete = (key: string, currentItem: BasicListItemDataType) => {
valleykid's avatar
valleykid committed
152 153 154 155 156 157 158 159 160 161 162 163
      if (key === 'edit') this.showEditModal(currentItem);
      else if (key === 'delete') {
        Modal.confirm({
          title: '删除任务',
          content: '确定删除该任务吗?',
          okText: '确认',
          cancelText: '取消',
          onOk: () => this.deleteItem(currentItem.id),
        });
      }
    };

jim's avatar
jim committed
164 165 166
    const modalFooter = done
      ? { footer: null, onCancel: this.handleDone }
      : { okText: '保存', onOk: this.handleSubmit, onCancel: this.handleCancel };
167

陈帅's avatar
陈帅 committed
168 169 170 171 172
    const Info: React.SFC<{
      title: React.ReactNode;
      value: React.ReactNode;
      bordered?: boolean;
    }> = ({ title, value, bordered }) => (
173 174 175 176 177 178 179 180 181 182 183 184 185 186
      <div className={styles.headerInfo}>
        <span>{title}</span>
        <p>{value}</p>
        {bordered && <em />}
      </div>
    );

    const extraContent = (
      <div className={styles.extraContent}>
        <RadioGroup defaultValue="all">
          <RadioButton value="all">全部</RadioButton>
          <RadioButton value="progress">进行中</RadioButton>
          <RadioButton value="waiting">等待中</RadioButton>
        </RadioGroup>
jim's avatar
jim committed
187
        <Search className={styles.extraContentSearch} placeholder="请输入" onSearch={() => ({})} />
188 189 190 191 192 193 194 195 196 197
      </div>
    );

    const paginationProps = {
      showSizeChanger: true,
      showQuickJumper: true,
      pageSize: 5,
      total: 50,
    };

陈帅's avatar
陈帅 committed
198 199 200 201 202
    const ListContent = ({
      data: { owner, createdAt, percent, status },
    }: {
      data: BasicListItemDataType;
    }) => (
203
      <div className={styles.listContent}>
afc163's avatar
afc163 committed
204
        <div className={styles.listContentItem}>
205 206 207
          <span>Owner</span>
          <p>{owner}</p>
        </div>
afc163's avatar
afc163 committed
208
        <div className={styles.listContentItem}>
209
          <span>开始时间</span>
spiritree's avatar
spiritree committed
210
          <p>{moment(createdAt).format('YYYY-MM-DD HH:mm')}</p>
211
        </div>
afc163's avatar
afc163 committed
212 213
        <div className={styles.listContentItem}>
          <Progress percent={percent} status={status} strokeWidth={6} style={{ width: 180 }} />
214 215 216 217
        </div>
      </div>
    );

陈帅's avatar
陈帅 committed
218
    const MoreBtn: React.SFC<{
陈帅's avatar
陈帅 committed
219 220
      item: BasicListItemDataType;
    }> = ({ item }) => (
jim's avatar
jim committed
221 222
      <Dropdown
        overlay={
陈帅's avatar
陈帅 committed
223
          <Menu onClick={({ key }) => editAndDelete(key, item)}>
jim's avatar
jim committed
224 225 226 227
            <Menu.Item key="edit">编辑</Menu.Item>
            <Menu.Item key="delete">删除</Menu.Item>
          </Menu>
        }
valleykid's avatar
valleykid committed
228
      >
229 230 231 232 233 234
        <a>
          更多 <Icon type="down" />
        </a>
      </Dropdown>
    );

valleykid's avatar
valleykid committed
235 236 237 238 239 240 241
    const getModalContent = () => {
      if (done) {
        return (
          <Result
            type="success"
            title="操作成功"
            description="一系列的信息描述,很短同样也可以带标点。"
jim's avatar
jim committed
242 243 244 245 246
            actions={
              <Button type="primary" onClick={this.handleDone}>
                知道了
              </Button>
            }
valleykid's avatar
valleykid committed
247
            className={styles.formResult}
valleykid's avatar
valleykid committed
248 249 250 251 252 253 254 255 256
          />
        );
      }
      return (
        <Form onSubmit={this.handleSubmit}>
          <FormItem label="任务名称" {...this.formLayout}>
            {getFieldDecorator('title', {
              rules: [{ required: true, message: '请输入任务名称' }],
              initialValue: current.title,
jim's avatar
jim committed
257
            })(<Input placeholder="请输入" />)}
valleykid's avatar
valleykid committed
258 259 260 261 262 263 264 265 266 267 268
          </FormItem>
          <FormItem label="开始时间" {...this.formLayout}>
            {getFieldDecorator('createdAt', {
              rules: [{ required: true, message: '请选择开始时间' }],
              initialValue: current.createdAt ? moment(current.createdAt) : null,
            })(
              <DatePicker
                showTime
                placeholder="请选择"
                format="YYYY-MM-DD HH:mm:ss"
                style={{ width: '100%' }}
陈帅's avatar
陈帅 committed
269
              />,
valleykid's avatar
valleykid committed
270 271 272 273 274 275 276 277 278 279
            )}
          </FormItem>
          <FormItem label="任务负责人" {...this.formLayout}>
            {getFieldDecorator('owner', {
              rules: [{ required: true, message: '请选择任务负责人' }],
              initialValue: current.owner,
            })(
              <Select placeholder="请选择">
                <SelectOption value="付晓晓">付晓晓</SelectOption>
                <SelectOption value="周毛毛">周毛毛</SelectOption>
陈帅's avatar
陈帅 committed
280
              </Select>,
valleykid's avatar
valleykid committed
281 282 283 284 285 286
            )}
          </FormItem>
          <FormItem {...this.formLayout} label="产品描述">
            {getFieldDecorator('subDescription', {
              rules: [{ message: '请输入至少五个字符的产品描述!', min: 5 }],
              initialValue: current.subDescription,
jim's avatar
jim committed
287
            })(<TextArea rows={4} placeholder="请输入至少五个字符" />)}
valleykid's avatar
valleykid committed
288 289 290 291
          </FormItem>
        </Form>
      );
    };
292
    return (
293 294
      <>
        <PageHeaderWrapper>
陈帅's avatar
陈帅 committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308
          <div className={styles.standardList}>
            <Card bordered={false}>
              <Row>
                <Col sm={8} xs={24}>
                  <Info title="我的待办" value="8个任务" bordered />
                </Col>
                <Col sm={8} xs={24}>
                  <Info title="本周任务平均处理时间" value="32分钟" bordered />
                </Col>
                <Col sm={8} xs={24}>
                  <Info title="本周完成任务数" value="24个任务" />
                </Col>
              </Row>
            </Card>
309

陈帅's avatar
陈帅 committed
310 311 312 313 314 315 316
            <Card
              className={styles.listCard}
              bordered={false}
              title="基本列表"
              style={{ marginTop: 24 }}
              bodyStyle={{ padding: '0 32px 40px 32px' }}
              extra={extraContent}
valleykid's avatar
valleykid committed
317
            >
陈帅's avatar
陈帅 committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
              <Button
                type="dashed"
                style={{ width: '100%', marginBottom: 8 }}
                icon="plus"
                onClick={this.showModal}
                ref={component => {
                  this.addBtn = findDOMNode(component) as HTMLButtonElement;
                }}
              >
                添加
              </Button>
              <List
                size="large"
                rowKey="id"
                loading={loading}
                pagination={paginationProps}
                dataSource={list}
                renderItem={item => (
                  <List.Item
                    actions={[
                      <a
陈帅's avatar
陈帅 committed
339
                        key="edit"
陈帅's avatar
陈帅 committed
340 341 342 343 344 345 346
                        onClick={e => {
                          e.preventDefault();
                          this.showEditModal(item);
                        }}
                      >
                        编辑
                      </a>,
陈帅's avatar
陈帅 committed
347
                      <MoreBtn key="more" item={item} />,
陈帅's avatar
陈帅 committed
348 349 350 351 352 353 354 355 356 357 358 359 360
                    ]}
                  >
                    <List.Item.Meta
                      avatar={<Avatar src={item.logo} shape="square" size="large" />}
                      title={<a href={item.href}>{item.title}</a>}
                      description={item.subDescription}
                    />
                    <ListContent data={item} />
                  </List.Item>
                )}
              />
            </Card>
          </div>
361
        </PageHeaderWrapper>
陈帅's avatar
陈帅 committed
362

valleykid's avatar
valleykid committed
363
        <Modal
陈帅's avatar
陈帅 committed
364
          title={done ? null : `任务${current ? '编辑' : '添加'}`}
valleykid's avatar
valleykid committed
365
          className={styles.standardListForm}
valleykid's avatar
valleykid committed
366
          width={640}
valleykid's avatar
valleykid committed
367
          bodyStyle={done ? { padding: '72px 0' } : { padding: '28px 0 0' }}
valleykid's avatar
valleykid committed
368 369 370 371 372 373
          destroyOnClose
          visible={visible}
          {...modalFooter}
        >
          {getModalContent()}
        </Modal>
374
      </>
375 376 377
    );
  }
}
lijiehua's avatar
lijiehua committed
378

陈帅's avatar
陈帅 committed
379
export default Form.create<PAGE_NAME_UPPER_CAMEL_CASEProps>()(PAGE_NAME_UPPER_CAMEL_CASE);