Browse Source

fix: fixed role menu

pull/1/head
Watiah11 8 months ago
parent
commit
cf01b2be23
  1. 24
      src/views/Master/MasterRoles/DialogMenuRoles.js
  2. 67
      src/views/Master/MasterRoles/index.js

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

@ -1,6 +1,6 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import { Modal, ModalHeader, ModalBody, ModalFooter, Row, Col, Table } from 'reactstrap'; import { Modal, ModalHeader, ModalBody, ModalFooter, Table } from 'reactstrap';
import { Button, Form, FormGroup, Label, Input } from 'reactstrap'; import { Button, Form } 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';
@ -34,13 +34,11 @@ export default class DialogMenuRoles extends Component {
stateUpdateAll: false, stateUpdateAll: false,
stateDeleteAll: false, stateDeleteAll: false,
allChecked: true, allChecked: true,
company_id: props.company_id || null,
} }
} }
async componentDidMount() { async componentDidMount() {
this.props.showDialog(this.showDialog); this.props.showDialog(this.showDialog);
this.getAllMenu();
} }
async componentDidUpdate(prevProps, prevState) { async componentDidUpdate(prevProps, prevState) {
@ -51,10 +49,8 @@ export default class DialogMenuRoles extends Component {
this.setStateUpdate(true); this.setStateUpdate(true);
this.setStateCreate(true); this.setStateCreate(true);
this.setStateDelete(true); this.setStateDelete(true);
this.getAllMenu();
this.setState({ isParentClick: false, id: idRoles }); this.setState({ isParentClick: false, id: idRoles });
// if (prevState.menu == this.state.menu) {
// await this.setStateReadAfterRender();
// }
} }
} }
@ -72,11 +68,9 @@ export default class DialogMenuRoles extends Component {
], ],
"orders": { "columns": ["id"], "ascending": false } "orders": { "columns": ["id"], "ascending": false }
} }
if (this.state.role_name !== "Super Admin") {
payload.columns.push( payload.columns.push(
{ "name": "company_id", "logic_operator": "=", "value": this.state.company_id, "operator": "AND" } { "name": "company_id", "logic_operator": "=", "value": this.props.company_id, "operator": "AND" }
) )
}
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)
@ -116,14 +110,6 @@ export default class DialogMenuRoles extends Component {
}) })
} }
// setStateReadAfterRender = async () => {
// await new Promise((resolve) => {
// this.setState({}, resolve);
// });
// // Now you have access to the rendered elements and can update state based on it
// this.setStateRead(true);
// }
setStateCreate = edit => { setStateCreate = edit => {
const stateCreate = []; const stateCreate = [];
this.state.menu.map((val) => { this.state.menu.map((val) => {
@ -484,7 +470,7 @@ export default class DialogMenuRoles extends Component {
}; };
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) => {
const currentIndex = menuIdx; const currentIndex = menuIdx;
menuIdxList[currentIndex] = menuItem.menu_id; menuIdxList[currentIndex] = menuItem.menu_id;
menuIdx++ menuIdx++

67
src/views/Master/MasterRoles/index.js

@ -43,7 +43,6 @@ class index extends Component {
tooltipTambah: false, tooltipTambah: false,
totalPage: 0, totalPage: 0,
typeDialog: 'Save', typeDialog: 'Save',
company_id: props.company_id || 0,
role_name: props.role_name || '', role_name: props.role_name || '',
role_id: props.role_id || 0, role_id: props.role_id || 0,
user_id: props.user_id || 0, user_id: props.user_id || 0,
@ -69,21 +68,19 @@ class index extends Component {
<Tooltip title={this.props.t('menuRoles')}> <Tooltip title={this.props.t('menuRoles')}>
{ {
checkActMenup('/roles', 'update') ? checkActMenup('/roles', 'update') ?
<i className="cil-menu fa-lg" style={{ color: 'green', marginRight: 10, cursor: "pointer" }} onClick={() => this.handleMenuRoles(text.id)}></i> <i className="cil-menu fa-lg" style={{ color: 'green', marginRight: 10, cursor: "pointer" }} onClick={() => this.handleMenuRoles(text)}></i>
: :
null null
} }
</Tooltip> </Tooltip>
<Tooltip title={this.props.t('delete')}> <Tooltip title={this.props.t('delete')}>
{ {
checkActMenup('/roles', 'delete') ? checkActMenup('/roles', 'delete') ?
<i className="fa fa-trash" style={{ color: 'red', marginRight: 10, cursor: "pointer" }} onClick={() => this.handleDelete(text.id)}></i> <i className="fa fa-trash" style={{ color: 'red', marginRight: 10, cursor: "pointer" }} onClick={() => this.handleDelete(text)}></i>
: :
null null
} }
</Tooltip> </Tooltip>
<Tooltip title={this.props.t('edit')}> <Tooltip title={this.props.t('edit')}>
{ {
checkActMenup('/roles', 'update') ? checkActMenup('/roles', 'update') ?
@ -133,7 +130,6 @@ class index extends Component {
if (this.state.currentPage !== 1 && this.state.currentPage > 1) { if (this.state.currentPage !== 1 && this.state.currentPage > 1) {
start = (this.state.currentPage * this.state.rowsPerPage) - this.state.rowsPerPage start = (this.state.currentPage * this.state.rowsPerPage) - this.state.rowsPerPage
} }
const formData = { const formData = {
"paging": { "start": start, "length": this.state.rowsPerPage }, "paging": { "start": start, "length": this.state.rowsPerPage },
"columns": [], "columns": [],
@ -153,12 +149,9 @@ class index extends Component {
} }
if (this.state.role_name !== "Super Admin") { if (this.state.role_name !== "Super Admin") {
formData.columns.push( formData.columns.push(
{ "name": "company_id", "logic_operator": "=", "value": this.state.company_id, "operator": "AND" }, { "name": "company_id", "logic_operator": "=", "value": this.props.company_id, "operator": "AND" },
) )
} else { } else {
// formData.columns.push(
// { "name": "company_id", "logic_operator": "is null", "value": "", "operator": "AND" },
// )
formData.joins.push( formData.joins.push(
{ "name": "m_company", "column_join": "company_id", "column_results": ["company_name"] } { "name": "m_company", "column_join": "company_id", "column_results": ["company_name"] }
) )
@ -228,7 +221,6 @@ class index extends Component {
} }
saveRole = async (data) => { saveRole = async (data) => {
const formData = { const formData = {
name: data.name, name: data.name,
description: data.description, description: data.description,
@ -247,7 +239,6 @@ class index extends Component {
} else { } else {
NotificationManager.error(`Data role gagal ditambahkan`, 'Failed!!'); NotificationManager.error(`Data role gagal ditambahkan`, 'Failed!!');
} }
} }
editRole = async (data) => { editRole = async (data) => {
@ -272,13 +263,13 @@ class index extends Component {
} }
handleEdit = (data) => { handleEdit = (data) => {
this.setState({ dataEdit: data }); this.setState({ dataEdit: data, company_id: data.company_id });
this.handleOpenDialog('Edit'); this.handleOpenDialog('Edit');
} }
handleDelete = (id) => { handleDelete = (data) => {
id == '1' ? this.setState({ alertNotDelete: true }) : data.id == '1' ? this.setState({ alertNotDelete: true }) :
this.setState({ alertDelete: true, idDelete: id }); this.setState({ alertDelete: true, idDelete: data.id });
} }
onShowSizeChange = (current, pageSize) => { onShowSizeChange = (current, pageSize) => {
@ -317,29 +308,24 @@ class index extends Component {
} }
} }
handleMenuRoles = async (id) => { handleMenuRoles = async (data) => {
const formData = { const formData = {
"paging": { "start": 0, "length": -1 }, "paging": { "start": 0, "length": -1 },
"columns": [], "columns": [],
"joins": [{ "name": "m_menu", "column_join": "menu_id", "column_results": ["parent_id", "name"] }], "joins": [{ "name": "m_menu", "column_join": "menu_id", "column_results": ["parent_id", "name"] }],
"orders": { "columns": ["id"], "ascending": false } "orders": { "columns": ["id"], "ascending": false }
} }
if (this.state.role_name !== "Super Admin") {
formData.columns.push( formData.columns.push(
{ "name": "role_id", "logic_operator": "=", "value": `${id}`, "operator": "AND" } { "name": "role_id", "logic_operator": "=", "value": parseInt(data.id), "operator": "AND" }
) )
}
const result = await axios const result = await axios
.post(ROLEMENU_SEARCH, formData, this.state.config) .post(ROLEMENU_SEARCH, formData, this.state.config)
.then(res => res) .then(res => res)
.catch((error) => error.response); .catch((error) => error.response);
if (result && result.data && result.data.code == 200) { if (result && result.data && result.data.code == 200) {
this.setState({ menuRoles: result.data.data, idRoles: id }, () => { this.setState({ menuRoles: result.data.data, idRoles: data.id, company_id: data.company_id }, () => {
this.handleOpenDialogMr(); this.handleOpenDialogMr();
}); });
} else {
} }
} }
@ -348,8 +334,7 @@ class index extends Component {
if (payloadArray.length > 0) { if (payloadArray.length > 0) {
await this.deleteCurrentRoleMenu(payloadArray[0].roles_id) await this.deleteCurrentRoleMenu(payloadArray[0].roles_id)
} }
let promises = [] let dataArr = []
let result = []
dataArray.map((val, index) => { dataArray.map((val, index) => {
if (val.read === true) { if (val.read === true) {
const formData = { const formData = {
@ -359,23 +344,21 @@ class index extends Component {
read: val.read, read: val.read,
update: val.update, update: val.update,
delete: val.delete delete: val.delete
} }
promises.push(axios.post(ROLEMENU_ADD, formData, this.state.config) dataArr.push(formData);
.then(res => result.push(res)))
} }
}) });
const result = await axios
.post(ROLEMENU_ADD, dataArr, this.state.config)
.then((res) => res)
.catch((err) => err.response);
await Promise.all(promises); if (result && result.data && result.data.code == 200) {
if (result) {
if (result.length > 0) {
if (result[0].data.code === 200) {
this.getDataRoles(); this.getDataRoles();
NotificationManager.success('Data roles berhasil ditambahkan!!', 'Success!!'); NotificationManager.success('Data roles berhasil ditambahkan!!', 'Success!!');
} else { } else {
NotificationManager.error(`${result[0].data.message}`, 'Failed!!'); NotificationManager.error(`${result.data.message}`, 'Failed!!');
}
}
} }
} }
@ -413,12 +396,9 @@ class index extends Component {
if (this.state.role_name !== "Super Admin") { if (this.state.role_name !== "Super Admin") {
formData.columns.push( formData.columns.push(
{ "name": "company_id", "logic_operator": "=", "value": this.state.company_id, "operator": "AND" }, { "name": "company_id", "logic_operator": "=", "value": this.props.company_id, "operator": "AND" },
) )
} else { } else {
// formData.columns.push(
// { "name": "company_id", "logic_operator": "is null", "value": "", "operator": "AND" },
// )
formData.joins.push( formData.joins.push(
{ "name": "m_company", "column_join": "company_id", "column_results": ["company_name"] } { "name": "m_company", "column_join": "company_id", "column_results": ["company_name"] }
) )
@ -462,9 +442,7 @@ class index extends Component {
} }
render() { render() {
const { t } = this.props; const { dialogMenuForm, dataTable, openDialog, currentPage, rowsPerPage, totalPage, search } = this.state
const { tooltipTambah, tooltipExport, dialogMenuForm, dataTable, openDialog, currentPage, rowsPerPage, totalPage, search, tooltipEdit, tooltipDelete, tooltipMenu } = this.state
let noSeq = 0;
return ( return (
<div> <div>
<NotificationContainer /> <NotificationContainer />
@ -527,7 +505,6 @@ class index extends Component {
checkActMenup('/roles', 'create') ? checkActMenup('/roles', 'create') ?
<Button Button id="TooltipTambah" color="success" onClick={() => this.handleOpenDialog('Save')}><i className="fa fa-plus"></i> <Button Button id="TooltipTambah" color="success" onClick={() => this.handleOpenDialog('Save')}><i className="fa fa-plus"></i>
</Button> </Button>
: :
null null
} }

Loading…
Cancel
Save