From 5167374699b4354aa1ee89e0a044fa8ec9d24f0c Mon Sep 17 00:00:00 2001 From: Meissa Dia Date: Wed, 5 Aug 2020 13:57:23 -0600 Subject: [PATCH] Disable Regeneration button when there is no Filing history --- cypress/integration/publication.spec.js | 3 +- src/publications/PublicationRow.jsx | 65 ++++++++++++------------- src/publications/RegenerateButton.jsx | 4 +- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/cypress/integration/publication.spec.js b/cypress/integration/publication.spec.js index 87222c6..9a02861 100644 --- a/cypress/integration/publication.spec.js +++ b/cypress/integration/publication.spec.js @@ -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 diff --git a/src/publications/PublicationRow.jsx b/src/publications/PublicationRow.jsx index 556c1f3..c834deb 100644 --- a/src/publications/PublicationRow.jsx +++ b/src/publications/PublicationRow.jsx @@ -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' @@ -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, + }) } } @@ -70,35 +74,30 @@ export const PublicationRow = ({ error={state.error} message={state.message} waiting={state.waiting} + disabled={[null, undefined].indexOf(seqNum) > -1} /> ) } -// 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 }) diff --git a/src/publications/RegenerateButton.jsx b/src/publications/RegenerateButton.jsx index 76bcd0c..a8066c7 100644 --- a/src/publications/RegenerateButton.jsx +++ b/src/publications/RegenerateButton.jsx @@ -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 (