BasicList.js 9.29 KB
Newer Older
1
import React, { PureComponent } 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';
23

24
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
25
import Result from '@/components/Result';
26 27 28

import styles from './BasicList.less';

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

afc163's avatar
afc163 committed
35
export default
Andreas Cederström's avatar
Andreas Cederström committed
36 37 38
@connect(({ list, loading }) => ({
  list,
  loading: loading.models.list,
39
}))
valleykid's avatar
valleykid committed
40
@Form.create()
afc163's avatar
afc163 committed
41
class BasicList extends PureComponent {
valleykid's avatar
valleykid committed
42 43
  state = { visible: false, done: false };

44 45 46 47 48
  formLayout = {
    labelCol: { span: 7 },
    wrapperCol: { span: 13 },
  };

49
  componentDidMount() {
陈帅's avatar
陈帅 committed
50 51
    const { dispatch } = this.props;
    dispatch({
52 53 54 55 56 57
      type: 'list/fetch',
      payload: {
        count: 5,
      },
    });
  }
陈帅's avatar
陈帅 committed
58

valleykid's avatar
valleykid committed
59 60 61 62 63
  showModal = () => {
    this.setState({
      visible: true,
      current: undefined,
    });
jim's avatar
jim committed
64
  };
陈帅's avatar
陈帅 committed
65

jim's avatar
jim committed
66
  showEditModal = item => {
valleykid's avatar
valleykid committed
67 68 69 70
    this.setState({
      visible: true,
      current: item,
    });
jim's avatar
jim committed
71
  };
陈帅's avatar
陈帅 committed
72

valleykid's avatar
valleykid committed
73
  handleDone = () => {
valleykid's avatar
valleykid committed
74
    setTimeout(() => this.addBtn.blur(), 0);
valleykid's avatar
valleykid committed
75 76 77 78
    this.setState({
      done: false,
      visible: false,
    });
jim's avatar
jim committed
79
  };
陈帅's avatar
陈帅 committed
80

valleykid's avatar
valleykid committed
81
  handleCancel = () => {
valleykid's avatar
valleykid committed
82
    setTimeout(() => this.addBtn.blur(), 0);
valleykid's avatar
valleykid committed
83 84 85
    this.setState({
      visible: false,
    });
jim's avatar
jim committed
86
  };
陈帅's avatar
陈帅 committed
87

jim's avatar
jim committed
88
  handleSubmit = e => {
valleykid's avatar
valleykid committed
89 90
    e.preventDefault();
    const { dispatch, form } = this.props;
陈帅's avatar
陈帅 committed
91 92
    const { current } = this.state;
    const id = current ? current.id : '';
valleykid's avatar
valleykid committed
93

valleykid's avatar
valleykid committed
94
    setTimeout(() => this.addBtn.blur(), 0);
valleykid's avatar
valleykid committed
95 96 97 98 99 100 101 102 103 104
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      this.setState({
        done: true,
      });
      dispatch({
        type: 'list/submit',
        payload: { id, ...fieldsValue },
      });
    });
jim's avatar
jim committed
105
  };
陈帅's avatar
陈帅 committed
106

jim's avatar
jim committed
107
  deleteItem = id => {
陈帅's avatar
陈帅 committed
108 109
    const { dispatch } = this.props;
    dispatch({
valleykid's avatar
valleykid committed
110 111 112
      type: 'list/submit',
      payload: { id },
    });
jim's avatar
jim committed
113
  };
114 115

  render() {
陈帅's avatar
陈帅 committed
116 117 118 119 120 121 122
    const {
      list: { list },
      loading,
    } = this.props;
    const {
      form: { getFieldDecorator },
    } = this.props;
valleykid's avatar
valleykid committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    const { visible, done, current = {} } = this.state;

    const editAndDelete = (key, currentItem) => {
      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
138 139 140
    const modalFooter = done
      ? { footer: null, onCancel: this.handleDone }
      : { okText: '保存', onOk: this.handleSubmit, onCancel: this.handleCancel };
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

    const Info = ({ title, value, bordered }) => (
      <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
157
        <Search className={styles.extraContentSearch} placeholder="请输入" onSearch={() => ({})} />
158 159 160 161 162 163 164 165 166 167 168 169
      </div>
    );

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

    const ListContent = ({ data: { owner, createdAt, percent, status } }) => (
      <div className={styles.listContent}>
afc163's avatar
afc163 committed
170
        <div className={styles.listContentItem}>
171 172 173
          <span>Owner</span>
          <p>{owner}</p>
        </div>
afc163's avatar
afc163 committed
174
        <div className={styles.listContentItem}>
175
          <span>开始时间</span>
spiritree's avatar
spiritree committed
176
          <p>{moment(createdAt).format('YYYY-MM-DD HH:mm')}</p>
177
        </div>
afc163's avatar
afc163 committed
178 179
        <div className={styles.listContentItem}>
          <Progress percent={percent} status={status} strokeWidth={6} style={{ width: 180 }} />
180 181 182 183
        </div>
      </div>
    );

valleykid's avatar
valleykid committed
184
    const MoreBtn = props => (
jim's avatar
jim committed
185 186 187 188 189 190 191
      <Dropdown
        overlay={
          <Menu onClick={({ key }) => editAndDelete(key, props.current)}>
            <Menu.Item key="edit">编辑</Menu.Item>
            <Menu.Item key="delete">删除</Menu.Item>
          </Menu>
        }
valleykid's avatar
valleykid committed
192
      >
193 194 195 196 197 198
        <a>
          更多 <Icon type="down" />
        </a>
      </Dropdown>
    );

valleykid's avatar
valleykid committed
199 200 201 202 203 204 205
    const getModalContent = () => {
      if (done) {
        return (
          <Result
            type="success"
            title="操作成功"
            description="一系列的信息描述,很短同样也可以带标点。"
jim's avatar
jim committed
206 207 208 209 210
            actions={
              <Button type="primary" onClick={this.handleDone}>
                知道了
              </Button>
            }
valleykid's avatar
valleykid committed
211
            className={styles.formResult}
valleykid's avatar
valleykid committed
212 213 214 215 216 217 218 219 220
          />
        );
      }
      return (
        <Form onSubmit={this.handleSubmit}>
          <FormItem label="任务名称" {...this.formLayout}>
            {getFieldDecorator('title', {
              rules: [{ required: true, message: '请输入任务名称' }],
              initialValue: current.title,
jim's avatar
jim committed
221
            })(<Input placeholder="请输入" />)}
valleykid's avatar
valleykid committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
          </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%' }}
              />
            )}
          </FormItem>
          <FormItem label="任务负责人" {...this.formLayout}>
            {getFieldDecorator('owner', {
              rules: [{ required: true, message: '请选择任务负责人' }],
              initialValue: current.owner,
            })(
              <Select placeholder="请选择">
                <SelectOption value="付晓晓">付晓晓</SelectOption>
                <SelectOption value="周毛毛">周毛毛</SelectOption>
              </Select>
            )}
          </FormItem>
          <FormItem {...this.formLayout} label="产品描述">
            {getFieldDecorator('subDescription', {
              rules: [{ message: '请输入至少五个字符的产品描述!', min: 5 }],
              initialValue: current.subDescription,
jim's avatar
jim committed
251
            })(<TextArea rows={4} placeholder="请输入至少五个字符" />)}
valleykid's avatar
valleykid committed
252 253 254 255
          </FormItem>
        </Form>
      );
    };
256
    return (
257
      <PageHeaderWrapper>
258
        <div className={styles.standardList}>
259
          <Card bordered={false}>
260 261
            <Row>
              <Col sm={8} xs={24}>
niko's avatar
niko committed
262
                <Info title="我的待办" value="8个任务" bordered />
263 264 265 266 267 268 269 270 271 272 273
              </Col>
              <Col sm={8} xs={24}>
                <Info title="本周任务平均处理时间" value="32分钟" bordered />
              </Col>
              <Col sm={8} xs={24}>
                <Info title="本周完成任务数" value="24个任务" />
              </Col>
            </Row>
          </Card>

          <Card
niko's avatar
niko committed
274
            className={styles.listCard}
275
            bordered={false}
niko's avatar
niko committed
276
            title="标准列表"
nikogu's avatar
nikogu committed
277
            style={{ marginTop: 24 }}
niko's avatar
niko committed
278
            bodyStyle={{ padding: '0 32px 40px 32px' }}
279 280
            extra={extraContent}
          >
valleykid's avatar
valleykid committed
281 282 283 284 285
            <Button
              type="dashed"
              style={{ width: '100%', marginBottom: 8 }}
              icon="plus"
              onClick={this.showModal}
jim's avatar
jim committed
286
              ref={component => {
valleykid's avatar
valleykid committed
287 288 289 290 291
                /* eslint-disable */
                this.addBtn = findDOMNode(component);
                /* eslint-enable */
              }}
            >
afc163's avatar
afc163 committed
292
              添加
293 294
            </Button>
            <List
niko's avatar
niko committed
295
              size="large"
nikogu's avatar
nikogu committed
296
              rowKey="id"
297 298
              loading={loading}
              pagination={paginationProps}
nikogu's avatar
nikogu committed
299 300 301
              dataSource={list}
              renderItem={item => (
                <List.Item
valleykid's avatar
valleykid committed
302
                  actions={[
jim's avatar
jim committed
303 304 305 306 307 308 309 310
                    <a
                      onClick={e => {
                        e.preventDefault();
                        this.showEditModal(item);
                      }}
                    >
                      编辑
                    </a>,
valleykid's avatar
valleykid committed
311 312
                    <MoreBtn current={item} />,
                  ]}
nikogu's avatar
nikogu committed
313 314 315 316 317 318 319 320 321 322
                >
                  <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>
              )}
            />
323 324
          </Card>
        </div>
valleykid's avatar
valleykid committed
325
        <Modal
陈帅's avatar
陈帅 committed
326
          title={done ? null : `任务${current ? '编辑' : '添加'}`}
valleykid's avatar
valleykid committed
327
          className={styles.standardListForm}
valleykid's avatar
valleykid committed
328
          width={640}
valleykid's avatar
valleykid committed
329
          bodyStyle={done ? { padding: '72px 0' } : { padding: '28px 0 0' }}
valleykid's avatar
valleykid committed
330 331 332 333 334 335
          destroyOnClose
          visible={visible}
          {...modalFooter}
        >
          {getModalContent()}
        </Modal>
336
      </PageHeaderWrapper>
337 338 339
    );
  }
}