From 44b5fd9bb1160ec36875c86acf9fb8f469debf3e Mon Sep 17 00:00:00 2001 From: Theophile Sandoz Date: Thu, 29 Feb 2024 11:23:02 +0100 Subject: [PATCH] Sort proposals by creation date second to the user define order --- packages/ui/src/common/hooks/useSort.ts | 10 +++++++--- packages/ui/src/proposals/hooks/useProposals.ts | 9 +++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/ui/src/common/hooks/useSort.ts b/packages/ui/src/common/hooks/useSort.ts index 7342e5c69c..742a745bfc 100644 --- a/packages/ui/src/common/hooks/useSort.ts +++ b/packages/ui/src/common/hooks/useSort.ts @@ -13,10 +13,14 @@ export interface SortOrder { isDescending: boolean } -export function toQueryOrderByInput(order: SortOrder) { - const value = order.isDescending ? `${order.orderKey}_DESC` : `${order.orderKey}_ASC` +export function toQueryOrderByInput(...orders: (SortOrder | undefined)[]): Order[] { + return orders.flatMap((order) => { + if (!order) return [] - return value as Order + const value = order.isDescending ? `${order.orderKey}_DESC` : `${order.orderKey}_ASC` + + return value as Order + }) } export type GetSortProps = (key: OrderKey) => { diff --git a/packages/ui/src/proposals/hooks/useProposals.ts b/packages/ui/src/proposals/hooks/useProposals.ts index 3961ecd630..026e0a00fb 100644 --- a/packages/ui/src/proposals/hooks/useProposals.ts +++ b/packages/ui/src/proposals/hooks/useProposals.ts @@ -33,7 +33,10 @@ export const useProposals = ({ perPage = 10, fetchAll = false, }: UseProposalsProps): UseProposals => { - const orderBy = order ? toQueryOrderByInput(order) : ProposalOrderByInput.CreatedAtDesc + const orderBy = toQueryOrderByInput(order, { + orderKey: 'createdAt', + isDescending: order?.isDescending ?? false, + }) const where = useMemo(() => { const where: ProposalWhereInput = filters?.stage @@ -68,7 +71,9 @@ export const useProposals = ({ } return where }, [status, JSON.stringify(filters)]) - const { data: proposalCount } = useGetProposalsCountQuery({ variables: { where } }) + const { data: proposalCount } = useGetProposalsCountQuery({ + variables: { where }, + }) const { offset, pagination } = usePagination(perPage, proposalCount?.proposalsConnection.totalCount ?? 0, []) const paginationVariables = fetchAll ? {} : { offset, limit: perPage } const { loading, data, previousData } = useGetProposalsQuery({