Skip to content

Commit

Permalink
🧮 Sort proposals by creation date second to the user define order (#4793
Browse files Browse the repository at this point in the history
)

* Sort proposals by creation date second to the user define order

* Fix types
  • Loading branch information
thesan authored Feb 29, 2024
1 parent 122e9f0 commit 9ba4d45
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 9 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
2 changes: 1 addition & 1 deletion packages/ui/src/council/hooks/useMyPastVotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const useMyPastVotes = ({ order, perPage = 5 }: UseMyPastVotesProps) => {

const variables = {
where,
orderBy: [toQueryOrderByInput<CastVoteOrderByInput>(order)],
orderBy: toQueryOrderByInput<CastVoteOrderByInput>(order),
limit: perPage,
offset,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/council/hooks/usePastElections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface UsePastElectionsProps {

export const usePastElections = ({ page = 1, order }: UsePastElectionsProps) => {
const variables = {
orderBy: [toQueryOrderByInput<ElectionRoundOrderByInput>(order)],
orderBy: toQueryOrderByInput<ElectionRoundOrderByInput>(order),
limit: ELECTION_PER_PAGE,
offset: (page - 1) * ELECTION_PER_PAGE,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/forum/hooks/useForumCategoryThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useForumCategoryThreads = (
const { data: threadsData } = useGetForumThreadsQuery({
variables: {
where: where(filters, categoryId, isArchive),
orderBy: [ForumThreadOrderByInput.IsStickyDesc, toQueryOrderByInput<ForumThreadOrderByInput>(options.order)],
orderBy: [ForumThreadOrderByInput.IsStickyDesc, ...toQueryOrderByInput<ForumThreadOrderByInput>(options.order)],
...(!pagination
? { limit: 30 }
: {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/forum/hooks/useMyThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const useMyThreads = ({ page, threadsPerPage = 5, order }: UseMyThreadsPr
},
limit: threadsPerPage,
offset: (page - 1) * threadsPerPage,
orderBy: [ForumThreadOrderByInput.IsStickyDesc, toQueryOrderByInput<ForumThreadOrderByInput>(order)],
orderBy: [ForumThreadOrderByInput.IsStickyDesc, ...toQueryOrderByInput<ForumThreadOrderByInput>(order)],
}
const { loading: loadingPosts, data: threadsData } = useGetForumThreadsQuery({ variables })
const { loading: loadingCount, data: countData } = useGetForumThreadsCountQuery({
Expand Down
5 changes: 4 additions & 1 deletion 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const usePastWorkersPagination = ({
group: { id_eq: group_eq },
status_json: { isTypeOf_not: 'WorkerStatusActive' },
},
orderBy: [toQueryOrderByInput<WorkerOrderByInput>(order)],
orderBy: toQueryOrderByInput<WorkerOrderByInput>(order),
}

const { loading: loadingCount, data: countData } = useGetWorkersCountQuery({ variables })
Expand Down

0 comments on commit 9ba4d45

Please sign in to comment.