|
|
@@ -1,8 +1,9 @@
|
|
|
-import React from 'react';
|
|
|
+import React, { useCallback, useEffect } from 'react';
|
|
|
|
|
|
import { pathUtils } from '@growi/core/dist/utils';
|
|
|
import { useTranslation } from 'next-i18next';
|
|
|
import PropTypes from 'prop-types';
|
|
|
+import { useForm } from 'react-hook-form';
|
|
|
import urljoin from 'url-join';
|
|
|
|
|
|
import AdminGeneralSecurityContainer from '~/client/services/AdminGeneralSecurityContainer';
|
|
|
@@ -12,18 +13,29 @@ import { useSiteUrl } from '~/stores-universal/context';
|
|
|
|
|
|
import { withUnstatedContainers } from '../../UnstatedUtils';
|
|
|
|
|
|
-class GoogleSecurityManagementContents extends React.Component {
|
|
|
+const GoogleSecurityManagementContents = (props) => {
|
|
|
+ const {
|
|
|
+ t, adminGeneralSecurityContainer, adminGoogleSecurityContainer, siteUrl,
|
|
|
+ } = props;
|
|
|
|
|
|
- constructor(props) {
|
|
|
- super(props);
|
|
|
+ const { isGoogleEnabled } = adminGeneralSecurityContainer.state;
|
|
|
+ const { googleClientId, googleClientSecret, retrieveError } = adminGoogleSecurityContainer.state;
|
|
|
+ const googleCallbackUrl = urljoin(pathUtils.removeTrailingSlash(siteUrl), '/passport/google/callback');
|
|
|
|
|
|
- this.onClickSubmit = this.onClickSubmit.bind(this);
|
|
|
- }
|
|
|
+ const { register, handleSubmit, reset } = useForm();
|
|
|
|
|
|
- async onClickSubmit() {
|
|
|
- const { t, adminGoogleSecurityContainer, adminGeneralSecurityContainer } = this.props;
|
|
|
+ // Sync form with container state
|
|
|
+ useEffect(() => {
|
|
|
+ reset({
|
|
|
+ googleClientId,
|
|
|
+ googleClientSecret,
|
|
|
+ });
|
|
|
+ }, [reset, googleClientId, googleClientSecret]);
|
|
|
|
|
|
+ const onClickSubmit = useCallback(async(data) => {
|
|
|
try {
|
|
|
+ await adminGoogleSecurityContainer.changeGoogleClientId(data.googleClientId ?? '');
|
|
|
+ await adminGoogleSecurityContainer.changeGoogleClientSecret(data.googleClientSecret ?? '');
|
|
|
await adminGoogleSecurityContainer.updateGoogleSetting();
|
|
|
await adminGeneralSecurityContainer.retrieveSetupStratedies();
|
|
|
toastSuccess(t('security_settings.OAuth.Google.updated_google'));
|
|
|
@@ -31,26 +43,19 @@ class GoogleSecurityManagementContents extends React.Component {
|
|
|
catch (err) {
|
|
|
toastError(err);
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- render() {
|
|
|
- const {
|
|
|
- t, adminGeneralSecurityContainer, adminGoogleSecurityContainer, siteUrl,
|
|
|
- } = this.props;
|
|
|
- const { isGoogleEnabled } = adminGeneralSecurityContainer.state;
|
|
|
- const googleCallbackUrl = urljoin(pathUtils.removeTrailingSlash(siteUrl), '/passport/google/callback');
|
|
|
-
|
|
|
- return (
|
|
|
+ }, [adminGoogleSecurityContainer, adminGeneralSecurityContainer, t]);
|
|
|
|
|
|
+ return (
|
|
|
+ <form onSubmit={handleSubmit(onClickSubmit)}>
|
|
|
<React.Fragment>
|
|
|
|
|
|
<h2 className="alert-anchor border-bottom">
|
|
|
{t('security_settings.OAuth.Google.name')}
|
|
|
</h2>
|
|
|
|
|
|
- {adminGoogleSecurityContainer.state.retrieveError != null && (
|
|
|
+ {retrieveError != null && (
|
|
|
<div className="alert alert-danger">
|
|
|
- <p>{t('Error occurred')} : {adminGoogleSecurityContainer.state.retrieveError}</p>
|
|
|
+ <p>{t('Error occurred')} : {retrieveError}</p>
|
|
|
</div>
|
|
|
)}
|
|
|
|
|
|
@@ -107,9 +112,7 @@ class GoogleSecurityManagementContents extends React.Component {
|
|
|
<input
|
|
|
className="form-control"
|
|
|
type="text"
|
|
|
- name="googleClientId"
|
|
|
- value={adminGoogleSecurityContainer.state.googleClientId || ''}
|
|
|
- onChange={e => adminGoogleSecurityContainer.changeGoogleClientId(e.target.value)}
|
|
|
+ {...register('googleClientId')}
|
|
|
/>
|
|
|
<p className="form-text text-muted">
|
|
|
<small dangerouslySetInnerHTML={{ __html: t('security_settings.Use env var if empty', { env: 'OAUTH_GOOGLE_CLIENT_ID' }) }} />
|
|
|
@@ -123,9 +126,7 @@ class GoogleSecurityManagementContents extends React.Component {
|
|
|
<input
|
|
|
className="form-control"
|
|
|
type="password"
|
|
|
- name="googleClientSecret"
|
|
|
- value={adminGoogleSecurityContainer.state.googleClientSecret || ''}
|
|
|
- onChange={e => adminGoogleSecurityContainer.changeGoogleClientSecret(e.target.value)}
|
|
|
+ {...register('googleClientSecret')}
|
|
|
/>
|
|
|
<p className="form-text text-muted">
|
|
|
<small dangerouslySetInnerHTML={{ __html: t('security_settings.Use env var if empty', { env: 'OAUTH_GOOGLE_CLIENT_SECRET' }) }} />
|
|
|
@@ -157,12 +158,7 @@ class GoogleSecurityManagementContents extends React.Component {
|
|
|
|
|
|
<div className="row mb-4">
|
|
|
<div className="offset-3 col-5">
|
|
|
- <button
|
|
|
- type="button"
|
|
|
- className="btn btn-primary"
|
|
|
- disabled={adminGoogleSecurityContainer.state.retrieveError != null}
|
|
|
- onClick={this.onClickSubmit}
|
|
|
- >
|
|
|
+ <button type="submit" className="btn btn-primary" disabled={retrieveError != null}>
|
|
|
{t('Update')}
|
|
|
</button>
|
|
|
</div>
|
|
|
@@ -191,20 +187,10 @@ class GoogleSecurityManagementContents extends React.Component {
|
|
|
</div>
|
|
|
|
|
|
</React.Fragment>
|
|
|
-
|
|
|
-
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-const GoogleSecurityManagementContentsFc = (props) => {
|
|
|
- const { t } = useTranslation('admin');
|
|
|
- const { data: siteUrl } = useSiteUrl();
|
|
|
- return <GoogleSecurityManagementContents t={t} siteUrl={siteUrl} {...props} />;
|
|
|
+ </form>
|
|
|
+ );
|
|
|
};
|
|
|
|
|
|
-
|
|
|
GoogleSecurityManagementContents.propTypes = {
|
|
|
t: PropTypes.func.isRequired, // i18next
|
|
|
adminGeneralSecurityContainer: PropTypes.instanceOf(AdminGeneralSecurityContainer).isRequired,
|
|
|
@@ -212,6 +198,12 @@ GoogleSecurityManagementContents.propTypes = {
|
|
|
siteUrl: PropTypes.string,
|
|
|
};
|
|
|
|
|
|
+const GoogleSecurityManagementContentsFc = (props) => {
|
|
|
+ const { t } = useTranslation('admin');
|
|
|
+ const { data: siteUrl } = useSiteUrl();
|
|
|
+ return <GoogleSecurityManagementContents t={t} siteUrl={siteUrl} {...props} />;
|
|
|
+};
|
|
|
+
|
|
|
const GoogleSecurityManagementContentsWrapper = withUnstatedContainers(GoogleSecurityManagementContentsFc, [
|
|
|
AdminGeneralSecurityContainer,
|
|
|
AdminGoogleSecurityContainer,
|