From f1a497289816194e163844ec0ab93d5001857092 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Thu, 15 Feb 2024 15:01:03 +0200 Subject: [PATCH 01/10] UHF-8988: added a scheduler with hardcoded dates --- assets/js/chat_leijuke.js | 27 ++++++++++++++++++++++++++- helfi_platform_config.libraries.yml | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/assets/js/chat_leijuke.js b/assets/js/chat_leijuke.js index 1806f3265..ee471e150 100644 --- a/assets/js/chat_leijuke.js +++ b/assets/js/chat_leijuke.js @@ -29,7 +29,9 @@ return new GenesysAdapter; } if (chatSelection.indexOf('user_inquiry') != -1) { - return new UserInquiryAdapter; + if (UserInquiryAdapter.scheduler()) { + return new UserInquiryAdapter; + } } console.warn(`No adapter found for ${chatSelection}!`); } @@ -100,6 +102,29 @@ open(callback) {} onClosed(callback) {} onLoaded(callback) {} + + // Return true or false based on hardcoded dates or value in localstorage. + static scheduler() { + const now = new Date(); + + const dates = [ + {start: '2024-02-26', end: '2024-03-01'}, + {start: '2024-03-04', end: '2024-03-08'}, + ] + + // Run the code below in browser to activate the popup for a minute + // localStorage.setItem('user_inquiry_debug', `{"start": "${new Date((Date.now()-60000)).toString()}", "end": "${new Date((Date.now()+60000))}"}`); + let debug = localStorage.getItem('user_inquiry_debug'); + if (debug) { + dates.push(JSON.parse(debug)); + } + + const date= dates.find((object)=>{ + return now >= new Date(object.start) && now < new Date(object.end); + }); + + return date ? true : false; + } } class Leijuke { diff --git a/helfi_platform_config.libraries.yml b/helfi_platform_config.libraries.yml index 53bae8e9f..91dcfdb6d 100644 --- a/helfi_platform_config.libraries.yml +++ b/helfi_platform_config.libraries.yml @@ -64,7 +64,7 @@ genesys_auth_redirect: - core/drupalSettings chat_leijuke: - version: 1.0.x + version: 1.0.1 header: true js: assets/js/chat_leijuke.js: {} From 79b84833f130f57de838239bff8ef77f9c347d4e Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Thu, 15 Feb 2024 15:05:02 +0200 Subject: [PATCH 02/10] UHF-8988: let to const --- assets/js/chat_leijuke.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/chat_leijuke.js b/assets/js/chat_leijuke.js index ee471e150..31e05a668 100644 --- a/assets/js/chat_leijuke.js +++ b/assets/js/chat_leijuke.js @@ -114,7 +114,7 @@ // Run the code below in browser to activate the popup for a minute // localStorage.setItem('user_inquiry_debug', `{"start": "${new Date((Date.now()-60000)).toString()}", "end": "${new Date((Date.now()+60000))}"}`); - let debug = localStorage.getItem('user_inquiry_debug'); + const debug = localStorage.getItem('user_inquiry_debug'); if (debug) { dates.push(JSON.parse(debug)); } From 42428c90cb2ef704ae83023ee281bbd3c5d6ad42 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Thu, 15 Feb 2024 15:21:40 +0200 Subject: [PATCH 03/10] UHF-8988: change 'chat'-cookie category to statistics --- assets/js/chat_leijuke.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/chat_leijuke.js b/assets/js/chat_leijuke.js index 31e05a668..9b5242e9e 100644 --- a/assets/js/chat_leijuke.js +++ b/assets/js/chat_leijuke.js @@ -93,7 +93,7 @@ */ class UserInquiryAdapter { constructor() { - this.requiredCookies = ['chat']; + this.requiredCookies = ['statistics']; this.bot = false; this.persist = false; this.hasButton = false; From 52d25e0ab96951c3a279a8c4084b249df27996bc Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Fri, 16 Feb 2024 09:42:36 +0200 Subject: [PATCH 04/10] UHF-8988: programmatically configure & install user inquiry block --- helfi_platform_config.install | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/helfi_platform_config.install b/helfi_platform_config.install index efe117a33..913fe11f6 100644 --- a/helfi_platform_config.install +++ b/helfi_platform_config.install @@ -66,3 +66,55 @@ function helfi_platform_config_update_9304() : void { $config->set('links', $links) ->save(); } + +function helfi_platform_config_update_9305() : void { + if (!\Drupal::moduleHandler()->moduleExists('helfi_api_base')) { + return; + } + + $environment_resolver = \Drupal::service('helfi_api_base.environment_resolver'); + try { + $environment_resolver->getActiveProject(); + } + catch(Exception $exception) { + // No active project found, stop execution. + return; + } + + $block_installer = Drupal::service('helfi_platform_config.helper.block_installer'); + $theme_handler = \Drupal::service('theme_handler'); + if (!str_starts_with($theme_handler->getDefault(), 'hdbt_subtheme')) { + return; + } + + $block = [ + 'id' => 'hdbt_subtheme_user_inquiry', + 'plugin' => 'chat_leijuke', + 'provider' => 'helfi_platform_config', + 'settings' => [ + 'label' => 'User inquiry', + 'label_display' => FALSE, + 'chat_title' => '', + 'chat_selection' => 'user_inquiry', + ], + 'visibility' => [ + 'language' => [ + 'id' => 'language', + 'langcodes' => ['fi' => 'fi', 'en' => 'en', 'sv' => 'sv'] + ], + 'user_role' => [ + 'id' => 'user_role', + 'roles' => ['anonymous' => 'anonymous'] + ], + ], + ]; + + $variations = [ + [ + 'theme' => 'hdbt_subtheme', + 'region' => 'attachments' + ] + ]; + + $block_installer->install($block, $variations); +} From 8b8bf9e289b67bfafee51f8dd7bec34f22acd5a2 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Fri, 16 Feb 2024 11:47:48 +0200 Subject: [PATCH 05/10] UHF-8988: tweak --- assets/js/chat_leijuke.js | 4 ++-- helfi_platform_config.install | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/assets/js/chat_leijuke.js b/assets/js/chat_leijuke.js index 9b5242e9e..4ac0cd0e1 100644 --- a/assets/js/chat_leijuke.js +++ b/assets/js/chat_leijuke.js @@ -29,7 +29,7 @@ return new GenesysAdapter; } if (chatSelection.indexOf('user_inquiry') != -1) { - if (UserInquiryAdapter.scheduler()) { + if (UserInquiryAdapter.idScheduled()) { return new UserInquiryAdapter; } } @@ -104,7 +104,7 @@ onLoaded(callback) {} // Return true or false based on hardcoded dates or value in localstorage. - static scheduler() { + static idScheduled() { const now = new Date(); const dates = [ diff --git a/helfi_platform_config.install b/helfi_platform_config.install index 913fe11f6..982926997 100644 --- a/helfi_platform_config.install +++ b/helfi_platform_config.install @@ -67,7 +67,11 @@ function helfi_platform_config_update_9304() : void { ->save(); } +/** + * Enable leijuke-block which contains the user inquiry -popup + */ function helfi_platform_config_update_9305() : void { + if (!\Drupal::moduleHandler()->moduleExists('helfi_api_base')) { return; } @@ -76,17 +80,19 @@ function helfi_platform_config_update_9305() : void { try { $environment_resolver->getActiveProject(); } - catch(Exception $exception) { + catch (Exception $exception) { // No active project found, stop execution. return; } $block_installer = Drupal::service('helfi_platform_config.helper.block_installer'); $theme_handler = \Drupal::service('theme_handler'); - if (!str_starts_with($theme_handler->getDefault(), 'hdbt_subtheme')) { + if (!str_starts_with($theme_handler->getDefault(), 'hdbt')) { return; } + $theme = $theme_handler->getDefault() === 'hdbt_subtheme' ? 'hdbt_subtheme' : 'hdbt'; + $block = [ 'id' => 'hdbt_subtheme_user_inquiry', 'plugin' => 'chat_leijuke', @@ -111,7 +117,7 @@ function helfi_platform_config_update_9305() : void { $variations = [ [ - 'theme' => 'hdbt_subtheme', + 'theme' => $theme, 'region' => 'attachments' ] ]; From 9d7ad8318eb690d4d1aa68bd131d9b60169117e1 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Fri, 16 Feb 2024 12:55:55 +0200 Subject: [PATCH 06/10] UHF-8988: code fixes --- helfi_platform_config.install | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/helfi_platform_config.install b/helfi_platform_config.install index 982926997..7592f2c30 100644 --- a/helfi_platform_config.install +++ b/helfi_platform_config.install @@ -68,7 +68,7 @@ function helfi_platform_config_update_9304() : void { } /** - * Enable leijuke-block which contains the user inquiry -popup + * Enable leijuke-block which contains the user inquiry -popup. */ function helfi_platform_config_update_9305() : void { @@ -106,11 +106,11 @@ function helfi_platform_config_update_9305() : void { 'visibility' => [ 'language' => [ 'id' => 'language', - 'langcodes' => ['fi' => 'fi', 'en' => 'en', 'sv' => 'sv'] + 'langcodes' => ['fi' => 'fi', 'en' => 'en', 'sv' => 'sv'], ], 'user_role' => [ 'id' => 'user_role', - 'roles' => ['anonymous' => 'anonymous'] + 'roles' => ['anonymous' => 'anonymous'], ], ], ]; @@ -118,8 +118,8 @@ function helfi_platform_config_update_9305() : void { $variations = [ [ 'theme' => $theme, - 'region' => 'attachments' - ] + 'region' => 'attachments', + ], ]; $block_installer->install($block, $variations); From 934a1ed6ebf14028691b9c7269e09846435f2a60 Mon Sep 17 00:00:00 2001 From: rpnykanen <48206082+rpnykanen@users.noreply.github.com> Date: Mon, 19 Feb 2024 09:02:42 +0200 Subject: [PATCH 07/10] added context mapping to user_role --- helfi_platform_config.install | 1 + 1 file changed, 1 insertion(+) diff --git a/helfi_platform_config.install b/helfi_platform_config.install index 7592f2c30..8fe176520 100644 --- a/helfi_platform_config.install +++ b/helfi_platform_config.install @@ -111,6 +111,7 @@ function helfi_platform_config_update_9305() : void { 'user_role' => [ 'id' => 'user_role', 'roles' => ['anonymous' => 'anonymous'], + 'context_mapping' => ['user' => '@user.current_user_context:current_user'], ], ], ]; From 25a728375dcc112dcf5b22559968770a079d3aba Mon Sep 17 00:00:00 2001 From: rpnykanen <48206082+rpnykanen@users.noreply.github.com> Date: Mon, 19 Feb 2024 09:19:44 +0200 Subject: [PATCH 08/10] added language context mapping --- helfi_platform_config.install | 3 +++ 1 file changed, 3 insertions(+) diff --git a/helfi_platform_config.install b/helfi_platform_config.install index 8fe176520..9c7053a78 100644 --- a/helfi_platform_config.install +++ b/helfi_platform_config.install @@ -106,6 +106,9 @@ function helfi_platform_config_update_9305() : void { 'visibility' => [ 'language' => [ 'id' => 'language', + 'context_mapping' => [ + 'language' => '@language.current_language_context:language_interface' + ], 'langcodes' => ['fi' => 'fi', 'en' => 'en', 'sv' => 'sv'], ], 'user_role' => [ From 253ed6f1dad72f520f2c370f50a85bf0b838cfd1 Mon Sep 17 00:00:00 2001 From: rpnykanen <48206082+rpnykanen@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:13:16 +0200 Subject: [PATCH 09/10] add missing comma --- helfi_platform_config.install | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helfi_platform_config.install b/helfi_platform_config.install index 9c7053a78..2a9f0de8c 100644 --- a/helfi_platform_config.install +++ b/helfi_platform_config.install @@ -107,7 +107,7 @@ function helfi_platform_config_update_9305() : void { 'language' => [ 'id' => 'language', 'context_mapping' => [ - 'language' => '@language.current_language_context:language_interface' + 'language' => '@language.current_language_context:language_interface', ], 'langcodes' => ['fi' => 'fi', 'en' => 'en', 'sv' => 'sv'], ], From b0d4b7e4f131ed09505d37e9c4c276046c4967c6 Mon Sep 17 00:00:00 2001 From: rpnykanen <48206082+rpnykanen@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:19:10 +0200 Subject: [PATCH 10/10] simplified ternary operator --- assets/js/chat_leijuke.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/js/chat_leijuke.js b/assets/js/chat_leijuke.js index 4ac0cd0e1..19c49d4ac 100644 --- a/assets/js/chat_leijuke.js +++ b/assets/js/chat_leijuke.js @@ -119,11 +119,11 @@ dates.push(JSON.parse(debug)); } - const date= dates.find((object)=>{ + const date = dates.find((object)=>{ return now >= new Date(object.start) && now < new Date(object.end); }); - return date ? true : false; + return !!date; } }