diff --git a/packages/core/package.json b/packages/core/package.json index c2284925..b305d4b8 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -13,6 +13,7 @@ "prettier": "^3.4.2" }, "dependencies": { + "@faker-js/faker": "^9.5.1", "dotenv": "^16.4.7", "jotai": "^2.9.0", "path": "^0.12.7", diff --git a/packages/core/src/api/wallet.ts b/packages/core/src/api/wallet.ts new file mode 100644 index 00000000..0b7963d6 --- /dev/null +++ b/packages/core/src/api/wallet.ts @@ -0,0 +1,59 @@ +import { faker } from "@faker-js/faker"; +import { Wallet } from "../types/wallet"; + +async function getWalletsRecentActivity(userId: string): Promise { + await new Promise(resolve => setTimeout(resolve, 100)); + + return Array.from({ length: 4 }, () => { + const status = getRandomStatus(); + return { + id: faker.string.uuid(), + name: faker.internet.username(), + balance: getRandomBalance(status), + createdAt: faker.date.past(), + logo: faker.image.url(), + status, + }; + }); +} + +const wallets = Array.from({ length: 10 }, () => { + const status = getRandomStatus(); + return { + id: faker.string.uuid(), + name: faker.internet.username(), + balance: getRandomBalance(status), + createdAt: faker.date.past(), + logo: faker.image.url(), + status, + }; +}); + +function getRandomStatus(): "Received" | "Pending" | "Sent" { + const statuses: ("Received" | "Pending" | "Sent")[] = [ + "Received", + "Pending", + "Sent", + ]; + return statuses[Math.floor(Math.random() * statuses.length)]; +} + +function getRandomBalance(status: "Received" | "Pending" | "Sent"): string { + const balance = faker.number.int({ min: 100, max: 300 }); + + if (status === "Pending") { + return `${balance}`; + } + + return status === "Sent" ? `-${balance}` : `+${balance}`; +} + +async function getWalletByKeyword(keyword: string): Promise { + await new Promise(resolve => setTimeout(resolve, 100)); + + return wallets.filter(option => + option.name.toLowerCase().includes(keyword.toLowerCase()), + ); +} + +export { getWalletsRecentActivity, getWalletByKeyword }; diff --git a/packages/core/src/hooks/index.ts b/packages/core/src/hooks/index.ts index afe40a1f..b43f14f1 100644 --- a/packages/core/src/hooks/index.ts +++ b/packages/core/src/hooks/index.ts @@ -1,2 +1,3 @@ export * from "./useSession"; -export * from "./logout"; +// export * from "./logout"; +export * from "./wallet"; diff --git a/packages/core/src/hooks/wallet.ts b/packages/core/src/hooks/wallet.ts new file mode 100644 index 00000000..26375031 --- /dev/null +++ b/packages/core/src/hooks/wallet.ts @@ -0,0 +1,20 @@ +import { useEffect, useState } from "react"; +import * as wallets from "../api/wallet"; +import { Wallet } from "../types/wallet"; + +export default function useWalletList(userId: string | undefined) { + const [list, setList] = useState([]); + const [isWalletLoading, setIsWalletLoading] = useState(true); + + useEffect(() => { + async function load() { + setIsWalletLoading(true); + const ws = await wallets.getWalletsRecentActivity(userId || ""); + setIsWalletLoading(false); + setList(ws); + } + load(); + }, []); + + return { list, isWalletLoading }; +} diff --git a/packages/core/src/types/wallet.ts b/packages/core/src/types/wallet.ts new file mode 100644 index 00000000..4db3b37a --- /dev/null +++ b/packages/core/src/types/wallet.ts @@ -0,0 +1,8 @@ +export type Wallet = { + id: string; + name: string; + logo: string; + balance: number; + createdAt: Date; + status: string; +}; diff --git a/yarn.lock b/yarn.lock index 6b6ea26c..1103feb6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1733,6 +1733,11 @@ find-up "^5.0.0" js-yaml "^4.1.0" +"@faker-js/faker@^9.5.1": + version "9.5.1" + resolved "https://registry.yarnpkg.com/@faker-js/faker/-/faker-9.5.1.tgz#1f32fb726db5f539415455cadbc93c9fb457e42d" + integrity sha512-0fzMEDxkExR2cn731kpDaCCnBGBUOIXEi2S1N5l8Hltp6aPf4soTMJ+g4k8r2sI5oB+rpwIW8Uy/6jkwGpnWPg== + "@floating-ui/core@^1.6.0": version "1.6.9" resolved "https://registry.yarnpkg.com/@floating-ui/core/-/core-1.6.9.tgz#64d1da251433019dafa091de9b2886ff35ec14e6"