feat: deals fetch
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { Card } from "@mantine/core";
|
||||
import { DealSchema } from "@/types/DealSchema";
|
||||
import { DealSchema } from "@/client";
|
||||
|
||||
type Props = {
|
||||
deal: DealSchema;
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
import React, { FC, useMemo } from "react";
|
||||
import { Box } from "@mantine/core";
|
||||
import DealCard from "@/app/deals/components/DealCard/DealCard";
|
||||
import { DealSchema } from "@/client";
|
||||
import { SortableItem } from "@/components/SortableDnd/SortableItem";
|
||||
import { DealSchema } from "@/types/DealSchema";
|
||||
|
||||
|
||||
type Props = {
|
||||
deal: DealSchema;
|
||||
|
||||
@ -3,8 +3,7 @@ import { defaultDropAnimation, DragOverlay } from "@dnd-kit/core";
|
||||
import DealCard from "@/app/deals/components/DealCard/DealCard";
|
||||
import StatusColumn from "@/app/deals/components/StatusColumn/StatusColumn";
|
||||
import { useStatusesContext } from "@/app/deals/contexts/StatusesContext";
|
||||
import { DealSchema } from "@/types/DealSchema";
|
||||
import { StatusSchema } from "@/client";
|
||||
import { DealSchema, StatusSchema } from "@/client";
|
||||
|
||||
type Props = {
|
||||
activeDeal: DealSchema | null;
|
||||
|
||||
@ -6,8 +6,7 @@ import {
|
||||
} from "@dnd-kit/sortable";
|
||||
import { Box, Stack, Text } from "@mantine/core";
|
||||
import DealContainer from "@/app/deals/components/DealContainer/DealContainer";
|
||||
import { StatusSchema } from "@/client";
|
||||
import { DealSchema } from "@/types/DealSchema";
|
||||
import { DealSchema, StatusSchema } from "@/client";
|
||||
import { sortByLexorank } from "@/utils/lexorank";
|
||||
|
||||
type Props = {
|
||||
|
||||
@ -8,9 +8,8 @@ import React, {
|
||||
useState,
|
||||
} from "react";
|
||||
import { useBoardsContext } from "@/app/deals/contexts/BoardsContext";
|
||||
import useDeals from "@/app/deals/hooks/useDeals";
|
||||
import { StatusSchema } from "@/client";
|
||||
import { DealSchema } from "@/types/DealSchema";
|
||||
import { DealSchema, StatusSchema } from "@/client";
|
||||
import useDealsList from "@/hooks/useDealsList";
|
||||
|
||||
type StatusesContextState = {
|
||||
statuses: StatusSchema[];
|
||||
@ -25,8 +24,8 @@ const StatusesContext = createContext<StatusesContextState | undefined>(
|
||||
|
||||
const useStatusesContextState = () => {
|
||||
const [statuses, setStatuses] = useState<StatusSchema[]>([]);
|
||||
const { deals, setDeals } = useDeals();
|
||||
const { selectedBoard } = useBoardsContext();
|
||||
const { deals, setDeals } = useDealsList({ boardId: selectedBoard?.id });
|
||||
|
||||
useEffect(() => {
|
||||
setStatuses(selectedBoard?.statuses ?? []);
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
import { useState } from "react";
|
||||
import { BoardSchema } from "@/client";
|
||||
|
||||
const useBoards = () => {
|
||||
const [boards, setBoards] = useState<BoardSchema[]>([]);
|
||||
|
||||
return {
|
||||
boards,
|
||||
setBoards,
|
||||
};
|
||||
};
|
||||
|
||||
export default useBoards;
|
||||
@ -1,37 +0,0 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { DealSchema } from "@/types/DealSchema";
|
||||
|
||||
const useDeals = () => {
|
||||
const [deals, setDeals] = useState<DealSchema[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const INITIAL_DEALS: DealSchema[] = [
|
||||
{
|
||||
id: 1,
|
||||
name: "Deal 1",
|
||||
lexorank: "0|gggggg:",
|
||||
statusId: 1,
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "Deal 2",
|
||||
lexorank: "0|mmmmmm:",
|
||||
statusId: 1,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: "Deal 3",
|
||||
lexorank: "0|ssssss:",
|
||||
statusId: 2,
|
||||
},
|
||||
];
|
||||
setDeals(INITIAL_DEALS);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
deals,
|
||||
setDeals,
|
||||
};
|
||||
};
|
||||
|
||||
export default useDeals;
|
||||
@ -4,8 +4,7 @@ import { throttle } from "lodash";
|
||||
import { useStatusesContext } from "@/app/deals/contexts/StatusesContext";
|
||||
import useGetNewRank from "@/app/deals/hooks/useGetNewRank";
|
||||
import { getStatusId, isStatusId } from "@/app/deals/utils/statusId";
|
||||
import { StatusSchema } from "@/client";
|
||||
import { DealSchema } from "@/types/DealSchema";
|
||||
import { DealSchema, StatusSchema } from "@/client";
|
||||
import { sortByLexorank } from "@/utils/lexorank";
|
||||
|
||||
type Props = {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { LexoRank } from "lexorank";
|
||||
import { useStatusesContext } from "@/app/deals/contexts/StatusesContext";
|
||||
import { DealSchema } from "@/types/DealSchema";
|
||||
import { DealSchema } from "@/client";
|
||||
import { getNewLexorank, sortByLexorank } from "@/utils/lexorank";
|
||||
|
||||
const useGetNewRank = () => {
|
||||
|
||||
@ -2,8 +2,12 @@
|
||||
|
||||
import { queryOptions } from "@tanstack/react-query";
|
||||
import { client as _heyApiClient } from "../client.gen";
|
||||
import { getBoards, getProjects, type Options } from "../sdk.gen";
|
||||
import type { GetBoardsData, GetProjectsData } from "../types.gen";
|
||||
import { getBoards, getDeals, getProjects, type Options } from "../sdk.gen";
|
||||
import type {
|
||||
GetBoardsData,
|
||||
GetDealsData,
|
||||
GetProjectsData,
|
||||
} from "../types.gen";
|
||||
|
||||
export type QueryKey<TOptions extends Options> = [
|
||||
Pick<TOptions, "baseURL" | "body" | "headers" | "path" | "query"> & {
|
||||
@ -82,3 +86,24 @@ export const getBoardsOptions = (options: Options<GetBoardsData>) => {
|
||||
queryKey: getBoardsQueryKey(options),
|
||||
});
|
||||
};
|
||||
|
||||
export const getDealsQueryKey = (options: Options<GetDealsData>) =>
|
||||
createQueryKey("getDeals", options);
|
||||
|
||||
/**
|
||||
* Get Deals
|
||||
*/
|
||||
export const getDealsOptions = (options: Options<GetDealsData>) => {
|
||||
return queryOptions({
|
||||
queryFn: async ({ queryKey, signal }) => {
|
||||
const { data } = await getDeals({
|
||||
...options,
|
||||
...queryKey[0],
|
||||
signal,
|
||||
throwOnError: true,
|
||||
});
|
||||
return data;
|
||||
},
|
||||
queryKey: getDealsQueryKey(options),
|
||||
});
|
||||
};
|
||||
|
||||
@ -6,6 +6,9 @@ import type {
|
||||
GetBoardsData,
|
||||
GetBoardsErrors,
|
||||
GetBoardsResponses,
|
||||
GetDealsData,
|
||||
GetDealsErrors,
|
||||
GetDealsResponses,
|
||||
GetProjectsData,
|
||||
GetProjectsResponses,
|
||||
} from "./types.gen";
|
||||
@ -60,3 +63,20 @@ export const getBoards = <ThrowOnError extends boolean = false>(
|
||||
...options,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Get Deals
|
||||
*/
|
||||
export const getDeals = <ThrowOnError extends boolean = false>(
|
||||
options: Options<GetDealsData, ThrowOnError>
|
||||
) => {
|
||||
return (options.client ?? _heyApiClient).get<
|
||||
GetDealsResponses,
|
||||
GetDealsErrors,
|
||||
ThrowOnError
|
||||
>({
|
||||
responseType: "json",
|
||||
url: "/deal/{board_id}",
|
||||
...options,
|
||||
});
|
||||
};
|
||||
|
||||
@ -22,6 +22,28 @@ export type BoardSchema = {
|
||||
statuses: Array<StatusSchema>;
|
||||
};
|
||||
|
||||
/**
|
||||
* DealSchema
|
||||
*/
|
||||
export type DealSchema = {
|
||||
/**
|
||||
* Name
|
||||
*/
|
||||
name: string;
|
||||
/**
|
||||
* Id
|
||||
*/
|
||||
id: number;
|
||||
/**
|
||||
* Lexorank
|
||||
*/
|
||||
lexorank: string;
|
||||
/**
|
||||
* Statusid
|
||||
*/
|
||||
statusId: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* GetBoardsResponse
|
||||
*/
|
||||
@ -32,6 +54,16 @@ export type GetBoardsResponse = {
|
||||
boards: Array<BoardSchema>;
|
||||
};
|
||||
|
||||
/**
|
||||
* GetDealsResponse
|
||||
*/
|
||||
export type GetDealsResponse = {
|
||||
/**
|
||||
* Deals
|
||||
*/
|
||||
deals: Array<DealSchema>;
|
||||
};
|
||||
|
||||
/**
|
||||
* GetProjectsResponse
|
||||
*/
|
||||
@ -149,6 +181,36 @@ export type GetBoardsResponses = {
|
||||
|
||||
export type GetBoardsResponse2 = GetBoardsResponses[keyof GetBoardsResponses];
|
||||
|
||||
export type GetDealsData = {
|
||||
body?: never;
|
||||
path: {
|
||||
/**
|
||||
* Board Id
|
||||
*/
|
||||
board_id: number;
|
||||
};
|
||||
query?: never;
|
||||
url: "/deal/{board_id}";
|
||||
};
|
||||
|
||||
export type GetDealsErrors = {
|
||||
/**
|
||||
* Validation Error
|
||||
*/
|
||||
422: HttpValidationError;
|
||||
};
|
||||
|
||||
export type GetDealsError = GetDealsErrors[keyof GetDealsErrors];
|
||||
|
||||
export type GetDealsResponses = {
|
||||
/**
|
||||
* Successful Response
|
||||
*/
|
||||
200: GetDealsResponse;
|
||||
};
|
||||
|
||||
export type GetDealsResponse2 = GetDealsResponses[keyof GetDealsResponses];
|
||||
|
||||
export type ClientOptions = {
|
||||
baseURL: "http://localhost:8000" | (string & {});
|
||||
};
|
||||
|
||||
29
src/hooks/useDealsList.ts
Normal file
29
src/hooks/useDealsList.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { DealSchema } from "@/client";
|
||||
import { getDealsOptions } from "@/client/@tanstack/react-query.gen";
|
||||
|
||||
type Props = {
|
||||
boardId?: number;
|
||||
};
|
||||
|
||||
const useDealsList = ({ boardId }: Props) => {
|
||||
const [deals, setDeals] = useState<DealSchema[]>([]);
|
||||
|
||||
const { data, refetch, isLoading } = useQuery({
|
||||
...getDealsOptions({ path: { board_id: boardId! } }),
|
||||
enabled: boardId !== undefined,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (boardId === undefined) {
|
||||
setDeals([]);
|
||||
} else if (data?.deals) {
|
||||
setDeals(data.deals);
|
||||
}
|
||||
}, [data?.deals, boardId]);
|
||||
|
||||
return { deals, setDeals, refetch, isLoading };
|
||||
};
|
||||
|
||||
export default useDealsList;
|
||||
@ -1,6 +0,0 @@
|
||||
export type DealSchema = {
|
||||
id: number;
|
||||
name: string;
|
||||
lexorank: string;
|
||||
statusId: number;
|
||||
};
|
||||
Reference in New Issue
Block a user