Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Exploratory] IRS File Proxy redesign #1720

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/filing/api/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export function fetchData(options = { method: 'GET' }) {
options.querystring = createQueryString(options.params)
}

const url = makeUrl(options)
const url = options.url || makeUrl(options)

if (typeof options.body === 'object' && !isFormData)
options.body = JSON.stringify(options.body)

Expand Down Expand Up @@ -60,10 +61,12 @@ export function fetchData(options = { method: 'GET' }) {
}

return fetch(url, fetchOptions)
.then(response => {
return new Promise(resolve => {
log('got res', response, response.status)
.then(response => {
return new Promise(resolve => {
if (response.status === 401 || response.status === 403) login()
if (fetchOptions.method === 'HEAD') {
return resolve(response.status === 200) // 304?
}
if (response.status > 399) return resolve(response)
if (options.params && options.params.format === 'csv') {
return resolve(response.text())
Expand Down
65 changes: 65 additions & 0 deletions src/filing/submission/irs/DownloadIRS.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { fetchData } from '../../api/fetch.js'
import { IRS } from '../../../common/s3/fileProxy.js'
import FileSaver from 'file-saver'
import { GoCloudDownload } from 'react-icons/go'
import { IoWarningOutline } from 'react-icons/io5'

/**
* Download an Institution's IRS file (CSV)
* @param {String} period YearQuarter
* @param {String} lei Institution identifier
*/
const download = (period, lei) => {
const options = {
url: IRS.buildURL(period, lei),
params: {
format: 'csv',
},
}

// Endpoint requires Authentication and the fetchData method encapsulates that
fetchData(options).then(data => {
console.log('Response: ', data)

if (data && typeof data !== 'object') {
console.log('Saving file!')
FileSaver.saveAs(
new Blob([data], { type: 'text/csv;charset=utf-16' }),
`${period}-${lei}-nationwide-irs.csv`
)
}
})
}

const DownloadIRS = ({ period, lei, isReady, setIrsReady }) => {
if (!isReady) return (
<p className='download-irs' onClick={() => setIrsReady(true)}>
<IoWarningOutline />
<span style={{ marginLeft: '5px' }}>
{' '}
When ready, the IRS will be available for download here.
</span>
</p>
)

return (
<p className='flex-inline'>
<button
className='download-irs ready'
disabled={!isReady}
onClick={() => setIrsReady(false)}
>
<GoCloudDownload />
</button>
<button
className='download-irs ready'
disabled={!isReady}
onClick={() => download(period, lei)}
>
<span style={{ marginLeft: '5px' }}>Download your IRS here</span>
</button>
</p>
)
}

export default DownloadIRS
10 changes: 10 additions & 0 deletions src/filing/submission/irs/IRSReport.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@
.IRSReport button:hover {
color: #205493;
}

.IRSReport .download-irs {
display: flex;
align-items: center;
}

.IRSReport .flex-inline {
display:flex;
flex-direction: row;
}
45 changes: 31 additions & 14 deletions src/filing/submission/irs/index.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import React from 'react'
import PropTypes from 'prop-types'
import React, { useEffect, useState } from 'react'
import { isBeta } from '../../../common/Beta.jsx'
import { IRS } from '../../../common/s3/fileProxy.js'
import DownloadIRS from './DownloadIRS.jsx'
import { fetchData } from '../../api/fetch.js'

import './IRSReport.css'

// Check if IRS file exists
// TODO: Needs Proxy service to support HEAD
const checkStatusIRS = (period, lei, handleResponse) => {
const options = {
method: 'HEAD',
url: IRS.buildURL(period, lei),
}

fetchData(options).then(data => {
console.log('checkStatusIRS Response: ', data)
handleResponse(data)
})
}

const IRSReport = props => {
const filingPeriod=props.filingPeriod
const filingPeriod = props.filingPeriod
const [irsReady, setIrsReady] = useState(false)

useEffect(() => {
checkStatusIRS(filingPeriod, props.lei, setIrsReady)
}, [])

if(isBeta()) return null
return (
<section className="IRSReport">
Expand All @@ -17,18 +40,12 @@ const IRSReport = props => {
will not be available immediately. Please check back shortly after
submitting your data to access your IRS.
</p>
<p>
When ready, the IRS will be available for{' '}
<a
href={`https://s3.amazonaws.com/cfpb-hmda-public/prod/reports/disclosure/${filingPeriod}/${
props.lei
}/nationwide/IRS.csv`}
download={true}
>
download here
</a>
.
</p>
<DownloadIRS
period={filingPeriod}
lei={props.lei}
isReady={irsReady}
setIrsReady={setIrsReady}
/>
<p className="text-small">
Loan amounts in the IRS are binned and disclosed in accordance
with the 2018 HMDA data publication policy guidance. An overview
Expand Down