Skip to content

Commit

Permalink
#1441: rm selectedLanguage func to global scope;
Browse files Browse the repository at this point in the history
  • Loading branch information
migbash committed Aug 15, 2023
1 parent 2197c1c commit 2277600
Showing 1 changed file with 4 additions and 222 deletions.
226 changes: 4 additions & 222 deletions src/lib/components/_main_/header/Header-C-Lang.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ COMPONENT JS (w/ TS)
// #region ➤ 📦 Package Imports
import { dev } from "$app/environment";
import { goto } from "$app/navigation";
import { page } from "$app/stores";
import { fly } from "svelte/transition";
import sessionStore from "$lib/store/session.js";
import userBetarenaSettings from '$lib/store/user-settings.js';
import { NB_W_TAG, dlog, dlogv2 } from "$lib/utils/debug.js";
import { ROUTE_ID_PROFILE } from "$lib/utils/user.js";
import { NB_W_TAG, dlog } from "$lib/utils/debug.js";
import arrow_down from './assets/arrow-down.svg';
import arrow_up from './assets/arrow-up.svg';
import { selectLanguage } from "$lib/utils/platform-functions.js";
import type { B_NAV_T } from '@betarena/scores-lib/types/navbar.js';
// #endregion ➤ 📦 Package Imports
Expand Down Expand Up @@ -136,221 +133,6 @@ COMPONENT JS (w/ TS)
}
}
/**
* @summary
* 📌 MAIN | IMPORTANT
*
* @description
* 📌 Updates `user` language platform selection.
*
* ⚡️ Manages platform main navigation,
* for some of the section routes.
*
* @param
* { string } lang - Target new `selected` language.
*/
async function selectLanguage
(
lang: string
): Promise < void >
{
if ($sessionStore?.serverLang == lang) return;
// ➫ NOTE:
// ➫ Past/previous lang option.
const pastLang: string =
$sessionStore?.serverLang == 'en'
? '/'
: `/${$sessionStore?.serverLang}`
;
userBetarenaSettings.setLang
(
lang
);
// [🐞]
dlogv2
(
`${NB_W_TAG[0]} selectLanguage()`,
[
`$userBetarenaSettings.lang: ${$userBetarenaSettings.lang}`,
`$sessionStore?.serverLang: ${$sessionStore?.serverLang}`,
`lang: ${lang}`,
`pastLang: ${pastLang}`,
`$page.route.id: ${$page.route.id}`
],
true,
NB_W_TAG[2]
);
isLangDropdown = false;
// ➫ NOTE:
// ➫ Update <html {lang}> in DOCTYPE.
let tempLang: string = lang;
if (lang === 'br') tempLang = 'pt-BR';
document.documentElement.setAttribute
(
'lang',
tempLang
);
// ➫ CHECK
// ➫ on error', navigate back to homepage;
const if_M_0: boolean =
$page.error
&& !dev
;
if (if_M_0)
{
const targetUrl: string =
lang == 'en'
? `/`
: `/${lang}`
;
// [🐞]
dlog
(
`${NB_W_TAG[0]} -> ${lang}`
);
await goto
(
targetUrl
);
return;
}
// ➫ CHECK
// ➫ Omit 'special' routes cases, as these routes
// ➫ manage their own navigation/translation switch.
const if_M_1: boolean =
OMIT_URLS.includes($page.route.id)
;
if (if_M_1)
{
// [🐞]
dlog
(
`${NB_W_TAG[0]} omitting route: ${$page.route.id}`
);
return;
}
// ➫ CHECK
// ➫ On profile page route, handle.
else if (ROUTE_ID_PROFILE == $page.route.id)
{
const pastLangV2: string =
pastLang == `/`
? `/en`
: pastLang
;
let tempUrl: string = `${$page.url.pathname}/`;
const newURL: string = tempUrl
?.replace
(
`${pastLangV2}/`,
`/${lang}`
);
// [🐞]
dlog
(
`${NB_W_TAG[0]} inside (PROFILE) ${lang},
pastLangV2: ${pastLangV2}; tempUrl: ${tempUrl}; newURL: ${newURL}`
);
await goto
(
newURL,
{
replaceState: true
}
);
}
// ➫ NOTE:
// ➫ Otherwise, continue navigation switch.
// ➫ NOTE:
// ➫ CHECK
// ➫ for 'EN' naviagtion.
else if (lang == 'en' && pastLang != '/')
{
// prefetch(`/`); [? - maybe ?] // NOTE:
// [ℹ] count number of slashes URL;
var count = $page.url.pathname.split('/').length - 1;
// [ℹ] replace path-name accordingly for "EN" - first occurance;
const newURL: string =
count == 1
? $page.url.pathname.replace(pastLang, '/')
: $page.url.pathname.replace(pastLang, '')
;
// [🐞]
dlog
(
`${NB_W_TAG[0]} inside (EN) ${lang}, pastLang: ${pastLang}, countSlash: ${countSlash}, newURL: ${newURL}`
);
// [ℹ] update URL breadcrumb;
// window.history.replaceState({}, "NewPage", newURL);
await goto(newURL, { replaceState: true });
}
// ➫ CHECK
// ➫ for 'incoming (past)' from an 'EN (/)' route.
else if (lang != 'en' && pastLang == '/')
{
// [ℹ] count number of slashes URL;
var countSlash = $page.url.pathname.split('/').length - 1;
// [ℹ] replace path-name accordingly for "<lang>" - first occurance;
const newURL: string =
countSlash > 1
? $page.url.pathname.replace(pastLang, `/${lang}/`)
: $page.url.pathname.replace(pastLang, `/${lang}`)
;
// [🐞]
dlog
(
`${NB_W_TAG[0]} inside (V2) ${lang}, pastLang: ${pastLang}, countSlash: ${countSlash}, newURL: ${newURL}`
);
// [ℹ] update URL breadcrumb;
// window.history.replaceState({}, "NewPage", newURL);
await goto(newURL, { replaceState: true });
}
// ➫ CHECK
// ➫ for 'incoming (past)' from an 'non-EN (/)' route.
else if (lang != 'en' && pastLang != '/')
{
// [ℹ] count number of slashes URL;
var countSlash = $page.url.pathname.split('/').length - 1;
// [ℹ] replace path-name accordingly for "<lang>" - first occurance;
const newURL: string = $page.url.pathname.replace(pastLang, `/${lang}`);
// [🐞]
dlog
(
`${NB_W_TAG[0]} inside (V3) ${lang}, pastLang: ${pastLang}, countSlash: ${countSlash}, newURL: ${newURL}`
);
// [ℹ] update URL breadcrumb;
// window.history.replaceState({}, "NewPage", newURL);
await goto(newURL, { replaceState: true });
}
}
// #endregion ➤ 🛠️ METHODS
</script>
Expand Down Expand Up @@ -421,8 +203,8 @@ COMPONENT JS (w/ TS)
{#if lang.toUpperCase() != $sessionStore?.serverLang?.toUpperCase()}
<div
id="lang-select"
on:click={() => selectLanguage(lang)}
on:keydown={() => selectLanguage(lang)}
on:click={() => selectLanguage(lang, $page)}
on:keydown={() => selectLanguage(lang, $page)}
on:mouseout={() => detectIntentBuffer(undefined)}
on:mouseover={() => detectIntentBuffer(lang)}
on:focus={() => detectIntentBuffer(lang)}
Expand Down

0 comments on commit 2277600

Please sign in to comment.