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

Add support for HTML meta tag output #31

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions src/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ body,
max-width: 100%;
overflow-x: scroll;
}

.output-format-fieldset {
margin-top: 0;
}
69 changes: 63 additions & 6 deletions src/pages/txt-generator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
Label,
TextInput,
FormGroup,
Fieldset,
Radio,
} from "@trussworks/react-uswds"
import Layout from "../layouts"

Expand All @@ -30,6 +32,27 @@ contact-email: ${contactEmail}`
)
.join("\n\n")

const metaTagOutput = (hospitals) =>
hospitals
.map((hospital, idx) => {
const prefix = "og:reg:hpt"
const suffix = hospitals.length > 1 ? `:${idx + 1}` : ``
const hospitalProperties = [
["location-name", hospital.name],
["source-page-url", hospital.sourcePageUrl],
["mrf-url", hospital.mrfUrl],
["contact-name", hospital.contactName],
["contact-email", hospital.contactEmail],
]
return hospitalProperties
.map(
([prop, value]) =>
`<meta property="${prefix}:${prop}${suffix}" content="${value}" />`
)
.join("\n")
})
.join("\n\n")

const removeIndex = (array, index) => [
...array.slice(0, index),
...array.slice(index + 1),
Expand All @@ -50,21 +73,29 @@ const TxtGenerator = () => {
const [state, setState] = useState({
hospitals: [{ ...baseHospital }],
downloadUrl: "",
outputFormat: "txt",
})

useEffect(() => {
new Clipboard("[data-clipboard-target]")
}, [])

useEffect(() => {
let blob = new Blob([txtFileOutput(state.hospitals)], {
type: "text/plain;charset=utf8",
})
let blob
if (state.outputFormat === "txt") {
blob = new Blob([txtFileOutput(state.hospitals)], {
type: "text/plain;charset=utf8",
})
} else if (state.outputFormat === "html") {
blob = new Blob([metaTagOutput(state.hospitals)], {
type: "text/html;charset=utf8",
})
}
setState({
...state,
downloadUrl: window.URL.createObjectURL(blob),
})
}, [state.hospitals])
}, [state.hospitals, state.outputFormat])

const updateHospital = (index, updatedHospital) => {
const hospitals = [...state.hospitals]
Expand Down Expand Up @@ -261,12 +292,34 @@ const TxtGenerator = () => {
>
{alertMessages}
</Alert>
<Fieldset
legend="Output format"
className="usa-form-group output-format-fieldset"
onChange={(e) => {
setState({ ...state, outputFormat: e.target.value })
}}
>
<Radio
id="output-format-txt"
name="output-format"
label="TXT"
value="txt"
checked={state.outputFormat === "txt"}
/>
<Radio
id="output-format-html"
name="output-format"
label="HTML meta tags"
value="html"
checked={state.outputFormat === "html"}
/>
</Fieldset>
<hr className="width-full margin-top-3 margin-bottom-3" />
<Grid className="generator-results-row" row>
<h3 className="margin-y-0">Results</h3>
<a
href={state.downloadUrl}
download="cms-hpt.txt"
download={`cms-hpt.${state.outputFormat}`}
className={
"usa-button margin-right-0" +
(alertTypes === "success" ? "" : " usa-button--disabled")
Expand All @@ -281,7 +334,11 @@ const TxtGenerator = () => {
</a>
</Grid>

<pre id="generator-output">{txtFileOutput(state.hospitals)}</pre>
<pre id="generator-output">
{state.outputFormat === "txt" && txtFileOutput(state.hospitals)}
{state.outputFormat === "html" &&
metaTagOutput(state.hospitals)}
</pre>
<br />
<h3 className="margin-bottom-0">
<u>Example #1 TXT File</u>
Expand Down
Loading