diff --git a/frontend/src/pages/binding/create/[contractIfricId].tsx b/frontend/src/pages/binding/create/[contractIfricId].tsx index c434a16..8e32d82 100644 --- a/frontend/src/pages/binding/create/[contractIfricId].tsx +++ b/frontend/src/pages/binding/create/[contractIfricId].tsx @@ -12,7 +12,7 @@ import 'primereact/resources/primereact.min.css'; import 'primeicons/primeicons.css'; import "../../../styles/add-binding.css"; import { getCompanyDetailsById, verifyCompanyCertificate } from '../../../utility/auth'; -import { getTemplateByName, getCompanyCertificate, getContractByType, getAssetByType, getAssetCertificateById, createBinding } from '@/utility/contract'; +import { getTemplateByName, getCompanyCertificate, getContractByType, getAssetManagementData, getAssetCertificateById, createBinding } from '@/utility/contract'; import { Dropdown } from 'primereact/dropdown'; import moment from 'moment'; import Navbar from '@/components/navBar/navbar'; @@ -173,12 +173,17 @@ const CreateBinding: React.FC = () => { setSelectedAssetProperties(selectedProperties); // fetch assets of template type - const assetResponse = await getAssetByType(btoa(template.properties.asset_type.default)); - if(assetResponse?.data) { - const options = assetResponse.data.map((value: { id: string }) => ({ - label: value.id, - value: value.id - })); + const assetResponse = await getAssetManagementData(userData.company_ifric_id, template.properties.asset_type.default); + if(assetResponse) { + const options = assetResponse.map((value: Record) => { + const productKey = Object.keys(value).find(key => key.includes('product_name')); + if(productKey && value[productKey]) { + return { + label: value[productKey].value, + value: value.id + } + } + }); setAssetOptions(options); } } @@ -223,12 +228,12 @@ const CreateBinding: React.FC = () => { } } } else { - setAssetVerified(null); + setAssetVerified(false); toast.current?.show({ severity: 'warn', summary: 'Warn', detail: "Asset Certificate not found" }); } } } catch (error) { - setAssetVerified(null); + setAssetVerified(false); console.error('Error fetching data:', error); toast.current?.show({ severity: 'error', summary: 'Error', detail: 'Failed to load necessary data' }); } diff --git a/frontend/src/utility/contract.ts b/frontend/src/utility/contract.ts index e91f6e8..b880159 100644 --- a/frontend/src/utility/contract.ts +++ b/frontend/src/utility/contract.ts @@ -342,4 +342,32 @@ export const getAllContractByAssetType = async () => { throw error; } } -}; \ No newline at end of file +}; + +export const getAssetManagementData = async (company_ifric_id: string, type: string) => { + try { + const result: Record = []; + const response = await api.get( + `${BACKEND_API_URL}/asset/asset-management/${company_ifric_id}`, { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + }, + }); + if(response.data) { + response.data.forEach((value: Record) => { + if(value.type === type) { + result.push(value); + } + }) + } + return result; + } catch (error: any) { + if (error?.response && error?.response?.status === 401) { + updatePopupVisible(true); + return null; + } else { + throw error; + } + } +} \ No newline at end of file