Skip to content

Commit a5b1f02

Browse files
authored
Merge pull request #260 from scientist-softserv/138-display-pos-refactor
138 display pos refactor
2 parents a5c1e58 + b69fcfa commit a5b1f02

File tree

3 files changed

+59
-51
lines changed

3 files changed

+59
-51
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: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,37 +185,45 @@ export const configureFiles = (data) => {
185185
return allFiles
186186
}
187187

188-
export const configureDocuments = (documents, requestIdentifier) => {
189-
return documents?.map(document => ({
190-
identifier: document.identifier,
188+
export const configureDocument = (document, requestIdentifier) => {
189+
return {
191190
date: normalizeDate(document.created_at),
192191
documentStatus: document.status,
193192
documentStatusColor: statusColors[configureStatus(document.status)].bg,
194193
documentType: document.type,
195194
documentTypeColor: 'bg-dark',
195+
identifier: document.identifier,
196196
lineItems: configureLineItems(document.line_items),
197197
requestIdentifier,
198-
subtotalPrice: document.retail_subtotal_price_currency,
199-
taxAmount: document.tax_cost_currency,
200-
terms: document.payment_terms,
201-
totalPrice: document.retail_total_price_currency,
202198
shippingPrice: document.shipping_cost_currency,
203199
shipTo: {
204-
organizationName: document.ship_to?.organization_name,
205-
text: document.ship_to?.text,
200+
organizationName: document.ship_to.organization_name,
201+
text: document.ship_to.text,
206202
},
207203
shipFrom: {
208-
organizationName: document.ship_from?.organization_name,
209-
text: document.ship_from?.text,
204+
organizationName: document.ship_from.organization_name,
205+
text: document.ship_from.text,
210206
},
211-
// the following properties only need to exist on POs
212-
turnaroundTime: document.turn_around_time.human || null,
213-
poNumber: document.po_number || null,
214-
relatedSOWIdentifier: document.proposal_ref?.identifier || null,
215-
adPO: document.scientist_identifier || null,
207+
subtotalPrice: document.retail_subtotal_price_currency,
208+
taxAmount: document.tax_cost_currency,
209+
terms: document.payment_terms,
210+
totalPrice: document.retail_total_price_currency,
211+
}
212+
}
213+
214+
export const configureSOWs = (sows, requestIdentifier) => {
215+
return sows?.map((sow) => ({
216+
...configureDocument(sow, requestIdentifier),
216217
}))
217218
}
218219

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+
})
219227

220228
const configureLineItems = (lineItems) => (lineItems.map(lineItem => ({
221229
id: lineItem.id,

utils/api/requests.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import useSWR from 'swr'
22
import {
33
configureFiles,
4-
configureDocuments,
54
configureDynamicFormSchema,
65
configureDynamicFormUiSchema,
76
configureMessages,
7+
configurePO,
88
configureRequests,
9+
configureSOWs,
910
} from './configurations'
1011
import { fetcher, posting, updating } from './base'
1112

@@ -45,7 +46,7 @@ export const useAllSOWs = (id, requestIdentifier, accessToken) => {
4546
const { data, error } = useSWR(id ? [`/quote_groups/${id}/proposals.json`, accessToken] : null)
4647
let allSOWs
4748
if (data) {
48-
allSOWs = configureDocuments(data, requestIdentifier)
49+
allSOWs = configureSOWs(data, requestIdentifier)
4950
}
5051

5152
return {
@@ -55,27 +56,29 @@ export const useAllSOWs = (id, requestIdentifier, accessToken) => {
5556
}
5657
}
5758

58-
// The name of this function is getAllPOs vs. useAllPOs.
59-
// Since it is async, it is technically not a custom hook and according to linter should not start with use.
59+
// The name of this function is getAllPOs vs. useAllPOs. Since it is not a hook, it should not start with "use".
6060
export const getAllPOs = async (quotedWareId, uuid, requestIdentifier, accessToken) => {
61-
let allPOs
62-
let enhancedPOArray = []
63-
let arrayOfPOIds = []
6461
try {
6562
// TODO(summer-cook): eventually we can use the useSWRList hook here instead of mapping & calling the fetcher.
6663
// This hook is actively being contributed to the swr repo, but the semantics of the work are still being debated.
6764
// 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 }
65+
const data = await fetcher(`quote_groups/${uuid}/quoted_wares/${quotedWareId}/purchase_orders.json`, accessToken)
66+
const configuredPOs = data.map(async (po) => {
67+
const purchaseOrder = await fetcher(`quote_groups/${uuid}/quoted_wares/${quotedWareId}/purchase_orders/${po.id}.json`, accessToken)
68+
return configurePO(purchaseOrder, requestIdentifier)
69+
})
70+
const allPOs = await Promise.all(configuredPOs).then(res => res)
71+
72+
return {
73+
allPOs,
74+
isLoadingPOs: !allPOs,
75+
isPOError: false,
76+
}
7677
} catch (error) {
7778
return {
78-
isPOError: error
79+
allPOs: [],
80+
isLoadingPOs: false,
81+
isPOError: error,
7982
}
8083
}
8184
}

0 commit comments

Comments
 (0)