From 7264b269c0a06f6c060941452b483ae2dc286145 Mon Sep 17 00:00:00 2001 From: morbalint Date: Sat, 23 Dec 2023 21:37:11 +0100 Subject: [PATCH] kinda works, but wasteful and misinforming :( --- Kemkas.Web/Controllers/CharacterController.cs | 2 +- Kemkas.Web/Controllers/UserController.cs | 18 ++++ Kemkas.Web/Program.cs | 2 +- Kemkas.Web/frontend/src/App.tsx | 89 ++++++++++++++----- .../src/first-edition/api/character.api.ts | 2 +- .../src/first-edition/pages/CharacterList.tsx | 4 +- .../pages => shared}/ErrorBoundary.tsx | 0 Kemkas.Web/nginx.conf | 2 +- 8 files changed, 93 insertions(+), 26 deletions(-) create mode 100644 Kemkas.Web/Controllers/UserController.cs rename Kemkas.Web/frontend/src/{first-edition/pages => shared}/ErrorBoundary.tsx (100%) diff --git a/Kemkas.Web/Controllers/CharacterController.cs b/Kemkas.Web/Controllers/CharacterController.cs index 14136ec..0d42bb8 100644 --- a/Kemkas.Web/Controllers/CharacterController.cs +++ b/Kemkas.Web/Controllers/CharacterController.cs @@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Mvc; namespace Kemkas.Web.Controllers; [ApiController] -[Route("[controller]")] +[Route("api/[controller]")] public class CharacterController( ICharacterValidationService validationService, ICharacterDtoToDbModelService dtoToDbModelService, diff --git a/Kemkas.Web/Controllers/UserController.cs b/Kemkas.Web/Controllers/UserController.cs new file mode 100644 index 0000000..6c11d68 --- /dev/null +++ b/Kemkas.Web/Controllers/UserController.cs @@ -0,0 +1,18 @@ +using Kemkas.Web.Db.Models; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; + +namespace Kemkas.Web.Controllers; + +[Route("api/[controller]")] +[ApiController] +public class UserController : ControllerBase +{ + // GET + [HttpGet("me")] + public async Task Me([FromServices] UserManager userManager) + { + var user = await userManager.GetUserAsync(User); + return user?.UserName ?? user?.Email; + } +} \ No newline at end of file diff --git a/Kemkas.Web/Program.cs b/Kemkas.Web/Program.cs index 52249cc..f02d4a1 100644 --- a/Kemkas.Web/Program.cs +++ b/Kemkas.Web/Program.cs @@ -70,7 +70,7 @@ app.UseAuthorization(); app.MapControllerRoute( name: "default", - pattern: "{controller}/{action=Index}/{id?}"); + pattern: "/api/{controller}/{action=Index}/{id?}"); app.MapRazorPages(); app.MapFallbackToFile("index.html"); diff --git a/Kemkas.Web/frontend/src/App.tsx b/Kemkas.Web/frontend/src/App.tsx index 5f8a32c..150411b 100644 --- a/Kemkas.Web/frontend/src/App.tsx +++ b/Kemkas.Web/frontend/src/App.tsx @@ -1,54 +1,103 @@ -import React from 'react'; +import React, {useEffect, useState} from 'react'; import './App.css'; -import CreateCharacter from "./first-edition/pages/CreateCharacter"; -import { createBrowserRouter, RouterProvider } from "react-router-dom"; import {Faro} from "@grafana/faro-web-sdk"; +import {createBrowserRouter, RouterProvider} from "react-router-dom"; +import CreateCharacter from "./first-edition/pages/CreateCharacter"; import CharacterList from "./first-edition/pages/CharacterList"; -import ErrorBoundary from "./first-edition/pages/ErrorBoundary"; +import ErrorBoundary from "./shared/ErrorBoundary"; + +type LoadingState = "not-started" | "loading" | "finished" function App(props: {faro?: Faro}) { const router = createBrowserRouter([ { path: "/", element: , + ErrorBoundary: ErrorBoundary, }, { path: "/karaktereim", element: , - loader: () => fetch(`/Character`), - ErrorBoundary: ErrorBoundary + loader: () => fetch(`api/Character`), + ErrorBoundary: ErrorBoundary, }, { path: "/:id", element: , - loader: args => fetch(`/Character/${args.params.id}`) + loader: args => fetch(`api/Character/${args.params.id}`), + ErrorBoundary: ErrorBoundary, } ]); + + let [loading, setLoading] = useState("not-started" as LoadingState); + let [userName, setUserName] = useState(null as string | null); + useEffect(() => { + let ignore = false; + if (loading === "not-started") { + setLoading("loading"); + fetch('api/User/me') + .then(response => { + if (response.ok) { + response.text() + .then((data) => data?.length > 0 ? setUserName(data) : setUserName(null)) + .catch((err) => { + console.log(err) + !ignore && setUserName(null); + }) + } else { + console.log(response) + !ignore && setUserName(null); + } + }) + .catch((err) => { + console.log(err); + !ignore && setUserName(null) + }) + .finally(() => { + setLoading("finished"); + }) + } + return () => { + ignore = true + } + }, [loading, setLoading, userName, setUserName]) + return ( <>
diff --git a/Kemkas.Web/frontend/src/first-edition/api/character.api.ts b/Kemkas.Web/frontend/src/first-edition/api/character.api.ts index 96634c8..91b5df2 100644 --- a/Kemkas.Web/frontend/src/first-edition/api/character.api.ts +++ b/Kemkas.Web/frontend/src/first-edition/api/character.api.ts @@ -2,7 +2,7 @@ import {KarakterInputs} from "../domain-models/karakter"; import axios from "axios"; export async function StoreNewCharacter(karakter: KarakterInputs) { - let response = await axios.post(`${window.location.origin}/Character/`, karakter, { + let response = await axios.post(`${window.location.origin}/api/Character/`, karakter, { withCredentials: true, }) if (response.status < 300){ diff --git a/Kemkas.Web/frontend/src/first-edition/pages/CharacterList.tsx b/Kemkas.Web/frontend/src/first-edition/pages/CharacterList.tsx index 139797d..2f3caec 100644 --- a/Kemkas.Web/frontend/src/first-edition/pages/CharacterList.tsx +++ b/Kemkas.Web/frontend/src/first-edition/pages/CharacterList.tsx @@ -25,8 +25,8 @@ function CharacterList(props: {faro?: Faro}) { {characters.map(c => ( - - + + ))} diff --git a/Kemkas.Web/frontend/src/first-edition/pages/ErrorBoundary.tsx b/Kemkas.Web/frontend/src/shared/ErrorBoundary.tsx similarity index 100% rename from Kemkas.Web/frontend/src/first-edition/pages/ErrorBoundary.tsx rename to Kemkas.Web/frontend/src/shared/ErrorBoundary.tsx diff --git a/Kemkas.Web/nginx.conf b/Kemkas.Web/nginx.conf index 8681ba3..6b9ef44 100644 --- a/Kemkas.Web/nginx.conf +++ b/Kemkas.Web/nginx.conf @@ -75,7 +75,7 @@ http { proxy_pass_request_headers on; } - location /Character/ { + location /api/ { proxy_pass https://backend; proxy_pass_request_headers on; }