Skip to content

Commit

Permalink
Code refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
makarkotlov committed Sep 5, 2019
1 parent 4dd990a commit 5d262f4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 39 deletions.
34 changes: 22 additions & 12 deletions src/components/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,45 @@ import { Button, Input } from 'antd'

const Search = ({ handleSearch, getData, dataIndex }) => {
const [searchRef, setSearchRef] = useState({})
const setSearch = () => c => {
setSearchRef(c)
}
const onSearch = phrase => {
if (phrase) {
handleSearch(dataIndex, phrase)
}
}
const handleSearchOnClick = () => {
const phrase = searchRef.input.state.value
if (phrase) {
handleSearch(dataIndex, phrase)
}
}
const resetSearch = () => {
searchRef.input.state.value = ''
getData()
}
return (
<div className="search-container">
<div className="search-input-container">
<Input.Search
placeholder={`Input search ${dataIndex}`}
onSearch={phrase => {
if (phrase) handleSearch(dataIndex, phrase)
}}
ref={c => setSearchRef(c)}
onSearch={onSearch}
ref={setSearch()}
/>
</div>
<div>
<Button
type="primary"
onClick={() => {
const phrase = searchRef.input.state.value
if (phrase) handleSearch(dataIndex, phrase)
}}
onClick={handleSearchOnClick}
icon="search"
size="small"
className="search-input-confirm-btn"
>
Search
</Button>
<Button
onClick={() => {
searchRef.input.state.value = ''
getData()
}}
onClick={resetSearch}
size="small"
className="search-input-reset-btn"
>
Expand Down
35 changes: 16 additions & 19 deletions src/components/TableComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const TableComponent = ({
const [submitBtnRef, _setSubmitBtnRef] = useState({})
const [updateBtnRef, _setUpdateBtnRef] = useState({})
const [record, _setRecord] = useState({})
const [pageNumber, setPageNumber] = useState(1)
const [pageNumber, _setPageNumber] = useState(1)

const _handleSearch = (dataIndex, phrase) => {
const search = {
name: dataIndex === 'name',
Expand Down Expand Up @@ -72,6 +73,16 @@ const TableComponent = ({
})
.catch(err => Notification.error(err))
}
const handleEdit = () => record => {
_setRecord(record)
_setShowEdit(true)
_toggleModal()
}
const handleDelete = () => record => _deleteUser(record)
const handleAdd = () => {
_setShowEdit(false)
_toggleModal()
}
const columns = [
{
title: 'Name',
Expand All @@ -87,19 +98,11 @@ const TableComponent = ({
title: 'Actions',
render: (text, record) => (
<span>
<Button
onClick={() => {
_setRecord(record)
_setShowEdit(true)
_toggleModal()
}}
>
Edit
</Button>
<Button onClick={handleEdit(record)}>Edit</Button>
<Divider type="vertical" />
<Popconfirm
title="Are you sure delete this user?"
onConfirm={() => _deleteUser(record)}
onConfirm={handleDelete(record)}
okText="Yes"
cancelText="No"
>
Expand All @@ -114,7 +117,7 @@ const TableComponent = ({
const _handleSubmit = () => submitBtnRef.buttonNode.click()
const _handleUpdate = () => updateBtnRef.buttonNode.click()
const _handleChange = ({ current }) => {
setPageNumber(current)
_setPageNumber(current)
fetchUsers(current)
}

Expand All @@ -132,13 +135,7 @@ const TableComponent = ({
onChange={_handleChange}
/>

<Button
className="flexed-centered"
onClick={() => {
_setShowEdit(false)
_toggleModal()
}}
>
<Button className="flexed-centered" onClick={handleAdd}>
Add New User
</Button>

Expand Down
7 changes: 2 additions & 5 deletions src/components/UserForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const UserForm = ({
}
})
}
const getSubmitBtnRef = () => ref => _getSubmitBtnRef(ref)
const { getFieldDecorator } = form
const formItemLayout = {
labelCol: { span: 6 },
Expand Down Expand Up @@ -70,11 +71,7 @@ const UserForm = ({
})(<Input.Password />)}
</Form.Item>

<Button
htmlType="submit"
className="hidden"
ref={ref => _getSubmitBtnRef(ref)}
/>
<Button htmlType="submit" className="hidden" ref={getSubmitBtnRef} />
</Form>
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/containers/EditUserFormContainer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pick } from 'ramda'
import { pick } from 'rambda'
import { connect } from 'react-redux'

import EditUserForm from '../components/EditUserForm'
Expand Down
2 changes: 1 addition & 1 deletion src/containers/TableContainer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pick, path, compose } from 'ramda'
import { pick, path, compose } from 'rambda'
import { connect } from 'react-redux'

import TableComponent from '../components/TableComponent'
Expand Down
2 changes: 1 addition & 1 deletion src/store/users/reducer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { propOr, identity } from 'ramda'
import { propOr, identity } from 'rambda'
import { map } from 'lodash'
import cuid from 'cuid'
import * as ActionTypes from './actionTypes'
Expand Down

0 comments on commit 5d262f4

Please sign in to comment.