From 42e20239fb802dd185676618224916539ef6baa6 Mon Sep 17 00:00:00 2001 From: farhantock Date: Wed, 28 Feb 2024 16:34:56 +0700 Subject: [PATCH] resolve conflict --- .../Master/MasterRoles/DialogMenuRoles.js | 153 +++--------------- 1 file changed, 26 insertions(+), 127 deletions(-) diff --git a/src/views/Master/MasterRoles/DialogMenuRoles.js b/src/views/Master/MasterRoles/DialogMenuRoles.js index 348f614..a00128e 100644 --- a/src/views/Master/MasterRoles/DialogMenuRoles.js +++ b/src/views/Master/MasterRoles/DialogMenuRoles.js @@ -3,11 +3,8 @@ import { Modal, ModalHeader, ModalBody, ModalFooter, Row, Col, Table } from 'rea import { Button, Form, FormGroup, Label, Input } from 'reactstrap'; import 'antd/dist/antd.css'; import axios from 'axios'; -import { MENU_SEARCH } from '../../../const/ApiConst.js'; - +import { MENU_COMPANY_SEARCH } from '../../../const/ApiConst.js'; const token = window.localStorage.getItem('token'); - - const config = { headers: { @@ -15,7 +12,6 @@ const config = { "Content-type": `application/json` } }; - export default class DialogMenuRoles extends Component { constructor(props) { super(props) @@ -36,13 +32,13 @@ export default class DialogMenuRoles extends Component { stateUpdateAll: false, stateDeleteAll: false, allChecked: true, + company_id: props.company_id || null, } } async componentDidMount() { this.props.showDialog(this.showDialog); this.getAllMenu(); } - async componentDidUpdate() { if (this.state.isParentClick === true) { const { idRoles } = this.props @@ -54,43 +50,32 @@ export default class DialogMenuRoles extends Component { this.setState({ isParentClick: false, id: idRoles }); } } - - showDialog = () => { this.setState({ isParentClick: true }); } - getAllMenu = async () => { const payload = { "paging": { "start": 0, "length": -1 }, "columns": [ - { "name": "name", "logic_operator": "ilike", "value": "", "operator": "AND" } + { "name": "company_id", "logic_operator": "=", "value": this.state.company_id, "operator": "AND" } ], "joins": [ - { "name": "t_roles_menu", "column_join": "id", "column_results": ["create", "update", "delete", "read"] } + { "name": "t_roles_menu", "column_join": "menu_id", "column_results": ["create", "update", "delete", "read"] }, + { "name": "m_menu", "column_join": "menu_id", "column_results": ["name"] } ], "orders": { "columns": ["id"], "ascending": false } } - - - const result = await axios - .post(MENU_SEARCH, payload, config) + .post(MENU_COMPANY_SEARCH, payload, config) .then(res => res) .catch((error) => error.response); - - // console.log('78', result); if (result && result.data && result.data.code == 200) { this.setState({ menu: result.data.data }, () => { this.setStateMenu(false); }); } else { } - } - - - setStateMenu = edit => { const stateMenu = []; this.state.menu.map((val) => { @@ -104,7 +89,6 @@ export default class DialogMenuRoles extends Component { }) }) } - setStateRead = edit => { const stateRead = []; this.state.menu.map((val) => { @@ -118,7 +102,6 @@ export default class DialogMenuRoles extends Component { }) }) } - setStateCreate = edit => { const stateCreate = []; this.state.menu.map((val) => { @@ -132,7 +115,6 @@ export default class DialogMenuRoles extends Component { }) }) } - setStateUpdate = edit => { const stateUpdate = []; this.state.menu.map((val) => { @@ -146,7 +128,6 @@ export default class DialogMenuRoles extends Component { }) }) } - setStateDelete = edit => { const stateDelete = []; this.state.menu.map((val) => { @@ -160,13 +141,10 @@ export default class DialogMenuRoles extends Component { }) }) } - checkMenuRoles = () => { let copyStateMenu = [...this.state.stateMenu]; - this.props.menuRoles.map((val, indexMenu) => { let index = this.getIndexDataMenu(val.menu_id); - if (index >= 0 || val.read === true || val.create === true || val.update === true || val.delete === true) { copyStateMenu[index] = true; } @@ -180,7 +158,6 @@ export default class DialogMenuRoles extends Component { } }) } - checkReadRoles = () => { let copyStateRead = [...this.state.stateRead]; this.props.menuRoles.map((val) => { @@ -191,7 +168,6 @@ export default class DialogMenuRoles extends Component { copyStateRead[index] = false; } }) - this.setState({ stateRead: [] }, () => { let check = copyStateRead.some(this.checkArray); if (check === false) { @@ -201,10 +177,8 @@ export default class DialogMenuRoles extends Component { } }) } - checkCreateRoles = () => { let copyStateCreate = [...this.state.stateCreate]; - this.props.menuRoles.map((val) => { let index = this.getIndexDataMenu(val.menu_id); if (val.create === true) { @@ -213,7 +187,6 @@ export default class DialogMenuRoles extends Component { copyStateCreate[index] = false; } }) - this.setState({ stateCreate: [] }, () => { let check = copyStateCreate.some(this.checkArray); if (check === false) { @@ -223,10 +196,8 @@ export default class DialogMenuRoles extends Component { } }) } - checkUpdateRoles = () => { let copyStateUpdate = [...this.state.stateUpdate]; - this.props.menuRoles.map((val) => { let index = this.getIndexDataMenu(val.menu_id); if (val.update === true) { @@ -235,7 +206,6 @@ export default class DialogMenuRoles extends Component { copyStateUpdate[index] = false; } }) - this.setState({ stateUpdate: [] }, () => { let check = copyStateUpdate.some(this.checkArray); if (check === false) { @@ -245,10 +215,8 @@ export default class DialogMenuRoles extends Component { } }) } - checkDeleteRoles = () => { let copyStateDelete = [...this.state.stateDelete]; - this.props.menuRoles.map((val) => { let index = this.getIndexDataMenu(val.menu_id); if (val.delete === true) { @@ -257,7 +225,6 @@ export default class DialogMenuRoles extends Component { copyStateDelete[index] = false; } }) - this.setState({ stateDelete: [] }, () => { let check = copyStateDelete.some(this.checkArray); if (check === false) { @@ -267,32 +234,26 @@ export default class DialogMenuRoles extends Component { } }) } - getIndexDataMenu = (id) => { let index = this.state.menu.findIndex(obj => obj.id === id); return index } - getIndexDataCreate = (id) => { let index = this.state.stateCreate.findIndex(obj => obj.id === id); return index } - getIndexDataDelete = (id) => { let index = this.state.stateDelete.findIndex(obj => obj.id === id); return index } - getIndexDataRead = (id) => { let index = this.state.stateRead.findIndex(obj => obj.id === id); return index } - getIndexDataUpdate = (id) => { let index = this.state.stateUpdate.findIndex(obj => obj.id === id); return index } - handleSave = () => { const { stateMenu, @@ -303,9 +264,7 @@ export default class DialogMenuRoles extends Component { menu, id } = this.state - const arrayData = []; - menu.map((val, index) => { let data = { roles_id: id, @@ -315,35 +274,27 @@ export default class DialogMenuRoles extends Component { update: stateUpdate[index], delete: stateDelete[index], checked: stateMenu[index], - } arrayData.push(data); - - }) - console.log('arrayData', arrayData); - // this.props.closeDialog('save', arrayData); + this.props.closeDialog('save', arrayData); this.setState({ id: 0 }); } - handleCancel = () => { this.props.closeDialog('cancel', 'none') } - handleChangeCheckbox = (checked, index) => { let copyStateMenu = [...this.state.stateMenu]; copyStateMenu[index] = checked; - this.setState({ stateMenu: copyStateMenu }) } - handleChangeCheckboxRead = (checked, index, menuItem = null, menuIdxList = []) => { let copyStateRead = [...this.state.stateRead]; copyStateRead[index] = checked; let menu = this.state.menu; let checkMenuParent = menu.map((state, index) => { - if (state.parent_id === menuItem.id) { - return state.id + if (state.parent_menu_id === menuItem.menu_id) { + return state.menu_id } else { return null } @@ -359,17 +310,15 @@ export default class DialogMenuRoles extends Component { stateReadIdx.map((stateRead) => { copyStateRead[stateRead] = checked }) - this.setState({ stateRead: copyStateRead }) } - handleChangeCheckboxCreate = (checked, index, menuItem = null, menuIdxList = []) => { let copyStateCreate = [...this.state.stateCreate]; copyStateCreate[index] = checked; let menu = this.state.menu; let checkMenuParent = menu.map((state, index) => { - if (state.parent_id === menuItem.id) { - return state.id + if (state.parent_menu_id === menuItem.menu_id) { + return state.menu_id } else { return null } @@ -387,14 +336,13 @@ export default class DialogMenuRoles extends Component { }) this.setState({ stateCreate: copyStateCreate }) } - handleChangeCheckboxEdit = (checked, index, menuItem = null, menuIdxList = []) => { let copyStateEdit = [...this.state.stateUpdate]; copyStateEdit[index] = checked; let menu = this.state.menu; let checkMenuParent = menu.map((state, index) => { - if (state.parent_id === menuItem.id) { - return state.id + if (state.parent_menu_id === menuItem.menu_id) { + return state.menu_id } else { return null } @@ -412,14 +360,13 @@ export default class DialogMenuRoles extends Component { }) this.setState({ stateUpdate: copyStateEdit }) } - handleChangeCheckboxDelete = (checked, index, menuItem = null, menuIdxList = []) => { let copyStateDelete = [...this.state.stateDelete]; copyStateDelete[index] = checked; let menu = this.state.menu; let checkMenuParent = menu.map((state, index) => { - if (state.parent_id === menuItem.id) { - return state.id + if (state.parent_menu_id === menuItem.menu_id) { + return state.menu_id } else { return null } @@ -437,7 +384,6 @@ export default class DialogMenuRoles extends Component { }) this.setState({ stateDelete: copyStateDelete }) } - handleChangeCheckboxReadAll = (checked) => { let copyStateRead = [...this.state.stateRead]; copyStateRead.map((val, index) => { @@ -445,7 +391,6 @@ export default class DialogMenuRoles extends Component { }) this.setState({ stateRead: copyStateRead }) } - handleChangeCheckboxCreateAll = (checked, index) => { let copyStateCreate = [...this.state.stateCreate]; copyStateCreate.map((val, index) => { @@ -453,7 +398,6 @@ export default class DialogMenuRoles extends Component { }) this.setState({ stateCreate: copyStateCreate }) } - handleChangeCheckboxEditAll = (checked, index) => { let copyStateEdit = [...this.state.stateUpdate]; copyStateEdit.map((val, index) => { @@ -461,7 +405,6 @@ export default class DialogMenuRoles extends Component { }) this.setState({ stateUpdate: copyStateEdit }) } - handleChangeCheckboxDeleteAll = (checked, index) => { let copyStateDelete = [...this.state.stateDelete]; copyStateDelete.map((val, index) => { @@ -469,74 +412,44 @@ export default class DialogMenuRoles extends Component { }) this.setState({ stateDelete: copyStateDelete }) } - - // renderForm = () => { - // const { menu, stateRead, stateCreate, stateUpdate, stateDelete } = this.state - // console.log('menu', menu); - // return ( - // menu.map((val, index) => { - - // return ( - // - // {val.name} - // this.handleChangeCheckboxRead(e.target.checked, index)} defaultChecked={stateRead[index]} /> - // this.handleChangeCheckboxCreate(e.target.checked, index)} defaultChecked={stateCreate[index]} /> - // this.handleChangeCheckboxEdit(e.target.checked, index)} defaultChecked={stateUpdate[index]} /> - // this.handleChangeCheckboxDelete(e.target.checked, index)} defaultChecked={stateDelete[index]} /> - // - // ) - // }) - // ) - - // } - renderForm = () => { const { menu, stateRead, stateCreate, stateUpdate, stateDelete } = this.state; let menuIdxList = [] let menuIdx = 0 const getChildren = (parentId) => { - return menu.filter(item => item.parent_id === parentId); + return menu.filter(item => item.parent_menu_id === parentId); }; - - // Function to render menu items const renderMenu = (parentId, depth = 0) => { const children = getChildren(parentId); - return children.map((menuItem, index) => { - const currentIndex = menuIdx; // Capture current index - menuIdxList[currentIndex] = menuItem.id; + const currentIndex = menuIdx; + menuIdxList[currentIndex] = menuItem.menu_id; menuIdx++ - const paddingLeft = depth * 20; - const fontWeight = menuItem.parent_id === null ? 'bold' : 'normal'; - + const paddingLeft = depth * 30; return ( - + - {menuItem.name} + {menuItem.join_second_name} this.handleChangeCheckboxRead(e.target.checked, currentIndex, menuItem, menuIdxList)} defaultChecked={stateRead[currentIndex]} /> this.handleChangeCheckboxCreate(e.target.checked, currentIndex, menuItem, menuIdxList)} defaultChecked={stateCreate[currentIndex]} /> this.handleChangeCheckboxEdit(e.target.checked, currentIndex, menuItem, menuIdxList)} defaultChecked={stateUpdate[currentIndex]} /> this.handleChangeCheckboxDelete(e.target.checked, currentIndex, menuItem, menuIdxList)} defaultChecked={stateDelete[currentIndex]} /> - {renderMenu(menuItem.id, depth + 1)} {/* Recursively render children */} + {renderMenu(menuItem.menu_id, depth + 1)} ); }); }; menuIdxList = [] menuIdx = 0 - // Render top-level menu items (parents) return ( - {this.renderAll()} {/* Render the "All" row */} - {renderMenu(null)} {/* Render the menu items */} + {this.renderAll()} + {renderMenu(null)} ); }; - - renderAll = () => { - return ( All @@ -547,45 +460,32 @@ export default class DialogMenuRoles extends Component { ) } - - - - checkArray = (val) => { return val === false; } - handleAllChecked = (checked) => { this.setState({ allChecked: !this.state.allChecked }); if (checked === true) { - let check = this.state.stateMenu.some(this.checkArray); - if (check) { const stateMenu = []; this.state.menu.map((val) => { stateMenu.push(true); }) - this.setState({ stateMenu: [] }, () => { this.setState({ stateMenu: stateMenu }); }) } - - } else { - const stateMenu = []; this.state.menu.map((val) => { stateMenu.push(false); }) - this.setState({ stateMenu: [] }, () => { this.setState({ stateMenu: stateMenu }); }) } } - render() { return ( @@ -603,12 +503,11 @@ export default class DialogMenuRoles extends Component { Create - Edite + Edit Delete - @@ -624,4 +523,4 @@ export default class DialogMenuRoles extends Component { ) } -} +} \ No newline at end of file