Skip to content

Commit

Permalink
Merge pull request #108 from amosproj/contract-info-provider
Browse files Browse the repository at this point in the history
Added information about contract agreements
  • Loading branch information
daku-de authored Jul 16, 2024
2 parents 334304a + 3ed91b7 commit f9581d5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
21 changes: 20 additions & 1 deletion src/frontend/actions/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export async function negotiateContract(item: CatalogItem) {
}
}

interface ContractAgreement {
export interface ContractAgreement {
providerId: string;
[key: string]: any;
}
Expand All @@ -371,6 +371,25 @@ export async function getNegotiatedContracts(counterpartyname: string) {
}
}

export async function getNegotiatedContractsAsProvider() {
try {
const response = await fetch("/api/getNegotiatedContracts", {
method: "GET"
});
if (!response.ok) {
throw new Error(`API call failed with status ${response.status}`);
}

const data: ContractAgreement[] = await response.json();

return data.filter(item => item.providerId === process.env.NEXT_PUBLIC_CONNECTOR_NAME);

} catch (err) {
console.error('Error getting contract agreement info: ', err);
throw new Error('Failed to get contract agreement info');
}
}

export async function getContractAgreementInfo(agreementId: string) {
try {
const response = await fetch("/api/getContractAgreementInfo", {
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/app/dashboard/download/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const DownloadPage: React.FC = () => {
<tbody className="bg-white divide-y divide-gray-200">
{catalogItems.map((item) => (
<tr key={item.id}>
<td className="px-6 py-4 text-sm text-gray-900 break-words">{item.title}</td>
<td className="px-6 py-4 text-sm text-gray-900 break-words font-bold">{item.title}</td>
<td className="px-6 py-4 text-sm font-medium text-gray-500 break-words">{item.name}</td>
<td className="px-6 py-4 text-sm text-gray-500">{item.size}</td>
<td className="px-6 py-4 text-sm text-gray-500">{item.date}</td>
Expand Down Expand Up @@ -188,7 +188,7 @@ const DownloadPage: React.FC = () => {
<tbody className="bg-white divide-y divide-gray-200">
{negotiatedContracts.map((item) => (
<tr key={item.id}>
<td className="px-6 py-4 text-sm text-gray-900 break-words">{item.title}</td>
<td className="px-6 py-4 text-sm text-gray-900 break-words font-bold">{item.title}</td>
<td className="px-6 py-4 text-sm font-medium text-gray-500 break-words">{item.fileName}</td>
<td className="px-6 py-4 text-sm text-gray-500">{item.fileSize}</td>
<td className="px-6 py-4 text-sm text-gray-500">{item.date}</td>
Expand Down
54 changes: 51 additions & 3 deletions src/frontend/app/dashboard/upload/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React, { useState, useEffect } from 'react';

import { ArrowPathIcon, TrashIcon, CloudArrowDownIcon, XCircleIcon, CheckCircleIcon } from '@heroicons/react/24/outline';
import { createAsset, createContractDefinition, getAssets, uploadFile, getPolicies, fetchCatalogItems, deleteAsset, deleteContractDefinition, deleteFile, getContractDefinitions } from '@/actions/api';
import { createAsset, createContractDefinition, getAssets, uploadFile, getPolicies, fetchCatalogItems, deleteAsset, deleteContractDefinition, deleteFile, getContractDefinitions, getNegotiatedContractsAsProvider, ContractAgreement } from '@/actions/api';
import { FileInfo, Policy, Asset, CatalogItem } from "@/data/interface/file";
import PolicyDropdown from './PolicyDropdown';
import PolicyModal from './policyModal';
Expand All @@ -22,6 +22,8 @@ const UploadPage: React.FC = () => {
const [policies, setPolicies] = useState<Policy[]>([]);
const [isSubmitted, setIsSubmitted] = useState<boolean>(false);
const [hoveredItem, setHoveredItem] = useState<string | null>(null);
const [hoveredAgreementCount, setHoveredAgreementCount] = useState<string | null>(null);
const [contractAgreements, setContractAgreements] = useState<ContractAgreement[]>([]);

const [loadingAssets, setLoadingAssets] = useState<boolean>(false);

Expand All @@ -37,6 +39,13 @@ const UploadPage: React.FC = () => {
setHoveredItem(null);
}

const handleMouseEnterAgreement = (fileId: string) => {
setHoveredAgreementCount(fileId);
}
const handleMouseLeaveAgreement = () => {
setHoveredAgreementCount(null);
}

const getPolicyInfo = (policyId: string) => {
const policy = policies.find(policy => policy.id === policyId);
return policy || null;
Expand All @@ -55,6 +64,7 @@ const UploadPage: React.FC = () => {
setFiles(assets);
const ownContractDefinitions = await getContractDefinitions();
setContractDefinitions(ownContractDefinitions);
await fetchContractAgreements();
} catch (error) {
console.error('Error fetching assets:', error);
toast.error("There was an error fetching the assets");
Expand All @@ -63,6 +73,32 @@ const UploadPage: React.FC = () => {
}
};

const fetchContractAgreements = async () => {
try {
setContractAgreements(await getNegotiatedContractsAsProvider());
} catch (error) {
console.error("Error fetching negotiated contracts: ", error);
toast.error("There was an error fetching the contract agreements");
}

}

const countContracts = (assetId: string) => {
let count = 0
for (const agreement of contractAgreements) {
if (agreement.assetId === assetId) count++;
}
return count;
}

const getAgreementConsumerIds = (assetId: string) => {
const consumerIdSet = new Set<string>();
for (const agreement of contractAgreements) {
if (agreement.assetId === assetId) consumerIdSet.add(agreement.consumerId);
}
return Array.from(consumerIdSet);
}

const deleteCallback = async (asset: Asset) => {
try {
await deleteContractDefinition("contract-" + asset.id)
Expand Down Expand Up @@ -209,7 +245,7 @@ const UploadPage: React.FC = () => {
<table className="min-w-full divide-y divide-gray-200">
<thead className="bg-gray-50">
<tr>
{['Title', 'Name', 'Size', 'Date', 'Author', 'Content Type', 'Offered', 'Actions'].map((label) => (
{['Title', 'Name', 'Size', 'Date', 'Author', 'Content Type', 'Negotiated', 'Offered', 'Actions'].map((label) => (
<th key={label} className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
{label}
</th>
Expand All @@ -221,12 +257,24 @@ const UploadPage: React.FC = () => {
const policy = getPolicyFromContract("contract-" + file.id);
return (
<tr key={file.id}>
<td className="px-6 py-4 text-sm text-gray-900 break-words">{file.title}</td>
<td className="px-6 py-4 text-sm text-gray-900 break-words font-bold">{file.title}</td>
<td className="px-6 py-4 text-sm font-medium text-gray-500 break-words">{file.name}</td>
<td className="px-6 py-4 text-sm text-gray-500">{file.size}</td>
<td className="px-6 py-4 text-sm text-gray-500">{file.date}</td>
<td className="px-6 py-4 text-sm text-gray-500">{file.author}</td>
<td className="px-6 py-4 text-sm text-gray-500">{file.contenttype}</td>
<td className={`px-6 py-4 text-sm text-gray-500 relative text-center ${countContracts(file.id) !== 0 ? "font-bold" : ""}`} onMouseEnter={() => handleMouseEnterAgreement(file.id)} onMouseLeave={() => handleMouseLeaveAgreement()}>
{countContracts(file.id)}

{ (hoveredAgreementCount === file.id && countContracts(file.id) !== 0) && (
<div className="absolute left-0 mt-2 text-left">
<div className="fixed mt-2 -translate-x-1/2 w-64 p-2 bg-white border border-gray-300 rounded shadow-lg z-10 ml-12">
<div className="text-xs text-gray-300 font-bold">AGREED WITH</div>
<div className="font-bold text-black text-lg">{getAgreementConsumerIds(file.id).join(", ")}</div>
</div>
</div>
)}
</td>
<td className="px-6 py-4 text-sm text-gray-500 relative">
{isOffered(file.id) ? (

Expand Down

0 comments on commit f9581d5

Please sign in to comment.