From ffd3c99292197f85d549806a3253d3794edd46f7 Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Thu, 17 Nov 2022 19:19:33 +0100 Subject: [PATCH] Full refactor to Typescript --- client/App.tsx | 33 +++++++++++++++---- client/{api.jsx => api.tsx} | 4 +-- client/components/{Header.jsx => Header.tsx} | 16 ++++++--- .../components/{Markdown.jsx => Markdown.tsx} | 16 +++++---- ...{MarkdownEditor.jsx => MarkdownEditor.tsx} | 20 +++++++---- .../components/{Problem.jsx => Problem.tsx} | 12 +++++-- client/components/Select.jsx | 12 ------- client/components/Select.tsx | 23 +++++++++++++ .../components/{Solution.jsx => Solution.tsx} | 22 ++++++++----- client/entry-client.tsx | 2 +- client/entry-server.tsx | 3 +- client/hooks.tsx | 31 ++++++++++++----- client/pages/Admin.tsx | 12 +++---- client/pages/Home.tsx | 11 ++++--- client/pages/Login.tsx | 4 ++- client/pages/Problem.tsx | 27 +++++++++------ client/pages/Profile.tsx | 18 +++++----- server/db/database.ts | 8 +++-- shared/model.ts | 3 +- 19 files changed, 181 insertions(+), 96 deletions(-) rename client/{api.jsx => api.tsx} (85%) rename client/components/{Header.jsx => Header.tsx} (77%) rename client/components/{Markdown.jsx => Markdown.tsx} (73%) rename client/components/{MarkdownEditor.jsx => MarkdownEditor.tsx} (72%) rename client/components/{Problem.jsx => Problem.tsx} (68%) delete mode 100644 client/components/Select.jsx create mode 100644 client/components/Select.tsx rename client/components/{Solution.jsx => Solution.tsx} (50%) diff --git a/client/App.tsx b/client/App.tsx index 28dace6..2d67f83 100644 --- a/client/App.tsx +++ b/client/App.tsx @@ -8,7 +8,7 @@ import { LoginPage } from './pages/Login' import { ProblemPage } from './pages/Problem' import { ProfilePage } from './pages/Profile' -const Redirect = ({ to }: { default: boolean; to: string }) => { +const Redirect = ({ to }: { to: string }) => { useEffect(() => { route(to, true) }, []) @@ -23,12 +23,31 @@ const Redirect = ({ to }: { default: boolean; to: string }) => { export const App = ({ url }: { url?: string }) => { return ( - - - - - - + + + + + + ) } diff --git a/client/api.jsx b/client/api.tsx similarity index 85% rename from client/api.jsx rename to client/api.tsx index b2e3135..dfb264a 100644 --- a/client/api.jsx +++ b/client/api.tsx @@ -1,9 +1,9 @@ export const server = { - async get(url) { + async get(url: string) { const res = await fetch(url, { credentials: 'include' }) return await res.json() }, - async post(url, body) { + async post(url: string, body?: T) { const res = await fetch(url, { method: 'POST', credentials: 'include', diff --git a/client/components/Header.jsx b/client/components/Header.tsx similarity index 77% rename from client/components/Header.jsx rename to client/components/Header.tsx index bd08868..238ad30 100644 --- a/client/components/Header.jsx +++ b/client/components/Header.tsx @@ -1,12 +1,18 @@ import { Link } from 'preact-router/match' -import { isAdministrator, USER_ROLE_ADMIN, USER_ROLE_MODERATOR } from '../../shared/constants.js' +import { isAdministrator, User, UserRole } from '../../shared/model' -const ROLE_LABEL = { - [USER_ROLE_ADMIN]: 'Admin', - [USER_ROLE_MODERATOR]: 'Moderatore', +const ROLE_LABEL: Record = { + ['admin']: 'Admin', + ['moderator']: 'Moderatore', + ['student']: 'Studente', } -export const Header = ({ user, noLogin }) => ( +type Props = { + user?: User | null + noLogin?: boolean +} + +export const Header = ({ user, noLogin }: Props) => (