From 03f1132dad4182672ef107cd5315303f59865b00 Mon Sep 17 00:00:00 2001 From: Danil Apsadikov Date: Sat, 27 Mar 2021 13:02:25 +0300 Subject: [PATCH 1/2] Add stratey pattern WebsocketStrategyUpdater and FetchStrategyUpdater for update categories --- api.js | 12 +++--- arch-front.iml | 9 +++++ auth.html | 18 ++++----- index.html | 103 ++++++++++++++++++++++++++++++++++++------------- 4 files changed, 101 insertions(+), 41 deletions(-) create mode 100644 arch-front.iml diff --git a/api.js b/api.js index 75c0cd3..5c93f28 100644 --- a/api.js +++ b/api.js @@ -2,21 +2,21 @@ const json = { token: '' -} +}; async function api(url, method, endpoint, message, data, headers) { try { const header = headers !== undefined ? headers : {}; header['Authorization'] = getCookie('token'); - const response = await fetch(url+endpoint, { + const response = await fetch(url + endpoint, { method: method, - body: data === null? undefined : data, - headers : header + body: data === null ? undefined : data, + headers: header }); return await response.json() } catch (e) { - console.error(e.message) - document.getElementById("status").innerText = "Error in "+ message + console.error(e.message); + document.getElementById("status").innerText = "Error in " + message } } diff --git a/arch-front.iml b/arch-front.iml new file mode 100644 index 0000000..8021953 --- /dev/null +++ b/arch-front.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/auth.html b/auth.html index 99eae43..6b7df03 100644 --- a/auth.html +++ b/auth.html @@ -14,24 +14,24 @@ From 2e4b1d11be953ac5d618e321a37110a29ef61d3b Mon Sep 17 00:00:00 2001 From: Danil Apsadikov Date: Thu, 29 Apr 2021 12:41:12 +0300 Subject: [PATCH 2/2] comment added --- index.html | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/index.html b/index.html index 3cdbc23..0d05b74 100644 --- a/index.html +++ b/index.html @@ -22,6 +22,10 @@ const url = "http://localhost:8080"; + + /* + * Созаем обЪект и передаем стратегию WebsocketUpdaterStrategy + */ new Updater(new WebsocketUpdaterStrategy(json => { const catElement = document.getElementById("categories"); catElement.innerHTML = ""; @@ -36,6 +40,11 @@ } }, url)).start(); + + + /* + * Класс, получает стратегию и вызывает у нее метод start для запуска обновления категорий + */ class Updater { constructor(strategy) { this.strategy = strategy; @@ -46,6 +55,10 @@ } } + + /* + * Базовый класс стратегии + */ class UpdaterStrategy { constructor(callback) { @@ -55,6 +68,10 @@ start(); } + + /* + * Стратегия, которая обновляет категории, используя websocket + */ class WebsocketUpdaterStrategy extends UpdaterStrategy { constructor(callback, url) { @@ -75,6 +92,9 @@ } } + /* + * Стратегия, которая обновляет категории, используя fetch + */ class FetchUpdaterStrategy extends UpdaterStrategy { constructor(callback, url) {