-
Notifications
You must be signed in to change notification settings - Fork 672
Expand file tree
/
Copy pathapp.component.ts
More file actions
152 lines (125 loc) · 4.06 KB
/
app.component.ts
File metadata and controls
152 lines (125 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { bootstrapApplication } from '@angular/platform-browser';
import {
ChangeDetectionStrategy,
Component,
enableProdMode,
provideZoneChangeDetection,
} from '@angular/core';
import {
DxButtonModule,
DxMultiViewModule,
} from 'devextreme-angular';
import { DxStepperModule, type DxStepperTypes } from 'devextreme-angular/ui/stepper';
import validationEngine from 'devextreme/ui/validation_engine';
import { AppService } from './app.service';
import type { BookingFormData } from './app.types';
import { DatesFormComponent } from './dates-form/dates-form.component';
import { GuestsFormComponent } from './guests-form/guests-form.component';
import { RoomMealPlanFormComponent } from './room-meal-plan-form/room-meal-plan-form.component';
import { AdditionalFormComponent } from './additional-form/additional-form.component';
import { ConfirmationComponent } from './confirmation/confirmation.component';
if (!/localhost/.test(document.location.host)) {
enableProdMode();
}
let modulePrefix = '';
// @ts-ignore
if (window && window.config?.packageConfigPaths) {
modulePrefix = '/app';
}
@Component({
changeDetection: ChangeDetectionStrategy.Eager,
selector: 'demo-app',
templateUrl: `.${modulePrefix}/app.component.html`,
styleUrls: [`.${modulePrefix}/app.component.css`],
imports: [
DxButtonModule,
DxMultiViewModule,
DxStepperModule,
DatesFormComponent,
GuestsFormComponent,
RoomMealPlanFormComponent,
AdditionalFormComponent,
ConfirmationComponent,
],
})
export class AppComponent {
steps: DxStepperTypes.Item[];
formData: BookingFormData;
selectedIndex: number;
isConfirmed: boolean;
isStepperReadonly: boolean;
validationGroups = ['dates', 'guests', 'roomAndMealPlan'];
constructor(private readonly appService: AppService) {
this.steps = this.appService.getInitialSteps();
this.formData = this.appService.getInitialFormData();
this.selectedIndex = 0;
this.isConfirmed = false;
this.isStepperReadonly = false;
}
getValidationResult(index: number) {
if (index >= this.validationGroups.length) {
return true;
}
return validationEngine.validateGroup(this.validationGroups[index]).isValid;
}
setStepValidationResult(index: number, isValid: boolean | undefined) {
this.steps[index].isValid = isValid;
}
onSelectionChanging(e: DxStepperTypes.SelectionChangingEvent) {
const { component, addedItems, removedItems } = e;
const { items = [] } = component.option();
const addedIndex = items.findIndex((item: DxStepperTypes.Item) => item === addedItems[0]);
const removedIndex = items.findIndex((item: DxStepperTypes.Item) => item === removedItems[0]);
const isMoveForward = removedIndex > -1 && addedIndex > removedIndex;
if (isMoveForward) {
const isValid = this.getValidationResult(removedIndex);
this.setStepValidationResult(removedIndex, isValid);
if (isValid === false) {
e.cancel = true;
}
}
}
getNextButtonText() {
if (this.selectedIndex < this.steps.length - 1) {
return 'Next';
}
return this.isConfirmed ? 'Reset' : 'Confirm';
}
onPrevButtonClick() {
this.selectedIndex -= 1;
}
moveNext() {
const isValid = this.getValidationResult(this.selectedIndex);
this.setStepValidationResult(this.selectedIndex, isValid);
if (isValid) {
this.selectedIndex += 1;
}
}
reset() {
this.isConfirmed = false;
this.selectedIndex = 0;
this.steps = this.appService.getInitialSteps();
this.formData = this.appService.getInitialFormData();
this.isStepperReadonly = false;
}
confirm() {
this.isConfirmed = true;
this.setStepValidationResult(this.selectedIndex, true);
this.isStepperReadonly = true;
}
onNextButtonClick() {
if (this.selectedIndex < this.steps.length - 1) {
this.moveNext();
} else if (this.isConfirmed) {
this.reset();
} else {
this.confirm();
}
}
}
bootstrapApplication(AppComponent, {
providers: [
provideZoneChangeDetection({ eventCoalescing: true, runCoalescing: true }),
AppService,
],
});