Created
February 5, 2022 11:08
-
-
Save ianrodrigues/635510e1bcd141e92d5025338f36ec11 to your computer and use it in GitHub Desktop.
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
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; | |
}; |
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
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", | |
}, | |
}); |
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
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