-
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.
Merge pull request #45 from piny940/client-update
Clientをupdateできるようにする
- Loading branch information
Showing
14 changed files
with
659 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ClientEditPage } from '@/components/ClientEditPage' | ||
|
||
type Props = { | ||
clientId: string | ||
} | ||
export default async function Page(req: { params: Promise<Props> }) { | ||
const params = await req.params | ||
return <ClientEditPage clientId={params.clientId} /> | ||
} |
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,59 @@ | ||
'use client' | ||
|
||
import { useForm } from 'react-hook-form' | ||
import { ClientForm, ClientInput } from './ClientForm' | ||
import { RedirectURIsFields } from './RedirectURIsEdit' | ||
import { Box, Typography } from '@mui/material' | ||
import { Client } from '@/utils/types' | ||
import { useCallback } from 'react' | ||
import { client as apiClient } from '@/utils/client' | ||
import { useRouter } from 'next/navigation' | ||
|
||
export type ClientEditProps = { | ||
client: Client | ||
} | ||
|
||
export const ClientEdit = ({ client }: ClientEditProps) => { | ||
const { handleSubmit, getValues, control } = useForm<ClientInput>({ | ||
defaultValues: { | ||
name: client.name, | ||
redirectUris: client.redirect_urls, | ||
}, | ||
}) | ||
const { control: redirectURIsControl, getValues: getRedirectURIsValues } = | ||
useForm<RedirectURIsFields>({ | ||
defaultValues: { | ||
redirectURIs: client.redirect_urls.map((url) => ({ url })), | ||
}, | ||
}) | ||
const router = useRouter() | ||
const submit = useCallback(async () => { | ||
const newClient = getValues() | ||
const urls = getRedirectURIsValues().redirectURIs.map((uri) => uri.url) | ||
const { error } = await apiClient.POST('/account/clients/{id}', { | ||
params: { path: { id: client.id } }, | ||
body: { | ||
client: { | ||
name: newClient.name, | ||
redirect_urls: urls, | ||
}, | ||
}, | ||
}) | ||
if (error) { | ||
console.error(error) | ||
throw new Error('Failed to update client: ' + error) | ||
} | ||
router.push('/member') | ||
}, [getValues, getRedirectURIsValues, client.id, router]) | ||
|
||
return ( | ||
<Box> | ||
<Typography variant="h4">Edit Client</Typography> | ||
<ClientForm | ||
control={control} | ||
redirectURIsControl={redirectURIsControl} | ||
submit={handleSubmit(submit)} | ||
/> | ||
</Box> | ||
) | ||
} |
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,31 @@ | ||
'use client' | ||
import { client } from '@/utils/client' | ||
import { Client } from '@/utils/types' | ||
import { useEffect, useState } from 'react' | ||
import { ClientEdit } from './ClientEdit' | ||
|
||
type ClientEditProps = { | ||
clientId: string | ||
} | ||
export const ClientEditPage = ({ clientId }: ClientEditProps) => { | ||
const [current, setCurrent] = useState<Client | null>(null) | ||
const fetchClient = async (clientId: string) => { | ||
const { data, error } = await client.GET('/account/clients/{id}', { | ||
params: { path: { id: clientId } }, | ||
}) | ||
if (error) { | ||
console.error(error) | ||
throw new Error('Failed to fetch client: ' + error) | ||
} | ||
setCurrent(data.client) | ||
} | ||
|
||
useEffect(() => { | ||
fetchClient(clientId) | ||
}, [clientId]) | ||
|
||
if (!current) { | ||
return <div>Loading...</div> | ||
} | ||
return <ClientEdit client={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
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.