refactor: crud objects in contexts
This commit is contained in:
32
src/hooks/baseCrud/types.ts
Normal file
32
src/hooks/baseCrud/types.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { Options } from "@/lib/client";
|
||||
|
||||
export type BaseEntity = {
|
||||
id: number;
|
||||
name: string;
|
||||
lexorank?: string;
|
||||
};
|
||||
|
||||
export type CreateMutationOptions = Options<{
|
||||
body: any;
|
||||
query: undefined;
|
||||
path: undefined;
|
||||
url: string;
|
||||
}>;
|
||||
|
||||
export type UpdateMutationOptions = Options<{
|
||||
body: any;
|
||||
query: undefined;
|
||||
path: {
|
||||
pk: number;
|
||||
};
|
||||
url: string;
|
||||
}>;
|
||||
|
||||
export type DeleteMutationOptions = Options<{
|
||||
body: undefined;
|
||||
query: undefined;
|
||||
path: {
|
||||
pk: number;
|
||||
};
|
||||
url: string;
|
||||
}>;
|
||||
@ -3,46 +3,23 @@ import { useMutation, UseMutationOptions } from "@tanstack/react-query";
|
||||
import { AxiosError } from "axios";
|
||||
import { Text } from "@mantine/core";
|
||||
import { modals } from "@mantine/modals";
|
||||
import { HttpValidationError, Options } from "@/lib/client";
|
||||
import {
|
||||
BaseEntity,
|
||||
CreateMutationOptions,
|
||||
DeleteMutationOptions,
|
||||
UpdateMutationOptions,
|
||||
} from "@/hooks/baseCrud/types";
|
||||
import { HttpValidationError } from "@/lib/client";
|
||||
import { notifications } from "@/lib/notifications";
|
||||
import { sortByLexorank } from "@/utils/lexorank";
|
||||
|
||||
type BaseEntity = {
|
||||
id: number;
|
||||
name: string;
|
||||
lexorank?: string;
|
||||
type CrudOperations<TEntity, TUpdate> = {
|
||||
onCreate: (name: string) => void;
|
||||
onUpdate: (id: number, update: TUpdate) => void;
|
||||
onDelete: (entity: TEntity) => void;
|
||||
};
|
||||
|
||||
type CreateMutationOptions = Options<{
|
||||
body: any;
|
||||
query: undefined;
|
||||
path: undefined;
|
||||
url: string;
|
||||
}>;
|
||||
|
||||
type UpdateMutationOptions = Options<{
|
||||
body: any;
|
||||
query: undefined;
|
||||
path: {
|
||||
pk: number;
|
||||
};
|
||||
url: string;
|
||||
}>;
|
||||
|
||||
type DeleteMutationOptions = Options<{
|
||||
body: undefined;
|
||||
query: undefined;
|
||||
path: {
|
||||
pk: number;
|
||||
};
|
||||
url: string;
|
||||
}>;
|
||||
|
||||
type UseEntityOperationsProps<
|
||||
TEntity extends BaseEntity,
|
||||
TUpdate,
|
||||
TCreate,
|
||||
> = {
|
||||
type UseEntityOperationsProps<TEntity extends BaseEntity, TUpdate, TCreate> = {
|
||||
entities: TEntity[];
|
||||
setEntities: React.Dispatch<React.SetStateAction<TEntity[]>>;
|
||||
refetch: () => void;
|
||||
@ -80,7 +57,10 @@ const useCrudOperations = <
|
||||
getCreateEntity,
|
||||
getUpdateEntity,
|
||||
getDeleteConfirmTitle,
|
||||
}: UseEntityOperationsProps<TEntity, TUpdate, TCreate>) => {
|
||||
}: UseEntityOperationsProps<TEntity, TUpdate, TCreate>): CrudOperations<
|
||||
TEntity,
|
||||
TUpdate
|
||||
> => {
|
||||
const onError = (error: AxiosError<HttpValidationError>) => {
|
||||
console.error(error);
|
||||
notifications.error({
|
||||
@ -1,6 +1,6 @@
|
||||
import React from "react";
|
||||
import { LexoRank } from "lexorank";
|
||||
import { useCrudOperations } from "@/hooks/base";
|
||||
import { useCrudOperations } from "@/hooks/baseCrud";
|
||||
import {
|
||||
BoardSchema,
|
||||
CreateBoardSchema,
|
||||
@ -20,18 +20,18 @@ type UseBoardsOperationsProps = {
|
||||
projectId?: number;
|
||||
};
|
||||
|
||||
type BoardsOperations = {
|
||||
export type BoardsCrud = {
|
||||
onCreate: (name: string) => void;
|
||||
onUpdate: (boardId: number, board: UpdateBoardSchema) => void;
|
||||
onDelete: (board: BoardSchema) => void;
|
||||
};
|
||||
|
||||
export const useBoardsOperations = ({
|
||||
export const useBoardsCrud = ({
|
||||
boards,
|
||||
setBoards,
|
||||
refetchBoards,
|
||||
projectId,
|
||||
}: UseBoardsOperationsProps): BoardsOperations => {
|
||||
}: UseBoardsOperationsProps): BoardsCrud => {
|
||||
return useCrudOperations<BoardSchema, UpdateBoardSchema, CreateBoardSchema>(
|
||||
{
|
||||
entities: boards,
|
||||
@ -1,5 +1,5 @@
|
||||
import React from "react";
|
||||
import { useCrudOperations } from "@/hooks/base";
|
||||
import { useCrudOperations } from "@/hooks/baseCrud";
|
||||
import {
|
||||
CreateProjectSchema,
|
||||
ProjectSchema,
|
||||
@ -17,17 +17,17 @@ type Props = {
|
||||
refetchProjects: () => void;
|
||||
};
|
||||
|
||||
type ProjectsOperations = {
|
||||
export type ProjectsCrud = {
|
||||
onCreate: (name: string) => void;
|
||||
onUpdate: (projectId: number, project: UpdateProjectSchema) => void;
|
||||
onDelete: (project: ProjectSchema) => void;
|
||||
};
|
||||
|
||||
export const useProjectsOperations = ({
|
||||
export const useProjectsCrud = ({
|
||||
projects,
|
||||
setProjects,
|
||||
refetchProjects,
|
||||
}: Props): ProjectsOperations => {
|
||||
}: Props): ProjectsCrud => {
|
||||
return useCrudOperations<
|
||||
ProjectSchema,
|
||||
UpdateProjectSchema,
|
||||
@ -1,6 +1,6 @@
|
||||
import React from "react";
|
||||
import { LexoRank } from "lexorank";
|
||||
import { useCrudOperations } from "@/hooks/base";
|
||||
import { useCrudOperations } from "@/hooks/baseCrud";
|
||||
import {
|
||||
CreateStatusSchema,
|
||||
StatusSchema,
|
||||
@ -20,18 +20,18 @@ type Props = {
|
||||
boardId?: number;
|
||||
};
|
||||
|
||||
type StatusesOperations = {
|
||||
export type StatusesCrud = {
|
||||
onCreate: (name: string) => void;
|
||||
onUpdate: (statusId: number, status: UpdateStatusSchema) => void;
|
||||
onDelete: (status: StatusSchema) => void;
|
||||
};
|
||||
|
||||
export const useStatusesOperations = ({
|
||||
export const useStatusesCrud = ({
|
||||
statuses,
|
||||
setStatuses,
|
||||
refetchStatuses,
|
||||
boardId,
|
||||
}: Props): StatusesOperations => {
|
||||
}: Props): StatusesCrud => {
|
||||
return useCrudOperations<
|
||||
StatusSchema,
|
||||
UpdateStatusSchema,
|
||||
Reference in New Issue
Block a user