Doppelter Aufruf für das Holen der Page-Contents nach Sprachwechsel gefixed.
This commit is contained in:
@@ -15,10 +15,12 @@
|
||||
let content: Content
|
||||
let article: TibiArticle
|
||||
let connectedContentNotFound: boolean = false
|
||||
let currentLocale: string = null
|
||||
let blockedDoubleCall: boolean = false
|
||||
|
||||
const loadContent = (type?: string) => {
|
||||
// Set default API call filter
|
||||
let filter = {
|
||||
let filter: { [key: string]: any } = {
|
||||
locale: $currentLang,
|
||||
path,
|
||||
}
|
||||
@@ -46,14 +48,15 @@
|
||||
getContent(apiParams)
|
||||
.then((c) => {
|
||||
if (c) {
|
||||
if (type === "changedLanguage") {
|
||||
if (type === "changedLanguage" && path !== c.path) {
|
||||
let newPath = c.path + $location.search
|
||||
window.history.pushState({}, document.getElementsByTagName("title")[0].innerHTML, newPath + "/")
|
||||
blockedDoubleCall = true
|
||||
path = newPath
|
||||
}
|
||||
|
||||
connectedContentNotFound = false
|
||||
content = c
|
||||
connectedContentNotFound = false
|
||||
} else {
|
||||
// Show message if not found simmilar content for changed language
|
||||
if (filter.tags) {
|
||||
@@ -73,25 +76,28 @@
|
||||
})
|
||||
}
|
||||
|
||||
const loadArticle = (type?: string) => {
|
||||
const loadArticle = (type?: string, locale: string = null) => {
|
||||
let pathParts = path.split("/")
|
||||
let slugOrId = pathParts[pathParts.length - 1].toString()
|
||||
let urlLocale = pathParts[0].toString()
|
||||
|
||||
let loc = $currentLang
|
||||
if (urlLocale) {
|
||||
locale = urlLocale
|
||||
}
|
||||
|
||||
// Set default API call filter
|
||||
|
||||
let filter = {
|
||||
"article.general.locale": $currentLang,
|
||||
let filter: { [key: string]: any } = {
|
||||
"article.general.locale": loc,
|
||||
"article.content.slug": slugOrId,
|
||||
}
|
||||
|
||||
// Changed filter to find simmilar content for changed language
|
||||
// Changed filter to find simmilar article for changed language
|
||||
if (type === "changedLanguage") {
|
||||
// filter = {
|
||||
// tags: { $in: content?.tags },
|
||||
// locale: $currentLang,
|
||||
// }
|
||||
// delete filter["article.general.locale"]
|
||||
// delete filter["article.content.slug"]
|
||||
filter = {
|
||||
_id: { $in: article?.article?.assignments?.articles },
|
||||
"article.general.locale": $currentLang,
|
||||
}
|
||||
}
|
||||
|
||||
// Get content by API call
|
||||
@@ -102,7 +108,26 @@
|
||||
loading = true
|
||||
getArticles("articles", apiParams)
|
||||
.then((respoonse) => {
|
||||
article = respoonse[0]
|
||||
if (respoonse) {
|
||||
if (type === "changedLanguage") {
|
||||
let newPath =
|
||||
"/" +
|
||||
respoonse[0].article.assignments.pages +
|
||||
"/" +
|
||||
respoonse[0].article.content.slug +
|
||||
$location.search
|
||||
window.history.pushState({}, document.getElementsByTagName("title")[0].innerHTML, newPath)
|
||||
}
|
||||
|
||||
article = respoonse[0]
|
||||
} else {
|
||||
article = null
|
||||
|
||||
// Redirect to HOME if no content has been found. So the page 404 content will never shown.
|
||||
if (!content) {
|
||||
navigate("/")
|
||||
}
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
loading = false
|
||||
@@ -110,29 +135,40 @@
|
||||
})
|
||||
}
|
||||
|
||||
currentLang.subscribe(() => {
|
||||
currentLang.subscribe((locale) => {
|
||||
currentLocale = locale
|
||||
|
||||
if (content) {
|
||||
loadContent("changedLanguage")
|
||||
} else if (article) {
|
||||
loadArticle("changedLanguage")
|
||||
loadArticle("changedLanguage", currentLocale)
|
||||
} else if (window.location.pathname.endsWith("/")) {
|
||||
loadContent()
|
||||
} else {
|
||||
loadArticle(currentLocale)
|
||||
}
|
||||
})
|
||||
|
||||
$: if (path) {
|
||||
content = null
|
||||
article = null
|
||||
|
||||
// Update current language when lang in URL not equal to current language
|
||||
let pathParts = window.location.pathname.split("/")
|
||||
if (pathParts.length > 3 && $currentLang !== pathParts[1]) {
|
||||
$currentLang = pathParts[1]
|
||||
// Set initial current locale for this page
|
||||
if (!currentLocale) {
|
||||
currentLocale = $currentLang
|
||||
}
|
||||
|
||||
if (window.location.pathname.endsWith("/")) {
|
||||
loadContent()
|
||||
} else {
|
||||
loadArticle()
|
||||
if (!blockedDoubleCall) {
|
||||
// Reset all content and articles
|
||||
content = null
|
||||
article = null
|
||||
|
||||
// Update current language when lang in URL not equal to current language
|
||||
if (path !== window.location.pathname) {
|
||||
let pathParts = window.location.pathname.split("/")
|
||||
if (pathParts.length > 3 && $currentLang !== pathParts[1]) {
|
||||
$currentLang = currentLocale
|
||||
}
|
||||
}
|
||||
}
|
||||
blockedDoubleCall = false
|
||||
}
|
||||
|
||||
afterUpdate(() => {
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
const setLanguage = (lang: string) => {
|
||||
$currentLang = lang
|
||||
locale.set($currentLang)
|
||||
locale.set(lang)
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user