Skip to content

Commit

Permalink
Add ability to rename crews
Browse files Browse the repository at this point in the history
  • Loading branch information
NotNite committed Nov 30, 2023
1 parent 730077a commit 4ff9b2e
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/api/crew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ export async function create(
return { ok: true, value: res };
}

export async function update(
crew: string,
name: string,
tag: string
): Promise<Result<null, string>> {
const token = useAuthStore.getState().key;
if (token == null) {
return {
ok: false,
error: "You must be logged in to update a crew"
};
}

const req = await tryFetch(
`${import.meta.env.VITE_SLOP_CREW_SERVER}api/crew/${crew}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: token
},
body: JSON.stringify({
name,
tag
})
}
);

if (!req.ok) return req;
return { ok: true, value: null };
}

export async function promote(
crew: string,
user: string
Expand Down
69 changes: 68 additions & 1 deletion src/routes/crews/CrewSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,72 @@ import { useNavigate, useRevalidator } from "react-router-dom";
import { CrewResponse, MeResponse } from "../../api/types";
import HiddenCode from "../../components/HiddenCode";
import { useRequiredAuth } from "../../util";
import { createInvite, deleteInvite, nukeItFromOrbit } from "../../api/crew";
import {
createInvite,
deleteInvite,
nukeItFromOrbit,
update
} from "../../api/crew";
import TMPInput from "../../components/TMPInput.tsx";
import Important from "../../components/Important.tsx";

function CrewUpdate({ crew }: { crew: CrewResponse }) {
const revalidator = useRevalidator();
const name = React.createRef<HTMLInputElement>();
const tag = React.createRef<HTMLInputElement>();

const [working, setWorking] = React.useState<boolean>(false);
const [error, setError] = React.useState<string | null>(null);

return (
<section className="smolPadding">
<h2>Name & tag</h2>

{error != null && <Important type="danger" message={error} />}

<input
type="text"
ref={name}
defaultValue={crew.name}
placeholder="Crew name"
minLength={3}
maxLength={32}
/>

<TMPInput
ref={tag}
defaultValue={crew.tag}
placeholder="Crew tag"
minLength={3}
maxLength={32}
/>

<button
className="normalWidthButton"
disabled={working}
onClick={async () => {
const nameVal = name.current?.value;
const tagVal = tag.current?.value;
if (nameVal == null || tagVal == null) return;

setWorking(true);
setError(null);

const req = await update(crew.id, nameVal, tagVal);
setWorking(false);

if (!req.ok) {
setError(req.error);
} else {
revalidator.revalidate();
}
}}
>
Update
</button>
</section>
);
}

function CrewSettingsInner({
crew,
Expand All @@ -23,6 +88,8 @@ function CrewSettingsInner({
<>
<h1>Crew settings</h1>

<CrewUpdate crew={crew} />

<section className="smolPadding">
<h2>Invites</h2>

Expand Down

0 comments on commit 4ff9b2e

Please sign in to comment.