Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random quests are fetched and rendered to dev page #34

Merged
merged 2 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';


import LandingPage from './pages/LandingPage'
import AuthenticatedDashboard from './pages/Dashboard';
import Dashboard from './pages/Dashboard';
import DevPage from './pages/DevPage';

import './App.css'

Expand All @@ -16,12 +16,17 @@ function App() {
<QueryClientProvider client={queryClient}>
<Router>
<Routes>
{/*public routes*/}
{/* public routes */}
<Route path="/" element={<LandingPage />} />
{/* protected routes*/}

{/* protected routes */}
<Route
path="/dashboard"
element={<AuthenticatedDashboard />}
element={<Dashboard />}
/>
<Route
path="/dev"
element={<DevPage />}
/>
</Routes>
</Router>
Expand Down
2 changes: 1 addition & 1 deletion src/components/LoginPanel/LoginPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const LoginPanel = () => {
setOpen(false);
}
catch (error: unknown) {
console.log(error);
console.error(error);
if (error instanceof AxiosError) {
setError(error.response?.data.error || "An error occurred");
return;
Expand Down
10 changes: 2 additions & 8 deletions src/hocs/WithAuth/WithAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import React, { useEffect, useState, ComponentType } from "react";
import { useNavigate } from "react-router-dom";


// eslint-disable-next-line @typescript-eslint/no-empty-object-type
type WithAuthProps = {

};

const withAuth = <P extends object>(WrappedComponent: ComponentType<P>) => {
const ComponentWithAuth: React.FC<P & WithAuthProps> = (props) => {
const ComponentWithAuth: React.FC<P> = (props) => {
const navigate = useNavigate();
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);

useEffect(() => {
const token = localStorage.getItem("token");
if (!token) {
navigate("/"); //maybe propmt them to login?
navigate("/"); //maybe propmt them to login?
return;
}

Expand Down
9 changes: 8 additions & 1 deletion src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import { useState, useEffect } from "react";

const TOKEN_STORAGE_KEY = "token";

interface User {
userId: number;
username: string;
role: string;
iat: number;
}

const useAuth = () => {
const [accessToken, setAccessToken] = useState<string | null>(null);
const [user, setUser] = useState<unknown | null>(null);
const [user, setUser] = useState<User | null>(null);

useEffect(() => {
const storedToken = localStorage.getItem(TOKEN_STORAGE_KEY);
Expand Down
6 changes: 6 additions & 0 deletions src/models/QuestModels/questResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Quest {
title: string,
description: string,
objectives: string[],
image_url: string,
}
1 change: 1 addition & 0 deletions src/pages/Dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ const Dashboard: React.FC = () => {
};

const AuthenticatedDashboard = withAuth(Dashboard);

export default AuthenticatedDashboard;
41 changes: 41 additions & 0 deletions src/pages/DevPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import useAuth from "@/hooks/useAuth"
import questService from "@/service/questService";
import { useState } from "react";

import { Quest } from "../models/QuestModels/questResponse"

const DevPage: React.FC = () => {
const { accessToken } = useAuth();
const [currentQuest, setCurrentQuest] = useState<Quest | null>(null);

const handleGetRandomQuest = async () => {
if (!accessToken) return;
const randomQuestResponse = await questService.getRandomQuest(accessToken);

setCurrentQuest(randomQuestResponse);
}

return (
<div>
{
<button
onClick={handleGetRandomQuest}>
Get Random Test
</button>
}
{
currentQuest &&
<div className="flex flex-col">
<span className="text-xlg">{currentQuest.title}</span>
<span className="text-lg">{currentQuest.description}</span>
{
currentQuest.objectives?.map((objective, index) =>
<span key={objective + index}>{objective}</span>
)}
</div>
}
</div>
)
}

export default DevPage
5 changes: 2 additions & 3 deletions src/pages/LandingPage/LandingPage.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import LoginPanel from "@/components/LoginPanel";
import SuggestionsPanel from "@/components/SuggestionsPanel/SuggestionsPanel";

import { Link } from "react-router-dom";

const LandingPage = () => {

return (
<div className="h-screen font-bold text-white bg-idk font-rust-like">
<header className='flex justify-between p-4 text-3xl '>
{/* logo? */}
<h1 className='text-5xl'>
Welcome to the resurected rust app
</h1>

<Link to='/dev'>Dev hre</Link>
</header>
<main className="px-12 py-2">
<div className="flex flex-col items-start gap-4 pl-32 text-4xl mt-60">
Expand Down
14 changes: 14 additions & 0 deletions src/service/questService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import sendRequest from "@/lib/sendRequest";
import { Quest } from "../models/QuestModels/questResponse"

const basePath = '/quests';

export default {
getRandomQuest: async (accessToken: string) => {
return await sendRequest({
method: 'GET',
endpoint: `${basePath}/random-quest?filters=pvp,raiding`,
accessToken
}) as Promise<Quest>;
},
}