-
Notifications
You must be signed in to change notification settings - Fork 3
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
8 changed files
with
130 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
41 changes: 41 additions & 0 deletions
41
packages/circle-demo-webapp/app/hooks/useUpdateWalletSet.ts
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,41 @@ | ||
import { useState } from 'react'; | ||
|
||
interface UpdateWalletSetArgs { | ||
id: string; | ||
name: string; | ||
} | ||
|
||
interface UseUpdateWalletSetResult { | ||
error: Error | undefined; | ||
isLoading: boolean; | ||
updateWalletSet: (args: UpdateWalletSetArgs) => Promise<void>; | ||
} | ||
|
||
export const useUpdateWalletSet = (): UseUpdateWalletSetResult => { | ||
const [error, setError] = useState<Error | undefined>(undefined); | ||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
|
||
const updateWalletSet = async ({ id, name }: UpdateWalletSetArgs) => { | ||
setIsLoading(true); | ||
setError(undefined); | ||
try { | ||
await fetch('/api/updateWalletSet', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ id, name }), | ||
}); | ||
} catch (err) { | ||
setError(err as Error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
return { | ||
error, | ||
isLoading, | ||
updateWalletSet, | ||
}; | ||
}; |
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 useSWR, { mutate } from 'swr'; | ||
|
||
import { WalletSet } from '~/lib/types'; | ||
|
||
interface UseWalletSetsResult { | ||
data: WalletSet[] | undefined; | ||
error: Error | undefined; | ||
isLoading: boolean; | ||
refetch: () => Promise<void>; | ||
} | ||
|
||
const url = '/api/listWalletSets'; | ||
|
||
export const useWalletSets = (): UseWalletSetsResult => { | ||
const { data, error, isLoading } = useSWR<WalletSet[], Error>(url); | ||
|
||
const refetch = () => mutate(url); | ||
|
||
return { | ||
data, | ||
error, | ||
isLoading, | ||
refetch, | ||
}; | ||
}; |
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
7 changes: 7 additions & 0 deletions
7
packages/circle-demo-webapp/app/routes/api.listWalletSets.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { sdk } from '~/lib/sdk'; | ||
|
||
export async function loader() { | ||
const res = await sdk.listWalletSets(); | ||
|
||
return Response.json(res.data?.walletSets); | ||
} |
33 changes: 30 additions & 3 deletions
33
packages/circle-demo-webapp/app/routes/api.updateWalletSet.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,36 @@ | ||
import { UpdateWalletSetInput } from '@circle-fin/developer-controlled-wallets'; | ||
import { ActionFunctionArgs } from '@remix-run/node'; | ||
|
||
import { invalidateCache } from '~/lib/cache'; | ||
import { sdk } from '~/lib/sdk'; | ||
import { ErrorResponseObject } from '~/lib/types'; | ||
import { isValidString } from '~/lib/utils'; | ||
|
||
interface RequestBody { | ||
id: string; | ||
name: string; | ||
} | ||
|
||
export async function action({ request }: ActionFunctionArgs) { | ||
const res = await sdk.updateWalletSet((await request.json()) as UpdateWalletSetInput); | ||
return Response.json(res.data); | ||
const { id, name } = (await request.json()) as RequestBody; | ||
|
||
if (!isValidString(id)) { | ||
return Response.json({ error: 'Invalid wallet set id' }); | ||
} | ||
|
||
if (!isValidString(name)) { | ||
return Response.json({ error: 'Invalid name' }); | ||
} | ||
|
||
try { | ||
await sdk.updateWalletSet({ | ||
id, | ||
name, | ||
}); | ||
|
||
invalidateCache('walletSets'); | ||
|
||
return Response.json({ message: 'Success' }); | ||
} catch (e: unknown) { | ||
return Response.json({ error: (e as ErrorResponseObject)?.response?.data }); | ||
} | ||
} |
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