Skip to content

Commit

Permalink
fix(firmware): display spinners in download from release
Browse files Browse the repository at this point in the history
  • Loading branch information
germanferrero committed Feb 21, 2022
1 parent 2ba79d1 commit a50d314
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
6 changes: 5 additions & 1 deletion plugins/lime-plugin-firmware/firmware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,11 @@ describe('firmware form', () => {
version: 'SomeNewVersionName'
}));
render(<FirmwarePage />)
downloadRelease.mockImplementation(() =>
new Promise(res => setTimeout(res, 10))); // let loading enter screen
const downloadButton = await screen.findByRole('button', {name: /Download/i})
fireEvent.click(downloadButton);
stepSubmit();
expect(await screen.findByLabelText('loading')).toBeInTheDocument();
await waitForExpect(() => {
expect(downloadRelease).toHaveBeenCalled();
})
Expand Down Expand Up @@ -252,9 +254,11 @@ describe('firmware form', () => {
.mockImplementation(async () => (
{ download_status: 'downloaded', fw_path: '/tmp/upgrade.sh' }
));
upgradeFirmware.mockImplementation(() => new Promise(res => setTimeout(res, 10)));
render(<FirmwarePage />)
const upgradeButton = await screen.findByRole('button', {name: /Upgrade to SomeNewVersionName/i});
fireEvent.click(upgradeButton);
expect(await screen.findByLabelText('loading')).toBeInTheDocument();
expect(upgradeFirmware).toHaveBeenCalledWith('/tmp/upgrade.sh');
});

Expand Down
7 changes: 6 additions & 1 deletion plugins/lime-plugin-firmware/src/firmwareQueries.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useQuery, useMutation } from 'react-query';
import queryCache from 'utils/queryCache';
import { getUpgradeInfo, getNewVersion, getDownloadStatus,
upgradeConfirm, upgradeRevert, downloadRelease} from './firmwareApi'
upgradeConfirm, upgradeRevert, downloadRelease,
upgradeFirmware } from './firmwareApi'

export function useUpgradeInfo() {
return useQuery(["lime-utils", "get_upgrade_info"], getUpgradeInfo, {});
Expand Down Expand Up @@ -41,3 +42,7 @@ export function useDownloadRelease() {
)}
)
}

export function useUpgradeFirwmare() {
return useMutation(upgradeFirmware);
}
9 changes: 7 additions & 2 deletions plugins/lime-plugin-firmware/src/upgradePage/upgradePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { useState, useEffect } from 'preact/hooks';
import { useForm } from 'react-hook-form';
import Loading from 'components/loading';
import { uploadFile, upgradeFirmware } from '../firmwareApi';
import { useUpgradeInfo, useDownloadRelease, useDownloadStatus, useNewVersion} from '../firmwareQueries';
import { useUpgradeInfo, useDownloadRelease, useDownloadStatus,
useNewVersion, useUpgradeFirwmare} from '../firmwareQueries';

export const SafeUpgradeBadge = () => {
const { isLoading, data: upgradeInfo} = useUpgradeInfo();
Expand Down Expand Up @@ -48,7 +49,8 @@ const UpgradeFromRelease = ({onUpgrading, onSwitch}) => {
}
});

const [downloadRelease] = useDownloadRelease();
const [downloadRelease, { isLoading: submittingDownload }] = useDownloadRelease();
const [upgradeFirmware, { isLoading: submittingUpgrade }] = useUpgradeFirwmare();
const filePath = downloadStatus && downloadStatus.fw_path;
const status = downloadStatus && downloadStatus.download_status;

Expand All @@ -70,6 +72,9 @@ const UpgradeFromRelease = ({onUpgrading, onSwitch}) => {
{status === 'not-initiated' &&
<button onClick={onDownload}><Trans>Download</Trans></button>
}
{(submittingDownload || submittingUpgrade) &&
<Loading />
}
{status === 'downloading' &&
<div>
<div class="withLoadingEllipsis"><Trans>Downloading</Trans></div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/loading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { h } from 'preact';
import style from './style.less';

export const Loading = ({ color }) => {
const styleOverride = color ? { 'backgroundColor': color } : '';
const styleOverride = color ? { backgroundColor: color } : '';
return (
<div className={style.spinner} aria-label="loading" data-testid="loading">
<div className={style.bounce1} style={styleOverride} />
Expand Down

0 comments on commit a50d314

Please sign in to comment.