This repository was archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
2,574 additions
and
1,244 deletions.
There are no files selected for viewing
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
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
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
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,5 @@ | ||
import { objectSnakeToCamel } from "@/ions/utils/object"; | ||
import { useMemo } from "react"; | ||
|
||
export const useObjectSnakeToCamel = <T>(value: Record<string, T>) => | ||
useMemo(() => objectSnakeToCamel<T>(value), [value]); |
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,81 @@ | ||
import { useObjectSnakeToCamel } from "@/ions/hooks/case"; | ||
import { useQueryRouter } from "@/ions/hooks/query-router"; | ||
import { objectCamelToSnake } from "@/ions/utils/object"; | ||
import { decreaseStringNumber, increaseStringNumber, toString } from "@/ions/utils/string"; | ||
import { useCallback, useEffect } from "react"; | ||
|
||
export const usePaging = (length: number, { first = 0, property = "page" }) => { | ||
const { push, query } = useQueryRouter(); | ||
const camelObject = useObjectSnakeToCamel<string | string[]>({ | ||
[property]: toString(first), | ||
...query, | ||
}); | ||
|
||
const last = length - 1 + first; | ||
const current = camelObject[property] | ||
? Number.parseInt(camelObject[property] as string, 10) | ||
: first; | ||
// Clamp to max | ||
const clamped = Math.max(Math.min(current, last), first); | ||
|
||
useEffect(() => { | ||
if (clamped < current) { | ||
void push( | ||
objectCamelToSnake({ | ||
[property]: toString(last), | ||
}) | ||
); | ||
} | ||
}, [push, property, current, clamped, last]); | ||
|
||
const goTo = useCallback( | ||
(nextState: number) => { | ||
void push( | ||
objectCamelToSnake({ | ||
[property]: toString(nextState), | ||
}) | ||
); | ||
}, | ||
[property, push] | ||
); | ||
|
||
const toNext = useCallback(() => { | ||
void push( | ||
objectCamelToSnake({ | ||
[property]: increaseStringNumber(camelObject[property] as string), | ||
}) | ||
); | ||
}, [property, push, camelObject]); | ||
|
||
const toPrevious = useCallback(() => { | ||
void push( | ||
objectCamelToSnake({ | ||
[property]: decreaseStringNumber(camelObject[property] as string), | ||
}) | ||
); | ||
}, [property, push, camelObject]); | ||
|
||
const toFirst = useCallback(() => { | ||
void push( | ||
objectCamelToSnake({ | ||
[property]: toString(first), | ||
}) | ||
); | ||
}, [property, push, first]); | ||
|
||
const toLast = useCallback(() => { | ||
void push( | ||
objectCamelToSnake({ | ||
[property]: toString(last), | ||
}) | ||
); | ||
}, [property, push, last]); | ||
return { | ||
goTo, | ||
toNext, | ||
toPrevious, | ||
toFirst, | ||
toLast, | ||
current, | ||
}; | ||
}; |
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,34 @@ | ||
import { useRouter } from "next/router"; | ||
import { useCallback } from "react"; | ||
|
||
export const cleanQuery = <T>(query: Record<string, T>) => { | ||
const NextQuery = {}; | ||
for (const key in query) { | ||
if (query[key] !== null && query[key] !== undefined) { | ||
NextQuery[key] = query[key]; | ||
} | ||
} | ||
|
||
return NextQuery; | ||
}; | ||
|
||
export const useQueryRouter = () => { | ||
const { query, push: routerPush, pathname } = useRouter(); | ||
const push = useCallback( | ||
async (nextQuery: Record<string, string>) => { | ||
return routerPush( | ||
{ | ||
pathname, | ||
query: cleanQuery({ | ||
...query, | ||
...nextQuery, | ||
}), | ||
}, | ||
undefined, | ||
{ shallow: true } | ||
); | ||
}, | ||
[pathname, routerPush, query] | ||
); | ||
return { push, query, pathname }; | ||
}; |
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
Oops, something went wrong.