-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): add question level filtering (#413)
* feat(app): add question level filtering * refactor(app): remove Boolean * feat(app): add Params type * feat(app): create useQuestionsLevel hook * refactor(app): simplify query validation * feat(app): improve types * refactor(app): refactor questions filtering * refactor(app): change way to remove level from query * refactor(app): change options map * refactor(app): remove unused exports * refactor(app): change variable name * refactor(app): invoke to methods from Number object Co-authored-by: Michał Miszczyszyn <michal@mmiszy.pl>
- Loading branch information
1 parent
874d911
commit 744b1b8
Showing
9 changed files
with
123 additions
and
49 deletions.
There are no files selected for viewing
22 changes: 13 additions & 9 deletions
22
apps/app/src/app/(main-layout)/questions/[technology]/[page]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 21 additions & 9 deletions
30
apps/app/src/components/QuestionsSidebar/LevelFilter/LevelFilter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useSearchParams } from "next/navigation"; | ||
import { parseQueryLevels, Level } from "../lib/level"; | ||
import { useDevFAQRouter } from "./useDevFAQRouter"; | ||
|
||
export const useQuestionsLevels = () => { | ||
const searchParams = useSearchParams(); | ||
const { mergeQueryParams } = useDevFAQRouter(); | ||
|
||
const queryLevel = searchParams.get("level"); | ||
const queryLevels = parseQueryLevels(queryLevel); | ||
|
||
const addLevel = (level: Level) => { | ||
mergeQueryParams({ level: [...(queryLevels || []), level].join(",") }); | ||
}; | ||
|
||
const removeLevel = (level: Level) => { | ||
if (queryLevels) { | ||
const newQueryLevels = queryLevels.filter((l) => l !== level); | ||
|
||
mergeQueryParams({ level: newQueryLevels.join(",") }); | ||
} | ||
}; | ||
|
||
return { queryLevels, addLevel, removeLevel }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { QueryParam } from "../types"; | ||
|
||
export const levels = ["junior", "mid", "senior"] as const; | ||
|
||
export type Level = typeof levels[number]; | ||
|
||
export const parseQueryLevels = (query?: QueryParam | null) => { | ||
if (typeof query !== "string") { | ||
return null; | ||
} | ||
|
||
const splittedQuery = query.split(","); | ||
|
||
if (!splittedQuery.every((level) => levels.includes(level))) { | ||
return null; | ||
} | ||
|
||
return splittedQuery as Level[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
const orderBy = ["acceptedAt", "level", "votesCount"] as const; | ||
const order = ["asc", "desc"] as const; | ||
import { QueryParam } from "../types"; | ||
|
||
type OrderBy = typeof orderBy[number]; | ||
type Order = typeof order[number]; | ||
const ordersBy = ["acceptedAt", "level", "votesCount"] as const; | ||
const orders = ["asc", "desc"] as const; | ||
|
||
export const DEFAULT_SORT_BY_QUERY = "acceptedAt*desc"; | ||
|
||
export const validateOrderBy = (data: string): data is OrderBy => { | ||
return orderBy.includes(data); | ||
export const sortByLabels: Record<`${OrderBy}*${Order}`, string> = { | ||
"acceptedAt*asc": "od najstarszych", | ||
"acceptedAt*desc": "od najnowszych", | ||
"level*asc": "od najprostszych", | ||
"level*desc": "od najtrudniejszych", | ||
"votesCount*asc": "od najmniej popularnych", | ||
"votesCount*desc": "od najpopularniejszych", | ||
}; | ||
|
||
export const validateOrder = (data: string): data is Order => { | ||
return order.includes(data); | ||
}; | ||
type OrderBy = typeof ordersBy[number]; | ||
type Order = typeof orders[number]; | ||
|
||
export const validateSortByQuery = (query?: string): query is `${OrderBy}*${Order}` => { | ||
const [orderBy, order] = query?.split("*") || []; | ||
export const parseQuerySortBy = (query: QueryParam) => { | ||
if (typeof query !== "string") { | ||
return null; | ||
} | ||
|
||
return Boolean(orderBy && order && validateOrderBy(orderBy) && validateOrder(order)); | ||
}; | ||
const [orderBy, order] = query.split("*"); | ||
|
||
export const getQuerySortBy = (query?: string) => { | ||
if (!validateSortByQuery(query)) { | ||
if (!orderBy || !order || !ordersBy.includes(orderBy) || !orders.includes(order)) { | ||
return null; | ||
} | ||
|
||
const [orderBy, order] = query.split("*") as [OrderBy, Order]; | ||
|
||
return { orderBy, order }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters