1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-12-22 14:44:21 +00:00

I keep forgetting to make smaller commits

This commit is contained in:
Steffo 2022-05-31 16:35:13 +02:00
parent ab89b3e464
commit 1181564e9e
Signed by: steffo
GPG key ID: 6965406171929D01
8 changed files with 166 additions and 102 deletions

View file

@ -0,0 +1,3 @@
export function FormEventCreate() {
}

View file

@ -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<HTMLFormElement>) {
const { t } = useTranslation("common")
const [_, setLogin] = useDefinedContext(LoginContext)
const [working, setWorking] = useState<boolean>(false)
const [error, setError] = useState<ApiError | null | undefined>(null)
const onLogin = useCallback(
async (data: TelegramLoginData) => {
setError(null)
setWorking(true)
try {
var response = await axios.post<ApiResult<FestaLoginData>>("/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 = (
<div>
<code>
{JSON.stringify(error)}
</code>
</div>
)
}
else if(working) {
message = t("formTelegramLoginWorking")
contents = <div/>
}
else {
message = t("formTelegramLoginDescription")
contents = (
<TelegramLoginButton
dataOnauth={onLogin}
botName={process.env.NEXT_PUBLIC_TELEGRAM_USERNAME}
/>
)
}
return (
<form className={newClassName}>
<p>
{message}
</p>
{contents}
</form>
)
}

View file

@ -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<SetStateAction<FestaLoginData | null>>, setError: Dispatch<SetStateAction<ApiError | null | undefined>>, setWorking: Dispatch<SetStateAction<boolean>>): (data: TelegramLoginData) => Promise<void> {
return useCallback(
async (data: TelegramLoginData) => {
setError(null)
setWorking(true)
try {
var response = await axios.post<ApiResult<FestaLoginData>>("/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))
},
[]
)
}

View file

@ -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",

23
pages/api/events/mine.ts Normal file
View file

@ -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<ApiResult<Event | Event[]>>) {
handleInterrupts(res, async () => {
const user = await authorizeUser(req, res)
const where = {
creatorId: user.id
}
await restInPeace(req, res, {
model: client.event,
list: {where}
})
})
}

View file

@ -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<boolean>(false)
const [error, setError] = useState<ApiError | null | undefined>(null)
const onLogin = useTelegramToFestaCallback(setLogin, setError, setWorking)
if(!login) return (
const [login, _] = useDefinedContext(LoginContext)
return (
<main id="page-index" className="page">
<hgroup className="hero-titles">
<h1>
@ -35,69 +29,35 @@ export default function PageIndex() {
{t("siteSubtitle")}
</h2>
</hgroup>
{error ?
<div className="hero-action negative">
<p>
{t("telegramLoginError")}
</p>
<p>
<code>
{JSON.stringify(error)}
</code>
</p>
</div>
:
working ?
<div>
{login ?
<div className="hero-action">
<label htmlFor="input-name">
<p>
{t("telegramLoginWorking")}
{t("eventsInputDescriptionFirst")}
</p>
</div>
:
<div className="hero-action">
<p>
{t("telegramLoginDescription")}
</p>
<TelegramLoginButton
dataOnauth={onLogin}
botName={process.env.NEXT_PUBLIC_TELEGRAM_USERNAME}
</label>
<form
className="form-event-create"
>
<input
id="input-name"
placeholder={t("eventsInputName")}
type="text"
/>
</div>
<input
id="input-submit"
type="submit"
aria-label={t("eventsInputSubmitLabel")}
value="→"
className="square-40 positive"
/>
</form>
</div>
:
<FormLoginTelegram
className="hero-action"
/>
}
</main>
)
return (
<main id="page-index" className="page">
<hgroup className="hero-titles">
<h1>
{t("siteTitle")}
</h1>
<h2>
{t("eventsSubtitleFirst")}
</h2>
</hgroup>
<div className="hero-action">
<label htmlFor="input-name">
<p>
{t("eventsInputDescriptionFirst")}
</p>
</label>
<form className="form-event-create">
<input
id="input-name"
placeholder={t("eventsInputName")}
type="text"
/>
<input
id="input-submit"
type="submit"
aria-label={t("eventsInputSubmitLabel")}
value="→"
className="square-40 positive"
/>
</form>
</div>
</main>
)
}

View file

@ -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!",

View file

@ -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"