Browse Source

Merge pull request 'Merge branch 'master' of https://git.oslog.id/ibnu/generic-ospro-frontend into Dev-Farhan' (#64) from Dev-Farhan into staging

Reviewed-on: ibnu/generic-ospro-frontend#64
pull/1/head
farhantock 9 months ago
parent
commit
19f817c7a6
  1. 77
      src/views/Master/MasterRoles/DialogMenuRoles.js

77
src/views/Master/MasterRoles/DialogMenuRoles.js

@ -4,10 +4,7 @@ import { Button, Form, FormGroup, Label, Input } from 'reactstrap';
import 'antd/dist/antd.css'; import 'antd/dist/antd.css';
import axios from 'axios'; import axios from 'axios';
import { MENU_COMPANY_SEARCH } from '../../../const/ApiConst.js'; import { MENU_COMPANY_SEARCH } from '../../../const/ApiConst.js';
const token = window.localStorage.getItem('token'); const token = window.localStorage.getItem('token');
const config = { const config = {
headers: headers:
{ {
@ -15,7 +12,6 @@ const config = {
"Content-type": `application/json` "Content-type": `application/json`
} }
}; };
export default class DialogMenuRoles extends Component { export default class DialogMenuRoles extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
@ -43,7 +39,6 @@ export default class DialogMenuRoles extends Component {
this.props.showDialog(this.showDialog); this.props.showDialog(this.showDialog);
this.getAllMenu(); this.getAllMenu();
} }
async componentDidUpdate() { async componentDidUpdate() {
if (this.state.isParentClick === true) { if (this.state.isParentClick === true) {
const { idRoles } = this.props const { idRoles } = this.props
@ -55,12 +50,9 @@ export default class DialogMenuRoles extends Component {
this.setState({ isParentClick: false, id: idRoles }); this.setState({ isParentClick: false, id: idRoles });
} }
} }
showDialog = () => { showDialog = () => {
this.setState({ isParentClick: true }); this.setState({ isParentClick: true });
} }
getAllMenu = async () => { getAllMenu = async () => {
const payload = { const payload = {
"paging": { "start": 0, "length": -1 }, "paging": { "start": 0, "length": -1 },
@ -73,9 +65,6 @@ export default class DialogMenuRoles extends Component {
], ],
"orders": { "columns": ["id"], "ascending": false } "orders": { "columns": ["id"], "ascending": false }
} }
const result = await axios const result = await axios
.post(MENU_COMPANY_SEARCH, payload, config) .post(MENU_COMPANY_SEARCH, payload, config)
.then(res => res) .then(res => res)
@ -86,11 +75,7 @@ export default class DialogMenuRoles extends Component {
}); });
} else { } else {
} }
} }
setStateMenu = edit => { setStateMenu = edit => {
const stateMenu = []; const stateMenu = [];
this.state.menu.map((val) => { this.state.menu.map((val) => {
@ -104,7 +89,6 @@ export default class DialogMenuRoles extends Component {
}) })
}) })
} }
setStateRead = edit => { setStateRead = edit => {
const stateRead = []; const stateRead = [];
this.state.menu.map((val) => { this.state.menu.map((val) => {
@ -118,7 +102,6 @@ export default class DialogMenuRoles extends Component {
}) })
}) })
} }
setStateCreate = edit => { setStateCreate = edit => {
const stateCreate = []; const stateCreate = [];
this.state.menu.map((val) => { this.state.menu.map((val) => {
@ -132,7 +115,6 @@ export default class DialogMenuRoles extends Component {
}) })
}) })
} }
setStateUpdate = edit => { setStateUpdate = edit => {
const stateUpdate = []; const stateUpdate = [];
this.state.menu.map((val) => { this.state.menu.map((val) => {
@ -146,7 +128,6 @@ export default class DialogMenuRoles extends Component {
}) })
}) })
} }
setStateDelete = edit => { setStateDelete = edit => {
const stateDelete = []; const stateDelete = [];
this.state.menu.map((val) => { this.state.menu.map((val) => {
@ -160,13 +141,10 @@ export default class DialogMenuRoles extends Component {
}) })
}) })
} }
checkMenuRoles = () => { checkMenuRoles = () => {
let copyStateMenu = [...this.state.stateMenu]; let copyStateMenu = [...this.state.stateMenu];
this.props.menuRoles.map((val, indexMenu) => { this.props.menuRoles.map((val, indexMenu) => {
let index = this.getIndexDataMenu(val.menu_id); let index = this.getIndexDataMenu(val.menu_id);
if (index >= 0 || val.read === true || val.create === true || val.update === true || val.delete === true) { if (index >= 0 || val.read === true || val.create === true || val.update === true || val.delete === true) {
copyStateMenu[index] = true; copyStateMenu[index] = true;
} }
@ -180,7 +158,6 @@ export default class DialogMenuRoles extends Component {
} }
}) })
} }
checkReadRoles = () => { checkReadRoles = () => {
let copyStateRead = [...this.state.stateRead]; let copyStateRead = [...this.state.stateRead];
this.props.menuRoles.map((val) => { this.props.menuRoles.map((val) => {
@ -191,7 +168,6 @@ export default class DialogMenuRoles extends Component {
copyStateRead[index] = false; copyStateRead[index] = false;
} }
}) })
this.setState({ stateRead: [] }, () => { this.setState({ stateRead: [] }, () => {
let check = copyStateRead.some(this.checkArray); let check = copyStateRead.some(this.checkArray);
if (check === false) { if (check === false) {
@ -201,10 +177,8 @@ export default class DialogMenuRoles extends Component {
} }
}) })
} }
checkCreateRoles = () => { checkCreateRoles = () => {
let copyStateCreate = [...this.state.stateCreate]; let copyStateCreate = [...this.state.stateCreate];
this.props.menuRoles.map((val) => { this.props.menuRoles.map((val) => {
let index = this.getIndexDataMenu(val.menu_id); let index = this.getIndexDataMenu(val.menu_id);
if (val.create === true) { if (val.create === true) {
@ -213,7 +187,6 @@ export default class DialogMenuRoles extends Component {
copyStateCreate[index] = false; copyStateCreate[index] = false;
} }
}) })
this.setState({ stateCreate: [] }, () => { this.setState({ stateCreate: [] }, () => {
let check = copyStateCreate.some(this.checkArray); let check = copyStateCreate.some(this.checkArray);
if (check === false) { if (check === false) {
@ -223,10 +196,8 @@ export default class DialogMenuRoles extends Component {
} }
}) })
} }
checkUpdateRoles = () => { checkUpdateRoles = () => {
let copyStateUpdate = [...this.state.stateUpdate]; let copyStateUpdate = [...this.state.stateUpdate];
this.props.menuRoles.map((val) => { this.props.menuRoles.map((val) => {
let index = this.getIndexDataMenu(val.menu_id); let index = this.getIndexDataMenu(val.menu_id);
if (val.update === true) { if (val.update === true) {
@ -235,7 +206,6 @@ export default class DialogMenuRoles extends Component {
copyStateUpdate[index] = false; copyStateUpdate[index] = false;
} }
}) })
this.setState({ stateUpdate: [] }, () => { this.setState({ stateUpdate: [] }, () => {
let check = copyStateUpdate.some(this.checkArray); let check = copyStateUpdate.some(this.checkArray);
if (check === false) { if (check === false) {
@ -245,10 +215,8 @@ export default class DialogMenuRoles extends Component {
} }
}) })
} }
checkDeleteRoles = () => { checkDeleteRoles = () => {
let copyStateDelete = [...this.state.stateDelete]; let copyStateDelete = [...this.state.stateDelete];
this.props.menuRoles.map((val) => { this.props.menuRoles.map((val) => {
let index = this.getIndexDataMenu(val.menu_id); let index = this.getIndexDataMenu(val.menu_id);
if (val.delete === true) { if (val.delete === true) {
@ -257,7 +225,6 @@ export default class DialogMenuRoles extends Component {
copyStateDelete[index] = false; copyStateDelete[index] = false;
} }
}) })
this.setState({ stateDelete: [] }, () => { this.setState({ stateDelete: [] }, () => {
let check = copyStateDelete.some(this.checkArray); let check = copyStateDelete.some(this.checkArray);
if (check === false) { if (check === false) {
@ -267,32 +234,26 @@ export default class DialogMenuRoles extends Component {
} }
}) })
} }
getIndexDataMenu = (id) => { getIndexDataMenu = (id) => {
let index = this.state.menu.findIndex(obj => obj.id === id); let index = this.state.menu.findIndex(obj => obj.id === id);
return index return index
} }
getIndexDataCreate = (id) => { getIndexDataCreate = (id) => {
let index = this.state.stateCreate.findIndex(obj => obj.id === id); let index = this.state.stateCreate.findIndex(obj => obj.id === id);
return index return index
} }
getIndexDataDelete = (id) => { getIndexDataDelete = (id) => {
let index = this.state.stateDelete.findIndex(obj => obj.id === id); let index = this.state.stateDelete.findIndex(obj => obj.id === id);
return index return index
} }
getIndexDataRead = (id) => { getIndexDataRead = (id) => {
let index = this.state.stateRead.findIndex(obj => obj.id === id); let index = this.state.stateRead.findIndex(obj => obj.id === id);
return index return index
} }
getIndexDataUpdate = (id) => { getIndexDataUpdate = (id) => {
let index = this.state.stateUpdate.findIndex(obj => obj.id === id); let index = this.state.stateUpdate.findIndex(obj => obj.id === id);
return index return index
} }
handleSave = () => { handleSave = () => {
const { const {
stateMenu, stateMenu,
@ -303,9 +264,7 @@ export default class DialogMenuRoles extends Component {
menu, menu,
id id
} = this.state } = this.state
const arrayData = []; const arrayData = [];
menu.map((val, index) => { menu.map((val, index) => {
let data = { let data = {
roles_id: id, roles_id: id,
@ -315,27 +274,20 @@ export default class DialogMenuRoles extends Component {
update: stateUpdate[index], update: stateUpdate[index],
delete: stateDelete[index], delete: stateDelete[index],
checked: stateMenu[index], checked: stateMenu[index],
} }
arrayData.push(data); arrayData.push(data);
}) })
this.props.closeDialog('save', arrayData); this.props.closeDialog('save', arrayData);
this.setState({ id: 0 }); this.setState({ id: 0 });
} }
handleCancel = () => { handleCancel = () => {
this.props.closeDialog('cancel', 'none') this.props.closeDialog('cancel', 'none')
} }
handleChangeCheckbox = (checked, index) => { handleChangeCheckbox = (checked, index) => {
let copyStateMenu = [...this.state.stateMenu]; let copyStateMenu = [...this.state.stateMenu];
copyStateMenu[index] = checked; copyStateMenu[index] = checked;
this.setState({ stateMenu: copyStateMenu }) this.setState({ stateMenu: copyStateMenu })
} }
handleChangeCheckboxRead = (checked, index, menuItem = null, menuIdxList = []) => { handleChangeCheckboxRead = (checked, index, menuItem = null, menuIdxList = []) => {
let copyStateRead = [...this.state.stateRead]; let copyStateRead = [...this.state.stateRead];
copyStateRead[index] = checked; copyStateRead[index] = checked;
@ -358,10 +310,8 @@ export default class DialogMenuRoles extends Component {
stateReadIdx.map((stateRead) => { stateReadIdx.map((stateRead) => {
copyStateRead[stateRead] = checked copyStateRead[stateRead] = checked
}) })
this.setState({ stateRead: copyStateRead }) this.setState({ stateRead: copyStateRead })
} }
handleChangeCheckboxCreate = (checked, index, menuItem = null, menuIdxList = []) => { handleChangeCheckboxCreate = (checked, index, menuItem = null, menuIdxList = []) => {
let copyStateCreate = [...this.state.stateCreate]; let copyStateCreate = [...this.state.stateCreate];
copyStateCreate[index] = checked; copyStateCreate[index] = checked;
@ -386,7 +336,6 @@ export default class DialogMenuRoles extends Component {
}) })
this.setState({ stateCreate: copyStateCreate }) this.setState({ stateCreate: copyStateCreate })
} }
handleChangeCheckboxEdit = (checked, index, menuItem = null, menuIdxList = []) => { handleChangeCheckboxEdit = (checked, index, menuItem = null, menuIdxList = []) => {
let copyStateEdit = [...this.state.stateUpdate]; let copyStateEdit = [...this.state.stateUpdate];
copyStateEdit[index] = checked; copyStateEdit[index] = checked;
@ -411,7 +360,6 @@ export default class DialogMenuRoles extends Component {
}) })
this.setState({ stateUpdate: copyStateEdit }) this.setState({ stateUpdate: copyStateEdit })
} }
handleChangeCheckboxDelete = (checked, index, menuItem = null, menuIdxList = []) => { handleChangeCheckboxDelete = (checked, index, menuItem = null, menuIdxList = []) => {
let copyStateDelete = [...this.state.stateDelete]; let copyStateDelete = [...this.state.stateDelete];
copyStateDelete[index] = checked; copyStateDelete[index] = checked;
@ -436,7 +384,6 @@ export default class DialogMenuRoles extends Component {
}) })
this.setState({ stateDelete: copyStateDelete }) this.setState({ stateDelete: copyStateDelete })
} }
handleChangeCheckboxReadAll = (checked) => { handleChangeCheckboxReadAll = (checked) => {
let copyStateRead = [...this.state.stateRead]; let copyStateRead = [...this.state.stateRead];
copyStateRead.map((val, index) => { copyStateRead.map((val, index) => {
@ -444,7 +391,6 @@ export default class DialogMenuRoles extends Component {
}) })
this.setState({ stateRead: copyStateRead }) this.setState({ stateRead: copyStateRead })
} }
handleChangeCheckboxCreateAll = (checked, index) => { handleChangeCheckboxCreateAll = (checked, index) => {
let copyStateCreate = [...this.state.stateCreate]; let copyStateCreate = [...this.state.stateCreate];
copyStateCreate.map((val, index) => { copyStateCreate.map((val, index) => {
@ -452,7 +398,6 @@ export default class DialogMenuRoles extends Component {
}) })
this.setState({ stateCreate: copyStateCreate }) this.setState({ stateCreate: copyStateCreate })
} }
handleChangeCheckboxEditAll = (checked, index) => { handleChangeCheckboxEditAll = (checked, index) => {
let copyStateEdit = [...this.state.stateUpdate]; let copyStateEdit = [...this.state.stateUpdate];
copyStateEdit.map((val, index) => { copyStateEdit.map((val, index) => {
@ -460,7 +405,6 @@ export default class DialogMenuRoles extends Component {
}) })
this.setState({ stateUpdate: copyStateEdit }) this.setState({ stateUpdate: copyStateEdit })
} }
handleChangeCheckboxDeleteAll = (checked, index) => { handleChangeCheckboxDeleteAll = (checked, index) => {
let copyStateDelete = [...this.state.stateDelete]; let copyStateDelete = [...this.state.stateDelete];
copyStateDelete.map((val, index) => { copyStateDelete.map((val, index) => {
@ -468,7 +412,6 @@ export default class DialogMenuRoles extends Component {
}) })
this.setState({ stateDelete: copyStateDelete }) this.setState({ stateDelete: copyStateDelete })
} }
renderForm = () => { renderForm = () => {
const { menu, stateRead, stateCreate, stateUpdate, stateDelete } = this.state; const { menu, stateRead, stateCreate, stateUpdate, stateDelete } = this.state;
let menuIdxList = [] let menuIdxList = []
@ -476,16 +419,13 @@ export default class DialogMenuRoles extends Component {
const getChildren = (parentId) => { const getChildren = (parentId) => {
return menu.filter(item => item.parent_menu_id === parentId); return menu.filter(item => item.parent_menu_id === parentId);
}; };
const renderMenu = (parentId, depth = 0) => { const renderMenu = (parentId, depth = 0) => {
const children = getChildren(parentId); const children = getChildren(parentId);
return children.map((menuItem, index) => { return children.map((menuItem, index) => {
const currentIndex = menuIdx; const currentIndex = menuIdx;
menuIdxList[currentIndex] = menuItem.menu_id; menuIdxList[currentIndex] = menuItem.menu_id;
menuIdx++ menuIdx++
const paddingLeft = depth * 30; const paddingLeft = depth * 30;
return ( return (
<React.Fragment key={menuIdx - 1}> <React.Fragment key={menuIdx - 1}>
<tr > <tr >
@ -509,10 +449,7 @@ export default class DialogMenuRoles extends Component {
</React.Fragment> </React.Fragment>
); );
}; };
renderAll = () => { renderAll = () => {
return ( return (
<tr> <tr>
<td>All</td> <td>All</td>
@ -523,45 +460,32 @@ export default class DialogMenuRoles extends Component {
</tr> </tr>
) )
} }
checkArray = (val) => { checkArray = (val) => {
return val === false; return val === false;
} }
handleAllChecked = (checked) => { handleAllChecked = (checked) => {
this.setState({ allChecked: !this.state.allChecked }); this.setState({ allChecked: !this.state.allChecked });
if (checked === true) { if (checked === true) {
let check = this.state.stateMenu.some(this.checkArray); let check = this.state.stateMenu.some(this.checkArray);
if (check) { if (check) {
const stateMenu = []; const stateMenu = [];
this.state.menu.map((val) => { this.state.menu.map((val) => {
stateMenu.push(true); stateMenu.push(true);
}) })
this.setState({ stateMenu: [] }, () => { this.setState({ stateMenu: [] }, () => {
this.setState({ stateMenu: stateMenu }); this.setState({ stateMenu: stateMenu });
}) })
} }
} else { } else {
const stateMenu = []; const stateMenu = [];
this.state.menu.map((val) => { this.state.menu.map((val) => {
stateMenu.push(false); stateMenu.push(false);
}) })
this.setState({ stateMenu: [] }, () => { this.setState({ stateMenu: [] }, () => {
this.setState({ stateMenu: stateMenu }); this.setState({ stateMenu: stateMenu });
}) })
} }
} }
render() { render() {
return ( return (
<Modal size="xl" scrollable={true} isOpen={this.props.openDialog} toggle={this.props.toggleDialog}> <Modal size="xl" scrollable={true} isOpen={this.props.openDialog} toggle={this.props.toggleDialog}>
@ -584,7 +508,6 @@ export default class DialogMenuRoles extends Component {
<th> <th>
Delete Delete
</th> </th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>

Loading…
Cancel
Save