Skip to content

Commit

Permalink
Major refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andersholt committed May 4, 2022
1 parent ee320dc commit 4970fd8
Show file tree
Hide file tree
Showing 13 changed files with 437 additions and 37 deletions.
5 changes: 5 additions & 0 deletions client/src/components/navbar/navbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
height: 50px;
}

.navbar .pages h2 {
display: inline-block;
font-size: 20px;
}

.navbar .profile-stuff img {
vertical-align: middle;
display: inline-block;
Expand Down
41 changes: 34 additions & 7 deletions client/src/components/navbar/navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,48 @@ export async function handleLogout() {
window.location.reload();
}

export function Navbar() {

export function Navbar({editMode, setEditMode}) {
const {userinfo} = useContext(ProfileContext)

function switchEditMode() {
if (editMode === true) {
setEditMode(false)
} else {
setEditMode(true)
}
}

let editButtonText = "Enter editmode"
if (editMode === true) {
editButtonText = "Leave editmode"
}

return (
<div className={"navbar"}>
<div className={"pages"}>
<h2>Kristiania News</h2>
<Link to={"/"}><p>Home</p></Link>
<Link to={"/"}><p>Page 2</p></Link>
<Link to={"/"}><p>Page 3</p></Link>
{(userinfo && userinfo.usertype) === "kristiania" &&
<>
<Link to={"/article/new"}><p>Write new article</p></Link>
<button onClick={switchEditMode}>{editButtonText}</button>
</>
}
</div>

<div className={"profile-stuff"}>
<Link to={"/profile"}><p>Profile</p></Link>
<button onClick={handleLogout}>Log out</button>
{(userinfo.picture && userinfo.usertype !== "kristiania") &&
<img src={userinfo.picture}/>
{userinfo &&
<>
<Link to={"/profile"}><p>Profile</p></Link>
<button onClick={handleLogout}>Log out</button>
{(userinfo.picture && userinfo.usertype !== "kristiania") &&
<img src={userinfo.picture}/>
}
</>
}
{!userinfo &&
<Link to={"/login"}>Login</Link>
}
</div>
</div>
Expand Down
21 changes: 2 additions & 19 deletions client/src/hooks/loginProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, {useContext, useEffect, useState} from "react";
import {fetchJSON} from "./global";
import {Link, useNavigate, useParams} from "react-router-dom";
import LoadingAnimation from "../pages/loadingpage/LoadingPage";
import {sha256} from "../lib/sha256";
import {randomString} from "../lib/randomString";

export const ProfileContext = React.createContext({
userinfo: undefined,
Expand Down Expand Up @@ -157,22 +159,3 @@ export function LoginCallback({reload}) {
return <LoadingAnimation/>;
}

async function sha256(string) {
const binaryHash = await crypto.subtle.digest(
"SHA-256",
new TextEncoder("utf-8").encode(string)
);
return btoa(String.fromCharCode.apply(null, new Uint8Array(binaryHash)))
.split("=")[0]
.replace(/\+/g, "-")
.replace(/\//g, "_");
}

export function randomString(length) {
const possible = "ABCDEFGIKJKNKLBIELILNLA01234456789";
let result = "";
for (let i = 0; i < length; i++) {
result += possible.charAt(Math.floor(Math.random() * possible.length));
}
return result;
}
5 changes: 4 additions & 1 deletion client/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import ReactDOM from "react-dom";
import {BrowserRouter, Route, Routes} from "react-router-dom";
import {fetchJSON} from "./hooks/global";
import {LoginCallback, LoginProvider, ProfileContext,} from "./hooks/loginProvider";
import {FrontPage} from "./pages/frontpage/frontpage";
import {ArticleWrite, FrontPage} from "./pages/frontpage/frontpage";
import LoadingAnimation from "./pages/loadingpage/LoadingPage";
import "./index.css"
import {ProfilePage} from "./pages/profilepage/profilepage";
import {LoginPage} from "./pages/loginpage/loginPage";

function Application() {
const [loading, setLoading] = useState(true);
Expand Down Expand Up @@ -35,6 +36,8 @@ function Application() {
<Routes>
<Route path={"/"} element={<FrontPage reload={loadLoginInfo}/>}/>
<Route path={"/login/:provider"} element={<LoginProvider/>}/>
<Route path={"/article/new"} element={<ArticleWrite/>}/>
<Route path={"/login"} element={<LoginPage/>}/>
<Route
path={"/login/:provider/callback"}
element={<LoginCallback reload={loadLoginInfo}/>}
Expand Down
8 changes: 8 additions & 0 deletions client/src/lib/randomString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function randomString(length) {
const possible = "ABCDEFGIKJKNKLBIELILNLA01234456789";
let result = "";
for (let i = 0; i < length; i++) {
result += possible.charAt(Math.floor(Math.random() * possible.length));
}
return result;
}
10 changes: 10 additions & 0 deletions client/src/lib/sha256.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export async function sha256(string) {
const binaryHash = await crypto.subtle.digest(
"SHA-256",
new TextEncoder("utf-8").encode(string)
);
return btoa(String.fromCharCode.apply(null, new Uint8Array(binaryHash)))
.split("=")[0]
.replace(/\+/g, "-")
.replace(/\//g, "_");
}
45 changes: 45 additions & 0 deletions client/src/pages/frontpage/frontpage.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,49 @@
width: fit-content;
margin: auto;
text-align: center;
}


.article-list {
width: 40vh;
border-right: black solid;
text-align: center;
height: 100vh;
}

.article-list input {
width: 30vh;
margin-top: 2vh;
}

#article-card {
margin: 10px;
}

main textarea {
width: 50vw;
height: 30vh;
}

main input {
width: 20vw;
height: 4vh;
font-size: 20px;
}

#article-card:hover {
background: black;
color: white;
transition: all ease-in-out .1s;
cursor: pointer;
}

.frontpage-container {
display: flex;
}

.frontpage-container main {
width: 50vw;
margin: auto;
margin-top: 0;
}
Loading

0 comments on commit 4970fd8

Please sign in to comment.