|
1 | 1 | import React, { Component, Fragment } from 'react';
|
2 | 2 | import { Button, Row, Col, Divider, AutoComplete, Input, Icon } from 'antd';
|
| 3 | +import API from 'API'; |
| 4 | +import { from } from 'rxjs'; |
| 5 | +import StakeholderForm from '../../../StakeholderForm'; |
3 | 6 | import './styles.css';
|
4 | 7 |
|
| 8 | +const Option = AutoComplete.Option; |
5 | 9 |
|
| 10 | + |
| 11 | +function renderOption(item) { |
| 12 | + return ( |
| 13 | + <Option key={item._id} name={`${item.name} <${item.email}>`}> |
| 14 | + <div><strong>{item.name}</strong></div> |
| 15 | + <div>{item.email}</div> |
| 16 | + </Option> |
| 17 | + ); |
| 18 | +} |
6 | 19 | class AddPersonnelForm extends Component {
|
7 |
| - state = { showAddPersonnelForm: false, dataSource: [], selected: null }; |
| 20 | + state = { showCreatePersonnelForm: false, dataSource: [], hits: [], selected: null, input: '' }; |
8 | 21 |
|
9 | 22 | onSelect = (value) => {
|
10 |
| - console.log(value); |
11 | 23 | this.setState({ selected: value });
|
12 | 24 | }
|
13 | 25 |
|
14 |
| - handleSearch = (value) => { |
| 26 | + handleSearch = () => { |
15 | 27 | this.setState({ selected: null });
|
16 |
| - this.setState({ |
17 |
| - dataSource: !value ? [] : [ |
18 |
| - value, |
19 |
| - value + value, |
20 |
| - value + value + value, |
21 |
| - ], |
22 |
| - }); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + handleChange = value => { |
| 32 | + this.setState({ input: value }); |
| 33 | + if (value.length > 1) { |
| 34 | + from(API.searchStakeholder(value)) |
| 35 | + .subscribe(result => { |
| 36 | + this.setState({ hits: result.data }); |
| 37 | + }); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + handleAttachPersonnel = () => { |
| 42 | + console.log(this.state.selected); |
| 43 | + this.setState({ input: '' }); |
| 44 | + } |
| 45 | + |
| 46 | + toggleCreatePersonnelForm = () => { |
| 47 | + this.setState({ showCreatePersonnelForm: !this.state.showCreatePersonnelForm }); |
23 | 48 | }
|
24 | 49 |
|
25 | 50 | render() {
|
26 |
| - const { showAddPersonnelForm, dataSource, selected } = this.state; |
| 51 | + const { showCreatePersonnelForm, hits, selected, input } = this.state; |
| 52 | + |
27 | 53 | return (<Fragment>
|
28 | 54 | {
|
29 |
| - showAddPersonnelForm ? '' : ( |
30 |
| - <Row type="flex" align="middle" justify="center"> |
31 |
| - <Col span={10}> |
32 |
| - <div> |
33 |
| - <div style={{ width: '100%' }}> |
34 |
| - <AutoComplete |
35 |
| - className="search-stakeholder" |
36 |
| - style={{ width: '100%' }} |
37 |
| - dataSource={dataSource} |
38 |
| - onSelect={this.onSelect} |
39 |
| - onSearch={this.handleSearch} |
40 |
| - placeholder="Search stakeholder...." |
41 |
| - > |
42 |
| - <Input |
43 |
| - suffix={selected ? ( |
44 |
| - <Button className="search-btn" type="primary"> |
45 |
| - <Icon type="plus" /> Add |
46 |
| - </Button> |
47 |
| - ) : ( |
48 |
| - <Button className="search-btn" type="primary" disabled> |
| 55 | + showCreatePersonnelForm ? <StakeholderForm |
| 56 | + handleCancelClick={this.toggleCreatePersonnelForm} /> : ( |
| 57 | + <Row type="flex" align="middle" justify="center"> |
| 58 | + <Col span={10}> |
| 59 | + <div> |
| 60 | + <div style={{ width: '100%' }}> |
| 61 | + <AutoComplete |
| 62 | + className="search-stakeholder" |
| 63 | + style={{ width: '100%' }} |
| 64 | + dataSource={hits.map(renderOption)} |
| 65 | + onSelect={this.onSelect} |
| 66 | + placeholder="Search stakeholder...." |
| 67 | + onChange={this.handleChange} |
| 68 | + onSearch={this.handleSearch} |
| 69 | + value={input} |
| 70 | + optionLabelProp="name" |
| 71 | + > |
| 72 | + <Input |
| 73 | + suffix={selected ? ( |
| 74 | + <Button className="search-btn" |
| 75 | + type="primary" onClick={this.handleAttachPersonnel}> |
49 | 76 | <Icon type="plus" /> Add
|
50 | 77 | </Button>
|
51 |
| - )} |
52 |
| - /> |
53 |
| - </AutoComplete> |
| 78 | + ) : ( |
| 79 | + <Button className="search-btn" type="primary" disabled> |
| 80 | + <Icon type="plus" /> Add |
| 81 | + </Button> |
| 82 | + )} |
| 83 | + /> |
| 84 | + </AutoComplete> |
| 85 | + </div> |
| 86 | + <Divider><div>OR</div></Divider> |
| 87 | + <Button |
| 88 | + type="primary" |
| 89 | + className='block' |
| 90 | + onClick={this.toggleCreatePersonnelForm} |
| 91 | + >Create New Personnel |
| 92 | + </Button> |
54 | 93 | </div>
|
55 |
| - <Divider><div>OR</div></Divider> |
56 |
| - <Button type="primary" block className='block' >Create New Personnel</Button> |
57 |
| - </div> |
58 |
| - </Col> |
59 |
| - </Row> |
60 |
| - ) |
| 94 | + </Col> |
| 95 | + </Row> |
| 96 | + ) |
61 | 97 | }
|
62 | 98 |
|
63 | 99 | </Fragment>)
|
|
0 commit comments