Skip to content

Commit 18aa081

Browse files
committed
get allPOs to show up in uuid component
1 parent 4c30cfb commit 18aa081

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

pages/requests/[uuid].js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useState, useEffect } from 'react'
22
import { useRouter } from 'next/router'
33
import { useSession } from 'next-auth/react'
44
import {
@@ -37,14 +37,30 @@ const Request = () => {
3737
const { uuid } = router.query
3838
const { request, isLoadingRequest, isRequestError } = useOneRequest(uuid, session?.accessToken)
3939
const { allSOWs, isLoadingSOWs, isSOWError } = useAllSOWs(uuid, request?.identifier, session?.accessToken)
40-
const { allPOs, isLoadingPOs, isPOError } = useAllPOs(request?.quotedWareID, uuid, request?.identifier, session?.accessToken)
41-
//console.log(allPOs, 'uuid.js all POS')
4240
const { messages, isLoadingMessages, isMessagesError, mutateMessages, messagesData } = useMessages(uuid, session?.accessToken)
4341
const { files, isLoadingFiles, isFilesError, mutateFiles, filesData } = useFiles(uuid, session?.accessToken)
44-
const documents = (allSOWs) ? [...allSOWs] : []
4542

46-
const isLoading = isLoadingRequest || isLoadingSOWs || isLoadingFiles || isLoadingMessages
43+
const [allPOs, setAllPOs] = useState(false)
44+
const [isPOError, setIsPOError] = useState(false)
45+
const [isPOLoading, setIsPOLoading] = useState(false)
46+
useEffect(() => {
47+
const getPOsAsync = async () => {
48+
const response = await useAllPOs(request?.quotedWareID, uuid, request?.identifier, session?.accessToken)
49+
if (response.allPOs) {
50+
setAllPOs(response.allPOs)
51+
} else if (response.isPOError) {
52+
setIsPOError(response.isPOError)
53+
} else {
54+
setIsPOLoading(true)
55+
}
56+
}
57+
getPOsAsync()
58+
}, [allPOs]);
59+
60+
console.log(allPOs)
61+
const isLoading = isLoadingRequest || isLoadingSOWs || isLoadingFiles || isLoadingMessages || isPOLoading
4762
const isError = isRequestError || isSOWError || isFilesError|| isMessagesError
63+
const documents = (allSOWs) ? [...allSOWs] : []
4864

4965
if (isLoading) return <Loading wrapperClass='item-page mt-5' />
5066

utils/api/configurations.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ export const configureFiles = (data) => {
186186
}
187187

188188
export const configureDocuments = (documents, requestIdentifier) => {
189-
//lineitems, shipto, shipfrom, and terms are only in SOWs
190-
// POs will need a SOW identifier to match up and get data from the correct SOW
191189
return documents?.map(document => ({
192190
identifier: document.identifier,
193191
date: normalizeDate(document.created_at),
@@ -210,12 +208,14 @@ export const configureDocuments = (documents, requestIdentifier) => {
210208
organizationName: document.ship_from?.organization_name,
211209
text: document.ship_from?.text,
212210
},
211+
// the following properties only need to exist on POs
212+
turnaroundTime: document.turn_around_time.human || '',
213+
poNumber: document.po_number || '',
214+
relatedSOWIdentifier: document.proposal_ref?.identifier || '',
215+
adPO: document.scientist_identifier,
213216
}))
214217
}
215218

216-
// export const configurePOs = (POs, requestIdentifier, relatedSOWIdentifier) => {
217-
218-
// }
219219

220220
const configureLineItems = (lineItems) => (lineItems.map(lineItem => ({
221221
id: lineItem.id,

utils/api/requests.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,24 @@ export const useAllSOWs = (id, requestIdentifier, accessToken) => {
5555
}
5656
}
5757

58-
export const useAllPOs = (quotedWareId, uuid, requestIdentifier, accessToken) => {
59-
const { data: allPOData, error: allPOErrors } = useSWR(quotedWareId ? [`quote_groups/${uuid}/quoted_wares/${quotedWareId}/purchase_orders.json`, accessToken] : null)
58+
// TODO(summer-cook): eventually we can use the useSWRList hook here instead of mapping & calling the fetcher. This hook is actively being contributed to the swr repo, but the semantics of the work are still being debated. see https://github.com/vercel/swr/discussions/1988 for the RFC and https://github.com/vercel/swr/pull/2047 for the PR.
59+
export const useAllPOs = async (quotedWareId, uuid, requestIdentifier, accessToken) => {
6060
let allPOs
61-
let arrayOfPOIds = []
6261
let enhancedPOArray = []
63-
allPOData && allPOData.map((po) => {arrayOfPOIds.push(po.id)})
64-
arrayOfPOIds && arrayOfPOIds.map( async (poId) => {
65-
let onePOData = await fetcher(`quote_groups/${uuid}/quoted_wares/${quotedWareId}/purchase_orders/${poId}.json`, accessToken)
66-
enhancedPOArray.push(onePOData)
67-
})
68-
// need to still configure POs to make sure they are returning all the right stuff, also need to figure out how best to use loading and error in this call
69-
if (enhancedPOArray.length) {
70-
allPOs = configurePOs(enhancedPOArray, requestIdentifier)
71-
}
72-
return {
73-
allPOs,
74-
//isLoadingPOs: !error && !data,
75-
//isPOError: error,
62+
let arrayOfPOIds = []
63+
try {
64+
const allPOData = await fetcher(`quote_groups/${uuid}/quoted_wares/${quotedWareId}/purchase_orders.json`, accessToken)
65+
allPOData && allPOData.map((po) => {arrayOfPOIds.push(po.id)})
66+
arrayOfPOIds && await Promise.all(arrayOfPOIds.map(async (poId) => {
67+
const onePOData = await fetcher(`quote_groups/${uuid}/quoted_wares/${quotedWareId}/purchase_orders/${poId}.json`, accessToken)
68+
return enhancedPOArray.push(onePOData)
69+
}))
70+
allPOs = configureDocuments(enhancedPOArray, requestIdentifier)
71+
return { allPOs }
72+
} catch (error) {
73+
return {
74+
isPOError: error
75+
}
7676
}
7777
}
7878

0 commit comments

Comments
 (0)