diff --git a/front/packages/models/src/query.tsx b/front/packages/models/src/query.tsx index eec1a898..ef765c38 100644 --- a/front/packages/models/src/query.tsx +++ b/front/packages/models/src/query.tsx @@ -39,9 +39,9 @@ export const kyooUrl = ? process.env.KYOO_URL ?? "http://localhost:5000" : "/api"; -const queryFn = async ( - type: z.ZodType, - context: QueryFunctionContext, +export const queryFn = async ( + context: QueryFunctionContext | { path: string[]; body?: object; method: "GET" | "POST" }, + type?: z.ZodType, ): Promise => { if (!kyooUrl) console.error("Kyoo's url is not defined."); @@ -50,10 +50,21 @@ const queryFn = async ( resp = await fetch( [kyooUrl] .concat( - context.pageParam ? [context.pageParam] : (context.queryKey.filter((x) => x) as string[]), + "path" in context + ? context.path + : context.pageParam + ? [context.pageParam] + : (context.queryKey.filter((x) => x) as string[]), ) .join("/") .replace("/?", "?"), + { + // @ts-ignore + method: context.method, + // @ts-ignore + body: context.body ? JSON.stringify(context.body) : undefined, + headers: "body" in context ? { "Content-Type": "application/json" } : undefined, + }, ); } catch (e) { console.log("Fetch error", e); @@ -81,6 +92,7 @@ const queryFn = async ( console.error("Invald json from kyoo", e); throw { errors: ["Invalid repsonse from kyoo"] }; } + if (!type) return data; const parsed = await type.safeParseAsync(data); if (!parsed.success) { console.log("Parse error: ", parsed.error); @@ -137,7 +149,7 @@ const toQueryKey = (query: QueryIdentifier) => { export const useFetch = (query: QueryIdentifier) => { return useQuery({ queryKey: toQueryKey(query), - queryFn: (ctx) => queryFn(query.parser, ctx), + queryFn: (ctx) => queryFn(ctx, query.parser), }); }; @@ -149,7 +161,7 @@ export const useInfiniteFetch = ( // eslint-disable-next-line react-hooks/rules-of-hooks const ret = useInfiniteQuery({ queryKey: toQueryKey(query), - queryFn: (ctx) => queryFn(z.array(query.parser), ctx), + queryFn: (ctx) => queryFn(ctx, z.array(query.parser)), getNextPageParam: query.getNext, ...options, }); @@ -158,7 +170,7 @@ export const useInfiniteFetch = ( // eslint-disable-next-line react-hooks/rules-of-hooks const ret = useInfiniteQuery, KyooErrors>({ queryKey: toQueryKey(query), - queryFn: (ctx) => queryFn(Paged(query.parser), ctx), + queryFn: (ctx) => queryFn(ctx, Paged(query.parser)), getNextPageParam: (page: Page) => page?.next || undefined, }); return { ...ret, items: ret.data?.pages.flatMap((x) => x.items) }; @@ -175,12 +187,12 @@ export const fetchQuery = async (queries: QueryIdentifier[]) => { if (query.infinite) { return client.prefetchInfiniteQuery({ queryKey: toQueryKey(query), - queryFn: (ctx) => queryFn(Paged(query.parser), ctx), + queryFn: (ctx) => queryFn(ctx, Paged(query.parser)), }); } else { return client.prefetchQuery({ queryKey: toQueryKey(query), - queryFn: (ctx) => queryFn(query.parser, ctx), + queryFn: (ctx) => queryFn(ctx, query.parser), }); } }), diff --git a/front/packages/primitives/src/button.tsx b/front/packages/primitives/src/button.tsx index 7fe4af8d..a00fe936 100644 --- a/front/packages/primitives/src/button.tsx +++ b/front/packages/primitives/src/button.tsx @@ -26,7 +26,6 @@ import { ts } from "./utils"; export const Button = ({ text, - onPress, ...props }: { text: string } & ComponentProps) => { const { css } = useYoshiki(); diff --git a/front/packages/ui/src/login/api.ts b/front/packages/ui/src/login/api.ts new file mode 100644 index 00000000..ecf9584d --- /dev/null +++ b/front/packages/ui/src/login/api.ts @@ -0,0 +1,35 @@ +/* + * Kyoo - A portable and vast media library solution. + * Copyright (c) Kyoo. + * + * See AUTHORS.md and LICENSE file in the project root for full license information. + * + * Kyoo is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * Kyoo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Kyoo. If not, see . + */ + +import { KyooErrors, queryFn } from "@kyoo/models"; + +export const loginFunc = async (action: "register" | "login", body: object) => { + try { + const token = await queryFn({ + path: ["auth", action], + method: "POST", + body: body, + }); + + return null; + } catch (e) { + return (e as KyooErrors).errors[0]; + } +}; diff --git a/front/packages/ui/src/login/login.tsx b/front/packages/ui/src/login/login.tsx index 19f18b54..59700d1d 100644 --- a/front/packages/ui/src/login/login.tsx +++ b/front/packages/ui/src/login/login.tsx @@ -18,7 +18,7 @@ * along with Kyoo. If not, see . */ -import { KyooErrors, kyooUrl, QueryPage } from "@kyoo/models"; +import { QueryPage } from "@kyoo/models"; import { Button, P, Input, ts, H1, A } from "@kyoo/primitives"; import { useState } from "react"; import { useTranslation } from "react-i18next"; @@ -28,32 +28,7 @@ import { percent, px, useYoshiki } from "yoshiki/native"; import { DefaultLayout } from "../layout"; import { FormPage } from "./form"; import { PasswordInput } from "./password-input"; - -const login = async (username: string, password: string) => { - let resp; - try { - resp = await fetch(`${kyooUrl}/auth/login`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - username, - password, - }), - }); - } catch (e) { - console.error("Login error", e); - throw { errors: ["Could not reach Kyoo's server."] } as KyooErrors; - } - if (!resp.ok) { - const err = (await resp.json()) as KyooErrors; - return { type: "error", value: null, error: err.errors[0] }; - } - const token = await resp.json(); - // TODO: Save the token in the secure storage. - return { type: "value", value: token, error: null }; -}; +import { loginFunc } from "./api"; export const LoginPage: QueryPage = () => { const [username, setUsername] = useState(""); @@ -88,7 +63,7 @@ export const LoginPage: QueryPage = () => {