Skip to content

Commit c02f649

Browse files
committed
refactor and simplify how we get and show our purchase orders
1 parent d3ecc85 commit c02f649

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

pages/requests/[uuid].js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,24 @@ const Request = () => {
4040
const { messages, isLoadingMessages, isMessagesError, mutateMessages, messagesData } = useMessages(uuid, session?.accessToken)
4141
const { files, isLoadingFiles, isFilesError, mutateFiles, filesData } = useFiles(uuid, session?.accessToken)
4242

43-
const [allPOs, setAllPOs] = useState(false)
43+
const [allPOs, setAllPOs] = useState([])
4444
const [isPOError, setIsPOError] = useState(false)
45-
const [isPOLoading, setIsPOLoading] = useState(false)
45+
const [isLoadingPOs, setIsLoadingPOs] = useState(true)
4646
useEffect(() => {
47-
const getPOsAsync = async () => {
48-
const response = await getAllPOs(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-
}
47+
if (request) {
48+
(async () => {
49+
const { allPOs, isLoadingPOs, isPOError } = await getAllPOs(request?.quotedWareID, uuid, request?.identifier, session?.accessToken)
50+
51+
setIsLoadingPOs(isLoadingPOs)
52+
setAllPOs(allPOs)
53+
setIsPOError(isPOError)
54+
})()
5655
}
57-
getPOsAsync()
58-
}, [allPOs, isPOError, request?.quotedWareID, uuid, request?.identifier, session?.accessToken])
59-
60-
const isLoading = isLoadingRequest || isLoadingSOWs || isLoadingFiles || isLoadingMessages || isPOLoading
61-
const isError = isRequestError || isSOWError || isFilesError|| isMessagesError
62-
let documents = (allSOWs) ? [...allSOWs] : []
63-
allPOs ? (documents = [...documents, ...allPOs]) : (documents = [...documents])
56+
}, [allPOs, isPOError, request, uuid, session])
57+
58+
const isLoading = isLoadingRequest || isLoadingSOWs || isLoadingFiles || isLoadingMessages || isLoadingPOs
59+
const isError = isRequestError || isSOWError || isFilesError|| isMessagesError || isPOError
60+
const documents = (allSOWs) ? [...allSOWs, ...allPOs] : []
6461

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

@@ -80,7 +77,7 @@ const Request = () => {
8077
if (isError) {
8178
return (
8279
<Notice
83-
alert={configureErrors([isRequestError, isSOWError, isMessagesError, isFilesError])}
80+
alert={configureErrors([isRequestError, isSOWError, isMessagesError, isFilesError, isPOError])}
8481
dismissible={false}
8582
withBackButton={true}
8683
buttonProps={{

utils/api/configurations.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ export const configureSOWs = (sows, requestIdentifier) => {
217217
}))
218218
}
219219

220+
export const configurePO = (po, requestIdentifier) => ({
221+
...configureDocument(po, requestIdentifier),
222+
turnaroundTime: po.turn_around_time.human,
223+
poNumber: po.po_number,
224+
relatedSOWIdentifier: po.proposal_ref?.identifier,
225+
adPO: po.scientist_identifier,
226+
})
220227

221228
const configureLineItems = (lineItems) => (lineItems.map(lineItem => ({
222229
id: lineItem.id,

utils/api/requests.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
configureDynamicFormSchema,
55
configureDynamicFormUiSchema,
66
configureMessages,
7+
configurePO,
78
configureRequests,
89
configureSOWs,
910
} from './configurations'
@@ -58,24 +59,24 @@ export const useAllSOWs = (id, requestIdentifier, accessToken) => {
5859
// The name of this function is getAllPOs vs. useAllPOs.
5960
// Since it is async, it is technically not a custom hook and according to linter should not start with use.
6061
export const getAllPOs = async (quotedWareId, uuid, requestIdentifier, accessToken) => {
61-
let allPOs
62-
let enhancedPOArray = []
63-
let arrayOfPOIds = []
6462
try {
65-
// TODO(summer-cook): eventually we can use the useSWRList hook here instead of mapping & calling the fetcher.
66-
// This hook is actively being contributed to the swr repo, but the semantics of the work are still being debated.
67-
// See https://github.com/vercel/swr/discussions/1988 for the RFC and https://github.com/vercel/swr/pull/2047 for the PR.
68-
const allPOData = await fetcher(`quote_groups/${uuid}/quoted_wares/${quotedWareId}/purchase_orders.json`, accessToken)
69-
allPOData && allPOData.map((po) => {arrayOfPOIds.push(po.id)})
70-
arrayOfPOIds && await Promise.all(arrayOfPOIds.map(async (poId) => {
71-
const onePOData = await fetcher(`quote_groups/${uuid}/quoted_wares/${quotedWareId}/purchase_orders/${poId}.json`, accessToken)
72-
return enhancedPOArray.push(onePOData)
73-
}))
74-
allPOs = configureDocuments(enhancedPOArray, requestIdentifier)
75-
return { allPOs }
63+
const data = await fetcher(`quote_groups/${uuid}/quoted_wares/${quotedWareId}/purchase_orders.json`, accessToken)
64+
const configuredPOs = data.map(async (po) => {
65+
const purchaseOrder = await fetcher(`quote_groups/${uuid}/quoted_wares/${quotedWareId}/purchase_orders/${po.id}.json`, accessToken)
66+
return configurePO(purchaseOrder, requestIdentifier)
67+
})
68+
const allPOs = await Promise.all(configuredPOs).then(res => res)
69+
70+
return {
71+
allPOs,
72+
isLoadingPOs: !allPOs,
73+
isPOError: false,
74+
}
7675
} catch (error) {
7776
return {
78-
isPOError: error
77+
allPOs: [],
78+
isLoadingPOs: false,
79+
isPOError: error,
7980
}
8081
}
8182
}

0 commit comments

Comments
 (0)