2023-05-04 22:05:58 +02:00
|
|
|
import '@maicol07/material-web-additions/card/elevated-card.js';
|
2023-05-07 14:07:09 +02:00
|
|
|
|
2023-05-07 17:02:00 +02:00
|
|
|
import {router} from '@maicol07/inertia-mithril';
|
2023-05-07 14:07:09 +02:00
|
|
|
import Page, {PageAttributes} from '@osm/Components/Page';
|
|
|
|
import {showSnackbar} from '@osm/utils/misc';
|
|
|
|
import AdminUserStep from '@osm/Views/Setup/Steps/AdminUserStep';
|
|
|
|
import DatabaseStep from '@osm/Views/Setup/Steps/DatabaseStep';
|
|
|
|
import RegionalSettings from '@osm/Views/Setup/Steps/RegionalSettings';
|
2023-05-04 22:05:58 +02:00
|
|
|
import type {Vnode} from 'mithril';
|
2023-05-07 17:02:00 +02:00
|
|
|
import Stream from 'mithril/stream';
|
2023-05-04 22:05:58 +02:00
|
|
|
import {
|
|
|
|
Request,
|
|
|
|
RequestError
|
|
|
|
} from 'mithril-utilities';
|
2023-07-05 10:17:27 +02:00
|
|
|
import {match} from 'ts-pattern';
|
2023-07-19 16:55:40 +02:00
|
|
|
import {Class} from 'type-fest';
|
2023-04-20 20:00:38 +02:00
|
|
|
|
|
|
|
import {
|
|
|
|
SetupStep,
|
|
|
|
SetupSteps
|
|
|
|
} from './Steps/SetupStep';
|
|
|
|
import WelcomeStep from './Steps/WelcomeStep';
|
|
|
|
|
|
|
|
export interface SetupPageAttributes extends PageAttributes<{
|
|
|
|
languages: string[];
|
|
|
|
license: string;
|
|
|
|
}> {
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class SetupPage extends Page<SetupPageAttributes> {
|
2023-07-19 16:55:40 +02:00
|
|
|
initialStep = SetupSteps.Welcome;
|
2023-04-20 20:00:38 +02:00
|
|
|
currentStep = Stream<SetupSteps>(SetupSteps.Welcome);
|
|
|
|
steps: Record<SetupSteps, SetupStep<any>> = {
|
|
|
|
[SetupSteps.Welcome]: new WelcomeStep(),
|
|
|
|
[SetupSteps.RegionalSettings]: new RegionalSettings(),
|
|
|
|
[SetupSteps.Database]: new DatabaseStep(),
|
|
|
|
[SetupSteps.AdminUser]: new AdminUserStep()
|
|
|
|
};
|
|
|
|
|
2023-07-05 10:17:27 +02:00
|
|
|
oninit(vnode: Vnode<SetupPageAttributes, this>) {
|
|
|
|
super.oninit(vnode);
|
|
|
|
// @ts-expect-error
|
|
|
|
const {step} = route().params;
|
|
|
|
if (step) {
|
|
|
|
const setupStep = match(step)
|
|
|
|
.with('regional_settings', () => SetupSteps.RegionalSettings)
|
|
|
|
.with('database', () => SetupSteps.Database)
|
|
|
|
.with('admin_user', () => SetupSteps.AdminUser)
|
|
|
|
.otherwise(() => SetupSteps.Welcome);
|
|
|
|
this.currentStep(setupStep);
|
2023-07-19 16:55:40 +02:00
|
|
|
this.initialStep = setupStep;
|
2023-07-05 10:17:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 20:00:38 +02:00
|
|
|
contents(vnode: Vnode<SetupPageAttributes>) {
|
2023-07-19 16:55:40 +02:00
|
|
|
// noinspection LocalVariableNamingConventionJS - Capitalized name is needed for JSX. Cast to unknown to avoid TS error about JSX element type
|
|
|
|
const Step = this.steps[this.currentStep()] as unknown as Class<SetupStep>;
|
2023-04-20 20:00:38 +02:00
|
|
|
return <>
|
|
|
|
<h1>{__('Configurazione iniziale')}</h1>
|
2023-07-13 12:38:03 +02:00
|
|
|
<div auto-animate>
|
2023-07-19 16:55:40 +02:00
|
|
|
<Step {...vnode.attrs.page.props}
|
|
|
|
disablePreviousButton={this.currentStep() === this.initialStep}
|
|
|
|
onSaveInstall={this.onSaveInstall.bind(this)}
|
|
|
|
onStepChange={this.currentStep.bind(this)}/>
|
2023-04-20 20:00:38 +02:00
|
|
|
</div>
|
|
|
|
</>;
|
|
|
|
}
|
|
|
|
|
|
|
|
async onSaveInstall() {
|
|
|
|
let data = {};
|
|
|
|
for (const step of Object.values(this.steps)) {
|
|
|
|
data = {...data, ...step.data};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Request.put(route('setup.save'), data);
|
2023-07-02 13:56:16 +02:00
|
|
|
void showSnackbar(__('Impostazioni salvate correttamente'));
|
|
|
|
router.visit(route('login'));
|
2023-04-20 20:00:38 +02:00
|
|
|
} catch (error: any) {
|
2023-07-02 13:56:16 +02:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(error);
|
2023-04-20 20:00:38 +02:00
|
|
|
void showSnackbar((error as RequestError<{message: string}>).response.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|