Skip to content

Instantly share code, notes, and snippets.

@ianrodrigues
Created February 5, 2022 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianrodrigues/635510e1bcd141e92d5025338f36ec11 to your computer and use it in GitHub Desktop.
Save ianrodrigues/635510e1bcd141e92d5025338f36ec11 to your computer and use it in GitHub Desktop.
import { client } from '~/services/api.server';
import { getAccessToken } from '~/services/session.server';
import { Team } from './types';
export const getTeam = async (
request: Request,
{ teamId }: { teamId: string }
): Promise<Team> => {
const { data } = await client.get(`/teams/${teamId}`, {
headers: {
Authorization: `Bearer ${await getAccessToken(request)}`,
},
});
return data;
};
export const updateTeam = async (
request: Request,
{ teamId, name }: { teamId: string; name: string }
): Promise<Team> => {
const { data } = await client.patch(
`/teams/${teamId}`,
{
name,
},
{
headers: {
Authorization: `Bearer ${await getAccessToken(request)}`,
},
}
);
return data;
};
import axios from "axios";
export const client = axios.create({
baseURL: process.env.API_URL,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"X-Requested-With": "XMLHttpRequest",
},
});
export const action: ActionFunction = async ({ params, request }) => {
const { name } = Object.fromEntries(await request.formData());
if (typeof name !== "string") return form.notFilledOutCorrectly();
try {
await TeamsApi.updateTeam(request, {
teamId: params.teamId!,
name,
});
return null;
} catch (error: any) {
if (!error.response) {
throw error;
}
const { detail, ...rest } = error.response.data;
return json<ActionData>(
{
generalError: detail,
formFieldErrors: rest,
},
{ status: error.response.status }
);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment