-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
984 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { useAuthStore } from "../stores"; | ||
import { CrewResponse, Result, SimpleCrewResponse } from "./types"; | ||
import { tryFetch } from "./util"; | ||
|
||
export async function getCrews() { | ||
const token = useAuthStore.getState().key; | ||
if (token == null) return null; | ||
|
||
const req = await fetch( | ||
`${import.meta.env.VITE_SLOP_CREW_SERVER}api/crew/crews`, | ||
{ | ||
headers: { | ||
Authorization: token | ||
} | ||
} | ||
); | ||
|
||
if (!req.ok) return null; | ||
const res: SimpleCrewResponse[] = await req.json(); | ||
return res; | ||
} | ||
|
||
export async function getCrew(id: string) { | ||
const token = useAuthStore.getState().key; | ||
if (token == null) return null; | ||
|
||
const req = await fetch( | ||
`${import.meta.env.VITE_SLOP_CREW_SERVER}api/crew/${id}`, | ||
{ | ||
headers: { | ||
Authorization: token | ||
} | ||
} | ||
); | ||
|
||
if (!req.ok) return null; | ||
const res: CrewResponse = await req.json(); | ||
return res; | ||
} | ||
|
||
export async function create( | ||
name: string, | ||
tag: string | ||
): Promise<Result<SimpleCrewResponse, string>> { | ||
const token = useAuthStore.getState().key; | ||
if (token == null) { | ||
return { | ||
ok: false, | ||
error: "You must be logged in to create a crew" | ||
}; | ||
} | ||
|
||
const req = await tryFetch( | ||
`${import.meta.env.VITE_SLOP_CREW_SERVER}api/crew/create`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: token | ||
}, | ||
body: JSON.stringify({ | ||
name, | ||
tag | ||
}) | ||
} | ||
); | ||
|
||
if (!req.ok) return req; | ||
const res: SimpleCrewResponse = await req.value.json(); | ||
return { ok: true, value: res }; | ||
} | ||
|
||
export async function promote( | ||
crew: string, | ||
user: string | ||
): Promise<Result<null, string>> { | ||
const token = useAuthStore.getState().key; | ||
if (token == null) { | ||
return { | ||
ok: false, | ||
error: "You must be logged in to promote a user" | ||
}; | ||
} | ||
|
||
const req = await tryFetch( | ||
`${ | ||
import.meta.env.VITE_SLOP_CREW_SERVER | ||
}api/crew/${crew}/promote?id=${user}`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
Authorization: token | ||
} | ||
} | ||
); | ||
if (!req.ok) return req; | ||
return { ok: true, value: null }; | ||
} | ||
|
||
export async function join( | ||
code: string | ||
): Promise<Result<SimpleCrewResponse, string>> { | ||
const token = useAuthStore.getState().key; | ||
if (token == null) { | ||
return { | ||
ok: false, | ||
error: "You must be logged in to join a crew" | ||
}; | ||
} | ||
|
||
const req = await tryFetch( | ||
`${import.meta.env.VITE_SLOP_CREW_SERVER}api/crew/join?code=${code}`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
Authorization: token | ||
} | ||
} | ||
); | ||
|
||
if (!req.ok) return req; | ||
const res: SimpleCrewResponse = await req.value.json(); | ||
return { ok: true, value: res }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Result } from "./types"; | ||
|
||
export async function tryFetch( | ||
input: string, | ||
info: RequestInit | ||
): Promise<Result<Response, string>> { | ||
let req; | ||
try { | ||
req = await fetch(input, info); | ||
} catch (e) { | ||
console.error(e); | ||
|
||
if (e instanceof Error) { | ||
return { ok: false, error: e.message }; | ||
} else { | ||
return { ok: false, error: "Unknown error" }; | ||
} | ||
} | ||
|
||
if (!req.ok) { | ||
const text = await req.text(); | ||
if (text == "") { | ||
return { | ||
ok: false, | ||
error: `Unknown error: ${req.status} ${req.statusText}` | ||
}; | ||
} else { | ||
return { ok: false, error: text }; | ||
} | ||
} | ||
|
||
return { | ||
ok: true, | ||
value: req | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
type HasAvatar = { | ||
id: string; | ||
avatar: string | null; | ||
username: string; | ||
}; | ||
|
||
export default function Avatar({ person }: { person: HasAvatar }) { | ||
if (person.avatar == null) return <></>; | ||
|
||
return ( | ||
<img | ||
className="avatar" | ||
src={`https://cdn.discordapp.com/avatars/${person.id}/${person.avatar}.png`} | ||
alt={person.username} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.