Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
feat: added working name and flag to profile + some working settings
Browse files Browse the repository at this point in the history
  • Loading branch information
vycdev committed Jul 12, 2020
1 parent 9bb5d49 commit 14c057d
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 11 deletions.
6 changes: 5 additions & 1 deletion packages/web/src/components/common/navbar/navBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
</Link>
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/components/loginPage/loginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const RegiserForm = () => {
statusDiv.current.innerHTML =
"Geolocation is not supported by this browser.";
}
return "";
return "us";
};

const checkRegisterFields = (): boolean => {
Expand Down
115 changes: 108 additions & 7 deletions packages/web/src/components/profilePage/profilePage.tsx
Original file line number Diff line number Diff line change
@@ -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(
<option key={"default"}>Loading...</option>
);
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 (
<option key={index + value.alpha2Code} value={value.alpha2Code}>
{value.name}
</option>
);
});

return generated;
};

const getUrlUserId = () => {
return location.hash.split("/")[location.hash.split("/").length - 1];
};

const switchTheme = () => {
const currentTheme = localStorage.getItem("theme");
if (currentTheme === "light") {
Expand All @@ -18,6 +99,10 @@ export const ProfilePage = () => {
return (
<Wrapper>
<InsideWrapper>
<ProfileName>
<FlagImage src={countryFlagUrl}></FlagImage>
{userData?.data?.name}
</ProfileName>
<button
onClick={async () => {
await fetch(`${apiUrl}/auth/logout`, {
Expand All @@ -41,22 +126,38 @@ export const ProfilePage = () => {
</button>
<button
onClick={async () => {
const updateCountry = await (
await (
await fetch(`${apiUrl}/users/updateCountry`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ country: "Ly" })
body: JSON.stringify({ country: countryValue })
})
).text();

console.log(updateCountry);
updateCountryFlagUrl();
updateUserData();
}}
>
UpdateCountry to {countryValue}
</button>
<button
onClick={() => {
console.log(getUrlUserId());
}}
>
UpdateCountry to Ro
Log user id from url
</button>
<select
name="countryCode"
onChange={e => {
setCountryValue(e.target.value);
}}
>
{countryList}
</select>
<img />
</InsideWrapper>
</Wrapper>
);
Expand Down
14 changes: 13 additions & 1 deletion packages/web/src/components/profilePage/style.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
`;
2 changes: 1 addition & 1 deletion packages/web/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const App = () => {
<Route exact path="/stats">
<StatisticsPage />
</Route>
<Route exact path="/profile">
<Route exact path="/profile/:id">
<ProfilePage />
</Route>
<Route path="/loginregister">
Expand Down

0 comments on commit 14c057d

Please sign in to comment.