2022-12-07 18:15:27 +08:00
|
|
|
import { FC, useEffect, useState } from 'react';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
|
|
|
import type * as Type from '@/common/interface';
|
2022-12-10 15:30:36 +08:00
|
|
|
import { getLoginSetting, putLoginSetting } from '@/services';
|
2022-12-07 18:15:27 +08:00
|
|
|
import { SchemaForm, JSONSchema, initFormData, UISchema } from '@/components';
|
|
|
|
import { useToast } from '@/hooks';
|
|
|
|
import { handleFormError } from '@/utils';
|
2022-12-14 15:08:52 +08:00
|
|
|
import { loginSettingStore } from '@/stores';
|
2022-12-07 18:15:27 +08:00
|
|
|
|
|
|
|
const Index: FC = () => {
|
|
|
|
const { t } = useTranslation('translation', {
|
|
|
|
keyPrefix: 'admin.login',
|
|
|
|
});
|
|
|
|
const Toast = useToast();
|
|
|
|
const schema: JSONSchema = {
|
|
|
|
title: t('page_title'),
|
|
|
|
properties: {
|
2022-12-10 15:30:36 +08:00
|
|
|
allow_new_registrations: {
|
2022-12-07 18:15:27 +08:00
|
|
|
type: 'boolean',
|
2022-12-10 15:30:36 +08:00
|
|
|
title: t('membership.title'),
|
|
|
|
label: t('membership.label'),
|
2022-12-07 18:15:27 +08:00
|
|
|
description: t('membership.text'),
|
|
|
|
default: true,
|
|
|
|
},
|
2022-12-10 15:30:36 +08:00
|
|
|
login_required: {
|
|
|
|
type: 'boolean',
|
|
|
|
title: t('private.title'),
|
|
|
|
label: t('private.label'),
|
2022-12-07 18:15:27 +08:00
|
|
|
description: t('private.text'),
|
2022-12-10 15:30:36 +08:00
|
|
|
default: false,
|
2022-12-07 18:15:27 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const uiSchema: UISchema = {
|
2022-12-10 15:30:36 +08:00
|
|
|
allow_new_registrations: {
|
2022-12-07 18:15:27 +08:00
|
|
|
'ui:widget': 'switch',
|
|
|
|
},
|
2022-12-10 15:30:36 +08:00
|
|
|
login_required: {
|
2022-12-07 18:15:27 +08:00
|
|
|
'ui:widget': 'switch',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const [formData, setFormData] = useState(initFormData(schema));
|
2022-12-14 15:08:52 +08:00
|
|
|
const { update: updateLoginSetting } = loginSettingStore((_) => _);
|
2022-12-07 18:15:27 +08:00
|
|
|
|
|
|
|
const onSubmit = (evt) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
evt.stopPropagation();
|
|
|
|
|
2022-12-10 15:30:36 +08:00
|
|
|
const reqParams: Type.AdminSettingsLogin = {
|
|
|
|
allow_new_registrations: formData.allow_new_registrations.value,
|
|
|
|
login_required: formData.login_required.value,
|
2022-12-07 18:15:27 +08:00
|
|
|
};
|
|
|
|
|
2022-12-10 15:30:36 +08:00
|
|
|
putLoginSetting(reqParams)
|
2022-12-07 18:15:27 +08:00
|
|
|
.then(() => {
|
|
|
|
Toast.onShow({
|
|
|
|
msg: t('update', { keyPrefix: 'toast' }),
|
|
|
|
variant: 'success',
|
|
|
|
});
|
2022-12-14 15:08:52 +08:00
|
|
|
updateLoginSetting(reqParams);
|
2022-12-07 18:15:27 +08:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
if (err.isError) {
|
|
|
|
const data = handleFormError(err, formData);
|
|
|
|
setFormData({ ...data });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-12-10 15:30:36 +08:00
|
|
|
getLoginSetting().then((setting) => {
|
2022-12-07 18:15:27 +08:00
|
|
|
if (setting) {
|
|
|
|
const formMeta = { ...formData };
|
2022-12-10 15:30:36 +08:00
|
|
|
formMeta.allow_new_registrations.value =
|
|
|
|
setting.allow_new_registrations;
|
|
|
|
formMeta.login_required.value = setting.login_required;
|
|
|
|
setFormData({ ...formMeta });
|
2022-12-07 18:15:27 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleOnChange = (data) => {
|
|
|
|
setFormData(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<h3 className="mb-4">{t('page_title')}</h3>
|
|
|
|
<SchemaForm
|
|
|
|
schema={schema}
|
|
|
|
formData={formData}
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
uiSchema={uiSchema}
|
|
|
|
onChange={handleOnChange}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Index;
|