summaryrefslogtreecommitdiff
path: root/frontend/src/components/modals/custom-components/ChangeTopologyModalComponent.js
blob: f1645dcfc2bbd1959b946b681a2fd3028915b4d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import PropTypes from 'prop-types'
import React from 'react'
import Shapes from '../../../shapes'
import Modal from '../Modal'

class ChangeTopologyModalComponent extends React.Component {
    static propTypes = {
        show: PropTypes.bool.isRequired,
        topologies: PropTypes.arrayOf(Shapes.Topology),
        currentTopologyId: PropTypes.number,
        onChooseTopology: PropTypes.func.isRequired,
        onCreateTopology: PropTypes.func.isRequired,
        onDuplicateTopology: PropTypes.func.isRequired,
        onDeleteTopology: PropTypes.func.isRequired,
        onCancel: PropTypes.func.isRequired,
    }

    reset() {
        this.textInput.value = ''
        this.originTopology.selectedIndex = 0
    }

    onSubmit() {
        if (this.originTopology.selectedIndex === 0) {
            this.onCreate()
        } else {
            this.onDuplicate()
        }
    }

    onChoose(id) {
        this.props.onChooseTopology(id)
        this.reset()
    }

    onCreate() {
        this.props.onCreateTopology(this.textInput.value)
        this.reset()
    }

    onDuplicate() {
        this.props.onCreateTopology(
            this.textInput.value,
            this.originTopology.value,
        )
        this.reset()
    }

    onDelete(id) {
        this.props.onDeleteTopology(id)
        this.reset()
    }

    onCancel() {
        this.props.onCancel()
        this.reset()
    }

    render() {
        return (
            <Modal
                title="Change Topology"
                show={this.props.show}
                onSubmit={this.onSubmit.bind(this)}
                onCancel={this.onCancel.bind(this)}
            >
                <div>
                    {this.props.topologies.map((topology, idx) => (
                        <div key={topology._id} className="row mb-1">
                            <div className="col-6">
                                <em>{topology._id === this.props.currentTopologyId ? 'Active: ' : ''}</em>
                                {topology.name}
                            </div>
                            <div className="col-6 text-right">
                                <span
                                    className="btn btn-primary mr-1"
                                    onClick={() => this.onChoose(topology._id)}
                                >
                                    Choose
                                </span>
                                <span
                                    className={'btn btn-danger ' + (idx === 0 ? 'disabled' : '')}
                                    onClick={() => idx !== 0 ? this.onDelete(topology._id) : undefined}
                                >
                                    Delete
                                </span>
                            </div>
                        </div>
                    ))}
                </div>

                <h5 className="pt-3 pt-1">New Topology</h5>
                <form
                    onSubmit={e => {
                        e.preventDefault()
                        this.onSubmit()
                    }}
                >
                    <div className="form-group">
                        <label className="form-control-label">Name</label>
                        <input
                            type="text"
                            className="form-control"
                            required
                            ref={textInput => (this.textInput = textInput)}
                        />
                    </div>
                    <div className="form-group">
                        <label className="form-control-label">Topology to duplicate</label>
                        <select
                            className="form-control"
                            ref={originTopology => (this.originTopology = originTopology)}
                        >
                            <option value={-1} key={-1}>
                                None - start from scratch
                            </option>
                            {this.props.topologies.map(topology => (
                                <option value={topology._id} key={topology._id}>
                                    {topology.name}
                                </option>
                            ))}
                        </select>
                    </div>
                </form>
            </Modal>
        )
    }
}

export default ChangeTopologyModalComponent