fix: only tanstack usage in optimistic updates
This commit is contained in:
@ -59,7 +59,7 @@ const DealsTable = () => {
|
|||||||
} as MRT_TableOptions<DealSchema>
|
} as MRT_TableOptions<DealSchema>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
{paginationInfo.totalPages > 1 && (
|
{paginationInfo && paginationInfo.totalPages > 1 && (
|
||||||
<Group justify={"flex-end"}>
|
<Group justify={"flex-end"}>
|
||||||
<Pagination
|
<Pagination
|
||||||
withEdges
|
withEdges
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import makeContext from "@/lib/contextFactory/contextFactory";
|
|||||||
|
|
||||||
type BoardsContextState = {
|
type BoardsContextState = {
|
||||||
boards: BoardSchema[];
|
boards: BoardSchema[];
|
||||||
setBoards: React.Dispatch<React.SetStateAction<BoardSchema[]>>;
|
setBoards: (boards: BoardSchema[]) => void;
|
||||||
selectedBoard: BoardSchema | null;
|
selectedBoard: BoardSchema | null;
|
||||||
setSelectedBoardId: React.Dispatch<React.SetStateAction<number | null>>;
|
setSelectedBoardId: React.Dispatch<React.SetStateAction<number | null>>;
|
||||||
refetchBoards: () => void;
|
refetchBoards: () => void;
|
||||||
@ -22,6 +22,7 @@ const useBoardsContextState = (): BoardsContextState => {
|
|||||||
boards,
|
boards,
|
||||||
setBoards,
|
setBoards,
|
||||||
refetch: refetchBoards,
|
refetch: refetchBoards,
|
||||||
|
queryKey,
|
||||||
} = useBoardsList({ projectId: project?.id });
|
} = useBoardsList({ projectId: project?.id });
|
||||||
|
|
||||||
const [selectedBoardId, setSelectedBoardId] = useState<number | null>(null);
|
const [selectedBoardId, setSelectedBoardId] = useState<number | null>(null);
|
||||||
@ -34,8 +35,7 @@ const useBoardsContextState = (): BoardsContextState => {
|
|||||||
|
|
||||||
const boardsCrud = useBoardsCrud({
|
const boardsCrud = useBoardsCrud({
|
||||||
boards,
|
boards,
|
||||||
setBoards,
|
queryKey,
|
||||||
refetchBoards,
|
|
||||||
projectId: project?.id,
|
projectId: project?.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -9,10 +9,10 @@ import makeContext from "@/lib/contextFactory/contextFactory";
|
|||||||
|
|
||||||
type DealsContextState = {
|
type DealsContextState = {
|
||||||
deals: DealSchema[];
|
deals: DealSchema[];
|
||||||
setDeals: React.Dispatch<React.SetStateAction<DealSchema[]>>;
|
setDeals: (deals: DealSchema[]) => void;
|
||||||
refetchDeals: () => void;
|
refetchDeals: () => void;
|
||||||
dealsCrud: DealsCrud;
|
dealsCrud: DealsCrud;
|
||||||
paginationInfo: PaginationInfoSchema;
|
paginationInfo?: PaginationInfoSchema;
|
||||||
page: number;
|
page: number;
|
||||||
setPage: React.Dispatch<React.SetStateAction<number>>;
|
setPage: React.Dispatch<React.SetStateAction<number>>;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -9,17 +9,13 @@ import makeContext from "@/lib/contextFactory/contextFactory";
|
|||||||
type ProjectsContextState = {
|
type ProjectsContextState = {
|
||||||
selectedProject: ProjectSchema | null;
|
selectedProject: ProjectSchema | null;
|
||||||
setSelectedProjectId: React.Dispatch<React.SetStateAction<number | null>>;
|
setSelectedProjectId: React.Dispatch<React.SetStateAction<number | null>>;
|
||||||
refetchProjects: () => Promise<void>;
|
refetchProjects: () => void;
|
||||||
projects: ProjectSchema[];
|
projects: ProjectSchema[];
|
||||||
projectsCrud: ProjectsCrud;
|
projectsCrud: ProjectsCrud;
|
||||||
};
|
};
|
||||||
|
|
||||||
const useProjectsContextState = (): ProjectsContextState => {
|
const useProjectsContextState = (): ProjectsContextState => {
|
||||||
const {
|
const { projects, refetch: refetchProjects, queryKey } = useProjectsList();
|
||||||
projects,
|
|
||||||
setProjects,
|
|
||||||
refetch: refetchProjects,
|
|
||||||
} = useProjectsList();
|
|
||||||
|
|
||||||
const [selectedProjectId, setSelectedProjectId] = useState<number | null>(
|
const [selectedProjectId, setSelectedProjectId] = useState<number | null>(
|
||||||
null
|
null
|
||||||
@ -31,11 +27,7 @@ const useProjectsContextState = (): ProjectsContextState => {
|
|||||||
setSelectedProjectId(projects[0].id);
|
setSelectedProjectId(projects[0].id);
|
||||||
}
|
}
|
||||||
|
|
||||||
const projectsCrud = useProjectsCrud({
|
const projectsCrud = useProjectsCrud({ queryKey });
|
||||||
projects,
|
|
||||||
setProjects,
|
|
||||||
refetchProjects,
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
projects,
|
projects,
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import React from "react";
|
|
||||||
import { useBoardsContext } from "@/app/deals/contexts/BoardsContext";
|
import { useBoardsContext } from "@/app/deals/contexts/BoardsContext";
|
||||||
import { StatusesCrud, useStatusesCrud } from "@/hooks/cruds/useStatusesCrud";
|
import { StatusesCrud, useStatusesCrud } from "@/hooks/cruds/useStatusesCrud";
|
||||||
import useStatusesList from "@/hooks/lists/useStatusesList";
|
import useStatusesList from "@/hooks/lists/useStatusesList";
|
||||||
@ -9,7 +8,7 @@ import makeContext from "@/lib/contextFactory/contextFactory";
|
|||||||
|
|
||||||
type StatusesContextState = {
|
type StatusesContextState = {
|
||||||
statuses: StatusSchema[];
|
statuses: StatusSchema[];
|
||||||
setStatuses: React.Dispatch<React.SetStateAction<StatusSchema[]>>;
|
setStatuses: (statuses: StatusSchema[]) => void;
|
||||||
refetchStatuses: () => void;
|
refetchStatuses: () => void;
|
||||||
statusesCrud: StatusesCrud;
|
statusesCrud: StatusesCrud;
|
||||||
};
|
};
|
||||||
@ -20,14 +19,14 @@ const useStatusesContextState = (): StatusesContextState => {
|
|||||||
statuses,
|
statuses,
|
||||||
setStatuses,
|
setStatuses,
|
||||||
refetch: refetchStatuses,
|
refetch: refetchStatuses,
|
||||||
|
queryKey,
|
||||||
} = useStatusesList({
|
} = useStatusesList({
|
||||||
boardId: selectedBoard?.id,
|
boardId: selectedBoard?.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
const statusesCrud = useStatusesCrud({
|
const statusesCrud = useStatusesCrud({
|
||||||
statuses,
|
statuses,
|
||||||
setStatuses,
|
queryKey,
|
||||||
refetchStatuses,
|
|
||||||
boardId: selectedBoard?.id,
|
boardId: selectedBoard?.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import React from "react";
|
|
||||||
import { StatusesCrud, useStatusesCrud } from "@/hooks/cruds/useStatusesCrud";
|
import { StatusesCrud, useStatusesCrud } from "@/hooks/cruds/useStatusesCrud";
|
||||||
import useStatusesList from "@/hooks/lists/useStatusesList";
|
import useStatusesList from "@/hooks/lists/useStatusesList";
|
||||||
import { BoardSchema, StatusSchema } from "@/lib/client";
|
import { BoardSchema, StatusSchema } from "@/lib/client";
|
||||||
@ -9,7 +8,7 @@ import makeContext from "@/lib/contextFactory/contextFactory";
|
|||||||
type BoardStatusesContextState = {
|
type BoardStatusesContextState = {
|
||||||
board: BoardSchema;
|
board: BoardSchema;
|
||||||
statuses: StatusSchema[];
|
statuses: StatusSchema[];
|
||||||
setStatuses: React.Dispatch<React.SetStateAction<StatusSchema[]>>;
|
setStatuses: (statuses: StatusSchema[]) => void;
|
||||||
refetchStatuses: () => void;
|
refetchStatuses: () => void;
|
||||||
statusesCrud: StatusesCrud;
|
statusesCrud: StatusesCrud;
|
||||||
};
|
};
|
||||||
@ -25,15 +24,15 @@ const useBoardStatusesContextState = ({
|
|||||||
statuses,
|
statuses,
|
||||||
setStatuses,
|
setStatuses,
|
||||||
refetch: refetchStatuses,
|
refetch: refetchStatuses,
|
||||||
|
queryKey,
|
||||||
} = useStatusesList({
|
} = useStatusesList({
|
||||||
boardId: board.id,
|
boardId: board.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
const statusesCrud = useStatusesCrud({
|
const statusesCrud = useStatusesCrud({
|
||||||
statuses,
|
statuses,
|
||||||
setStatuses,
|
|
||||||
refetchStatuses,
|
|
||||||
boardId: board.id,
|
boardId: board.id,
|
||||||
|
queryKey,
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import React from "react";
|
|
||||||
import { BoardsCrud, useBoardsCrud } from "@/hooks/cruds/useBoardsCrud";
|
import { BoardsCrud, useBoardsCrud } from "@/hooks/cruds/useBoardsCrud";
|
||||||
import useBoardsList from "@/hooks/lists/useBoardsList";
|
import useBoardsList from "@/hooks/lists/useBoardsList";
|
||||||
import { BoardSchema, ProjectSchema } from "@/lib/client";
|
import { BoardSchema, ProjectSchema } from "@/lib/client";
|
||||||
@ -8,7 +7,7 @@ import makeContext from "@/lib/contextFactory/contextFactory";
|
|||||||
|
|
||||||
type ProjectBoardsContextState = {
|
type ProjectBoardsContextState = {
|
||||||
boards: BoardSchema[];
|
boards: BoardSchema[];
|
||||||
setBoards: React.Dispatch<React.SetStateAction<BoardSchema[]>>;
|
setBoards: (boards: BoardSchema[]) => void;
|
||||||
project: ProjectSchema;
|
project: ProjectSchema;
|
||||||
refetchBoards: () => void;
|
refetchBoards: () => void;
|
||||||
boardsCrud: BoardsCrud;
|
boardsCrud: BoardsCrud;
|
||||||
@ -23,12 +22,12 @@ const useProjectBoardsContextState = ({ project }: Props) => {
|
|||||||
boards,
|
boards,
|
||||||
setBoards,
|
setBoards,
|
||||||
refetch: refetchBoards,
|
refetch: refetchBoards,
|
||||||
|
queryKey,
|
||||||
} = useBoardsList({ projectId: project?.id });
|
} = useBoardsList({ projectId: project?.id });
|
||||||
|
|
||||||
const boardsCrud = useBoardsCrud({
|
const boardsCrud = useBoardsCrud({
|
||||||
boards,
|
boards,
|
||||||
setBoards,
|
queryKey,
|
||||||
refetchBoards,
|
|
||||||
projectId: project?.id,
|
projectId: project?.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -112,7 +112,7 @@ const useDealsAndStatusesDnd = (): ReturnType => {
|
|||||||
);
|
);
|
||||||
if (!overStatusId) return;
|
if (!overStatusId) return;
|
||||||
|
|
||||||
debouncedSetDeals(deals =>
|
debouncedSetDeals(
|
||||||
deals.map(deal =>
|
deals.map(deal =>
|
||||||
deal.id === activeDealId
|
deal.id === activeDealId
|
||||||
? {
|
? {
|
||||||
@ -142,7 +142,7 @@ const useDealsAndStatusesDnd = (): ReturnType => {
|
|||||||
const newRank = getNewStatusRank(activeStatusId, overStatusId);
|
const newRank = getNewStatusRank(activeStatusId, overStatusId);
|
||||||
if (!newRank) return;
|
if (!newRank) return;
|
||||||
|
|
||||||
debouncedSetStatuses(statuses =>
|
debouncedSetStatuses(
|
||||||
statuses.map(status =>
|
statuses.map(status =>
|
||||||
status.id === activeStatusId
|
status.id === activeStatusId
|
||||||
? { ...status, lexorank: newRank }
|
? { ...status, lexorank: newRank }
|
||||||
|
|||||||
@ -1,5 +1,9 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { useMutation, UseMutationOptions } from "@tanstack/react-query";
|
import {
|
||||||
|
useMutation,
|
||||||
|
UseMutationOptions,
|
||||||
|
useQueryClient,
|
||||||
|
} from "@tanstack/react-query";
|
||||||
import { AxiosError } from "axios";
|
import { AxiosError } from "axios";
|
||||||
import { Text } from "@mantine/core";
|
import { Text } from "@mantine/core";
|
||||||
import { modals } from "@mantine/modals";
|
import { modals } from "@mantine/modals";
|
||||||
@ -20,9 +24,8 @@ type CrudOperations<TEntity, TUpdate> = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type UseEntityOperationsProps<TEntity extends BaseEntity, TUpdate, TCreate> = {
|
type UseEntityOperationsProps<TEntity extends BaseEntity, TUpdate, TCreate> = {
|
||||||
entities: TEntity[];
|
key: string;
|
||||||
setEntities: React.Dispatch<React.SetStateAction<TEntity[]>>;
|
queryKey: any[];
|
||||||
refetch: () => void;
|
|
||||||
mutations: {
|
mutations: {
|
||||||
create: UseMutationOptions<
|
create: UseMutationOptions<
|
||||||
any,
|
any,
|
||||||
@ -50,9 +53,8 @@ const useCrudOperations = <
|
|||||||
TUpdate extends object,
|
TUpdate extends object,
|
||||||
TCreate extends object,
|
TCreate extends object,
|
||||||
>({
|
>({
|
||||||
entities,
|
key,
|
||||||
setEntities,
|
queryKey,
|
||||||
refetch,
|
|
||||||
mutations,
|
mutations,
|
||||||
getCreateEntity,
|
getCreateEntity,
|
||||||
getUpdateEntity,
|
getUpdateEntity,
|
||||||
@ -61,30 +63,81 @@ const useCrudOperations = <
|
|||||||
TEntity,
|
TEntity,
|
||||||
TUpdate
|
TUpdate
|
||||||
> => {
|
> => {
|
||||||
const onError = (error: AxiosError<HttpValidationError>) => {
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const onError = (
|
||||||
|
error: AxiosError<HttpValidationError>,
|
||||||
|
_: any,
|
||||||
|
context: any
|
||||||
|
) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
notifications.error({
|
notifications.error({
|
||||||
message: error.response?.data?.detail as string | undefined,
|
message: error.response?.data?.detail as string | undefined,
|
||||||
});
|
});
|
||||||
refetch();
|
if (context?.previous) {
|
||||||
|
queryClient.setQueryData(queryKey, context.previous);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onSettled = () => queryClient.invalidateQueries({ queryKey });
|
||||||
|
|
||||||
const createMutation = useMutation({
|
const createMutation = useMutation({
|
||||||
...mutations.create,
|
...mutations.create,
|
||||||
onError,
|
onError,
|
||||||
onSuccess: (res: { entity: TEntity }) => {
|
onSettled,
|
||||||
setEntities([...entities, res.entity]);
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const updateMutation = useMutation({
|
const updateMutation = useMutation({
|
||||||
...mutations.update,
|
...mutations.update,
|
||||||
onError,
|
onError,
|
||||||
|
onSettled,
|
||||||
|
onMutate: async ({ body: { entity: update } }) => {
|
||||||
|
await queryClient.cancelQueries({ queryKey: [key] });
|
||||||
|
|
||||||
|
const previous = queryClient.getQueryData(queryKey);
|
||||||
|
|
||||||
|
queryClient.setQueryData(queryKey, (old: { items: TEntity[] }) => {
|
||||||
|
let updated = old.items.map((entity: TEntity) =>
|
||||||
|
entity.id === update.id
|
||||||
|
? getUpdateEntity(entity, update)
|
||||||
|
: entity
|
||||||
|
);
|
||||||
|
|
||||||
|
if ("lexorank" in update) {
|
||||||
|
updated = sortByLexorank(
|
||||||
|
updated as (TEntity & { lexorank: string })[]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...old,
|
||||||
|
items: updated,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return { previous };
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const deleteMutation = useMutation({
|
const deleteMutation = useMutation({
|
||||||
...mutations.delete,
|
...mutations.delete,
|
||||||
onError,
|
onError,
|
||||||
|
onSettled,
|
||||||
|
onMutate: async ({ path: { pk } }) => {
|
||||||
|
await queryClient.cancelQueries({ queryKey: [key] });
|
||||||
|
|
||||||
|
const previous = queryClient.getQueryData(queryKey);
|
||||||
|
|
||||||
|
queryClient.setQueryData(queryKey, (old: { items: TEntity[] }) => {
|
||||||
|
const filtered = old.items.filter(e => e.id !== pk);
|
||||||
|
return {
|
||||||
|
...old,
|
||||||
|
items: filtered,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return { previous };
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const onCreate = (name: string) => {
|
const onCreate = (name: string) => {
|
||||||
@ -99,7 +152,7 @@ const useCrudOperations = <
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const onUpdate = (id: number, update: TUpdate) => {
|
const onUpdate = async (id: number, update: TUpdate) => {
|
||||||
updateMutation.mutate({
|
updateMutation.mutate({
|
||||||
body: {
|
body: {
|
||||||
entity: update,
|
entity: update,
|
||||||
@ -107,17 +160,6 @@ const useCrudOperations = <
|
|||||||
path: { pk: id },
|
path: { pk: id },
|
||||||
query: undefined,
|
query: undefined,
|
||||||
});
|
});
|
||||||
setEntities(prev => {
|
|
||||||
const updated = prev.map(entity =>
|
|
||||||
entity.id === id ? getUpdateEntity(entity, update) : entity
|
|
||||||
);
|
|
||||||
if ("lexorank" in update) {
|
|
||||||
return sortByLexorank(
|
|
||||||
updated as (TEntity & { lexorank: string })[]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return updated;
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onDelete = (entity: TEntity, onSuccess?: () => void) => {
|
const onDelete = (entity: TEntity, onSuccess?: () => void) => {
|
||||||
@ -131,7 +173,6 @@ const useCrudOperations = <
|
|||||||
onConfirm: () => {
|
onConfirm: () => {
|
||||||
deleteMutation.mutate({ path: { pk: entity.id } } as any);
|
deleteMutation.mutate({ path: { pk: entity.id } } as any);
|
||||||
onSuccess && onSuccess();
|
onSuccess && onSuccess();
|
||||||
setEntities(prev => prev.filter(e => e.id !== entity.id));
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import React from "react";
|
|
||||||
import { LexoRank } from "lexorank";
|
import { LexoRank } from "lexorank";
|
||||||
import { useCrudOperations } from "@/hooks/cruds/baseCrud";
|
import { useCrudOperations } from "@/hooks/cruds/baseCrud";
|
||||||
import {
|
import {
|
||||||
@ -15,9 +14,8 @@ import { getMaxByLexorank, getNewLexorank } from "@/utils/lexorank";
|
|||||||
|
|
||||||
type UseBoardsOperationsProps = {
|
type UseBoardsOperationsProps = {
|
||||||
boards: BoardSchema[];
|
boards: BoardSchema[];
|
||||||
setBoards: React.Dispatch<React.SetStateAction<BoardSchema[]>>;
|
|
||||||
refetchBoards: () => void;
|
|
||||||
projectId?: number;
|
projectId?: number;
|
||||||
|
queryKey: any[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type BoardsCrud = {
|
export type BoardsCrud = {
|
||||||
@ -28,15 +26,13 @@ export type BoardsCrud = {
|
|||||||
|
|
||||||
export const useBoardsCrud = ({
|
export const useBoardsCrud = ({
|
||||||
boards,
|
boards,
|
||||||
setBoards,
|
|
||||||
refetchBoards,
|
|
||||||
projectId,
|
projectId,
|
||||||
|
queryKey,
|
||||||
}: UseBoardsOperationsProps): BoardsCrud => {
|
}: UseBoardsOperationsProps): BoardsCrud => {
|
||||||
return useCrudOperations<BoardSchema, UpdateBoardSchema, CreateBoardSchema>(
|
return useCrudOperations<BoardSchema, UpdateBoardSchema, CreateBoardSchema>(
|
||||||
{
|
{
|
||||||
entities: boards,
|
key: "getBoards",
|
||||||
setEntities: setBoards,
|
queryKey,
|
||||||
refetch: refetchBoards,
|
|
||||||
mutations: {
|
mutations: {
|
||||||
create: createBoardMutation(),
|
create: createBoardMutation(),
|
||||||
update: updateBoardMutation(),
|
update: updateBoardMutation(),
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import React from "react";
|
|
||||||
import { LexoRank } from "lexorank";
|
import { LexoRank } from "lexorank";
|
||||||
import { useCrudOperations } from "@/hooks/cruds/baseCrud";
|
import { useCrudOperations } from "@/hooks/cruds/baseCrud";
|
||||||
import {
|
import {
|
||||||
@ -16,10 +15,9 @@ import { getNewLexorank } from "@/utils/lexorank";
|
|||||||
|
|
||||||
type UseDealsOperationsProps = {
|
type UseDealsOperationsProps = {
|
||||||
deals: DealSchema[];
|
deals: DealSchema[];
|
||||||
setDeals: React.Dispatch<React.SetStateAction<DealSchema[]>>;
|
|
||||||
refetchDeals: () => void;
|
|
||||||
boardId?: number;
|
boardId?: number;
|
||||||
statuses: StatusSchema[];
|
statuses: StatusSchema[];
|
||||||
|
queryKey: any[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type DealsCrud = {
|
export type DealsCrud = {
|
||||||
@ -30,15 +28,13 @@ export type DealsCrud = {
|
|||||||
|
|
||||||
export const useDealsCrud = ({
|
export const useDealsCrud = ({
|
||||||
deals,
|
deals,
|
||||||
setDeals,
|
|
||||||
refetchDeals,
|
|
||||||
boardId,
|
boardId,
|
||||||
statuses,
|
statuses,
|
||||||
|
queryKey,
|
||||||
}: UseDealsOperationsProps): DealsCrud => {
|
}: UseDealsOperationsProps): DealsCrud => {
|
||||||
return useCrudOperations<DealSchema, UpdateDealSchema, CreateDealSchema>({
|
return useCrudOperations<DealSchema, UpdateDealSchema, CreateDealSchema>({
|
||||||
entities: deals,
|
key: "getDeals",
|
||||||
setEntities: setDeals,
|
queryKey,
|
||||||
refetch: refetchDeals,
|
|
||||||
mutations: {
|
mutations: {
|
||||||
create: createDealMutation(),
|
create: createDealMutation(),
|
||||||
update: updateDealMutation(),
|
update: updateDealMutation(),
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import React from "react";
|
|
||||||
import { useCrudOperations } from "@/hooks/cruds/baseCrud";
|
import { useCrudOperations } from "@/hooks/cruds/baseCrud";
|
||||||
import {
|
import {
|
||||||
CreateProjectSchema,
|
CreateProjectSchema,
|
||||||
@ -12,9 +11,7 @@ import {
|
|||||||
} from "@/lib/client/@tanstack/react-query.gen";
|
} from "@/lib/client/@tanstack/react-query.gen";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
projects: ProjectSchema[];
|
queryKey: any[];
|
||||||
setProjects: React.Dispatch<React.SetStateAction<ProjectSchema[]>>;
|
|
||||||
refetchProjects: () => void;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ProjectsCrud = {
|
export type ProjectsCrud = {
|
||||||
@ -23,19 +20,14 @@ export type ProjectsCrud = {
|
|||||||
onDelete: (project: ProjectSchema) => void;
|
onDelete: (project: ProjectSchema) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useProjectsCrud = ({
|
export const useProjectsCrud = ({ queryKey }: Props): ProjectsCrud => {
|
||||||
projects,
|
|
||||||
setProjects,
|
|
||||||
refetchProjects,
|
|
||||||
}: Props): ProjectsCrud => {
|
|
||||||
return useCrudOperations<
|
return useCrudOperations<
|
||||||
ProjectSchema,
|
ProjectSchema,
|
||||||
UpdateProjectSchema,
|
UpdateProjectSchema,
|
||||||
CreateProjectSchema
|
CreateProjectSchema
|
||||||
>({
|
>({
|
||||||
entities: projects,
|
key: "getProjects",
|
||||||
setEntities: setProjects,
|
queryKey,
|
||||||
refetch: refetchProjects,
|
|
||||||
mutations: {
|
mutations: {
|
||||||
create: createProjectMutation(),
|
create: createProjectMutation(),
|
||||||
update: updateProjectMutation(),
|
update: updateProjectMutation(),
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import React from "react";
|
|
||||||
import { LexoRank } from "lexorank";
|
import { LexoRank } from "lexorank";
|
||||||
import { useCrudOperations } from "@/hooks/cruds/baseCrud";
|
import { useCrudOperations } from "@/hooks/cruds/baseCrud";
|
||||||
import {
|
import {
|
||||||
@ -15,9 +14,8 @@ import { getMaxByLexorank, getNewLexorank } from "@/utils/lexorank";
|
|||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
statuses: StatusSchema[];
|
statuses: StatusSchema[];
|
||||||
setStatuses: React.Dispatch<React.SetStateAction<StatusSchema[]>>;
|
|
||||||
refetchStatuses: () => void;
|
|
||||||
boardId?: number;
|
boardId?: number;
|
||||||
|
queryKey: any[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type StatusesCrud = {
|
export type StatusesCrud = {
|
||||||
@ -28,18 +26,16 @@ export type StatusesCrud = {
|
|||||||
|
|
||||||
export const useStatusesCrud = ({
|
export const useStatusesCrud = ({
|
||||||
statuses,
|
statuses,
|
||||||
setStatuses,
|
|
||||||
refetchStatuses,
|
|
||||||
boardId,
|
boardId,
|
||||||
|
queryKey,
|
||||||
}: Props): StatusesCrud => {
|
}: Props): StatusesCrud => {
|
||||||
return useCrudOperations<
|
return useCrudOperations<
|
||||||
StatusSchema,
|
StatusSchema,
|
||||||
UpdateStatusSchema,
|
UpdateStatusSchema,
|
||||||
CreateStatusSchema
|
CreateStatusSchema
|
||||||
>({
|
>({
|
||||||
entities: statuses,
|
key: "getStatuses",
|
||||||
setEntities: setStatuses,
|
queryKey,
|
||||||
refetch: refetchStatuses,
|
|
||||||
mutations: {
|
mutations: {
|
||||||
create: createStatusMutation(),
|
create: createStatusMutation(),
|
||||||
update: updateStatusMutation(),
|
update: updateStatusMutation(),
|
||||||
|
|||||||
@ -1,7 +1,9 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import { useQueryClient } from "@tanstack/react-query";
|
|
||||||
import { BoardSchema } from "@/lib/client";
|
import { BoardSchema } from "@/lib/client";
|
||||||
import { getBoardsOptions } from "@/lib/client/@tanstack/react-query.gen";
|
import {
|
||||||
|
getBoardsOptions,
|
||||||
|
getBoardsQueryKey,
|
||||||
|
} from "@/lib/client/@tanstack/react-query.gen";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
projectId?: number;
|
projectId?: number;
|
||||||
@ -9,26 +11,29 @@ type Props = {
|
|||||||
|
|
||||||
const useBoardsList = ({ projectId }: Props) => {
|
const useBoardsList = ({ projectId }: Props) => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
const options = {
|
||||||
|
path: { projectId: projectId ?? 0 },
|
||||||
|
};
|
||||||
|
const { data, refetch } = useQuery({
|
||||||
|
...getBoardsOptions(options),
|
||||||
|
enabled: !!projectId,
|
||||||
|
});
|
||||||
|
|
||||||
const [boards, setBoards] = useState<BoardSchema[]>([]);
|
const queryKey = getBoardsQueryKey(options);
|
||||||
|
|
||||||
const fetchBoards = () => {
|
const setBoards = (boards: BoardSchema[]) => {
|
||||||
if (!projectId) return;
|
queryClient.setQueryData(queryKey, (old: { items: BoardSchema[] }) => ({
|
||||||
queryClient
|
...old,
|
||||||
.fetchQuery({
|
items: boards,
|
||||||
...getBoardsOptions({ path: { projectId } }),
|
}));
|
||||||
})
|
|
||||||
.then(data => {
|
|
||||||
setBoards(data.boards);
|
|
||||||
})
|
|
||||||
.catch(err => console.error(err));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
return {
|
||||||
fetchBoards();
|
boards: data?.items ?? [],
|
||||||
}, [projectId]);
|
setBoards,
|
||||||
|
refetch,
|
||||||
return { boards, setBoards, refetch: fetchBoards };
|
queryKey,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export default useBoardsList;
|
export default useBoardsList;
|
||||||
|
|||||||
@ -1,7 +1,10 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useState } from "react";
|
||||||
import { useQueryClient } from "@tanstack/react-query";
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import { DealSchema, PaginationInfoSchema } from "@/lib/client";
|
import { DealSchema } from "@/lib/client";
|
||||||
import { getDealsOptions } from "@/lib/client/@tanstack/react-query.gen";
|
import {
|
||||||
|
getDealsOptions,
|
||||||
|
getDealsQueryKey,
|
||||||
|
} from "@/lib/client/@tanstack/react-query.gen";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
boardId?: number | null;
|
boardId?: number | null;
|
||||||
@ -14,47 +17,40 @@ const useDealsList = ({
|
|||||||
projectId = null,
|
projectId = null,
|
||||||
boardId = null,
|
boardId = null,
|
||||||
}: Props) => {
|
}: Props) => {
|
||||||
const [deals, setDeals] = useState<DealSchema[]>([]);
|
|
||||||
const [page, setPage] = useState(1);
|
|
||||||
const [paginationInfo, setPaginationInfo] = useState<PaginationInfoSchema>({
|
|
||||||
totalPages: 1,
|
|
||||||
totalItems: 1,
|
|
||||||
});
|
|
||||||
const itemsPerPage = 10;
|
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
const [page, setPage] = useState(1);
|
||||||
|
const itemsPerPage = 10;
|
||||||
|
|
||||||
const refetchDeals = () => {
|
const options = {
|
||||||
if (!boardId && !projectId) return;
|
|
||||||
|
|
||||||
queryClient
|
|
||||||
.fetchQuery({
|
|
||||||
...getDealsOptions({
|
|
||||||
query: {
|
query: {
|
||||||
boardId: boardId ?? null,
|
boardId: boardId ?? null,
|
||||||
projectId: projectId ?? null,
|
projectId: projectId ?? null,
|
||||||
page: withPagination ? page : null,
|
page: withPagination ? page : null,
|
||||||
itemsPerPage: withPagination ? itemsPerPage : null,
|
itemsPerPage: withPagination ? itemsPerPage : null,
|
||||||
},
|
},
|
||||||
}),
|
};
|
||||||
})
|
const { data, refetch } = useQuery({
|
||||||
.then(data => {
|
...getDealsOptions(options),
|
||||||
setDeals(data.deals);
|
enabled: !!boardId || !!projectId,
|
||||||
setPaginationInfo(data.paginationInfo);
|
});
|
||||||
})
|
|
||||||
.catch(err => console.error(err));
|
const queryKey = getDealsQueryKey(options);
|
||||||
|
|
||||||
|
const setDeals = (deals: DealSchema[]) => {
|
||||||
|
queryClient.setQueryData(queryKey, (old: { items: DealSchema[] }) => ({
|
||||||
|
...old,
|
||||||
|
items: deals,
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
refetchDeals();
|
|
||||||
}, [boardId, withPagination, projectId, page]);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
deals,
|
deals: data?.items ?? [],
|
||||||
setDeals,
|
setDeals,
|
||||||
refetchDeals,
|
refetchDeals: refetch,
|
||||||
page,
|
page,
|
||||||
setPage,
|
setPage,
|
||||||
paginationInfo,
|
paginationInfo: data?.paginationInfo,
|
||||||
|
queryKey,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,29 +1,34 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import { useQueryClient } from "@tanstack/react-query";
|
|
||||||
import { ProjectSchema } from "@/lib/client";
|
import { ProjectSchema } from "@/lib/client";
|
||||||
import { getProjectsOptions } from "@/lib/client/@tanstack/react-query.gen";
|
import {
|
||||||
|
getProjectsOptions,
|
||||||
|
getProjectsQueryKey,
|
||||||
|
} from "@/lib/client/@tanstack/react-query.gen";
|
||||||
|
|
||||||
const useProjectsList = () => {
|
const useProjectsList = () => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
const { data, refetch } = useQuery({
|
||||||
const [projects, setProjects] = useState<ProjectSchema[]>([]);
|
|
||||||
|
|
||||||
const fetchProjects = async () => {
|
|
||||||
return queryClient
|
|
||||||
.fetchQuery({
|
|
||||||
...getProjectsOptions(),
|
...getProjectsOptions(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const queryKey = getProjectsQueryKey();
|
||||||
|
|
||||||
|
const setProjects = (statuses: ProjectSchema[]) => {
|
||||||
|
queryClient.setQueryData(
|
||||||
|
queryKey,
|
||||||
|
(old: { items: ProjectSchema[] }) => ({
|
||||||
|
...old,
|
||||||
|
items: statuses,
|
||||||
})
|
})
|
||||||
.then(data => {
|
);
|
||||||
setProjects(data.projects);
|
|
||||||
})
|
|
||||||
.catch(err => console.error(err));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
return {
|
||||||
fetchProjects();
|
projects: data?.items ?? [],
|
||||||
}, []);
|
setProjects,
|
||||||
|
refetch,
|
||||||
return { projects, setProjects, refetch: fetchProjects };
|
queryKey,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export default useProjectsList;
|
export default useProjectsList;
|
||||||
|
|||||||
@ -1,7 +1,9 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import { useQueryClient } from "@tanstack/react-query";
|
|
||||||
import { StatusSchema } from "@/lib/client";
|
import { StatusSchema } from "@/lib/client";
|
||||||
import { getStatusesOptions } from "@/lib/client/@tanstack/react-query.gen";
|
import {
|
||||||
|
getStatusesOptions,
|
||||||
|
getStatusesQueryKey,
|
||||||
|
} from "@/lib/client/@tanstack/react-query.gen";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
boardId?: number;
|
boardId?: number;
|
||||||
@ -9,30 +11,32 @@ type Props = {
|
|||||||
|
|
||||||
const useStatusesList = ({ boardId }: Props) => {
|
const useStatusesList = ({ boardId }: Props) => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
const options = {
|
||||||
|
path: { boardId: boardId ?? 0 },
|
||||||
|
};
|
||||||
|
const { data, refetch } = useQuery({
|
||||||
|
...getStatusesOptions(options),
|
||||||
|
enabled: !!boardId,
|
||||||
|
});
|
||||||
|
|
||||||
const [statuses, setStatuses] = useState<StatusSchema[]>([]);
|
const queryKey = getStatusesQueryKey(options);
|
||||||
|
|
||||||
const fetchStatuses = () => {
|
const setStatuses = (statuses: StatusSchema[]) => {
|
||||||
if (!boardId) {
|
queryClient.setQueryData(
|
||||||
setStatuses([]);
|
queryKey,
|
||||||
return;
|
(old: { items: StatusSchema[] }) => ({
|
||||||
}
|
...old,
|
||||||
|
items: statuses,
|
||||||
queryClient
|
|
||||||
.fetchQuery({
|
|
||||||
...getStatusesOptions({ path: { boardId } }),
|
|
||||||
})
|
})
|
||||||
.then(data => {
|
);
|
||||||
setStatuses(data.statuses);
|
|
||||||
})
|
|
||||||
.catch(err => console.error(err));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
return {
|
||||||
fetchStatuses();
|
statuses: data?.items ?? [],
|
||||||
}, [boardId]);
|
setStatuses,
|
||||||
|
refetch,
|
||||||
return { statuses, setStatuses, refetch: fetchStatuses };
|
queryKey,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export default useStatusesList;
|
export default useStatusesList;
|
||||||
|
|||||||
@ -229,9 +229,9 @@ export type DeleteStatusResponse = {
|
|||||||
*/
|
*/
|
||||||
export type GetBoardsResponse = {
|
export type GetBoardsResponse = {
|
||||||
/**
|
/**
|
||||||
* Boards
|
* Items
|
||||||
*/
|
*/
|
||||||
boards: Array<BoardSchema>;
|
items: Array<BoardSchema>;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -239,9 +239,9 @@ export type GetBoardsResponse = {
|
|||||||
*/
|
*/
|
||||||
export type GetDealsResponse = {
|
export type GetDealsResponse = {
|
||||||
/**
|
/**
|
||||||
* Deals
|
* Items
|
||||||
*/
|
*/
|
||||||
deals: Array<DealSchema>;
|
items: Array<DealSchema>;
|
||||||
paginationInfo: PaginationInfoSchema;
|
paginationInfo: PaginationInfoSchema;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -250,9 +250,9 @@ export type GetDealsResponse = {
|
|||||||
*/
|
*/
|
||||||
export type GetProjectsResponse = {
|
export type GetProjectsResponse = {
|
||||||
/**
|
/**
|
||||||
* Projects
|
* Items
|
||||||
*/
|
*/
|
||||||
projects: Array<ProjectSchema>;
|
items: Array<ProjectSchema>;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -260,9 +260,9 @@ export type GetProjectsResponse = {
|
|||||||
*/
|
*/
|
||||||
export type GetStatusesResponse = {
|
export type GetStatusesResponse = {
|
||||||
/**
|
/**
|
||||||
* Statuses
|
* Items
|
||||||
*/
|
*/
|
||||||
statuses: Array<StatusSchema>;
|
items: Array<StatusSchema>;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -168,7 +168,7 @@ export const zDeleteStatusResponse = z.object({
|
|||||||
* GetBoardsResponse
|
* GetBoardsResponse
|
||||||
*/
|
*/
|
||||||
export const zGetBoardsResponse = z.object({
|
export const zGetBoardsResponse = z.object({
|
||||||
boards: z.array(zBoardSchema),
|
items: z.array(zBoardSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -183,7 +183,7 @@ export const zPaginationInfoSchema = z.object({
|
|||||||
* GetDealsResponse
|
* GetDealsResponse
|
||||||
*/
|
*/
|
||||||
export const zGetDealsResponse = z.object({
|
export const zGetDealsResponse = z.object({
|
||||||
deals: z.array(zDealSchema),
|
items: z.array(zDealSchema),
|
||||||
paginationInfo: zPaginationInfoSchema,
|
paginationInfo: zPaginationInfoSchema,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -191,14 +191,14 @@ export const zGetDealsResponse = z.object({
|
|||||||
* GetProjectsResponse
|
* GetProjectsResponse
|
||||||
*/
|
*/
|
||||||
export const zGetProjectsResponse = z.object({
|
export const zGetProjectsResponse = z.object({
|
||||||
projects: z.array(zProjectSchema),
|
items: z.array(zProjectSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GetStatusesResponse
|
* GetStatusesResponse
|
||||||
*/
|
*/
|
||||||
export const zGetStatusesResponse = z.object({
|
export const zGetStatusesResponse = z.object({
|
||||||
statuses: z.array(zStatusSchema),
|
items: z.array(zStatusSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user