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 25
import PageHeaderLayout from '@/layouts/PageHeaderLayout';
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

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

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

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

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

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

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

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

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

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

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

  render() {
陈帅's avatar
陈帅 committed
115 116 117 118 119 120 121
    const {
      list: { list },
      loading,
    } = this.props;
    const {
      form: { getFieldDecorator },
    } = this.props;
valleykid's avatar
valleykid committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    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
137 138 139
    const modalFooter = done
      ? { footer: null, onCancel: this.handleDone }
      : { okText: '保存', onOk: this.handleSubmit, onCancel: this.handleCancel };
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

    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
156
        <Search className={styles.extraContentSearch} placeholder="请输入" onSearch={() => ({})} />
157 158 159 160 161 162 163 164 165 166 167 168
      </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
169
        <div className={styles.listContentItem}>
170 171 172
          <span>Owner</span>
          <p>{owner}</p>
        </div>
afc163's avatar
afc163 committed
173
        <div className={styles.listContentItem}>
174
          <span>开始时间</span>
spiritree's avatar
spiritree committed
175
          <p>{moment(createdAt).format('YYYY-MM-DD HH:mm')}</p>
176
        </div>
afc163's avatar
afc163 committed
177 178
        <div className={styles.listContentItem}>
          <Progress percent={percent} status={status} strokeWidth={6} style={{ width: 180 }} />
179 180 181 182
        </div>
      </div>
    );

valleykid's avatar
valleykid committed
183
    const MoreBtn = props => (
jim's avatar
jim committed
184 185 186 187 188 189 190
      <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
191
      >
192 193 194 195 196 197
        <a>
          更多 <Icon type="down" />
        </a>
      </Dropdown>
    );

valleykid's avatar
valleykid committed
198 199 200 201 202 203 204
    const getModalContent = () => {
      if (done) {
        return (
          <Result
            type="success"
            title="操作成功"
            description="一系列的信息描述,很短同样也可以带标点。"
jim's avatar
jim committed
205 206 207 208 209
            actions={
              <Button type="primary" onClick={this.handleDone}>
                知道了
              </Button>
            }
valleykid's avatar
valleykid committed
210
            className={styles.formResult}
valleykid's avatar
valleykid committed
211 212 213 214 215 216 217 218 219
          />
        );
      }
      return (
        <Form onSubmit={this.handleSubmit}>
          <FormItem label="任务名称" {...this.formLayout}>
            {getFieldDecorator('title', {
              rules: [{ required: true, message: '请输入任务名称' }],
              initialValue: current.title,
jim's avatar
jim committed
220
            })(<Input placeholder="请输入" />)}
valleykid's avatar
valleykid committed
221 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
          </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
250
            })(<TextArea rows={4} placeholder="请输入至少五个字符" />)}
valleykid's avatar
valleykid committed
251 252 253 254
          </FormItem>
        </Form>
      );
    };
255 256 257
    return (
      <PageHeaderLayout>
        <div className={styles.standardList}>
258
          <Card bordered={false}>
259 260
            <Row>
              <Col sm={8} xs={24}>
niko's avatar
niko committed
261
                <Info title="我的待办" value="8个任务" bordered />
262 263 264 265 266 267 268 269 270 271 272
              </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
273
            className={styles.listCard}
274
            bordered={false}
niko's avatar
niko committed
275
            title="标准列表"
nikogu's avatar
nikogu committed
276
            style={{ marginTop: 24 }}
niko's avatar
niko committed
277
            bodyStyle={{ padding: '0 32px 40px 32px' }}
278 279
            extra={extraContent}
          >
valleykid's avatar
valleykid committed
280 281 282 283 284
            <Button
              type="dashed"
              style={{ width: '100%', marginBottom: 8 }}
              icon="plus"
              onClick={this.showModal}
jim's avatar
jim committed
285
              ref={component => {
valleykid's avatar
valleykid committed
286 287 288 289 290
                /* eslint-disable */
                this.addBtn = findDOMNode(component);
                /* eslint-enable */
              }}
            >
afc163's avatar
afc163 committed
291
              添加
292 293
            </Button>
            <List
niko's avatar
niko committed
294
              size="large"
nikogu's avatar
nikogu committed
295
              rowKey="id"
296 297
              loading={loading}
              pagination={paginationProps}
nikogu's avatar
nikogu committed
298 299 300
              dataSource={list}
              renderItem={item => (
                <List.Item
valleykid's avatar
valleykid committed
301
                  actions={[
jim's avatar
jim committed
302 303 304 305 306 307 308 309
                    <a
                      onClick={e => {
                        e.preventDefault();
                        this.showEditModal(item);
                      }}
                    >
                      编辑
                    </a>,
valleykid's avatar
valleykid committed
310 311
                    <MoreBtn current={item} />,
                  ]}
nikogu's avatar
nikogu committed
312 313 314 315 316 317 318 319 320 321
                >
                  <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>
              )}
            />
322 323
          </Card>
        </div>
valleykid's avatar
valleykid committed
324
        <Modal
陈帅's avatar
陈帅 committed
325
          title={done ? null : `任务${current ? '编辑' : '添加'}`}
valleykid's avatar
valleykid committed
326
          className={styles.standardListForm}
valleykid's avatar
valleykid committed
327
          width={640}
valleykid's avatar
valleykid committed
328
          bodyStyle={done ? { padding: '72px 0' } : { padding: '28px 0 0' }}
valleykid's avatar
valleykid committed
329 330 331 332 333 334
          destroyOnClose
          visible={visible}
          {...modalFooter}
        >
          {getModalContent()}
        </Modal>
335 336 337 338
      </PageHeaderLayout>
    );
  }
}