mirror of
https://github.com/flarum/core.git
synced 2025-07-07 18:15:04 +02:00
Allow configuring all drivers via frontend (#1169)
This includes an API endpoint for fetching the list of possible drivers and their configuration fields. In the future, this can be extended to include more meta information about each field.
This commit is contained in:
@ -3,36 +3,54 @@ import FieldSet from '../../common/components/FieldSet';
|
||||
import Button from '../../common/components/Button';
|
||||
import Alert from '../../common/components/Alert';
|
||||
import Select from '../../common/components/Select';
|
||||
import LoadingIndicator from '../../common/components/LoadingIndicator';
|
||||
import saveSettings from '../utils/saveSettings';
|
||||
|
||||
export default class MailPage extends Page {
|
||||
init() {
|
||||
super.init();
|
||||
|
||||
this.loading = false;
|
||||
this.loading = true;
|
||||
this.saving = false;
|
||||
|
||||
this.driverFields = {
|
||||
smtp: ['mail_host', 'mail_port', 'mail_encryption', 'mail_username', 'mail_password'],
|
||||
mail: [],
|
||||
log: [],
|
||||
};
|
||||
|
||||
this.fields = [
|
||||
'mail_driver',
|
||||
'mail_host',
|
||||
'mail_from',
|
||||
'mail_port',
|
||||
'mail_username',
|
||||
'mail_password',
|
||||
'mail_encryption'
|
||||
];
|
||||
this.driverFields = {};
|
||||
this.fields = ['mail_driver', 'mail_from'];
|
||||
this.values = {};
|
||||
|
||||
const settings = app.data.settings;
|
||||
this.fields.forEach(key => this.values[key] = m.prop(settings[key]));
|
||||
|
||||
app.request({
|
||||
method: 'GET',
|
||||
url: app.forum.attribute('apiUrl') + '/mail-drivers'
|
||||
}).then(response => {
|
||||
this.driverFields = response['data'].reduce(
|
||||
(hash, driver) => ({...hash, [driver['id']]: driver['attributes']['fields']}),
|
||||
{}
|
||||
);
|
||||
|
||||
Object.keys(this.driverFields).flatMap(key => this.driverFields[key]).forEach(
|
||||
key => {
|
||||
this.fields.push(key);
|
||||
this.values[key] = m.prop(settings[key]);
|
||||
}
|
||||
);
|
||||
this.loading = false;
|
||||
m.redraw();
|
||||
});
|
||||
}
|
||||
|
||||
view() {
|
||||
if (this.loading) {
|
||||
return (
|
||||
<div className="MailPage">
|
||||
<div className="container">
|
||||
<LoadingIndicator />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="MailPage">
|
||||
<div className="container">
|
||||
@ -64,21 +82,15 @@ export default class MailPage extends Page {
|
||||
]
|
||||
})}
|
||||
|
||||
{this.values.mail_driver() == 'smtp' && FieldSet.component({
|
||||
label: app.translator.trans('core.admin.email.smtp_heading'),
|
||||
{Object.keys(this.driverFields[this.values.mail_driver()]).length > 0 && FieldSet.component({
|
||||
label: app.translator.trans(`core.admin.email.${this.values.mail_driver()}_heading`),
|
||||
className: 'MailPage-MailSettings',
|
||||
children: [
|
||||
<div className="MailPage-MailSettings-input">
|
||||
<label>{app.translator.trans('core.admin.email.host_label')}</label>
|
||||
<input className = "FormControl" value={this.values.mail_host() || ''} onInput={m.withAttr('value', this.values.mail_host)} />
|
||||
<label>{app.translator.trans('core.admin.email.port_label')}</label>
|
||||
<input className="FormControl" value={this.values.mail_port() || ''} oninput={m.withAttr('value', this.values.mail_port)} />
|
||||
<label>{app.translator.trans('core.admin.email.encryption_label')}</label>
|
||||
<input className="FormControl" value={this.values.mail_encryption() || ''} oninput={m.withAttr('value', this.values.mail_encryption)} />
|
||||
<label>{app.translator.trans('core.admin.email.username_label')}</label>
|
||||
<input className="FormControl" value={this.values.mail_username() || ''} onInput={m.withAttr('value', this.values.mail_username)}/>
|
||||
<label>{app.translator.trans('core.admin.email.password_label')}</label>
|
||||
<input className="FormControl" value={this.values.mail_password() || ''} onInput={m.withAttr('value', this.values.mail_password)}/>
|
||||
{this.driverFields[this.values.mail_driver()].flatMap(field => [
|
||||
<label>{app.translator.trans(`core.admin.email.${field}_label`)}</label>,
|
||||
<input className="FormControl" value={this.values[field]() || ''} onInput={m.withAttr('value', this.values[field])} />
|
||||
])}
|
||||
</div>
|
||||
]
|
||||
})}
|
||||
@ -87,7 +99,7 @@ export default class MailPage extends Page {
|
||||
type: 'submit',
|
||||
className: 'Button Button--primary',
|
||||
children: app.translator.trans('core.admin.email.submit_button'),
|
||||
loading: this.loading,
|
||||
loading: this.saving,
|
||||
disabled: !this.changed()
|
||||
})}
|
||||
</form>
|
||||
@ -103,9 +115,9 @@ export default class MailPage extends Page {
|
||||
onsubmit(e) {
|
||||
e.preventDefault();
|
||||
|
||||
if (this.loading) return;
|
||||
if (this.saving) return;
|
||||
|
||||
this.loading = true;
|
||||
this.saving = true;
|
||||
app.alerts.dismiss(this.successAlert);
|
||||
|
||||
const settings = {};
|
||||
@ -118,7 +130,7 @@ export default class MailPage extends Page {
|
||||
})
|
||||
.catch(() => {})
|
||||
.then(() => {
|
||||
this.loading = false;
|
||||
this.saving = false;
|
||||
m.redraw();
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user