team and terms update

This commit is contained in:
Nate Kelley 2025-03-10 22:15:16 -06:00
parent f9e9d765bf
commit 11ddf973cf
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
19 changed files with 118 additions and 224 deletions

View File

@ -4,6 +4,6 @@ import { runSQL } from './requests';
export const useRunSQL = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: runSQL
mutationFn: runSQL //TODO move the
});
};

View File

@ -1,14 +1,16 @@
import { CreateTeamParams } from '@/api/request_interfaces/teams/interfaces';
import { createTeam } from './requests';
import { useMemoizedFn } from '@/hooks';
import { useMutation } from '@tanstack/react-query';
import type { TeamListParams } from '@/api/request_interfaces/teams';
import { createTeam, getTeamsList } from './requests';
import { useMutation, useQuery } from '@tanstack/react-query';
export const useCreateTeam = () => {
const mutationFn = useMemoizedFn((params: CreateTeamParams) => {
return createTeam(params);
});
return useMutation({
mutationFn
mutationFn: createTeam
});
};
export const useGetTeamsList = (params: TeamListParams) => {
return useQuery({
queryKey: ['teams'],
queryFn: () => getTeamsList(params)
});
};

View File

@ -1,6 +1,11 @@
import { BusterUserTeam } from '@/api/asset_interfaces/users';
import { mainApi } from '../instances';
import type { CreateTeamParams } from '@/api/request_interfaces/teams';
import type { CreateTeamParams, TeamListParams } from '@/api/request_interfaces/teams';
export const createTeam = async (params: CreateTeamParams) => {
return mainApi.post<{ id: string }>('/teams', params).then((res) => res.data);
};
export const getTeamsList = async (params: TeamListParams) => {
return mainApi.get<BusterUserTeam[]>('/teams/list', { params }).then((res) => res.data);
};

View File

@ -0,0 +1,2 @@
export * from './requests';
export * from './queryRequests';

View File

@ -0,0 +1,54 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { createTerm, deleteTerms, getTerm, getTermsList, updateTerm } from './requests';
import { TermsListParams } from '@/api/request_interfaces/terms';
import { queryKeys } from '@/api/query_keys';
import { BusterTerm } from '@/api/asset_interfaces/terms';
export const useGetTermsList = (params: TermsListParams) => {
return useQuery({
...queryKeys.termsGetList,
queryFn: () => getTermsList(params)
});
};
export const useGetTerm = (id: string) => {
return useQuery({
...queryKeys.termsGetTerm(id),
queryFn: () => getTerm(id)
});
};
export const useCreateTerm = () => {
return useMutation({
mutationFn: createTerm
});
};
export const useUpdateTerm = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: updateTerm,
onMutate: (params) => {
const options = queryKeys.termsGetTerm(params.id);
const previousTerm = queryClient.getQueryData(options.queryKey);
if (previousTerm) {
const newTerm: BusterTerm = {
...previousTerm,
...params
};
queryClient.setQueryData(options.queryKey, newTerm);
}
}
});
};
export const useDeleteTerm = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: deleteTerms,
onSuccess: (data) => {
const options = queryKeys.termsGetList;
queryClient.invalidateQueries({ queryKey: options.queryKey });
}
});
};

View File

@ -0,0 +1,23 @@
import { TermPostParams, TermsListParams, TermUpdateParams } from '@/api/request_interfaces/terms';
import { mainApi } from '../instances';
import { BusterTerm, BusterTermListItem } from '@/api/asset_interfaces/terms';
export const getTermsList = async (params: TermsListParams) => {
return mainApi.get<BusterTermListItem[]>('/terms', { params }).then((res) => res.data);
};
export const getTerm = async (id: string) => {
return mainApi.get<BusterTerm>(`/terms/${id}`).then((res) => res.data);
};
export const createTerm = async (params: TermPostParams) => {
return mainApi.post<BusterTerm>('/terms', params).then((res) => res.data);
};
export const updateTerm = async (params: TermUpdateParams) => {
return mainApi.put<BusterTerm>(`/terms/${params.id}`, params).then((res) => res.data);
};
export const deleteTerms = async (ids: string[]) => {
return mainApi.delete<BusterTerm>(`/terms`, { data: { ids } }).then((res) => res.data);
};

View File

@ -1,18 +1,8 @@
import type { UserEmits, UserResponses, UserResponsesTypes } from './user';
import type { TeamEmits, TeamResponses, TeamResponsesTypes } from './teams';
import type { TermsEmits, TermsResponses, TermsResponseTypes } from './terms';
import type { ChatEmits, ChatResponseTypes, ChatsResponses } from './chats';
export type BusterSocketRequest = UserEmits | TeamEmits | TermsEmits | ChatEmits;
export type BusterSocketRequest = UserEmits | ChatEmits;
export type BusterSocketResponse =
| UserResponsesTypes
| TeamResponsesTypes
| TermsResponseTypes
| ChatResponseTypes;
export type BusterSocketResponse = UserResponsesTypes | ChatResponseTypes;
export type BusterSocketResponseRoute =
| keyof typeof UserResponses
| keyof typeof TeamResponses
| keyof typeof TermsResponses
| keyof typeof ChatsResponses;
export type BusterSocketResponseRoute = keyof typeof UserResponses | keyof typeof ChatsResponses;

View File

@ -1,2 +0,0 @@
export * from './teamRequests';
export * from './teamResponses';

View File

@ -1,24 +0,0 @@
import type { BusterSocketRequestBase } from '../base_interfaces';
/**
* Represents a request to list teams with optional filtering and pagination parameters.
* @template {'/teams/list'} - The endpoint path for listing teams
* @param {Object} params - The parameters for the teams list request
* @param {number} [params.page_size] - The number of teams to return per page
* @param {number} [params.page] - The page number to retrieve
* @param {string | null} [params.permission_group_id] - Filter teams by permission group ID
* @param {string | null} [params.user_id] - Filter teams by user ID
* @param {boolean | null} [params.belongs_to] - Filter teams by user membership
*/
export type TeamRequestsList = BusterSocketRequestBase<
'/teams/list',
{
page_size?: number;
page?: number;
permission_group_id?: null | string;
user_id?: null | string;
belongs_to?: boolean | null;
}
>;
export type TeamEmits = TeamRequestsList;

View File

@ -1,13 +0,0 @@
import type { BusterUserTeam } from '@/api/asset_interfaces/users';
export enum TeamResponses {
'/teams/list:listTeams' = '/teams/list:listTeams'
}
export type TeamList_listTeams = {
route: '/teams/list:listTeams';
callback: (d: BusterUserTeam[]) => void;
onError?: (d: unknown) => void;
};
export type TeamResponsesTypes = TeamList_listTeams;

View File

@ -1,2 +0,0 @@
export * from './termsRequest';
export * from './termsResponses';

View File

@ -1,47 +0,0 @@
/**
* @fileoverview Contains request type definitions for the Terms API endpoints
*/
import { BusterSocketRequestBase } from '../base_interfaces';
import {
TermsListParams,
TermsGetParams,
TermPostParams,
TermUpdateParams,
TermDeleteParams
} from '../../request_interfaces/terms/interfaces';
/**
* Request type for listing terms with pagination
*/
export type TermsListRequest = BusterSocketRequestBase<'/terms/list', TermsListParams>;
/**
* Request type for retrieving a specific term by ID
*/
export type TermsGetRequest = BusterSocketRequestBase<'/terms/get', TermsGetParams>;
/**
* Request type for creating a new term
*/
export type TermPostRequest = BusterSocketRequestBase<'/terms/post', TermPostParams>;
/**
* Request type for updating an existing term
*/
export type TermUpdateRequest = BusterSocketRequestBase<'/terms/update', TermUpdateParams>;
/**
* Request type for deleting terms
*/
export type TermDeleteRequest = BusterSocketRequestBase<'/terms/delete', TermDeleteParams>;
/**
* Union type of all possible term-related requests
*/
export type TermsEmits =
| TermsListRequest
| TermsGetRequest
| TermPostRequest
| TermUpdateRequest
| TermDeleteRequest;

View File

@ -1,46 +0,0 @@
import { BusterTerm, BusterTermListItem } from '@/api/asset_interfaces/terms';
export enum TermsResponses {
'/terms/list:ListTerms' = '/terms/list:ListTerms',
'/terms/get:GetTerm' = '/terms/get:GetTerm',
'/terms/update:UpdateTerm' = '/terms/update:UpdateTerm',
'/terms/post:PostTerm' = '/terms/post:PostTerm',
'/terms/delete:DeleteTerm' = '/terms/delete:DeleteTerm'
}
export type TermsResponses_listTerms = {
route: '/terms/list:ListTerms';
callback: (d: BusterTermListItem[]) => void;
onError?: (d: unknown) => void;
};
export type TermResponses_getTerm = {
route: '/terms/get:GetTerm';
callback: (d: BusterTerm) => void;
onError?: (d: unknown) => void;
};
export type TermResponses_updateTerm = {
route: '/terms/update:UpdateTerm';
callback: (d: BusterTerm) => void;
onError?: (d: unknown) => void;
};
export type TermResponses_postTerm = {
route: '/terms/post:PostTerm';
callback: (d: BusterTerm) => void;
onError?: (d: unknown) => void;
};
export type TermResponses_DeleteTerm = {
route: '/terms/delete:DeleteTerm';
callback: (d: { ids: string[] }) => void;
onError?: (d: unknown) => void;
};
export type TermsResponseTypes =
| TermsResponses_listTerms
| TermResponses_getTerm
| TermResponses_updateTerm
| TermResponses_postTerm
| TermResponses_DeleteTerm;

View File

@ -7,3 +7,11 @@ export interface CreateTeamParams {
/** Optional description of the team */
description?: string;
}
export interface TeamListParams {
page_size?: number;
page?: number;
permission_group_id?: string | null;
user_id?: string | null;
belongs_to?: boolean | null;
}

View File

@ -1,40 +1,19 @@
import { useSocketQueryMutation } from '@/api/buster_socket_query';
import { type BusterTerm } from '@/api/asset_interfaces';
import { queryKeys } from '@/api/query_keys';
import { queryOptions } from '@tanstack/react-query';
import { useBusterNotifications } from '@/context/BusterNotifications';
import { useMemoizedFn } from '@/hooks';
import type { TermDeleteParams } from '@/api/request_interfaces/terms';
import { useBusterTermsListContextSelector } from '../BusterTermsListProvider';
import { useCreateTerm, useDeleteTerm } from '@/api/buster_rest/terms';
export const useBusterTermsCreate = () => {
const { openConfirmModal } = useBusterNotifications();
const refetchTermsList = useBusterTermsListContextSelector((x) => x.refetchTermsList);
const { mutateAsync: createTerm, isPending: isCreatingTerm } = useSocketQueryMutation({
emitEvent: '/terms/post',
responseEvent: '/terms/post:PostTerm',
options: queryOptions<BusterTerm>({
queryKey: []
}),
callback: (newData, currentData, variables) => {
refetchTermsList();
return newData;
}
});
const { mutateAsync: createTerm, isPending: isCreatingTerm } = useCreateTerm();
const { mutate: deleteTermMutation, isPending: isDeletingTerm } = useSocketQueryMutation({
emitEvent: '/terms/delete',
responseEvent: '/terms/delete:DeleteTerm',
options: queryKeys.termsGetList,
preCallback: (currentData, variables) => {
return (currentData || []).filter((term) => !variables.ids.includes(term.id));
}
});
const { mutate: deleteTermMutation, isPending: isDeletingTerm } = useDeleteTerm();
const onDeleteTerm = useMemoizedFn(({ ids }: TermDeleteParams, ignoreConfirm = false) => {
const method = async () => {
deleteTermMutation({ ids });
deleteTermMutation(ids);
};
if (ignoreConfirm) {

View File

@ -1,17 +1,7 @@
import { queryKeys } from '@/api/query_keys';
import { useSocketQueryEmitOn } from '@/api/buster_socket_query';
import { useGetTerm } from '@/api/buster_rest/terms';
export const useBusterTermsIndividual = ({ termId }: { termId: string }) => {
const {
data: term,
refetch: refetchTerm,
isFetched: isFetchedTerm
} = useSocketQueryEmitOn({
emitEvent: { route: '/terms/get', payload: { id: termId } },
responseEvent: '/terms/get:GetTerm',
options: queryKeys.termsGetTerm(termId),
enabledTrigger: termId
});
const { data: term, refetch: refetchTerm, isFetched: isFetchedTerm } = useGetTerm(termId);
return {
term,

View File

@ -1,19 +1,7 @@
import type { BusterTerm } from '@/api/asset_interfaces';
import { useSocketQueryMutation } from '@/api/buster_socket_query';
import { queryOptions } from '@tanstack/react-query';
import { useUpdateTerm } from '@/api/buster_rest/terms';
export const useBusterTermsUpdate = () => {
const { mutate: updateTerm } = useSocketQueryMutation({
emitEvent: '/terms/update',
responseEvent: '/terms/update:UpdateTerm',
options: queryOptions<BusterTerm>({ queryKey: [] }),
callback: (currentData, variables) => {
return {
...currentData!,
...variables
};
}
});
const { mutate: updateTerm } = useUpdateTerm();
return {
updateTerm

View File

@ -1,24 +1,15 @@
'use client';
import React from 'react';
import { queryKeys } from '@/api/query_keys';
import { useSocketQueryEmitOn } from '@/api/buster_socket_query';
import { createContext, useContextSelector } from 'use-context-selector';
import { useAppLayoutContextSelector } from '../BusterAppLayout';
import { useGetTermsList } from '@/api/buster_rest/terms';
export const useBusterTermsList = () => {
const currentSegment = useAppLayoutContextSelector((x) => x.currentSegment);
const enabled = currentSegment === 'terms';
const {
data: termsList,
refetch: refetchTermsList,
isFetched: isFetchedTermsList
} = useSocketQueryEmitOn({
emitEvent: { route: '/terms/list', payload: { page: 0, page_size: 3000 } },
responseEvent: '/terms/list:ListTerms',
options: queryKeys.termsGetList,
enabledTrigger: enabled
});
} = useGetTermsList({ page: 0, page_size: 3000 });
return {
termsList,

View File

@ -1,7 +1,5 @@
import { isDev } from '@/config';
import { UserResponses } from '@/api/buster_socket/user';
import { TermsResponses } from '@/api/buster_socket/terms/termsResponses';
import { TeamResponses } from '@/api/buster_socket/teams/teamResponses';
import type {
BusterSocketResponseBase,
BusterSocketResponseMessage
@ -29,8 +27,6 @@ export const createBusterResponse = (
const isKnownMessageRoute = (parsedMessage: BusterSocketResponseMessage) => {
const allResponses = {
...UserResponses,
...TermsResponses,
...TeamResponses,
...ChatsResponses
};
const event = parsedMessage?.event;