Adding permission on update transfertype

This commit is contained in:
bagusajisaputroo
2025-04-12 13:23:01 +07:00
parent 4afc55952f
commit 2ebf7c9a0a
2 changed files with 27 additions and 11 deletions

View File

@ -37,7 +37,7 @@ const TransferType = () => {
</div> </div>
<AddDialog /> <AddDialog />
<DeleteDialog /> <DeleteDialog />
{/* <EditDialog /> */} <EditDialog />
</Container> </Container>
</ManageTransferTypeContextProvider> </ManageTransferTypeContextProvider>

View File

@ -43,7 +43,7 @@ interface CustomerProps {
msisdn: string; msisdn: string;
} }
interface GroupProps { interface PermissionObject {
id: string; id: string;
name: string; name: string;
description: string; description: string;
@ -56,7 +56,7 @@ const EditDialog = () => {
useManageTransferTypeContext(); useManageTransferTypeContext();
const { reload } = useDataGrid(); const { reload } = useDataGrid();
const [wallets, setWallets] = useState<WalletProps[]>([]); const [wallets, setWallets] = useState<WalletProps[]>([]);
const [groups, setGroups] = useState<GroupProps[]>([]); const [groups, setGroups] = useState<PermissionObject[]>([]);
const { GetData, PutData } = useCallApi(); const { GetData, PutData } = useCallApi();
const [isSubmitting, setIsSubmitting] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false);
const parsedUser = getAuth()?.user; const parsedUser = getAuth()?.user;
@ -215,9 +215,13 @@ const EditDialog = () => {
const doUpdateTransferType = useCallback(async () => { const doUpdateTransferType = useCallback(async () => {
try { try {
const response = await PutData(`${API_URL}/transactiontype/update/${selectedTransferType}`, { // Create a clean copy of the form data with properly formatted permissions
...formField const formDataToSend = {
}); ...formField,
permission: formField.permission.filter(id => typeof id === 'string')
};
const response = await PutData(`${API_URL}/transactiontype/update/${selectedTransferType}`, formDataToSend);
if (response?.status) { if (response?.status) {
setAlert({ show: false, message: '' }); setAlert({ show: false, message: '' });
@ -299,7 +303,7 @@ const EditDialog = () => {
limit: 100, limit: 100,
page: 1, page: 1,
with_deleted: false, with_deleted: false,
order_field: 'wallets.name', order_field: 'Wallets.name',
order_direction: 'ASC', order_direction: 'ASC',
}); });
@ -321,8 +325,21 @@ const EditDialog = () => {
const fetchTransactionType = async () => { const fetchTransactionType = async () => {
try { try {
const response = await GetData(`${API_URL}/transactiontype/getdata/${selectedTransferType}`, {}); const response = await GetData(`${API_URL}/transactiontype/getdata/${selectedTransferType}`, {});
console.log(response);
if (response?.status) { if (response?.status) {
// Process permissions to ensure they're always string IDs
let permissionIds: string[] = [];
if (Array.isArray(response.data.permission)) {
permissionIds = response.data.permission.map((perm: any) => {
// Check if permission is an object or string
if (typeof perm === 'object' && perm !== null) {
return perm.id;
}
return perm;
});
}
setFormField((prev) => ({ setFormField((prev) => ({
...prev, ...prev,
name: response.data.name, name: response.data.name,
@ -335,7 +352,7 @@ const EditDialog = () => {
status_approval: response.data.status_approval, status_approval: response.data.status_approval,
type: response.data.type || '', type: response.data.type || '',
status: response.data.status, status: response.data.status,
permission: response.data.permission || [] permission: permissionIds
})); }));
} }
} catch (error) { } catch (error) {
@ -346,14 +363,13 @@ const EditDialog = () => {
}); });
} }
}; };
const timer = setTimeout(() => { const timer = setTimeout(() => {
fetchTransactionType(); fetchTransactionType();
}, 150); }, 150);
return () => clearTimeout(timer); return () => clearTimeout(timer);
}, [showEditDialog, selectedTransferType, GetData]); }, [showEditDialog, selectedTransferType, GetData]);
useEffect(() => { useEffect(() => {
if (showEditDialog === false) { if (showEditDialog === false) {
resetForm(); resetForm();