Skip to content

Commit

Permalink
Sort proposals by creation date second to the user define order
Browse files Browse the repository at this point in the history
  • Loading branch information
thesan committed Feb 29, 2024
1 parent 122e9f0 commit 44b5fd9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 7 additions & 3 deletions packages/ui/src/common/hooks/useSort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ export interface SortOrder<Order extends BaseSortKey> {
isDescending: boolean
}

export function toQueryOrderByInput<Order extends BaseSortKey>(order: SortOrder<Order>) {
const value = order.isDescending ? `${order.orderKey}_DESC` : `${order.orderKey}_ASC`
export function toQueryOrderByInput<Order extends BaseSortKey>(...orders: (SortOrder<Order> | 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<Order extends BaseSortKey> = (key: OrderKey<Order>) => {
Expand Down
9 changes: 7 additions & 2 deletions packages/ui/src/proposals/hooks/useProposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export const useProposals = ({
perPage = 10,
fetchAll = false,
}: UseProposalsProps): UseProposals => {
const orderBy = order ? toQueryOrderByInput<ProposalOrderByInput>(order) : ProposalOrderByInput.CreatedAtDesc
const orderBy = toQueryOrderByInput<ProposalOrderByInput>(order, {
orderKey: 'createdAt',
isDescending: order?.isDescending ?? false,
})

const where = useMemo(() => {
const where: ProposalWhereInput = filters?.stage
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 44b5fd9

Please sign in to comment.