farhantock
10 months ago
6 changed files with 322 additions and 210 deletions
@ -1,139 +1,139 @@ |
|||||||
import React, { useEffect, useState } from 'react' |
import React, { useEffect, useState } from 'react' |
||||||
import { |
import { |
||||||
Modal, ModalHeader, ModalBody, ModalFooter, |
Modal, ModalHeader, ModalBody, ModalFooter, |
||||||
Button, Form, FormGroup, Label, Input, Col, Row |
Button, Form, FormGroup, Label, Input, Col, Row |
||||||
} from 'reactstrap'; |
} from 'reactstrap'; |
||||||
import { Select } from 'antd'; |
import { Select } from 'antd'; |
||||||
import 'antd/dist/antd.css'; |
import 'antd/dist/antd.css'; |
||||||
import { useTranslation } from 'react-i18next'; |
import { useTranslation } from 'react-i18next'; |
||||||
const { Option } = Select; |
const { Option } = Select; |
||||||
|
|
||||||
const DialogForm = ({ openDialog, closeDialog, toggleDialog, typeDialog, dataEdit, company_id, listCompany, role_name }) => { |
const DialogForm = ({ openDialog, closeDialog, toggleDialog, typeDialog, dataEdit, company_id, listCompany, role_name }) => { |
||||||
const [id, setId] = useState(0) |
const [id, setId] = useState(0) |
||||||
const [name, setName] = useState('') |
const [name, setName] = useState('') |
||||||
const [description, setDescription] = useState('') |
const [description, setDescription] = useState('') |
||||||
const [selectedCompany, setSelectedCompany] = useState(null) |
const [selectedCompany, setSelectedCompany] = useState(null) |
||||||
const { t } = useTranslation() |
const { t } = useTranslation() |
||||||
|
|
||||||
|
|
||||||
useEffect(() => { |
useEffect(() => { |
||||||
if (typeDialog === "Edit") { |
if (typeDialog === "Edit") { |
||||||
setId(dataEdit.id) |
setId(dataEdit.id) |
||||||
setDescription(dataEdit.description) |
setDescription(dataEdit.description) |
||||||
setName(dataEdit.name) |
setName(dataEdit.name) |
||||||
setSelectedCompany(dataEdit.company_id); |
setSelectedCompany(dataEdit.company_id); |
||||||
} else { |
} else { |
||||||
setId(0) |
setId(0) |
||||||
} |
} |
||||||
}, [dataEdit, openDialog]) |
}, [dataEdit, openDialog]) |
||||||
|
|
||||||
const handleSave = () => { |
const handleSave = () => { |
||||||
let data = ''; |
let data = ''; |
||||||
if (typeDialog === "Save") { |
if (typeDialog === "Save") { |
||||||
if (role_name === 'Super Admin') { |
if (role_name === 'Super Admin') { |
||||||
company_id = selectedCompany |
company_id = parseInt(selectedCompany) |
||||||
} |
} |
||||||
data = { |
data = { |
||||||
name: name, |
name: name, |
||||||
description: description, |
description: description, |
||||||
company_id: company_id |
company_id: parseInt(company_id) |
||||||
} |
} |
||||||
|
|
||||||
closeDialog('save', data); |
closeDialog('save', data); |
||||||
} else { |
} else { |
||||||
|
|
||||||
if (role_name === 'Super Admin') { |
if (role_name === 'Super Admin') { |
||||||
company_id = selectedCompany |
company_id = parseInt(selectedCompany) |
||||||
} |
} |
||||||
data = { |
data = { |
||||||
id, |
id, |
||||||
name: name, |
name: name, |
||||||
description: description, |
description: description, |
||||||
company_id: company_id |
company_id: parseInt(company_id) |
||||||
} |
} |
||||||
closeDialog('edit', data); |
closeDialog('edit', data); |
||||||
} |
} |
||||||
setId(0) |
setId(0) |
||||||
setDescription('') |
setDescription('') |
||||||
setSelectedCompany(null) |
setSelectedCompany(null) |
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
const handleCancel = () => { |
const handleCancel = () => { |
||||||
closeDialog('cancel', 'none') |
closeDialog('cancel', 'none') |
||||||
setId(0) |
setId(0) |
||||||
setDescription('') |
setDescription('') |
||||||
setSelectedCompany(null) |
setSelectedCompany(null) |
||||||
} |
} |
||||||
|
|
||||||
const onChangeCompany = (val) => { |
const onChangeCompany = (val) => { |
||||||
setSelectedCompany(val); |
setSelectedCompany(val); |
||||||
}; |
}; |
||||||
|
|
||||||
const renderForm = () => { |
const renderForm = () => { |
||||||
return ( |
return ( |
||||||
<Form> |
<Form> |
||||||
<Row> |
<Row> |
||||||
<Col md={6}> |
<Col md={6}> |
||||||
<FormGroup> |
<FormGroup> |
||||||
<Label className="capitalize">{t('name')}</Label> |
<Label className="capitalize">{t('name')}</Label> |
||||||
<Input type="text" value={name} onChange={(e) => setName(e.target.value)} placeholder={t('inputName')} /> |
<Input type="text" value={name} onChange={(e) => setName(e.target.value)} placeholder={t('inputName')} /> |
||||||
</FormGroup> |
</FormGroup> |
||||||
</Col> |
</Col> |
||||||
<Col md={6}> |
<Col md={6}> |
||||||
<FormGroup> |
<FormGroup> |
||||||
<Label className="capitalize">Description</Label> |
<Label className="capitalize">Description</Label> |
||||||
<Input row="4" type="textarea" value={description} onChange={(e) => setDescription(e.target.value)} placeholder={t('inputDescription')} /> |
<Input row="4" type="textarea" value={description} onChange={(e) => setDescription(e.target.value)} placeholder={t('inputDescription')} /> |
||||||
</FormGroup> |
</FormGroup> |
||||||
</Col> |
</Col> |
||||||
</Row> |
</Row> |
||||||
{role_name === 'Super Admin' && |
{role_name === 'Super Admin' && |
||||||
<Row> |
<Row> |
||||||
<Col md={6}> |
<Col md={6}> |
||||||
<FormGroup> |
<FormGroup> |
||||||
<Label className="capitalize"> |
<Label className="capitalize"> |
||||||
{t('company')}<span style={{ color: "red" }}>*</span> |
{t('company')}<span style={{ color: "red" }}>*</span> |
||||||
</Label> |
</Label> |
||||||
<Select |
<Select |
||||||
showSearch |
showSearch |
||||||
filterOption={(inputValue, option) => |
filterOption={(inputValue, option) => |
||||||
option.children.toLowerCase().includes(inputValue.toLowerCase()) |
option.children.toLowerCase().includes(inputValue.toLowerCase()) |
||||||
} |
} |
||||||
value={selectedCompany} |
value={selectedCompany} |
||||||
defaultValue={selectedCompany} |
defaultValue={selectedCompany} |
||||||
onChange={onChangeCompany} |
onChange={onChangeCompany} |
||||||
style={{ width: "100%" }} |
style={{ width: "100%" }} |
||||||
> |
> |
||||||
{listCompany.map((res) => ( |
{listCompany.map((res) => ( |
||||||
<Option key={res.id} value={res.id}> |
<Option key={res.id} value={res.id}> |
||||||
{res.company_name} |
{res.company_name} |
||||||
</Option> |
</Option> |
||||||
))} |
))} |
||||||
</Select> |
</Select> |
||||||
</FormGroup> |
</FormGroup> |
||||||
</Col> |
</Col> |
||||||
</Row> |
</Row> |
||||||
} |
} |
||||||
</Form> |
</Form> |
||||||
) |
) |
||||||
} |
} |
||||||
|
|
||||||
|
|
||||||
return ( |
return ( |
||||||
<> |
<> |
||||||
<Modal size="lg" isOpen={openDialog} toggle={toggleDialog}> |
<Modal size="lg" isOpen={openDialog} toggle={toggleDialog}> |
||||||
<ModalHeader className="capitalize" toggle={closeDialog}>{typeDialog == "Save" ? `Add` : "Edit"} {'uom'}</ModalHeader> |
<ModalHeader className="capitalize" toggle={closeDialog}>{typeDialog == "Save" ? `Add` : "Edit"} {'uom'}</ModalHeader> |
||||||
<ModalBody> |
<ModalBody> |
||||||
{renderForm()} |
{renderForm()} |
||||||
</ModalBody> |
</ModalBody> |
||||||
<ModalFooter> |
<ModalFooter> |
||||||
<Button color="primary" onClick={() => handleSave()}>{typeDialog}</Button>{' '} |
<Button color="primary" onClick={() => handleSave()}>{typeDialog}</Button>{' '} |
||||||
<Button className="capitalize" color="secondary" onClick={() => handleCancel()}>{t('cancel')}</Button> |
<Button className="capitalize" color="secondary" onClick={() => handleCancel()}>{t('cancel')}</Button> |
||||||
</ModalFooter> |
</ModalFooter> |
||||||
</Modal> |
</Modal> |
||||||
</> |
</> |
||||||
) |
) |
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
export default DialogForm; |
export default DialogForm; |
||||||
|
Loading…
Reference in new issue