Skip to content

Commit

Permalink
Further refactor how document dates passed
Browse files Browse the repository at this point in the history
  • Loading branch information
haworku committed Jul 20, 2023
1 parent 01dc15f commit 22d404f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import React, { useEffect, useState } from 'react'
import { Link } from '@trussworks/react-uswds'
import { NavLink } from 'react-router-dom'
import { NavLink, useOutletContext } from 'react-router-dom'
import dayjs from 'dayjs'
import { SubmissionDocument } from '../../../common-code/healthPlanFormDataType'
import styles from './UploadedDocumentsTable.module.scss'
import { usePreviousSubmission } from '../../../hooks'
import { SharedRateCertDisplay } from '../../../common-code/healthPlanFormDataType/UnlockedHealthPlanFormDataType'
import { DocumentTag } from './DocumentTag'
import { useDocument } from '../../../hooks/useDocument'
import { getDocumentKey } from '../../../documentHelpers/getDocumentKey'
import { DocumentDateLookupTableType } from '../../../documentHelpers/makeDocumentDateLookupTable'
import { SubmissionOutletContextType } from '../../../pages/SubmissionOutlet/SubmissionOutlet'
export type UploadedDocumentsTableProps = {
documents: SubmissionDocument[]
caption: string | null
documentDateLookupTable: DocumentDateLookupTableType,
packagesWithSharedRateCerts?: SharedRateCertDisplay[]
isSupportingDocuments?: boolean
documentCategory?: string // if this prop is not included, do not show category column
isEditing?: boolean
isCMSUser?: boolean
}

type DocumentWithLink = { url: string | null } & SubmissionDocument
// TODO - get the api to return documents in this state rather than frontend generating on demand
type DocumentWithS3Data = { url: string | null, s3Key: string | null } & SubmissionDocument

const isBothContractAndRateSupporting = (doc: SubmissionDocument) =>
doc.documentCategories.includes('CONTRACT_RELATED') &&
Expand Down Expand Up @@ -68,32 +66,53 @@ export const UploadedDocumentsTable = ({
documents,
caption,
documentCategory,
documentDateLookupTable,
packagesWithSharedRateCerts,
isSupportingDocuments = false,
isEditing = false,
isCMSUser,
}: UploadedDocumentsTableProps): React.ReactElement => {
const { documentDatesLookup } =
useOutletContext<SubmissionOutletContextType>()
const initialDocState = documents.map((doc) => ({
...doc,
url: null,
s3Key: null
}))
const { getDocumentsUrl } = useDocument()
const { getDocumentsWithS3KeyAndUrl } = useDocument()
const [refreshedDocs, setRefreshedDocs] =
useState<DocumentWithLink[]>(initialDocState)
useState<DocumentWithS3Data[]>(initialDocState)
const shouldShowEditButton = isEditing && isSupportingDocuments
const shouldShowAsteriskExplainer = refreshedDocs.some((doc) =>
isBothContractAndRateSupporting(doc)
)
const shouldHaveNewTag = (doc: SubmissionDocument) => {
const documentKey = getDocumentKey(doc)
if ( documentDateLookupTable[documentKey] === undefined || documentDateLookupTable.previousSubmissionDate === undefined){
return false // this is a document on a draft submission

// this is util needed to guard against passing in null or undefined to dayjs - we would get back today's date
const canDisplayDateAddedForDocument = (doc: DocumentWithS3Data) =>{
console.log(doc, doc.s3Key, documentDatesLookup[
doc.s3Key!
])
return doc.s3Key && documentDatesLookup[
doc.s3Key
]
}

const shouldHaveNewTag = (doc: DocumentWithS3Data) => {
if (!isCMSUser) {
return false // design requirement, don't show new tag to state users on review submit
}

if (!documentDatesLookup || !doc || !doc.s3Key) {
return false // this is a document with bad s3 data
}
const documentDate = documentDatesLookup?.[doc.s3Key]
const previousSubmissionDate = documentDatesLookup.previousSubmissionDate

if (!documentDate || !previousSubmissionDate) {
return false // this document is on an initial submission or not submitted yet
}
return (
isCMSUser &&
documentDateLookupTable['documentKey'] >
documentDateLookupTable.previousSubmissionDate
documentDate >
previousSubmissionDate
)
}

Expand Down Expand Up @@ -125,18 +144,22 @@ export const UploadedDocumentsTable = ({
)

useEffect(() => {
console.log(' in effect ')
const refreshDocuments = async () => {
const newDocuments = (await getDocumentsUrl(
const newDocuments = await getDocumentsWithS3KeyAndUrl(
documents,
'HEALTH_PLAN_DOCS'
)) as DocumentWithLink[]
) as DocumentWithS3Data[]

console.log('new documents', newDocuments)
if (newDocuments.length) {
console.log(newDocuments)
setRefreshedDocs(newDocuments)
}
}

void refreshDocuments()
}, [documents, getDocumentsUrl])
}, [documents, getDocumentsWithS3KeyAndUrl])
// Empty State
if (refreshedDocs.length === 0) {
return (
Expand Down Expand Up @@ -177,7 +200,7 @@ export const UploadedDocumentsTable = ({
<tbody>
{refreshedDocs.map((doc) => (
<tr key={doc.name}>
{doc.url ? (
{doc.url && doc.s3Key ? (
<td>
<DocumentTag
isNew={shouldHaveNewTag(doc)}
Expand All @@ -190,7 +213,7 @@ export const UploadedDocumentsTable = ({
target="_blank"
>
{isSupportingDocuments &&
isBothContractAndRateSupporting(doc)
isBothContractAndRateSupporting(doc)
? `*${doc.name}`
: doc.name}
</Link>
Expand All @@ -204,25 +227,27 @@ export const UploadedDocumentsTable = ({
</td>
)}
<td>
{documentDateLookupTable
{canDisplayDateAddedForDocument(doc) && !isEditing
? dayjs(
documentDateLookupTable[
getDocumentKey(doc)
]
).format('M/D/YY')
documentDatesLookup[
// can disable non-null here because we check in canDisplayDateAddedForDocument
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
doc.s3Key!
]
).format('M/D/YY')
: ''}
</td>
{documentCategory && <td>{documentCategory}</td>}
{showSharedInfo
? packagesWithSharedRateCerts && (
<td>
{linkedPackagesList({
unlinkDrafts: Boolean(isCMSUser),
packages:
packagesWithSharedRateCerts,
})}
</td>
)
<td>
{linkedPackagesList({
unlinkDrafts: Boolean(isCMSUser),
packages:
packagesWithSharedRateCerts,
})}
</td>
)
: null}
</tr>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
import { getAllDocuments } from './getAllDocuments'
import { getDocumentKey } from './getDocumentKey'

// DocumentDateLookupTableType - { S3 key string : date string for "date added" }
// DocumentDateLookupTableType - { document key string : date string for "date added" }
// see logic in getDocumentKey for how document key string is calculated
type DocumentDateLookupTableType = {
[key: string]: string
previousSubmissionDate: string | null,
[key: string]: string | null
}

// getDateAdded - picks out the submit info updatedAt date for a revision
Expand All @@ -23,7 +25,7 @@ const getDateAdded = (
function makeDocumentDateTable(
revisionsLookup: RevisionsLookupType
): DocumentDateLookupTableType {
const lookupTable: DocumentDateLookupTableType = {}
const lookupTable: DocumentDateLookupTableType = { previousSubmissionDate: null}
Object.keys(revisionsLookup).forEach(
(revisionId: string, index: number) => {
const revision = revisionsLookup[revisionId]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getAllDocuments } from './getAllDocuments'

// CurrentPreviousDocsLookup - array of document keys for currentDocuments and previousDocuments

type LookupListType = {
type DocumentKeyLookupType = {
currentDocuments: string[]
previousDocuments: string[]
}
Expand All @@ -19,7 +19,7 @@ const getKey = (s3URL: string) => {

function makeDocumentS3KeyLookup(
revisionsLookup: RevisionsLookupType
): LookupListType {
): DocumentKeyLookupType {
const currentDocumentsSet = new Set<string>()
const previousDocumentsSet = new Set<string>()

Expand Down Expand Up @@ -61,3 +61,4 @@ function makeDocumentS3KeyLookup(
}

export { makeDocumentS3KeyLookup }
export type {DocumentKeyLookupType}
9 changes: 6 additions & 3 deletions services/app-web/src/hooks/useDocument.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ type ParsedDocumentWithLinkType =

const useDocument = () => {
const { getKey, getURL } = useS3()

const getDocumentsUrl = useCallback(
const getDocumentsWithS3KeyAndUrl= useCallback(
async <T = Record<string, unknown>,>(
documents: Array<T & UnionDocumentType>,
bucket: BucketShortName
): Promise<Array<T & ParsedDocumentWithLinkType>> => {

return await Promise.all(
documents.map(async (doc) => {
const key = getKey(doc.s3URL)
if (!key)
return {
...doc,
s3Key: null,
url: null,
}

const documentLink = await getURL(key, bucket)
return {
...doc,
s3Key: key,
url: documentLink,
}
})
Expand All @@ -40,7 +42,8 @@ const useDocument = () => {
[getURL, getKey]
)

return { getDocumentsUrl }

return { getDocumentsWithS3KeyAndUrl }
}

export { useDocument }

0 comments on commit 22d404f

Please sign in to comment.