Skip to content

Commit

Permalink
Adding error in UI when creating source not works well (#28)
Browse files Browse the repository at this point in the history
Signed-off-by: Montse Ortega <mortegag@redhat.com>
  • Loading branch information
ammont82 authored Nov 6, 2024
1 parent 0a8262f commit c3d6196
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,33 @@ export const Provider: React.FC<PropsWithChildren> = (props) => {

const [downloadSourceState, downloadSource] = useAsyncFn(
async (sourceName: string, sourceSshKey: string): Promise<void> => {
const anchor = document.createElement("a");
anchor.download = sourceName + ".ova";

const newSource = await createSource(sourceName, sourceSshKey);
// TODO(jkilzi): See: ECOPROJECT-2192.
// Then don't forget to remove the '/planner/' prefix in production.
// const image = await sourceApi.getSourceImage({ id: newSource.id }); // This API is useless in production
// anchor.href = URL.createObjectURL(image); // Don't do this...
anchor.href = `/planner/api/v1/sources/${newSource.id}/image`;

document.body.appendChild(anchor);
anchor.click();
anchor.remove();
try {
const newSource = await createSource(sourceName, sourceSshKey);
const url = `/planner/api/v1/sources/${newSource.id}/image`;

const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const blob = await response.blob();
const anchor = document.createElement("a");
anchor.download = sourceName + ".ova";
anchor.href = URL.createObjectURL(blob);

document.body.appendChild(anchor);
anchor.click();
anchor.remove();
URL.revokeObjectURL(anchor.href);
} catch (error) {
downloadSourceState.error = error as Error;
console.error("Error downloading source:", error);
throw error; // Re-lanza el error para que useAsyncFn lo capture
}
}
);


const [isPolling, setIsPolling] = useState(false);
const [pollingDelay, setPollingDelay] = useState<number | null>(null);
const startPolling = useCallback(
Expand Down
9 changes: 9 additions & 0 deletions apps/demo/src/migration-wizard/steps/connect/ConnectStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ export const ConnectStep: React.FC = () => {
}}
/>
)}
</StackItem>
<StackItem>
{discoverySourcesContext.errorDownloadingSource && (
<Alert
isInline
variant="danger"
title="Download Source error"
/>
)}
</StackItem>
</Stack>
);
Expand Down

0 comments on commit c3d6196

Please sign in to comment.