feat: nodule create and delete

This commit is contained in:
Joshua Shoemaker 2020-07-17 23:31:33 -05:00
parent 5fb7ed01c6
commit 4f27a64abc
10 changed files with 135 additions and 46 deletions

View File

@ -27,14 +27,12 @@ class Nodules {
removeById = id => { removeById = id => {
const indexToRemove = this.collection.findIndex(n => n.id === id) const indexToRemove = this.collection.findIndex(n => n.id === id)
if (this.collection.length === 1 && indexToRemove > -1) { if (indexToRemove > -1) this.collection.splice(indexToRemove, 1)
this.collection = []
}
else {
const modifiedCollection = this.collection.splice(indexToRemove, 1)
this.collection = modifiedCollection
}
} }
getCollectionProps = () => this.collection.map(nodule => nodule.getProperties())
getTableByLabel = label => this.collection.find(n => label === n.label)
} }
export default Nodules export default Nodules

View File

@ -20,12 +20,11 @@ class CreateNoduleController {
filterParams, filterParams,
tables tables
}) })
console.log(this.nodules)
document.dispatchEvent(this.updatedNodulesEvent) document.dispatchEvent(this.updatedNodulesEvent)
} }
deleteNodule = id => { deleteNodule = id => {
this.nodules.removeTableById(id) this.nodules.removeById(id)
document.dispatchEvent(this.updatedNodulesEvent) document.dispatchEvent(this.updatedNodulesEvent)
} }
} }

View File

@ -0,0 +1,15 @@
import Nodules from '../Collections/Nodules'
class NoduleistController {
constructor() {
this.nodules = new Nodules()
this.updatedNodulesEvent = new Event('updateNodules')
}
deleteTable = id => {
this.nodules.removeById(id)
document.dispatchEvent(this.updatedNodulesEvent)
}
}
export default NoduleistController

View File

@ -7,7 +7,7 @@ class TableListController {
} }
deleteTable = id => { deleteTable = id => {
this.tables.removeTableById(id) this.tables.removeById(id)
document.dispatchEvent(this.updatedTablesEvent) document.dispatchEvent(this.updatedTablesEvent)
} }
} }

View File

@ -4,9 +4,7 @@ import App from './views/App'
import * as serviceWorker from './serviceWorker' import * as serviceWorker from './serviceWorker'
ReactDOM.render( ReactDOM.render(
<React.StrictMode> <App />,
<App />
</React.StrictMode>,
document.getElementById('root') document.getElementById('root')
) )

View File

@ -4,6 +4,7 @@ import 'semantic-ui-css/semantic.min.css'
import CreateTableForm from './CreateTable/CreateTableForm' import CreateTableForm from './CreateTable/CreateTableForm'
import TableList from './TableList/TableList' import TableList from './TableList/TableList'
import CreateNodule from './CreateNodule/CreateNodule' import CreateNodule from './CreateNodule/CreateNodule'
import NoduleList from './NoduleList/NoduleList'
class App extends Component { class App extends Component {
render = () => { render = () => {
@ -12,6 +13,7 @@ class App extends Component {
<CreateTableForm /> <CreateTableForm />
<CreateNodule /> <CreateNodule />
<TableList /> <TableList />
<NoduleList />
</div> </div>
) )
} }

View File

@ -1,44 +1,48 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import { Input, Dropdown, Grid } from 'semantic-ui-react' import { Input, Dropdown, Grid, Button, Icon, List } from 'semantic-ui-react'
import './CreateNodule.css' import './CreateNodule.css'
class CreateFilterNoduleForm extends Component { class CreateFilterNoduleForm extends Component {
constructor () { constructor () {
super() super()
this.state = { filterType: '' } this.state = {
filterType: '',
filterParams: {}
}
this.keyInput1 = React.createRef() this.keyInput = React.createRef()
this.keyInput2 = React.createRef() this.valueInput = React.createRef()
this.keyInput3 = React.createRef()
this.valueInput1 = React.createRef()
this.valueInput2 = React.createRef()
this.valueInput3 = React.createRef()
document.addEventListener('updateTables', this.updateTableList) document.addEventListener('updateTables', this.updateTableList)
} }
addKeyValueInput = () => {
let filterParams = this.state.filterParams || {}
if (this.keyInput.current.inputRef.current.value)
filterParams[this.keyInput.current.inputRef.current.value] = this.valueInput.current.inputRef.current.value
this.setState({ filterParams: filterParams })
}
handleChange = (e, value) => { handleChange = (e, value) => {
this.setState({ filterType: value.value }) this.setState({ filterType: value.value })
} }
getFilterProperties = () => { renderFilterParams = () => {
let filterParams = {} const { filterParams } = this.state
if (this.keyInput1.current.inputRef.current.value) let filterKeyElements = []
filterParams[this.keyInput1.current.inputRef.current.value] = this.valueInput1.current.inputRef.current.value let filterValueElements = []
for (let key in filterParams) {
if (this.keyInput2.current.inputRef.current.value) filterKeyElements.push(<List.Item>{key}</List.Item>)
filterParams[this.keyInput2.current.inputRef.current.value] = this.valueInput2.current.inputRef.current.value filterValueElements.push(<List.Item>{filterParams[key]}</List.Item>)
if (this.keyInput3.current.inputRef.current.value)
filterParams[this.keyInput3.current.inputRef.current.value] = this.valueInput3.current.inputRef.current.value
return {
filterType: this.state.filterType,
filterParams
} }
return { filterKeyElements, filterValueElements }
} }
render = () => { render = () => {
const filterParamElements = this.renderFilterParams()
return ( return (
<div className='CreateFiltrerNoduleForm'> <div className='CreateFiltrerNoduleForm'>
<Dropdown <Dropdown
@ -57,14 +61,20 @@ class CreateFilterNoduleForm extends Component {
<Grid columns={2} relaxed='very' stackable> <Grid columns={2} relaxed='very' stackable>
<Grid.Column> <Grid.Column>
<Input placeholder='Key' ref={this.keyInput1} style={{ width: '115px' }} /> <List celled>
<Input placeholder='Key' ref={this.keyInput2} style={{ width: '115px' }} /> { filterParamElements.filterKeyElements }
<Input placeholder='Key' ref={this.keyInput3} style={{ width: '115px' }} /> </List>
<Input placeholder='Key' ref={this.keyInput} style={{ width: '115px' }} />
</Grid.Column> </Grid.Column>
<Grid.Column> <Grid.Column>
<Input placeholder='Value' ref={this.valueInput1} style={{ width: '115px' }} /> <List celled>
<Input placeholder='Value' ref={this.valueInput2} style={{ width: '115px' }} /> { filterParamElements.filterValueElements }
<Input placeholder='Value' ref={this.valueInput3} style={{ width: '115px' }} /> </List>
<Input placeholder='Key' ref={this.valueInput} style={{ width: '115px' }} />
<Button animated='vertical' onClick={this.addKeyValueInput}>
<Button.Content hidden><Icon name='add' /></Button.Content>
<Button.Content visible>Add</Button.Content>
</Button>
</Grid.Column> </Grid.Column>
</Grid> </Grid>
</div> </div>

View File

@ -1,5 +1,5 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import { Button, Input, Header, Dropdown, Table } from 'semantic-ui-react' import { Button, Input, Header, Dropdown } from 'semantic-ui-react'
import CreateFilterNoduleForm from './CreateFilterNoduleForm' import CreateFilterNoduleForm from './CreateFilterNoduleForm'
import './CreateNodule.css' import './CreateNodule.css'
@ -20,6 +20,8 @@ class CreateNodule extends Component {
this.controller = new CreateNoduleController() this.controller = new CreateNoduleController()
this.filterNoduleForm = React.createRef() this.filterNoduleForm = React.createRef()
this.tableSelect = React.createRef()
this.noduleLabelInput = React.createRef()
document.addEventListener('updateTables', this.updateTableList) document.addEventListener('updateTables', this.updateTableList)
} }
@ -29,8 +31,19 @@ class CreateNodule extends Component {
} }
handleSubmit = () => { handleSubmit = () => {
if (this.state.noduleType === 'filter')
console.log(this.filterNoduleForm.current.getFilterProperties()) const noduleLabel = this.noduleLabelInput.current.inputRef.current.value
const selectedTableLabels = this.tableSelect.current.getSelectedTableLabels()
if (this.state.noduleType === 'filter') {
const filterProperties = this.filterNoduleForm.current.getFilterProperties()
this.controller.addNewFilterNodule({
label: noduleLabel,
tablesToImportByLabel: selectedTableLabels,
filterType: filterProperties.filterType,
filterParams: filterProperties.filterParams
})
}
} }
updateTableList = () => { updateTableList = () => {
@ -49,7 +62,7 @@ class CreateNodule extends Component {
<Input <Input
placeholder='Nodule Label' placeholder='Nodule Label'
ref={this.tableLabelInput} ref={this.noduleLabelInput}
icon='tags' icon='tags'
style={{ width: '300px' }} style={{ width: '300px' }}
/> />
@ -68,7 +81,7 @@ class CreateNodule extends Component {
onChange={this.handleChange} onChange={this.handleChange}
/> />
<TableSelect /> <TableSelect ref={this.tableSelect} />
{ this.renderNoduleForm() } { this.renderNoduleForm() }

View File

View File

@ -0,0 +1,54 @@
import React, { Component } from 'react'
import { Card, Icon, CardContent } from 'semantic-ui-react'
import './NoduleList.css'
import Nodule from '../../Collections/Nodules'
import NoduleListController from '../../Controllers/NoduleListController'
class NoduleList extends Component {
constructor () {
super()
this.nodules = new Nodule()
this.controller = new NoduleListController()
this.state = { nodules: this.nodules.getCollectionProps() }
document.addEventListener('updateNodules', this.updateNoduleList)
}
updateNoduleList = () => {
this.setState({nodules: this.nodules.getCollectionProps()})
}
renderTableListElements = () => {
const { nodules } = this.state
const noduleListElements = nodules.map(n =>
<Card key={n.id} id={n.id}>
<Card.Content>
<Card.Header>{ n.label }</Card.Header>
<Card.Meta>{`${n.tables.length} tables`}</Card.Meta>
</Card.Content>
<CardContent
extra
onClick={() => { this.controller.deleteNodule(n.id) }}
style={{ cursor: 'pointer' }}
>
Delete <Icon name='trash' />
</CardContent>
</Card>
)
return noduleListElements
}
render = () => {
return (
<div className='TableList'>
<Card.Group>
{ this.renderTableListElements() }
</Card.Group>
</div>
)
}
}
export default NoduleList