Skip to content

Commit

Permalink
Address broken export questionnaire
Browse files Browse the repository at this point in the history
  • Loading branch information
ibolton336 committed Oct 4, 2023
1 parent c0186be commit 669757a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
19 changes: 2 additions & 17 deletions client/src/app/api/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,23 +721,8 @@ export const updateProxy = (obj: Proxy): Promise<Proxy> =>
export const getQuestionnaires = (): Promise<Questionnaire[]> =>
axios.get(QUESTIONNAIRES).then((response) => response.data);

export const getQuestionnaireById = <T>(
id: number | string,
isBlob: boolean = false
): Promise<T> =>
axios
.get(
`${QUESTIONNAIRES}/${id}`,
isBlob
? {
responseType: "blob",
headers: {
Accept: "application/x-yaml",
},
}
: {}
)
.then((response) => response.data);
export const getQuestionnaireById = <T>(id: number | string): Promise<T> =>
axios.get(`${QUESTIONNAIRES}/${id}`).then((response) => response.data);

Check warning on line 725 in client/src/app/api/rest.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/api/rest.ts#L725

Added line #L725 was not covered by tests

export const createQuestionnaire = (
obj: Questionnaire
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import * as React from "react";
import { saveAs } from "file-saver";
import { DropdownItem } from "@patternfly/react-core";
import { useTranslation } from "react-i18next";

import { useFetchQuestionnaireBlob } from "@app/queries/questionnaires";
import { AxiosError } from "axios";
import { getAxiosErrorMessage } from "@app/utils/utils";
import { NotificationsContext } from "@app/components/NotificationsContext";
import { useDownloadQuestionnaire } from "@app/queries/questionnaires";

export interface IExportQuestionnaireDropdownItemProps {
id: number;
Expand All @@ -15,30 +10,18 @@ export const ExportQuestionnaireDropdownItem: React.FC<
IExportQuestionnaireDropdownItemProps
> = ({ id }) => {
const { t } = useTranslation();
const { pushNotification } = React.useContext(NotificationsContext);

const onExportQuestionnaireError = (error: AxiosError) => {
pushNotification({
title: getAxiosErrorMessage(error),
variant: "danger",
});
};

const { data: questionnaire, refetch } = useFetchQuestionnaireBlob(
id,
onExportQuestionnaireError
);
const {
mutate: downloadFile,
isLoading,
isError,
} = useDownloadQuestionnaire();

const exportQuestionnaire = () => {
refetch().then(() => {
if (questionnaire) {
saveAs(new Blob([questionnaire]), `questionnaire-${id}.yaml`);
}
});
const handleDownload = () => {
downloadFile(id);
};

return (
<DropdownItem key="export" component="button" onClick={exportQuestionnaire}>
<DropdownItem key="export" component="button" onClick={handleDownload}>
{t("actions.export")}
</DropdownItem>
);
Expand Down
34 changes: 32 additions & 2 deletions client/src/app/queries/questionnaires.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { AxiosError } from "axios";
import axios, { AxiosError } from "axios";

import {
QUESTIONNAIRES,
createQuestionnaire,
deleteQuestionnaire,
getQuestionnaireById,
getQuestionnaires,
updateQuestionnaire,
} from "@app/api/rest";
import { Questionnaire } from "@app/api/models";
import saveAs from "file-saver";

export const QuestionnairesQueryKey = "questionnaires";
export const QuestionnaireByIdQueryKey = "questionnaireById";
Expand Down Expand Up @@ -84,7 +86,7 @@ export const useFetchQuestionnaireBlob = (
) =>
useQuery({
queryKey: [QuestionnaireByIdQueryKey, id],
queryFn: () => getQuestionnaireById<Blob>(id, true),
queryFn: () => getQuestionnaireById<Blob>(id),

Check warning on line 89 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L89

Added line #L89 was not covered by tests
onError: onError,
enabled: false,
});
Expand Down Expand Up @@ -113,3 +115,31 @@ export const useCreateQuestionnaireMutation = (
error,
};
};

export const downloadQuestionnaire = async (
id: number | string
): Promise<void> => {
const url = `${QUESTIONNAIRES}/${id}`;

Check warning on line 122 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L121-L122

Added lines #L121 - L122 were not covered by tests

try {
const response = await axios.get(url, {

Check warning on line 125 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L124-L125

Added lines #L124 - L125 were not covered by tests
responseType: "blob",
headers: {
Accept: "application/x-yaml",
},
});

if (response.status !== 200) {
throw new Error("Network response was not ok when downloading file.");

Check warning on line 133 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L133

Added line #L133 was not covered by tests
}

const blob = new Blob([response.data]);
saveAs(blob, `questionnaire-${id}.yaml`);

Check warning on line 137 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L136-L137

Added lines #L136 - L137 were not covered by tests
} catch (error) {
console.error("There was an error downloading the file:", error);
throw error;

Check warning on line 140 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L139-L140

Added lines #L139 - L140 were not covered by tests
}
};
export const useDownloadQuestionnaire = () => {
return useMutation(downloadQuestionnaire);

Check warning on line 144 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L144

Added line #L144 was not covered by tests
};

0 comments on commit 669757a

Please sign in to comment.