From 14c057d4a62a2f92a6c2fbeff7fc35965bd91e8c Mon Sep 17 00:00:00 2001 From: Sandu Victor Date: Sun, 12 Jul 2020 12:00:09 +0300 Subject: [PATCH] feat: added working name and flag to profile + some working settings --- .../src/components/common/navbar/navBar.tsx | 6 +- .../src/components/loginPage/loginPage.tsx | 2 +- .../components/profilePage/profilePage.tsx | 115 ++++++++++++++++-- .../web/src/components/profilePage/style.tsx | 14 ++- packages/web/src/index.tsx | 2 +- 5 files changed, 128 insertions(+), 11 deletions(-) diff --git a/packages/web/src/components/common/navbar/navBar.tsx b/packages/web/src/components/common/navbar/navBar.tsx index 36f5522..a6a9407 100644 --- a/packages/web/src/components/common/navbar/navBar.tsx +++ b/packages/web/src/components/common/navbar/navBar.tsx @@ -46,7 +46,11 @@ export const Navbar = () => { onClick={async () => { isLoggedFunc(); }} - to={isLogged ? "/profile" : "/loginregister/login"} + to={ + isLogged + ? `/profile/${localStorage.getItem("userid")}` + : "/loginregister/login" + } > {isLogged ? "Profile" : "Login"} diff --git a/packages/web/src/components/loginPage/loginPage.tsx b/packages/web/src/components/loginPage/loginPage.tsx index 2bdce86..c367385 100644 --- a/packages/web/src/components/loginPage/loginPage.tsx +++ b/packages/web/src/components/loginPage/loginPage.tsx @@ -105,7 +105,7 @@ const RegiserForm = () => { statusDiv.current.innerHTML = "Geolocation is not supported by this browser."; } - return ""; + return "us"; }; const checkRegisterFields = (): boolean => { diff --git a/packages/web/src/components/profilePage/profilePage.tsx b/packages/web/src/components/profilePage/profilePage.tsx index 6448712..9c5d0bf 100644 --- a/packages/web/src/components/profilePage/profilePage.tsx +++ b/packages/web/src/components/profilePage/profilePage.tsx @@ -1,10 +1,91 @@ -import React from "react"; +import React, { useEffect, useState } from "react"; +import { Link } from "react-router-dom"; import { apiUrl } from "../../utils/constants"; -import { Wrapper, InsideWrapper } from "./style"; +import { Wrapper, InsideWrapper, ProfileName, FlagImage } from "./style"; export const ProfilePage = () => { + const [userId, setUserId] = useState(localStorage.getItem("userid")); + const [countryList, setCountryList] = useState( + + ); + const [countryValue, setCountryValue] = useState("AF"); + const [userData, setUserData] = useState({}); + const [countryFlagUrl, setCountryFlagUrl] = useState( + "https://restcountries.eu/data/usa.svg" + ); + + useEffect(() => { + updateCountryList(); + updateUserId(); + updateUserData(); + updateCountryFlagUrl(); + }, [userData?.data?.country]); + + const updateCountryList = async () => { + setCountryList(await generateCountryOptions()); + }; + + const updateUserData = async () => { + setUserData(await getUserData(userId)); + }; + + const updateUserId = async () => { + setUserId(localStorage.getItem("userid")); + }; + + const updateCountryFlagUrl = async () => { + setCountryFlagUrl(await getCountryUrlFlag()); + }; + + const getCountryUrlFlag = async () => { + const data = await ( + await fetch( + `https://restcountries.eu/rest/v2/alpha/${userData?.data?.country}`, + { + method: "GET" + } + ) + ).json(); + + return data.flag; + }; + + const getUserData = async (id: string) => { + const userData = await fetch(`${apiUrl}/users/userData/${id}`, { + method: "GET", + credentials: "include", + headers: { + "Content-Type": "application/json" + } + }); + + return await userData.json(); + }; + + const generateCountryOptions = async () => { + const data = await ( + await fetch("https://restcountries.eu/rest/v2", { + method: "GET" + }) + ).json(); + + const generated = data.map((value, index) => { + return ( + + ); + }); + + return generated; + }; + + const getUrlUserId = () => { + return location.hash.split("/")[location.hash.split("/").length - 1]; + }; + const switchTheme = () => { const currentTheme = localStorage.getItem("theme"); if (currentTheme === "light") { @@ -18,6 +99,10 @@ export const ProfilePage = () => { return ( + + + {userData?.data?.name} + + + + ); diff --git a/packages/web/src/components/profilePage/style.tsx b/packages/web/src/components/profilePage/style.tsx index f022633..37d55e9 100644 --- a/packages/web/src/components/profilePage/style.tsx +++ b/packages/web/src/components/profilePage/style.tsx @@ -7,9 +7,21 @@ export const Wrapper = styled.div` border-left: 1px solid ${theme.background.secondary}; border-right: 1px solid ${theme.background.secondary}; width: 80%; - height: 100%; + min-height: 100vh; margin: auto; `; export const InsideWrapper = styled.div` padding: 30px; `; + +export const ProfileName = styled.div` + color: ${theme.text.primary}; + font-size: 48px; + font-style: italic; + font-family: "Verdana"; + padding-bottom: 20px; +`; +export const FlagImage = styled.img` + max-height: 24px; + margin-right: 10px; +`; diff --git a/packages/web/src/index.tsx b/packages/web/src/index.tsx index 5cec8cf..8d7c7ae 100644 --- a/packages/web/src/index.tsx +++ b/packages/web/src/index.tsx @@ -35,7 +35,7 @@ const App = () => { - +