From 1181564e9ebe2f5a93bd0df4250f9b4b39a3891a Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 31 May 2022 16:35:13 +0200 Subject: [PATCH] I keep forgetting to make smaller commits --- components/FormEventCreate.tsx | 3 + components/FormLoginTelegram.tsx | 79 +++++++++++++++++++++++ hooks/useTelegramToFestaCallback.ts | 30 --------- package.json | 3 + pages/api/events/mine.ts | 23 +++++++ pages/index.tsx | 98 +++++++++-------------------- public/locales/it-IT/common.json | 6 +- yarn.lock | 26 ++++++++ 8 files changed, 166 insertions(+), 102 deletions(-) create mode 100644 components/FormEventCreate.tsx create mode 100644 components/FormLoginTelegram.tsx delete mode 100644 hooks/useTelegramToFestaCallback.ts create mode 100644 pages/api/events/mine.ts diff --git a/components/FormEventCreate.tsx b/components/FormEventCreate.tsx new file mode 100644 index 0000000..94fc06d --- /dev/null +++ b/components/FormEventCreate.tsx @@ -0,0 +1,3 @@ +export function FormEventCreate() { + +} \ No newline at end of file diff --git a/components/FormLoginTelegram.tsx b/components/FormLoginTelegram.tsx new file mode 100644 index 0000000..1312643 --- /dev/null +++ b/components/FormLoginTelegram.tsx @@ -0,0 +1,79 @@ +import axios, { AxiosError } from "axios" +import {default as classNames} from "classnames" +import { useTranslation } from "next-i18next" +import { HTMLProps, useCallback, useState } from "react" +import { LoginContext } from "../contexts/login" +import { ApiError, ApiResult } from "../types/api" +import { FestaLoginData, TelegramLoginData } from "../types/user" +import { useDefinedContext } from "../utils/definedContext" +import { TelegramLoginButton } from "./TelegramLoginButton" + +export function FormLoginTelegram({className, ...props}: HTMLProps) { + const { t } = useTranslation("common") + const [_, setLogin] = useDefinedContext(LoginContext) + const [working, setWorking] = useState(false) + const [error, setError] = useState(null) + + const onLogin = useCallback( + async (data: TelegramLoginData) => { + setError(null) + setWorking(true) + + try { + var response = await axios.post>("/api/login?provider=telegram", data) + } + catch(e) { + const axe = e as AxiosError + setError(axe?.response?.data as ApiError | undefined) + return + } + finally { + setWorking(false) + } + + setLogin(response.data as FestaLoginData) + localStorage.setItem("login", JSON.stringify(response.data)) + }, + [] + ) + + const newClassName = classNames(className, { + "negative": error, + }) + + let message: string + let contents: JSX.Element + + if(error) { + message = t("formTelegramLoginError") + contents = ( +
+ + {JSON.stringify(error)} + +
+ ) + } + else if(working) { + message = t("formTelegramLoginWorking") + contents =
+ } + else { + message = t("formTelegramLoginDescription") + contents = ( + + ) + } + + return ( +
+

+ {message} +

+ {contents} +
+ ) +} \ No newline at end of file diff --git a/hooks/useTelegramToFestaCallback.ts b/hooks/useTelegramToFestaCallback.ts deleted file mode 100644 index 1b3143a..0000000 --- a/hooks/useTelegramToFestaCallback.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { default as axios, AxiosError } from "axios" -import { useCallback, Dispatch, SetStateAction } from "react" -import { ApiError, ApiResult } from "../types/api" -import { FestaLoginData, TelegramLoginData } from "../types/user" - - -export function useTelegramToFestaCallback(setLogin: Dispatch>, setError: Dispatch>, setWorking: Dispatch>): (data: TelegramLoginData) => Promise { - return useCallback( - async (data: TelegramLoginData) => { - setError(null) - setWorking(true) - - try { - var response = await axios.post>("/api/login?provider=telegram", data) - } - catch(e) { - const axe = e as AxiosError - setError(axe?.response?.data as ApiError | undefined) - return - } - finally { - setWorking(false) - } - - setLogin(response.data as FestaLoginData) - localStorage.setItem("login", JSON.stringify(response.data)) - }, - [] - ) -} \ No newline at end of file diff --git a/package.json b/package.json index 3fb8864..43c5a8d 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,9 @@ "studio": "dotenv -e .env.local prisma studio" }, "dependencies": { + "@fortawesome/fontawesome-svg-core": "^6.1.1", + "@fortawesome/free-solid-svg-icons": "^6.1.1", + "@fortawesome/react-fontawesome": "^0.1.18", "@prisma/client": "3.14.0", "axios": "^0.27.2", "classnames": "^2.3.1", diff --git a/pages/api/events/mine.ts b/pages/api/events/mine.ts new file mode 100644 index 0000000..2b36308 --- /dev/null +++ b/pages/api/events/mine.ts @@ -0,0 +1,23 @@ +import { client } from "../../../utils/prismaClient"; +import { NextApiRequest, NextApiResponse } from "next"; +import { ApiResult } from "../../../types/api"; +import { restInPeace } from "../../../utils/restInPeace"; +import { handleInterrupts } from "../../../utils/interrupt"; +import { authorizeUser } from "../../../utils/apiAuth"; +import { Event } from "@prisma/client"; + + +export default async function handler(req: NextApiRequest, res: NextApiResponse>) { + handleInterrupts(res, async () => { + const user = await authorizeUser(req, res) + + const where = { + creatorId: user.id + } + + await restInPeace(req, res, { + model: client.event, + list: {where} + }) + }) +} \ No newline at end of file diff --git a/pages/index.tsx b/pages/index.tsx index ad29870..2f2d697 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,12 +1,9 @@ import { NextPageContext } from 'next' import { useTranslation } from 'next-i18next' import { serverSideTranslations } from 'next-i18next/serverSideTranslations'; -import { useState } from 'react'; import { LoginContext } from '../contexts/login'; import { useDefinedContext } from '../utils/definedContext'; -import { ApiError } from '../types/api'; -import { TelegramLoginButton } from "../components/TelegramLoginButton" -import { useTelegramToFestaCallback } from '../hooks/useTelegramToFestaCallback'; +import { FormLoginTelegram } from '../components/FormLoginTelegram'; export async function getStaticProps(context: NextPageContext) { @@ -20,12 +17,9 @@ export async function getStaticProps(context: NextPageContext) { export default function PageIndex() { const { t } = useTranslation("common") - const [login, setLogin] = useDefinedContext(LoginContext) - const [working, setWorking] = useState(false) - const [error, setError] = useState(null) - const onLogin = useTelegramToFestaCallback(setLogin, setError, setWorking) - - if(!login) return ( + const [login, _] = useDefinedContext(LoginContext) + + return (

@@ -35,69 +29,35 @@ export default function PageIndex() { {t("siteSubtitle")}

- {error ? -
-

- {t("telegramLoginError")} -

-

- - {JSON.stringify(error)} - -

-
- : - working ? -
+ {login ? +
+
- : -
-

- {t("telegramLoginDescription")} -

- +
+ -
+ + +
+ : + }
) - - return ( -
-
-

- {t("siteTitle")} -

-

- {t("eventsSubtitleFirst")} -

-
-
- -
- - -
-
-
- ) } diff --git a/public/locales/it-IT/common.json b/public/locales/it-IT/common.json index f467fe4..806bfbb 100644 --- a/public/locales/it-IT/common.json +++ b/public/locales/it-IT/common.json @@ -1,9 +1,9 @@ { "siteTitle": "Festa", "siteSubtitle": "Organizza eventi con facilità!", - "telegramLoginDescription": "Per iniziare, effettua il login con Telegram.", - "telegramLoginWorking": "Un attimo solo, sto effettuando il login...", - "telegramLoginError": "Si è verificato il seguente errore durante il login con Telegram:", + "formTelegramLoginDescription": "Per iniziare, effettua il login con Telegram.", + "formTelegramLoginWorking": "Un attimo solo, sto effettuando il login...", + "formTelegramLoginError": "Si è verificato il seguente errore durante il login con Telegram:", "logOutPrompt": "Non sei tu?", "eventsSubtitleFirst": "Crea il tuo primo evento!", "eventsInputDescriptionFirst": "Dai un nome al tuo primo evento!", diff --git a/yarn.lock b/yarn.lock index c381969..e9fb1d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -32,6 +32,32 @@ minimatch "^3.1.2" strip-json-comments "^3.1.1" +"@fortawesome/fontawesome-common-types@6.1.1": + version "6.1.1" + resolved "https://registry.npmjs.org/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-6.1.1.tgz#7dc996042d21fc1ae850e3173b5c67b0549f9105" + integrity sha512-wVn5WJPirFTnzN6tR95abCx+ocH+3IFLXAgyavnf9hUmN0CfWoDjPT/BAWsUVwSlYYVBeCLJxaqi7ZGe4uSjBA== + +"@fortawesome/fontawesome-svg-core@^6.1.1": + version "6.1.1" + resolved "https://registry.npmjs.org/@fortawesome/fontawesome-svg-core/-/fontawesome-svg-core-6.1.1.tgz#3424ec6182515951816be9b11665d67efdce5b5f" + integrity sha512-NCg0w2YIp81f4V6cMGD9iomfsIj7GWrqmsa0ZsPh59G7PKiGN1KymZNxmF00ssuAlo/VZmpK6xazsGOwzKYUMg== + dependencies: + "@fortawesome/fontawesome-common-types" "6.1.1" + +"@fortawesome/free-solid-svg-icons@^6.1.1": + version "6.1.1" + resolved "https://registry.npmjs.org/@fortawesome/free-solid-svg-icons/-/free-solid-svg-icons-6.1.1.tgz#3369e673f8fe8be2fba30b1ec274d47490a830a6" + integrity sha512-0/5exxavOhI/D4Ovm2r3vxNojGZioPwmFrKg0ZUH69Q68uFhFPs6+dhAToh6VEQBntxPRYPuT5Cg1tpNa9JUPg== + dependencies: + "@fortawesome/fontawesome-common-types" "6.1.1" + +"@fortawesome/react-fontawesome@^0.1.18": + version "0.1.18" + resolved "https://registry.npmjs.org/@fortawesome/react-fontawesome/-/react-fontawesome-0.1.18.tgz#dae37f718a24e14d7a99a5496c873d69af3fbd73" + integrity sha512-RwLIB4TZw0M9gvy5u+TusAA0afbwM4JQIimNH/j3ygd6aIvYPQLqXMhC9ErY26J23rDPyDZldIfPq/HpTTJ/tQ== + dependencies: + prop-types "^15.8.1" + "@humanwhocodes/config-array@^0.9.2": version "0.9.5" resolved "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz#2cbaf9a89460da24b5ca6531b8bbfc23e1df50c7"