Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Disable Regeneration button when there is no Filing history
Browse files Browse the repository at this point in the history
  • Loading branch information
meissadia committed Aug 5, 2020
1 parent 159430e commit 5167374
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
3 changes: 1 addition & 2 deletions cypress/integration/publication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ describe('HMDA Help', () => {
cy.get('@mlarRow').contains('td', 'Modified LAR')
cy.get('@mlarRow').contains('td', 'No file')
cy.get('@mlarRow').contains('td', 'Regenerate')
cy.findAllByText('Regenerate').eq(row).click()
cy.get('@mlarRow').contains('No Submissions exist for 2020')
cy.findAllByText('Regenerate').eq(row).should('have.class', 'disabled')

// Can generate Publication for past year
row = 3
Expand Down
65 changes: 32 additions & 33 deletions src/publications/PublicationRow.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useState, useEffect } from 'react'
import LoadingIcon from '../Loading'
import { LABELS, TOPICS } from './constants'
import { fetchData } from '../utils/api'
Expand All @@ -23,30 +23,34 @@ export const PublicationRow = ({
}) => {
const label = LABELS[type]
const topic = TOPICS[type]

const { lei, respondentName, activityYear: year } = institution
const latestURL = `/v2/filing/institutions/${lei}/filings/${year}/submissions/latest`
const headers = { Authorization: `Bearer ${token}` }

const [state, setState] = useState(defaultState)
const [seqNum, setSeqNum] = useState(null)

const updateState = newState => setState((oldState) => ({...oldState, ...newState }))
const saveError = message => updateState({ waiting: false, error: true, message})

// Determine if we are able to trigger a Regeneration
useEffect(() => {
fetchSequenceNumber(latestURL, { headers }, setSeqNum)
}, [headers, setSeqNum, latestURL])

const handleRegeneration = () => {
if (window.confirm(regenMsg(label))) {
const latestURL = `/v2/filing/institutions/${lei}/filings/${year}/submissions/latest`
const headers = { Authorization: `Bearer ${token}` }

updateState({ ...defaultState, waiting: true })

fetchLatestSubmission(latestURL, { headers }, saveError)
.then((json) =>
triggerRegeneration(saveError, updateState, {
json,
topic,
lei,
year,
label,
headers,
})
)
triggerRegeneration(saveError, updateState, {
seqNum,
topic,
lei,
year,
label,
headers,
})
}
}

Expand All @@ -70,35 +74,30 @@ export const PublicationRow = ({
error={state.error}
message={state.message}
waiting={state.waiting}
disabled={[null, undefined].indexOf(seqNum) > -1}
/>
</td>
</tr>
)
}

// Fetch the latest Filing to get the require Submission sequenceNumber
function fetchLatestSubmission(url, options, onError) {
// Sequence Number is required for Regeneration
function fetchSequenceNumber(url, options, setResult) {
return fetchData(url, options)
.then(({ error, response }) => {
if (error) {
onError('Error reaching the /latest endpoint')
return {}
}

return response.json()
})
.then(({ error, response }) => {
if (error) return {}
return response.json()
})
.then((json) => {
const sequenceNumber = json && json.id && json.id.sequenceNumber
setResult(sequenceNumber)
})
}

// Send a Kafka topic
function triggerRegeneration(onError, onSuccess, data) {
const { json, topic, lei, year, headers, label } = data
const sequenceNumber = json && json.id && json.id.sequenceNumber
const regenerationUrl = `/v2/admin/publish/${topic}/institutions/${lei}/filings/${year}/submissions/${sequenceNumber}`

if (!sequenceNumber) {
onError(`No Submissions exist for ${year}`)
return
}
const { seqNum, topic, lei, year, headers, label } = data
const regenerationUrl = `/v2/admin/publish/${topic}/institutions/${lei}/filings/${year}/submissions/${seqNum}`

// Trigger Publication regeneration
return fetchData(regenerationUrl, { method: 'POST', headers })
Expand Down
4 changes: 2 additions & 2 deletions src/publications/RegenerateButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import React from 'react'
import Message from './Message'
import LoadingIcon from '../Loading'

export const RegenerateButton = ({ onClick, error, message, waiting }) => {
export const RegenerateButton = ({ onClick, error, message, waiting, disabled }) => {
let cname = 'inputSubmit'
if(waiting) cname += ' disabled'
if(waiting || disabled) cname += ' disabled'

return (
<div className="regenerate-container">
Expand Down

0 comments on commit 5167374

Please sign in to comment.