✨(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:
17
src/frontend/apps/desk/src/features/teams/api/types.tsx
Normal file
17
src/frontend/apps/desk/src/features/teams/api/types.tsx
Normal 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[];
|
||||||
|
}
|
||||||
35
src/frontend/apps/desk/src/features/teams/api/useTeam.tsx
Normal file
35
src/frontend/apps/desk/src/features/teams/api/useTeam.tsx
Normal 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,
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -7,23 +7,7 @@ import {
|
|||||||
|
|
||||||
import { APIList, fetchAPI } from '@/api';
|
import { APIList, fetchAPI } from '@/api';
|
||||||
|
|
||||||
enum Role {
|
import { TeamResponse } from './types';
|
||||||
MEMBER = 'member',
|
|
||||||
ADMIN = 'administrator',
|
|
||||||
OWNER = 'owner',
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Access {
|
|
||||||
id: string;
|
|
||||||
role: Role;
|
|
||||||
user: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TeamResponse {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
accesses: Access[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum TeamsOrdering {
|
export enum TeamsOrdering {
|
||||||
BY_CREATED_ON = 'created_at',
|
BY_CREATED_ON = 'created_at',
|
||||||
@@ -42,14 +26,17 @@ export interface TeamsResponseError {
|
|||||||
detail: string;
|
detail: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getTeams = async ({ ordering, page }: TeamsAPIParams) => {
|
export const getTeams = async ({
|
||||||
|
ordering,
|
||||||
|
page,
|
||||||
|
}: TeamsAPIParams): Promise<TeamsResponse> => {
|
||||||
const orderingQuery = ordering ? `&ordering=${ordering}` : '';
|
const orderingQuery = ordering ? `&ordering=${ordering}` : '';
|
||||||
const response = await fetchAPI(`teams/?page=${page}${orderingQuery}`);
|
const response = await fetchAPI(`teams/?page=${page}${orderingQuery}`);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Couldn't fetch teams: ${response.statusText}`);
|
throw new Error(`Couldn't fetch teams: ${response.statusText}`);
|
||||||
}
|
}
|
||||||
return response.json();
|
return response.json() as Promise<TeamsResponse>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const KEY_LIST_TEAM = 'teams';
|
export const KEY_LIST_TEAM = 'teams';
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ import { useTranslation } from 'react-i18next';
|
|||||||
import { Box, Text } from '@/components';
|
import { Box, Text } from '@/components';
|
||||||
import { InfiniteScroll } from '@/components/InfiniteScroll';
|
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 { useTeamStore } from '../store/useTeamsStore';
|
||||||
|
|
||||||
import { PanelTeam } from './PanelTeam';
|
import { PanelTeam } from './PanelTeam';
|
||||||
|
|||||||
Reference in New Issue
Block a user