feat(frontend): incomplete port to next.js

This commit is contained in:
Sam 2022-08-16 00:01:54 +02:00
parent b9c30379ee
commit eec01dc070
50 changed files with 2874 additions and 3163 deletions

28
frontend/lib/fetch.ts Normal file
View file

@ -0,0 +1,28 @@
import type { APIError } from "./types";
export default async function fetchAPI<T>(
path: string,
method = "GET",
body: any = null
) {
let headers = {};
const token = localStorage.getItem("pronouns-token");
if (token) {
headers = {
Authorization: token,
};
}
const resp = await fetch(`/api/v1${path}`, {
method,
headers: {
...headers,
"Content-Type": "application/json",
},
body: body ? JSON.stringify(body) : null,
});
const data = await resp.json();
if (resp.status !== 200) throw data as APIError;
return data as T;
}

7
frontend/lib/state.ts Normal file
View file

@ -0,0 +1,7 @@
import { atom } from "recoil";
import { MeUser } from "./types";
export const userState = atom<MeUser | null>({
key: "userState",
default: null,
});

55
frontend/lib/types.ts Normal file
View file

@ -0,0 +1,55 @@
export interface MeUser extends User {
avatar_source: string | null;
discord: string | null;
discord_username: string | null;
}
export interface User {
id: string;
username: string;
display_name: string | null;
bio: string | null;
avatar_url: string | null;
links: string[] | null;
members: PartialMember[];
fields: Field[];
}
export interface PartialMember {
id: string;
name: string;
avatar_url: string | null;
}
export interface Field {
name: string;
favourite: string[] | null;
okay: string[] | null;
jokingly: string[] | null;
friends_only: string[] | null;
avoid: string[] | null;
}
export interface APIError {
code: ErrorCode;
message?: string;
details?: string;
}
export enum ErrorCode {
BadRequest = 400,
Forbidden = 403,
InternalServerError = 500,
InvalidState = 1001,
InvalidOAuthCode = 1002,
InvalidToken = 1003,
UserNotFound = 2001,
}
export interface SignupRequest {
username: string;
ticket: string;
invite_code?: string;
}