From ad45894074de7fd7475fdf4f148bb9d56b6faeb1 Mon Sep 17 00:00:00 2001 From: EliNygard Date: Wed, 22 May 2024 10:36:35 +0200 Subject: [PATCH] adding if user not logged in --- post/index.mjs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/post/index.mjs b/post/index.mjs index 4fb55ca..df9aaa9 100644 --- a/post/index.mjs +++ b/post/index.mjs @@ -9,25 +9,31 @@ import { generatePostPageHtml } from "../javascript/generateHtml/postPageHtml.mj import { hideLoader, showLoader } from "../javascript/ui/loader.mjs"; async function renderBlogPostPage(url) { - showLoader(); + const responseData = await getPosts(url); + console.log(responseData); + const singlePost = responseData.data; + console.log(singlePost); - try { - // Promise for testing loader, REMOVE - // await new Promise((resolve) => setTimeout(resolve, 1000)); - const responseData = await getPosts(url); - console.log(responseData); - const singlePost = responseData.data; - console.log(singlePost); + const main = document.querySelector("main"); + // main.innerHTML = ''; + const blogPost = generatePostPageHtml(singlePost); + main.appendChild(blogPost); +} - const main = document.querySelector("main"); - // main.innerHTML = ''; - const blogPost = generatePostPageHtml(singlePost); - main.appendChild(blogPost); - } catch (error) { - console.error("Error", error); +async function main() { + const userName = JSON.parse(localStorage.getItem("userName")); + showLoader(); + try { + if (userName) { + await renderBlogPostPage(API_BASE + API_POSTS + API_NAME + API_ID); + } else { + // If user is not logged in: + await renderBlogPostPage(API_BASE + API_POSTS + "/Leli_Nygard" + API_ID); + } + } catch { + console.error(error); } finally { hideLoader(); } } - -await renderBlogPostPage(API_BASE + API_POSTS + API_NAME + API_ID); +main();