2023-01-25 11:24:41 +01:00
|
|
|
import React, { useEffect } from 'react';
|
2023-01-18 17:40:47 +01:00
|
|
|
import { IApplication } from '../../models/application';
|
|
|
|
import { Loader } from '../base/loader';
|
|
|
|
import { react2angular } from 'react2angular';
|
|
|
|
import { ErrorBoundary } from '../base/error-boundary';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { useForm, SubmitHandler } from 'react-hook-form';
|
|
|
|
import { FabButton } from '../base/fab-button';
|
2023-01-25 11:24:41 +01:00
|
|
|
import { EditorialKeys, EditorialBlockForm } from '../editorial-block/editorial-block-form';
|
|
|
|
import SettingAPI from '../../api/setting';
|
|
|
|
import SettingLib from '../../lib/setting';
|
2023-01-26 09:48:37 +01:00
|
|
|
import { SettingName, SettingValue, machinesSettings } from '../../models/setting';
|
2023-02-02 15:18:09 +01:00
|
|
|
import { UnsavedFormAlert } from '../form/unsaved-form-alert';
|
|
|
|
import { UIRouter } from '@uirouter/angularjs';
|
2023-01-18 17:40:47 +01:00
|
|
|
|
|
|
|
declare const Application: IApplication;
|
|
|
|
|
|
|
|
interface MachinesSettingsProps {
|
|
|
|
onError: (message: string) => void,
|
|
|
|
onSuccess: (message: string) => void,
|
2023-01-25 11:24:41 +01:00
|
|
|
beforeSubmit?: (data: Record<SettingName, SettingValue>) => void,
|
2023-02-02 15:18:09 +01:00
|
|
|
uiRouter: UIRouter
|
2023-01-18 17:40:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Machines settings
|
|
|
|
*/
|
2023-02-02 15:18:09 +01:00
|
|
|
export const MachinesSettings: React.FC<MachinesSettingsProps> = ({ onError, onSuccess, beforeSubmit, uiRouter }) => {
|
2023-01-18 17:40:47 +01:00
|
|
|
const { t } = useTranslation('admin');
|
2023-01-25 14:34:31 +01:00
|
|
|
const { register, control, formState, handleSubmit, reset } = useForm<Record<SettingName, SettingValue>>();
|
2023-01-18 17:40:47 +01:00
|
|
|
|
2023-01-25 11:24:41 +01:00
|
|
|
/** Link Machines Banner Setting Names to generic keys expected by the Editorial Form */
|
|
|
|
const bannerKeys: Record<EditorialKeys, SettingName> = {
|
|
|
|
active_text_block: 'machines_banner_active',
|
|
|
|
text_block: 'machines_banner_text',
|
|
|
|
active_cta: 'machines_banner_cta_active',
|
|
|
|
cta_label: 'machines_banner_cta_label',
|
|
|
|
cta_url: 'machines_banner_cta_url'
|
2023-01-18 17:40:47 +01:00
|
|
|
};
|
|
|
|
|
2023-01-25 11:24:41 +01:00
|
|
|
/** Callback triggered when the form is submitted: save the settings */
|
|
|
|
const onSubmit: SubmitHandler<Record<SettingName, SettingValue>> = (data) => {
|
|
|
|
if (typeof beforeSubmit === 'function') beforeSubmit(data);
|
|
|
|
SettingAPI.bulkUpdate(SettingLib.objectToBulkMap(data)).then(() => {
|
|
|
|
onSuccess(t('app.admin.machines_settings.successfully_saved'));
|
|
|
|
}, reason => {
|
|
|
|
onError(reason);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/** On component mount, fetch existing Machines Banner Settings from API, and populate form with these values. */
|
|
|
|
useEffect(() => {
|
2023-01-26 09:48:37 +01:00
|
|
|
SettingAPI.query(machinesSettings)
|
2023-01-25 11:24:41 +01:00
|
|
|
.then(settings => reset(SettingLib.bulkMapToObject(settings)))
|
|
|
|
.catch(onError);
|
|
|
|
}, []);
|
|
|
|
|
2023-01-18 17:40:47 +01:00
|
|
|
return (
|
|
|
|
<div className="machines-settings">
|
|
|
|
<header>
|
|
|
|
<h2>{t('app.admin.machines_settings.title')}</h2>
|
|
|
|
<FabButton onClick={handleSubmit(onSubmit)} className='save-btn is-main'>{t('app.admin.machines_settings.save')}</FabButton>
|
|
|
|
</header>
|
|
|
|
<form className="machines-settings-content">
|
2023-02-02 15:18:09 +01:00
|
|
|
<UnsavedFormAlert uiRouter={uiRouter} formState={formState} />
|
2023-01-18 17:40:47 +01:00
|
|
|
<div className="settings-section">
|
2023-01-19 14:13:41 +01:00
|
|
|
<EditorialBlockForm register={register}
|
|
|
|
control={control}
|
|
|
|
formState={formState}
|
2023-01-25 11:24:41 +01:00
|
|
|
keys={bannerKeys}
|
2023-01-19 14:13:41 +01:00
|
|
|
info={t('app.admin.machines_settings.generic_text_block_info')} />
|
2023-01-18 17:40:47 +01:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const MachinesSettingsWrapper: React.FC<MachinesSettingsProps> = (props) => {
|
|
|
|
return (
|
|
|
|
<Loader>
|
|
|
|
<ErrorBoundary>
|
|
|
|
<MachinesSettings {...props} />
|
|
|
|
</ErrorBoundary>
|
|
|
|
</Loader>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-02-02 15:18:09 +01:00
|
|
|
Application.Components.component('machinesSettings', react2angular(MachinesSettingsWrapper, ['onError', 'onSuccess', 'beforeSubmit', 'uiRouter']));
|