feat: add role fetching functionality to DetailUser component

This commit is contained in:
2025-10-10 14:49:16 +07:00
parent c312577d50
commit 5f6c156c12
2 changed files with 52 additions and 12 deletions

View File

@@ -4,7 +4,14 @@ import { Modal, Form, Input, Select, Row, Col } from 'antd';
const { TextArea } = Input; const { TextArea } = Input;
const { Option } = Select; const { Option } = Select;
const DetailRole = memo(function DetailRole({ visible, onCancel, onOk, form, editingKey, readOnly }) { const DetailRole = memo(function DetailRole({
visible,
onCancel,
onOk,
form,
editingKey,
readOnly,
}) {
const getModalTitle = () => { const getModalTitle = () => {
if (readOnly) return 'Detail Role'; if (readOnly) return 'Detail Role';
if (editingKey) return 'Edit Role'; if (editingKey) return 'Edit Role';
@@ -22,12 +29,7 @@ const DetailRole = memo(function DetailRole({ visible, onCancel, onOk, form, edi
width={600} width={600}
cancelButtonProps={{ style: readOnly ? { display: 'none' } : {} }} cancelButtonProps={{ style: readOnly ? { display: 'none' } : {} }}
> >
<Form <Form form={form} layout="vertical" name="roleForm" disabled={readOnly}>
form={form}
layout="vertical"
name="roleForm"
disabled={readOnly}
>
<Row gutter={16}> <Row gutter={16}>
<Col span={24}> <Col span={24}>
<Form.Item <Form.Item
@@ -61,6 +63,7 @@ const DetailRole = memo(function DetailRole({ visible, onCancel, onOk, form, edi
<Option value={1}>Level 1</Option> <Option value={1}>Level 1</Option>
<Option value={2}>Level 2</Option> <Option value={2}>Level 2</Option>
<Option value={3}>Level 3</Option> <Option value={3}>Level 3</Option>
<Option value={4}>Level 4</Option>
</Select> </Select>
</Form.Item> </Form.Item>
</Col> </Col>

View File

@@ -2,12 +2,15 @@ import React, { useEffect, useState } from 'react';
import { Modal, Input, Divider, Typography, Switch, Button, ConfigProvider, Select } from 'antd'; import { Modal, Input, Divider, Typography, Switch, Button, ConfigProvider, Select } from 'antd';
import { NotifAlert, NotifOk } from '../../../components/Global/ToastNotif'; import { NotifAlert, NotifOk } from '../../../components/Global/ToastNotif';
import { createUser, updateUser } from '../../../api/user'; import { createUser, updateUser } from '../../../api/user';
import { getAllRole } from '../../../api/role';
const { Text } = Typography; const { Text } = Typography;
const { Option } = Select; const { Option } = Select;
const DetailUser = (props) => { const DetailUser = (props) => {
const [confirmLoading, setConfirmLoading] = useState(false); const [confirmLoading, setConfirmLoading] = useState(false);
const [roleList, setRoleList] = useState([]);
const [loadingRoles, setLoadingRoles] = useState(false);
const defaultData = { const defaultData = {
user_id: '', user_id: '',
@@ -208,6 +211,35 @@ const DetailUser = (props) => {
}); });
}; };
// Fetch all roles when component mounts or modal opens
const fetchRoles = async () => {
setLoadingRoles(true);
try {
// Create query params for fetching all roles without pagination limit
const queryParams = new URLSearchParams({
page: 1,
limit: 100, // Get all roles
search: '',
});
const response = await getAllRole(queryParams);
console.log('Fetched roles:', response);
if (response && response.data && response.data.data) {
setRoleList(response.data.data);
}
} catch (error) {
console.error('Error fetching roles:', error);
NotifAlert({
icon: 'error',
title: 'Error',
message: 'Gagal memuat daftar role',
});
} finally {
setLoadingRoles(false);
}
};
useEffect(() => { useEffect(() => {
const token = localStorage.getItem('token'); const token = localStorage.getItem('token');
if (token) { if (token) {
@@ -234,6 +266,9 @@ const DetailUser = (props) => {
setFormData(defaultData); setFormData(defaultData);
} }
setErrors({}); setErrors({});
// Fetch roles when modal opens
fetchRoles();
} }
}, [props.showModal]); }, [props.showModal]);
@@ -420,14 +455,16 @@ const DetailUser = (props) => {
value={FormData.role_id} value={FormData.role_id}
onChange={handleSelectChange} onChange={handleSelectChange}
disabled={props.readOnly} disabled={props.readOnly}
loading={loadingRoles}
style={{ width: '100%' }} style={{ width: '100%' }}
placeholder="Pilih role" placeholder={loadingRoles ? 'Memuat role...' : 'Pilih role'}
allowClear allowClear
> >
<Option value={1}>Administrator</Option> {roleList.map((role) => (
<Option value={2}>Operator</Option> <Option key={role.role_id} value={role.role_id}>
<Option value={3}>Engineer</Option> {role.role_name} (Level {role.role_level})
<Option value={4}>Guest</Option> </Option>
))}
</Select> </Select>
</div> </div>