From 565e7bfa31f1ee077cd2e29fbc6ed6a3eeec52af Mon Sep 17 00:00:00 2001
From: Bogdan Fazakas <bogdan.fazakas@gmail.com>
Date: Thu, 26 May 2022 08:37:31 +0300
Subject: [PATCH] compute jobs refetch and reuse order

---
 src/@utils/accessDetailsAndPricing.ts         | 21 ++++++++++++++++++-
 .../Asset/AssetActions/Compute/index.tsx      |  9 +++++++-
 .../Profile/History/ComputeJobs/index.tsx     |  9 +++++++-
 3 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/src/@utils/accessDetailsAndPricing.ts b/src/@utils/accessDetailsAndPricing.ts
index 5512bf698..12db101a3 100644
--- a/src/@utils/accessDetailsAndPricing.ts
+++ b/src/@utils/accessDetailsAndPricing.ts
@@ -38,6 +38,13 @@ const tokensPriceQuery = gql`
         tx
         serviceIndex
         createdTimestamp
+        reuses(orderBy: createdTimestamp, orderDirection: desc) {
+          id
+          caller
+          createdTimestamp
+          tx
+          block
+        }
       }
       dispensers {
         id
@@ -103,6 +110,13 @@ const tokenPriceQuery = gql`
         tx
         serviceIndex
         createdTimestamp
+        reuses(orderBy: createdTimestamp, orderDirection: desc) {
+          id
+          caller
+          createdTimestamp
+          tx
+          block
+        }
       }
       dispensers {
         id
@@ -157,12 +171,17 @@ function getAccessDetailsFromTokenPrice(
   timeout?: number
 ): AccessDetails {
   const accessDetails = {} as AccessDetails
+
   if (tokenPrice && tokenPrice.orders && tokenPrice.orders.length > 0) {
     const order = tokenPrice.orders[0]
+    const reusedOrder =
+      order && order.reuses && order.reuses.length > 0 ? order.reuses[0] : null
+    console.log('order', order)
+    console.log('reusedOrder', reusedOrder)
     // asset is owned if there is an order and asset has timeout 0 (forever) or if the condition is valid
     accessDetails.isOwned =
       timeout === 0 || Date.now() / 1000 - order.createdTimestamp < timeout
-    accessDetails.validOrderTx = order.tx
+    accessDetails.validOrderTx = reusedOrder ? reusedOrder.tx : order.tx
   }
 
   // TODO: fetch order fee from sub query
diff --git a/src/components/Asset/AssetActions/Compute/index.tsx b/src/components/Asset/AssetActions/Compute/index.tsx
index 1ccee8cb8..d4bdb9ccc 100644
--- a/src/components/Asset/AssetActions/Compute/index.tsx
+++ b/src/components/Asset/AssetActions/Compute/index.tsx
@@ -102,6 +102,7 @@ export default function Compute({
     useState<OrderPriceAndFees>()
   const [isRequestingAlgoOrderPrice, setIsRequestingAlgoOrderPrice] =
     useState(false)
+  const [refatchJobs, setRefatchJobs] = useState(false)
   // const [isProviderFeeValid, setIsProviderFeeValid] = useState(false)
   const isComputeButtonDisabled =
     isJobStarting === true ||
@@ -372,6 +373,8 @@ export default function Compute({
       }
       LoggerInstance.log('[compute] Starting compute job response: ', response)
       setIsPublished(true)
+      console.log('setRefatchJobs true')
+      setRefatchJobs(!refatchJobs)
       initPriceAndFees()
     } catch (error) {
       setError('Failed to start job!')
@@ -461,7 +464,11 @@ export default function Compute({
       </footer>
       {accountId && asset?.accessDetails?.datatoken && (
         <AssetActionHistoryTable title="Your Compute Jobs">
-          <ComputeJobs minimal assetChainId={[asset?.chainId]} />
+          <ComputeJobs
+            minimal
+            assetChainId={[asset?.chainId]}
+            refatchJobs={refatchJobs}
+          />
         </AssetActionHistoryTable>
       )}
     </>
diff --git a/src/components/Profile/History/ComputeJobs/index.tsx b/src/components/Profile/History/ComputeJobs/index.tsx
index b70d2357d..0f4a23e75 100644
--- a/src/components/Profile/History/ComputeJobs/index.tsx
+++ b/src/components/Profile/History/ComputeJobs/index.tsx
@@ -71,10 +71,12 @@ const columns = [
 
 export default function ComputeJobs({
   minimal,
-  assetChainId
+  assetChainId,
+  refatchJobs
 }: {
   minimal?: boolean
   assetChainId?: number[]
+  refatchJobs?: boolean
 }): ReactElement {
   const { accountId } = useWeb3()
   const { asset } = useAsset()
@@ -110,6 +112,11 @@ export default function ComputeJobs({
     fetchJobs()
   }, [fetchJobs])
 
+  useEffect(() => {
+    console.log('use effect ', refatchJobs)
+    fetchJobs()
+  }, [refatchJobs])
+
   return accountId ? (
     <>
       {jobs?.length >= 0 && !minimal && (