Skip to content

Commit 6afb3ca

Browse files
Merge pull request #34 from brandonnorsworthy/render-random-quests
random quests are fetched and rendered to dev page
2 parents 53df115 + 666d7e3 commit 6afb3ca

File tree

9 files changed

+85
-18
lines changed

9 files changed

+85
-18
lines changed

src/App.tsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
22
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
33

4-
54
import LandingPage from './pages/LandingPage'
6-
import AuthenticatedDashboard from './pages/Dashboard';
5+
import Dashboard from './pages/Dashboard';
6+
import DevPage from './pages/DevPage';
77

88
import './App.css'
99

@@ -16,12 +16,17 @@ function App() {
1616
<QueryClientProvider client={queryClient}>
1717
<Router>
1818
<Routes>
19-
{/*public routes*/}
19+
{/* public routes */}
2020
<Route path="/" element={<LandingPage />} />
21-
{/* protected routes*/}
21+
22+
{/* protected routes */}
2223
<Route
2324
path="/dashboard"
24-
element={<AuthenticatedDashboard />}
25+
element={<Dashboard />}
26+
/>
27+
<Route
28+
path="/dev"
29+
element={<DevPage />}
2530
/>
2631
</Routes>
2732
</Router>

src/components/LoginPanel/LoginPanel.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const LoginPanel = () => {
5757
setOpen(false);
5858
}
5959
catch (error: unknown) {
60-
console.log(error);
60+
console.error(error);
6161
if (error instanceof AxiosError) {
6262
setError(error.response?.data.error || "An error occurred");
6363
return;

src/hocs/WithAuth/WithAuth.tsx

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import React, { useEffect, useState, ComponentType } from "react";
22
import { useNavigate } from "react-router-dom";
33

4-
5-
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
6-
type WithAuthProps = {
7-
8-
};
9-
104
const withAuth = <P extends object>(WrappedComponent: ComponentType<P>) => {
11-
const ComponentWithAuth: React.FC<P & WithAuthProps> = (props) => {
5+
const ComponentWithAuth: React.FC<P> = (props) => {
126
const navigate = useNavigate();
137
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);
148

159
useEffect(() => {
1610
const token = localStorage.getItem("token");
1711
if (!token) {
18-
navigate("/"); //maybe propmt them to login?
12+
navigate("/"); //maybe propmt them to login?
1913
return;
2014
}
2115

src/hooks/useAuth.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ import { useState, useEffect } from "react";
22

33
const TOKEN_STORAGE_KEY = "token";
44

5+
interface User {
6+
userId: number;
7+
username: string;
8+
role: string;
9+
iat: number;
10+
}
11+
512
const useAuth = () => {
613
const [accessToken, setAccessToken] = useState<string | null>(null);
7-
const [user, setUser] = useState<unknown | null>(null);
14+
const [user, setUser] = useState<User | null>(null);
815

916
useEffect(() => {
1017
const storedToken = localStorage.getItem(TOKEN_STORAGE_KEY);
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface Quest {
2+
title: string,
3+
description: string,
4+
objectives: string[],
5+
image_url: string,
6+
}

src/pages/Dashboard/Dashboard.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ const Dashboard: React.FC = () => {
1717
};
1818

1919
const AuthenticatedDashboard = withAuth(Dashboard);
20+
2021
export default AuthenticatedDashboard;

src/pages/DevPage.tsx

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import useAuth from "@/hooks/useAuth"
2+
import questService from "@/service/questService";
3+
import { useState } from "react";
4+
5+
import { Quest } from "../models/QuestModels/questResponse"
6+
7+
const DevPage: React.FC = () => {
8+
const { accessToken } = useAuth();
9+
const [currentQuest, setCurrentQuest] = useState<Quest | null>(null);
10+
11+
const handleGetRandomQuest = async () => {
12+
if (!accessToken) return;
13+
const randomQuestResponse = await questService.getRandomQuest(accessToken);
14+
15+
setCurrentQuest(randomQuestResponse);
16+
}
17+
18+
return (
19+
<div>
20+
{
21+
<button
22+
onClick={handleGetRandomQuest}>
23+
Get Random Test
24+
</button>
25+
}
26+
{
27+
currentQuest &&
28+
<div className="flex flex-col">
29+
<span className="text-xlg">{currentQuest.title}</span>
30+
<span className="text-lg">{currentQuest.description}</span>
31+
{
32+
currentQuest.objectives?.map((objective, index) =>
33+
<span key={objective + index}>{objective}</span>
34+
)}
35+
</div>
36+
}
37+
</div>
38+
)
39+
}
40+
41+
export default DevPage

src/pages/LandingPage/LandingPage.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import LoginPanel from "@/components/LoginPanel";
22
import SuggestionsPanel from "@/components/SuggestionsPanel/SuggestionsPanel";
3-
3+
import { Link } from "react-router-dom";
44

55
const LandingPage = () => {
6-
76
return (
87
<div className="h-screen font-bold text-white bg-idk font-rust-like">
98
<header className='flex justify-between p-4 text-3xl '>
109
{/* logo? */}
1110
<h1 className='text-5xl'>
1211
Welcome to the resurected rust app
1312
</h1>
14-
13+
<Link to='/dev'>Dev hre</Link>
1514
</header>
1615
<main className="px-12 py-2">
1716
<div className="flex flex-col items-start gap-4 pl-32 text-4xl mt-60">

src/service/questService.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sendRequest from "@/lib/sendRequest";
2+
import { Quest } from "../models/QuestModels/questResponse"
3+
4+
const basePath = '/quests';
5+
6+
export default {
7+
getRandomQuest: async (accessToken: string) => {
8+
return await sendRequest({
9+
method: 'GET',
10+
endpoint: `${basePath}/random-quest?filters=pvp,raiding`,
11+
accessToken
12+
}) as Promise<Quest>;
13+
},
14+
}

0 commit comments

Comments
 (0)