Skip to content

Commit

Permalink
Adding new docx download, closes #31. Adding risk register to JSON, C…
Browse files Browse the repository at this point in the history
…SV and DOCx downloads, closes #32. Removed sharing reason mapping, closes #33
  • Loading branch information
davetaz committed Aug 7, 2024
1 parent 6061458 commit 859a794
Show file tree
Hide file tree
Showing 11 changed files with 1,013 additions and 221 deletions.
46 changes: 23 additions & 23 deletions client/src/DataCapture.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function DataCapture() {

setFormData(prevFormData => {
const newFormData = { ...prevFormData, [name]: { ...prevFormData[name], value } };
if (name === "sharing_reason" && value === "8") { // "Other" option is selected
if (name === "sharing_reason" && value === "Other") { // "Other" option is selected
newFormData["sharing_reason_details"].required = true;
} else if (name === "sharing_reason") {
newFormData["sharing_reason_details"].required = false;
Expand Down Expand Up @@ -86,9 +86,9 @@ export default function DataCapture() {
<div className={`radios ${formErrors[key] ? 'error' : ''}`} key={key}>
<legend>{field.label}</legend>
{field.options.map(option => (
<div className="form-element-checkbox" key={option.value}>
<input id={`opt${option.value}`} type="radio" name={key} value={option.value} checked={formData[key].value === option.value} onChange={handleChange} />
<label htmlFor={`opt${option.value}`}>{option.label}</label>
<div className="form-element-checkbox" key={option}>
<input id={`opt${option}`} type="radio" name={key} value={option} checked={formData[key].value === option} onChange={handleChange} />
<label htmlFor={`opt${option}`}>{option}</label>
</div>
))}
<div className="error">{formErrors[key] ? "This field is required" : ""}</div>
Expand All @@ -100,24 +100,24 @@ export default function DataCapture() {
};

return (
<div className="checkpoint-question">
<div className="form-title">About the Data</div>
<form className="form" onSubmit={handleSubmit}>
{metadata.form.map((fieldset, index) => (
<fieldset key={index} className="form-column">
{fieldset.items.map(field => renderField(field, metadata.schema[field]))}
</fieldset>
))}
<button
type="submit"
className="button button-white"
>
Next
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M8.30469 1.51733L16.9944 9.79311L8.71865 18.4828" stroke="#2254F4" strokeWidth="3" />
</svg>
</button>
</form>
</div>
<div className="checkpoint-question">
<div className="form-title">About the Data</div>
<form className="form" onSubmit={handleSubmit}>
{metadata.form.map((fieldset, index) => (
<fieldset key={index} className="form-column">
{fieldset.items.map(field => renderField(field, metadata.schema[field]))}
</fieldset>
))}
<button
type="submit"
className="button button-white"
>
Next
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M8.30469 1.51733L16.9944 9.79311L8.71865 18.4828" stroke="#2254F4" strokeWidth="3" />
</svg>
</button>
</form>
</div>
);
}
15 changes: 14 additions & 1 deletion client/src/DownloadReportButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@ import axiosInstance from './axiosInstance';
export default function DownloadReportButton({ assessmentId }) {
const handleDownloadReport = async (format) => {
try {
const contentType = format === 'docx'
? 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
: format === 'csv'
? 'text/csv'
: 'application/json';

if (format === 'docx') {
setTimeout(() => {
alert("DOCX will download soon after you close this message.\n\nOnce the document has downloaded, you will need to update the table of contents to correct page numbering and titles.");
}, 100); // Small delay to ensure the request is initiated before showing the alert
}

const response = await axiosInstance.get(`/assessments/${assessmentId}/report`, {
headers: {
Accept: format === 'csv' ? 'text/csv' : 'application/json'
Accept: contentType
},
responseType: 'blob' // Important for downloading files
});
Expand Down Expand Up @@ -37,6 +49,7 @@ export default function DownloadReportButton({ assessmentId }) {
<div className="buttons-container">
<button className="button button-white" onClick={() => handleDownloadReport('json')}>Download JSON</button>
<button className="button button-white" onClick={() => handleDownloadReport('csv')}>Download CSV</button>
<button className="button button-white" onClick={() => handleDownloadReport('docx')}>Download DOCX</button>
</div>
</div>
</div>
Expand Down
24 changes: 17 additions & 7 deletions client/src/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ import DownloadReportButton from './DownloadReportButton';
import TopRisksSidebar from './TopRisksSidebar';
import RiskClassificationChart from './RiskClassificationChart';
import { updateActiveCheckpointIndex, updateActiveCheckpoint } from './checkpointsSlice';
import metadata from './json/metadata.json';

export default function Report() {
useParams();
const navigate = useNavigate();
const dispatch = useDispatch();
const { activeAssessment, checkpoints } = useSelector((state) => state.checkpoints);
const form_data = activeAssessment.data_capture || {};
const sharingReasonOptions = metadata.schema.sharing_reason.options.reduce((acc, option) => {
acc[option.value] = option.label;
return acc;
}, {});

if (!activeAssessment || !checkpoints.length) {
return <div>Loading...</div>;
Expand All @@ -29,8 +24,23 @@ export default function Report() {
<div className="assessment-data">
{form_data.dataset_name ? <DataListItem value={form_data.dataset_name.value} label="Name of Data Set" /> : ""}
{form_data.dataset_description ? <DataListItem value={form_data.dataset_description.value} label="Description" /> : ""}
{form_data.sharing_reason ? <DataListItem value={sharingReasonOptions[form_data.sharing_reason.value]} label="Reason for Sharing" /> : ""}
{form_data.sharing_reason_details && form_data.sharing_reason.value === "8" ? <DataListItem value={form_data.sharing_reason_details.value} label="More Details" /> : ""}
{form_data.sharing_reason ? (
<>
<DataListItem
value={
form_data.sharing_reason.value === "Other"
? form_data.sharing_reason_details.value
: form_data.sharing_reason.value
}
label="Reason for Sharing"
/>
{form_data.sharing_reason.value !== "Other" && form_data.sharing_reason_details && (
<DataListItem value={form_data.sharing_reason_details.value} label="More Details" />
)}
</>
) : (
""
)}
</div>
{activeAssessment.answers.length ? <AnswersList answers={activeAssessment.answers} /> : ""}
</div>
Expand Down
4 changes: 0 additions & 4 deletions client/src/assets/scss/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,6 @@ h1 {
}






.template-healthcheck {
.template-inner {
@media (min-width: 768px){
Expand Down
94 changes: 47 additions & 47 deletions client/src/json/metadata.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
{
"schema": {
"dataset_name": {
"type": "text",
"label": "Name/Identifier of the dataset",
"required": true
},
"dataset_description": {
"type": "textarea",
"label": "What is the data about?",
"required": false
},
"sharing_reason": {
"type": "radio",
"label": "Why are you sharing the data",
"required": true,
"options": [
{ "value": "1", "label": "Encourage Innovation" },
{ "value": "2", "label": "Optimise supply chains" },
{ "value": "3", "label": "Address common challenges across a sector" },
{ "value": "4", "label": "Improve market reach" },
{ "value": "5", "label": "Benchmarking and insights" },
{ "value": "6", "label": "To comply with regulation or legislation (e.g. freedom of information)" },
{ "value": "7", "label": "Demonstrate trustworthiness" },
{ "value": "8", "label": "Other" }
]
},
"sharing_reason_details": {
"type": "textarea",
"label": "Please provide more detail",
"required": false
}
"schema": {
"dataset_name": {
"type": "text",
"label": "Name/Identifier of the dataset",
"required": true
},
"form": [
{
"type": "fieldset",
"items": [
"dataset_name",
"dataset_description"
]
},
{
"type": "fieldset",
"items": [
"sharing_reason",
"sharing_reason_details"
]
}
"dataset_description": {
"type": "textarea",
"label": "What is the data about?",
"required": false
},
"sharing_reason": {
"type": "radio",
"label": "Why are you sharing the data",
"required": true,
"options": [
"Encourage Innovation",
"Optimise supply chains",
"Address common challenges across a sector",
"Improve market reach",
"Benchmarking and insights",
"To comply with regulation or legislation (e.g. freedom of information)",
"Demonstrate trustworthiness",
"Other"
]
},
"sharing_reason_details": {
"type": "textarea",
"label": "Please provide more detail",
"required": false
}
},
"form": [
{
"type": "fieldset",
"items": [
"dataset_name",
"dataset_description"
]
},
{
"type": "fieldset",
"items": [
"sharing_reason",
"sharing_reason_details"
]
}
}
]
}
Loading

0 comments on commit 859a794

Please sign in to comment.