import React from 'react'; import { useTranslation } from 'next-i18next'; import { useForm } from 'react-hook-form'; import type { IAccessTokenInfo } from '~/interfaces/access-token'; import type { Scope } from '~/interfaces/scope'; import { AccessTokenScopeSelect } from './AccessTokenScopeSelect'; const MAX_DESCRIPTION_LENGTH = 200; type AccessTokenFormProps = { submitHandler: (info: IAccessTokenInfo) => Promise; } type FormInputs = { expiredAt: string; description: string; scopes: Scope[]; } export const AccessTokenForm = React.memo((props: AccessTokenFormProps): JSX.Element => { const { submitHandler } = props; const { t } = useTranslation(); const defaultExpiredAt = new Date(); defaultExpiredAt.setMonth(defaultExpiredAt.getMonth() + 1); const defaultExpiredAtStr = defaultExpiredAt.toISOString().split('T')[0]; const todayStr = new Date().toISOString().split('T')[0]; const { register, handleSubmit, formState: { errors, isValid }, watch, } = useForm({ defaultValues: { expiredAt: defaultExpiredAtStr, description: '', scopes: [], }, }); const onSubmit = (data: FormInputs) => { const expiredAtDate = new Date(data.expiredAt); expiredAtDate.setHours(23, 59, 59, 999); const scopes: Scope[] = data.scopes ? data.scopes : []; submitHandler({ expiredAt: expiredAtDate, description: data.description, scopes, }); }; return (
{t('page_me_access_token.form.title')}
{ const date = new Date(value); const now = new Date(); // Reset time portions to compare dates only date.setHours(0, 0, 0, 0); now.setHours(0, 0, 0, 0); return date >= now || 'Expiration date must be in the future'; }, })} />
{errors.expiredAt && (
{errors.expiredAt.message}
)}
{t('page_me_access_token.form.expiredAt_desc')}