Skip to content

Commit

Permalink
First round of features is live; bump version to 0.1.0.
Browse files Browse the repository at this point in the history
Add ability to select faction when creating new agent.
  • Loading branch information
ende23r committed May 12, 2024
1 parent 5e6128d commit 7ea772b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 32 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "space-traders-drone-manager",
"private": true,
"version": "0.0.0",
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
43 changes: 43 additions & 0 deletions src/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,49 @@ export function useFulfillContractMutation(contractId: string) {
});
}

export type FactionSymbol = z.infer<typeof schemas.FactionSymbol>;
export function useFactions() {
return useQuery({
queryKey: ["get-factions"],
queryFn: () => {
api["get-factions"];
},
retry: false,
// Make this function never re-fetch
staleTime: Infinity,
});
}
async function registerAgent(
agentSymbol: string,
faction: FactionSymbol,
registerTokenCallback: Function,
) {
const body = {
symbol: agentSymbol,
faction,
};
const response = await api["register"](body);
globalQueryClient.invalidateQueries();
registerTokenCallback(response.data.token);
return response.data;
}

export function useRegisterWithFaction() {
return useMutation({
mutationKey: ["register"],
mutationFn: ({
agentSymbol,
faction,
callback,
}: {
agentSymbol: string;
faction: FactionSymbol;
callback: Function;
}) => registerAgent(agentSymbol, faction, callback),
onError: handleAxiosError,
});
}

export const globalQueryClient = new QueryClient({
queryCache: new QueryCache({
onError: (error) => {
Expand Down
78 changes: 49 additions & 29 deletions src/BearerAuthDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,37 @@ import {
DialogContent,
TextField,
DialogActions,
Select,
MenuItem,
} from "@mui/material";
import { useState } from "react";
import { useLocalStorage } from "./hooks/useLocalStorage";
import { useMyAgent, globalQueryClient } from "./Api";
import { useMyAgent, globalQueryClient, useRegisterWithFaction } from "./Api";
import { schemas } from "./packages/SpaceTradersAPI";
import { z } from "zod";

// API for requests with a BODY: function [alias](body: BodyParam, config?: ZodiosRequestOptions): Promise<Response>;

async function generateBearerToken(playerSymbol: string, faction = "COSMIC") {
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
symbol: playerSymbol,
faction,
}),
};
const response = await fetch(
"https://api.spacetraders.io/v2/register",
options,
);
if (!response.ok) {
throw new Error(`${response.status}: ${response.statusText}`);
}
return await response.json();
}
type FactionSymbol = z.infer<typeof schemas.FactionSymbol>;
const knownFactions: FactionSymbol[] = [
"COSMIC",
"VOID",
"GALACTIC",
"QUANTUM",
"DOMINION",
"ASTRO",
"CORSAIRS",
"OBSIDIAN",
"AEGIS",
"UNITED",
"SOLITARY",
"COBALT",
"OMEGA",
"ECHO",
"LORDS",
"CULT",
"ANCIENTS",
"SHADOW",
"ETHEREAL",
];

function BearerAuthDialog(props: {
manuallyOpen: boolean;
Expand All @@ -40,8 +44,12 @@ function BearerAuthDialog(props: {
const { manuallyOpen, setManuallyOpen } = props;
const { agent } = useMyAgent();
const defaultAgentSymbol = agent.symbol || "";

const [bearerToken, setBearerToken] = useLocalStorage("bearerToken", "");
const [agentSymbol, setAgentSymbol] = useState("");
const [faction, setFaction] = useState<FactionSymbol>("COSMIC");

const { mutate: registerAsAgent } = useRegisterWithFaction();
return (
<Dialog open={!bearerToken || manuallyOpen}>
<DialogTitle>Register Agent</DialogTitle>
Expand Down Expand Up @@ -74,14 +82,26 @@ function BearerAuthDialog(props: {
setAgentSymbol(e.target.value);
}}
/>
<Select
label="Faction"
value={faction}
onChange={(event: any) => {
setFaction(event.target.value);
}}
>
{knownFactions.map((symbol) => (
<MenuItem value={symbol}>{symbol}</MenuItem>
))}
</Select>
<Button
variant="contained"
onClick={async () => {
const data = await generateBearerToken(agentSymbol);
const { token } = data.data;
setBearerToken(token);
globalQueryClient.invalidateQueries();
}}
onClick={() =>
registerAsAgent({
agentSymbol,
faction,
callback: setBearerToken,
})
}
>
Create New Agent
</Button>
Expand Down

0 comments on commit 7ea772b

Please sign in to comment.