CEC-381 Fix install progress (#77)
* Fix install progress * Remove unused components and inline styles * Update test * errors are not the final update state * Remove max width for main container * Progress starts at 0
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
|
||||
import api from "../../services/updates";
|
||||
import { validateStatusMessage } from "../../utils/statusMessage";
|
||||
|
||||
const FINAL_UPDATE_STATES = ["package_install_complete"];
|
||||
const CarUpdatesContext = React.createContext();
|
||||
|
||||
const validateDeployCarUpdates = (data) => {
|
||||
@@ -49,6 +51,8 @@ export const CarUpdatesProvider = ({ children }) => {
|
||||
result = await api.getCarUpdates(search, token);
|
||||
if (result.error)
|
||||
throw new Error(`Get car updates error. ${result.message}`);
|
||||
result.data.forEach((item) => (item.progress = 0));
|
||||
console.log(result.data);
|
||||
setCarUpdates(result.data);
|
||||
if (search && search.offset === 0 && result.total) {
|
||||
setTotalCarUpdates(result.total);
|
||||
@@ -75,47 +79,40 @@ export const CarUpdatesProvider = ({ children }) => {
|
||||
return result;
|
||||
};
|
||||
|
||||
const getDownloadProgress = (status) => {
|
||||
if (status.package_total > 0)
|
||||
return Math.floor((100 * status.package_current) / status.package_total);
|
||||
return 0;
|
||||
};
|
||||
|
||||
const getInstallProgress = (status) => {
|
||||
if (status.total_files > 0)
|
||||
return Math.floor((100 * status.installed) / status.total_files);
|
||||
return 0;
|
||||
};
|
||||
|
||||
const applyProgressStatus = (item, status) => {
|
||||
if (
|
||||
status.msg === "package_download_start" ||
|
||||
status.msg === "download_start"
|
||||
) {
|
||||
item.progress = 1;
|
||||
item.status = "downloading";
|
||||
} else if (status.msg === "package_download_complete") {
|
||||
item.status = "downloaded";
|
||||
} else if (
|
||||
status.msg === "downloading" &&
|
||||
status.package_current &&
|
||||
status.package_total &&
|
||||
status.package_total > 0
|
||||
) {
|
||||
let progress = Math.floor(
|
||||
(100 * status.package_current) / status.package_total
|
||||
);
|
||||
if (progress > 99) progress = 0;
|
||||
item.progress = progress;
|
||||
item.status = `downloading ${progress}%`;
|
||||
} else if (status.error > 0 || status.msg === "download_error") {
|
||||
item.status = "download error";
|
||||
} else if (status.msg === "package_install_start") {
|
||||
item.progress = 1;
|
||||
item.status = "installing";
|
||||
} else if (
|
||||
status.msg === "installing" &&
|
||||
status.installed &&
|
||||
status.total_files &&
|
||||
status.total_files > 0
|
||||
) {
|
||||
let progress = Math.floor((100 * status.installed) / status.total_files);
|
||||
item.progress = progress;
|
||||
item.status = `installing ${progress}%`;
|
||||
} else if (status.msg === "package_install_complete") {
|
||||
item.status = "installed";
|
||||
} else {
|
||||
delete item.progress;
|
||||
item.status = status.msg;
|
||||
if (validateStatusMessage(status)) {
|
||||
if (status.msg === "downloading") {
|
||||
item.progress = getDownloadProgress(status);
|
||||
item.status = `downloading ${item.progress}%`;
|
||||
return;
|
||||
} else if (status.msg === "package_download_complete") {
|
||||
item.progress = 100;
|
||||
item.status = "downloaded";
|
||||
return;
|
||||
} else if (status.msg === "installing") {
|
||||
item.progress = getInstallProgress(status);
|
||||
item.status = `installing ${item.progress}%`;
|
||||
return;
|
||||
} else if (status.msg === "package_install_complete") {
|
||||
item.progress = 100;
|
||||
item.status = "installed";
|
||||
return;
|
||||
}
|
||||
}
|
||||
delete item.progress;
|
||||
item.status = status.msg;
|
||||
};
|
||||
|
||||
const applyProgressStatuses = (statuses) => {
|
||||
@@ -163,7 +160,7 @@ export const CarUpdatesProvider = ({ children }) => {
|
||||
return 1000;
|
||||
}
|
||||
for (let i = 0, len = carUpdates.length; i < len; i++) {
|
||||
if (carUpdates[i].status.indexOf("downloading") > -1) return 1000;
|
||||
if (FINAL_UPDATE_STATES.indexOf(carUpdates[i].status) === -1) return 1000;
|
||||
}
|
||||
return 10000;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user