(app-desk) integrate team endpoint

Integrate team endpoint with react-query.
It will be used to get the team on the team page.
This commit is contained in:
Anthony LC
2024-02-07 16:16:17 +01:00
committed by Anthony LC
parent 77efb1a89c
commit d0562029e8
4 changed files with 60 additions and 20 deletions

View File

@@ -0,0 +1,17 @@
enum Role {
MEMBER = 'member',
ADMIN = 'administrator',
OWNER = 'owner',
}
export interface Access {
id: string;
role: Role;
user: string;
}
export interface TeamResponse {
id: string;
name: string;
accesses: Access[];
}

View File

@@ -0,0 +1,35 @@
import { UseQueryOptions, useQuery } from '@tanstack/react-query';
import { fetchAPI } from '@/api';
import { TeamResponse } from './types';
export type TeamParams = {
id: string;
};
export interface TeamResponseError {
detail: string;
}
export const getTeam = async ({ id }: TeamParams): Promise<TeamResponse> => {
const response = await fetchAPI(`teams/${id}`);
if (!response.ok) {
throw new Error(`Couldn't fetch team: ${response.statusText}`);
}
return response.json() as Promise<TeamResponse>;
};
export const KEY_TEAM = 'team';
export function useTeam(
param: TeamParams,
queryConfig?: UseQueryOptions<TeamResponse, TeamResponseError, TeamResponse>,
) {
return useQuery<TeamResponse, TeamResponseError, TeamResponse>({
queryKey: [KEY_TEAM, param],
queryFn: () => getTeam(param),
...queryConfig,
});
}

View File

@@ -7,23 +7,7 @@ import {
import { APIList, fetchAPI } from '@/api';
enum Role {
MEMBER = 'member',
ADMIN = 'administrator',
OWNER = 'owner',
}
interface Access {
id: string;
role: Role;
user: string;
}
export interface TeamResponse {
id: string;
name: string;
accesses: Access[];
}
import { TeamResponse } from './types';
export enum TeamsOrdering {
BY_CREATED_ON = 'created_at',
@@ -42,14 +26,17 @@ export interface TeamsResponseError {
detail: string;
}
export const getTeams = async ({ ordering, page }: TeamsAPIParams) => {
export const getTeams = async ({
ordering,
page,
}: TeamsAPIParams): Promise<TeamsResponse> => {
const orderingQuery = ordering ? `&ordering=${ordering}` : '';
const response = await fetchAPI(`teams/?page=${page}${orderingQuery}`);
if (!response.ok) {
throw new Error(`Couldn't fetch teams: ${response.statusText}`);
}
return response.json();
return response.json() as Promise<TeamsResponse>;
};
export const KEY_LIST_TEAM = 'teams';

View File

@@ -5,7 +5,8 @@ import { useTranslation } from 'react-i18next';
import { Box, Text } from '@/components';
import { InfiniteScroll } from '@/components/InfiniteScroll';
import { TeamResponse, useTeams } from '../api/useTeams';
import { TeamResponse } from '../api/types';
import { useTeams } from '../api/useTeams';
import { useTeamStore } from '../store/useTeamsStore';
import { PanelTeam } from './PanelTeam';