-
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
9 changed files
with
156 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
|
||
export function useDebouncedValue<T = unknown>( | ||
value: T, | ||
wait: number, | ||
options = { leading: false }, | ||
) { | ||
const [_value, setValue] = useState(value); | ||
const mountedRef = useRef(false); | ||
const timeoutRef = useRef<number | null>(null); | ||
const cooldownRef = useRef(false); | ||
|
||
const cancel = () => window.clearTimeout(timeoutRef.current!); | ||
|
||
useEffect(() => { | ||
if (mountedRef.current) { | ||
if (!cooldownRef.current && options.leading) { | ||
cooldownRef.current = true; | ||
setValue(value); | ||
} else { | ||
cancel(); | ||
timeoutRef.current = window.setTimeout(() => { | ||
cooldownRef.current = false; | ||
setValue(value); | ||
}, wait); | ||
} | ||
} | ||
}, [value, options.leading, wait]); | ||
|
||
useEffect(() => { | ||
mountedRef.current = true; | ||
return cancel; | ||
}, []); | ||
|
||
return [_value, cancel] as const; | ||
} |
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,11 @@ | ||
import { create } from "zustand"; | ||
|
||
interface SearchState { | ||
search: string; | ||
setSearch: (search: string) => void; | ||
} | ||
|
||
export const useSearchStore = create<SearchState>()((set) => ({ | ||
search: "", | ||
setSearch: (search) => set({ search }), | ||
})); |
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,7 @@ | ||
export default function rupiahFormatter(value: number): string { | ||
return new Intl.NumberFormat("id-ID", { | ||
style: "currency", | ||
currency: "IDR", | ||
minimumFractionDigits: 0, | ||
}).format(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 |
---|---|---|
@@ -1,7 +1,16 @@ | ||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc"; | ||
import { z } from "zod"; | ||
|
||
import { createTRPCRouter, protectedProcedure } from "../trpc"; | ||
|
||
export const productRouter = createTRPCRouter({ | ||
getProduct: protectedProcedure.query(({ ctx }) => { | ||
return ctx.db.query.products.findMany(); | ||
}), | ||
getProduct: protectedProcedure | ||
.input(z.string()) | ||
.query(({ input, ctx }) => { | ||
return ctx.db.query.products.findMany({ | ||
with: { | ||
user: true, | ||
}, | ||
where: (products, { like }) => like(products.name, `%${input.toLowerCase()}%`), | ||
}); | ||
}), | ||
}); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.