From 2fe9819150f3a6d5eeeee632879eb85dc632559f Mon Sep 17 00:00:00 2001
From: Ildar Kamalov <i.kamalov@adguard.com>
Date: Fri, 5 Jul 2019 18:21:46 +0300
Subject: [PATCH] - client: fix update now button and notification

---
 client/src/__locales/en.json |  5 +++--
 client/src/actions/index.js  | 17 +++++++++++++----
 client/src/reducers/index.js |  5 ++++-
 3 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/client/src/__locales/en.json b/client/src/__locales/en.json
index 33d3fa53..b032abfc 100644
--- a/client/src/__locales/en.json
+++ b/client/src/__locales/en.json
@@ -1,4 +1,5 @@
 {
+    "client_settings": "Client settings",
     "example_upstream_reserved": "you can specify DNS upstream <0>for a specific domain(s)<\/0>",
     "upstream_parallel": "Use parallel queries to speed up resolving by simultaneously querying all upstream servers",
     "bootstrap_dns": "Bootstrap DNS servers",
@@ -52,7 +53,7 @@
     "filters": "Filters",
     "query_log": "Query Log",
     "faq": "FAQ",
-    "version": "version",
+    "version": "Version",
     "address": "address",
     "on": "ON",
     "off": "OFF",
@@ -99,7 +100,6 @@
     "dns_settings": "DNS settings",
     "encryption_settings": "Encryption settings",
     "dhcp_settings": "DHCP settings",
-    "client_settings": "Client settings",
     "upstream_dns": "Upstream DNS servers",
     "upstream_dns_hint": "If you keep this field empty, AdGuard Home will use <a href='https:\/\/1.1.1.1\/' target='_blank'>Cloudflare DNS<\/a> as an upstream.",
     "test_upstream_btn": "Test upstreams",
@@ -314,6 +314,7 @@
     "access_blocked_desc": "Don't confuse this with filters. AdGuard Home will drop DNS queries with these domains in query's question.",
     "access_settings_saved": "Access settings successfully saved",
     "updates_checked": "Updates successfully checked",
+    "updates_version_equal": "AdGuard Home is up-to-date",
     "check_updates_now": "Check for updates now",
     "dns_privacy": "DNS Privacy",
     "setup_dns_privacy_1": "<0>DNS-over-TLS:</0> Use <1>{{address}}</1> string.",
diff --git a/client/src/actions/index.js b/client/src/actions/index.js
index 047b1e7d..71fa352e 100644
--- a/client/src/actions/index.js
+++ b/client/src/actions/index.js
@@ -4,6 +4,7 @@ import { t } from 'i18next';
 import { showLoading, hideLoading } from 'react-redux-loading-bar';
 import axios from 'axios';
 
+import versionCompare from '../helpers/versionCompare';
 import { normalizeHistory, normalizeFilteringStatus, normalizeLogs, normalizeTextarea, sortClients } from '../helpers/helpers';
 import { SETTINGS_NAMES, CHECK_TIMEOUT } from '../helpers/constants';
 import { getTlsStatus } from './encryption';
@@ -146,13 +147,21 @@ export const getVersionRequest = createAction('GET_VERSION_REQUEST');
 export const getVersionFailure = createAction('GET_VERSION_FAILURE');
 export const getVersionSuccess = createAction('GET_VERSION_SUCCESS');
 
-export const getVersion = (recheck = false) => async (dispatch) => {
+export const getVersion = (recheck = false) => async (dispatch, getState) => {
     dispatch(getVersionRequest());
     try {
-        const newVersion = await apiClient.getGlobalVersion({ recheck_now: recheck });
-        dispatch(getVersionSuccess(newVersion));
+        const data = await apiClient.getGlobalVersion({ recheck_now: recheck });
+        dispatch(getVersionSuccess(data));
+
         if (recheck) {
-            dispatch(addSuccessToast('updates_checked'));
+            const { dnsVersion } = getState().dashboard;
+            const currentVersion = dnsVersion === 'undefined' ? 0 : dnsVersion;
+
+            if (data && versionCompare(currentVersion, data.new_version) === -1) {
+                dispatch(addSuccessToast('updates_checked'));
+            } else {
+                dispatch(addSuccessToast('updates_version_equal'));
+            }
         }
     } catch (error) {
         dispatch(addErrorToast({ error }));
diff --git a/client/src/reducers/index.js b/client/src/reducers/index.js
index 94301e25..d3eb7342 100644
--- a/client/src/reducers/index.js
+++ b/client/src/reducers/index.js
@@ -142,7 +142,10 @@ const dashboard = handleActions({
             return newState;
         }
 
-        return state;
+        return {
+            ...state,
+            processingVersion: false,
+        };
     },
 
     [actions.getUpdateRequest]: state => ({ ...state, processingUpdate: true }),