import * as React from 'react'; import I18n from 'i18n-js'; import { SubmitHandler, useForm } from 'react-hook-form'; import { DangerText } from '../../common/CustomTexts'; import { getLabel, getValidationMessage } from '../../../helpers/formUtils'; import Button from '../../common/Button'; import { URL_REGEX } from '../../../constants/regex'; import { IOAuth } from '../../../interfaces/IOAuth'; import { AuthenticationPages } from './AuthenticationSiteSettingsP'; import { useState } from 'react'; import Separator from '../../common/Separator'; interface Props { selectedOAuth: IOAuth; page: AuthenticationPages; setPage: React.Dispatch>; handleSubmitOAuth(oAuth: IOAuth): void; handleUpdateOAuth(id: number, form: ISiteSettingsOAuthForm): void; } export interface ISiteSettingsOAuthForm { name: string; logo: string; clientId: string; clientSecret: string; authorizeUrl: string; tokenUrl: string; profileUrl: string; scope: string; jsonUserEmailPath: string; jsonUserNamePath: string; } const OAuthForm = ({ selectedOAuth, page, setPage, handleSubmitOAuth, handleUpdateOAuth, }: Props) => { const [editClientSecret, setEditClientSecret] = useState(false); const { register, handleSubmit, formState: { errors, isDirty } } = useForm({ defaultValues: page === 'new' ? { name: '', logo: '', clientId: '', clientSecret: '', authorizeUrl: '', tokenUrl: '', profileUrl: '', scope: '', jsonUserEmailPath: '', jsonUserNamePath: '', } : { name: selectedOAuth.name, logo: selectedOAuth.logo, clientId: selectedOAuth.clientId, clientSecret: selectedOAuth.clientSecret, authorizeUrl: selectedOAuth.authorizeUrl, tokenUrl: selectedOAuth.tokenUrl, profileUrl: selectedOAuth.profileUrl, scope: selectedOAuth.scope, jsonUserEmailPath: selectedOAuth.jsonUserEmailPath, jsonUserNamePath: selectedOAuth.jsonUserNamePath, }, }); const onSubmit: SubmitHandler = data => { const oAuth = { ...data, isEnabled: false }; if (page === 'new') { handleSubmitOAuth(oAuth); } else if (page === 'edit') { if (editClientSecret === false) { delete oAuth.clientSecret; } handleUpdateOAuth(selectedOAuth.id, oAuth as ISiteSettingsOAuthForm); } }; return ( <> { let confirmation = true; if (isDirty) confirmation = confirm(I18n.t('common.unsaved_changes') + ' ' + I18n.t('common.confirmation')); if (confirmation) setPage('index'); }} className="backButton link"> ← { I18n.t('common.buttons.back') }

{ I18n.t(`site_settings.authentication.form.title_${page}`) }

{errors.name && getValidationMessage(errors.name.type, 'o_auth', 'name')}
{ I18n.t('site_settings.authentication.form.subtitle_oauth_config') }
{errors.clientId && getValidationMessage(errors.clientId.type, 'o_auth', 'client_id')}
{ page === 'edit' && <> {I18n.t('site_settings.authentication.form.client_secret_help') + "\t"} { editClientSecret ? setEditClientSecret(false)} className="link">{I18n.t('common.buttons.cancel')} : setEditClientSecret(true)} className="link">{I18n.t('common.buttons.edit')} }
} {errors.clientSecret && getValidationMessage(errors.clientSecret.type, 'o_auth', 'client_secret')}
{errors.authorizeUrl?.type === 'required' && getValidationMessage(errors.authorizeUrl.type, 'o_auth', 'authorize_url')} {errors.authorizeUrl?.type === 'pattern' && I18n.t('common.validations.url')}
{errors.tokenUrl?.type === 'required' && getValidationMessage(errors.tokenUrl.type, 'o_auth', 'token_url')} {errors.tokenUrl?.type === 'pattern' && I18n.t('common.validations.url')}
{errors.scope && getValidationMessage(errors.scope.type, 'o_auth', 'scope')}
{ I18n.t('site_settings.authentication.form.subtitle_user_profile_config') }
{errors.profileUrl?.type === 'required' && getValidationMessage(errors.profileUrl.type, 'o_auth', 'profile_url')} {errors.profileUrl?.type === 'pattern' && I18n.t('common.validations.url')}
{errors.jsonUserEmailPath && getValidationMessage(errors.jsonUserEmailPath.type, 'o_auth', 'json_user_email_path')}
); } export default OAuthForm;