* client: add update timeout

This commit is contained in:
Ildar Kamalov 2019-05-17 16:57:38 +03:00 committed by Simon Zolin
parent 9cffe865ec
commit 24f582d36d
2 changed files with 38 additions and 7 deletions

View File

@ -263,5 +263,6 @@
"dns_providers": "Here is a <0>list of known DNS providers</0> to choose from.",
"update_now": "Update now",
"update_failed": "Update failed",
"update_failed_try_later": "Update failed, please try again later",
"processing_update": "Please wait, AdGuard Home is being updated"
}

View File

@ -2,6 +2,7 @@ import { createAction } from 'redux-actions';
import round from 'lodash/round';
import { t } from 'i18next';
import { showLoading, hideLoading } from 'react-redux-loading-bar';
import axios from 'axios';
import { normalizeHistory, normalizeFilteringStatus, normalizeLogs, normalizeTextarea } from '../helpers/helpers';
import { SETTINGS_NAMES, CHECK_TIMEOUT } from '../helpers/constants';
@ -163,14 +164,43 @@ export const getUpdate = () => async (dispatch) => {
try {
await apiClient.getUpdate();
const timer = setInterval(async () => {
const dnsStatus = await apiClient.getGlobalStatus();
if (dnsStatus) {
clearInterval(timer);
dispatch(getUpdateSuccess());
window.location.reload(true);
const checkUpdate = async (attempts) => {
let count = attempts || 1;
let timeout;
if (count > 60) {
dispatch(addErrorToast({ error: 'update_failed_try_later' }));
dispatch(getUpdateFailure());
return false;
}
}, CHECK_TIMEOUT);
const rmTimeout = t => t && clearTimeout(t);
const setRecursiveTimeout = (time, ...args) => setTimeout(
checkUpdate,
time,
...args,
);
console.log(count);
axios.get('control/status')
.then((response) => {
rmTimeout(timeout);
if (response) {
dispatch(getUpdateSuccess());
window.location.reload(true);
}
timeout = setRecursiveTimeout(CHECK_TIMEOUT, count += 1);
})
.catch(() => {
rmTimeout(timeout);
timeout = setRecursiveTimeout(CHECK_TIMEOUT, count += 1);
});
return false;
};
checkUpdate();
} catch (error) {
dispatch(addErrorToast({ error: 'update_failed' }));
dispatch(getUpdateFailure());