Start saving starting point form state
This commit is contained in:
parent
9c34be0bb5
commit
7b52d4fbe9
3 changed files with 21 additions and 7 deletions
|
@ -1,13 +1,14 @@
|
||||||
import { ChangeDetectionStrategy, Component, inject, OnInit } from '@angular/core';
|
import { ChangeDetectionStrategy, Component, inject, OnInit } from '@angular/core';
|
||||||
import { AppStateStore } from './app.state';
|
import { AppStateStore } from './app.state';
|
||||||
import { StartingPoint } from './views/starting-point/starting-point';
|
import { StartingPoint } from './views/starting-point/starting-point';
|
||||||
|
import { CollegeSelection } from './views/college-selection/college-selection';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-root',
|
selector: 'app-root',
|
||||||
templateUrl: './app.html',
|
templateUrl: './app.html',
|
||||||
styleUrl: './app.scss',
|
styleUrl: './app.scss',
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
imports: [StartingPoint],
|
imports: [StartingPoint, CollegeSelection],
|
||||||
})
|
})
|
||||||
export class App implements OnInit {
|
export class App implements OnInit {
|
||||||
private readonly state = inject(AppStateStore);
|
private readonly state = inject(AppStateStore);
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import { effect } from '@angular/core';
|
import { FormControlStatus } from '@angular/forms';
|
||||||
import { getState, patchState, signalStore, watchState, withHooks, withMethods, withState } from '@ngrx/signals';
|
import { patchState, signalStore, watchState, withHooks, withMethods, withState } from '@ngrx/signals';
|
||||||
|
|
||||||
export type StartingPointState = {
|
export type StartingPointState = {
|
||||||
name: string;
|
name: string;
|
||||||
gender: Gender;
|
gender: Gender;
|
||||||
afterHighSchool: AfterHighSchool;
|
afterHighSchool: AfterHighSchool;
|
||||||
|
formStatus: FormControlStatus;
|
||||||
};
|
};
|
||||||
|
|
||||||
export enum Gender {
|
export enum Gender {
|
||||||
|
@ -23,16 +24,26 @@ const initialState: StartingPointState = {
|
||||||
name: '',
|
name: '',
|
||||||
gender: Gender.Undefined,
|
gender: Gender.Undefined,
|
||||||
afterHighSchool: AfterHighSchool.Undefined,
|
afterHighSchool: AfterHighSchool.Undefined,
|
||||||
|
formStatus: 'INVALID',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const StartingPointStateStore = signalStore(
|
export const StartingPointStateStore = signalStore(
|
||||||
withState(initialState),
|
withState(initialState),
|
||||||
withMethods(store => ({
|
withMethods(store => ({
|
||||||
updateState: ({ name, gender, afterHighSchool }: Partial<StartingPointState>) =>
|
updateState: ({ name, gender, afterHighSchool }: Partial<StartingPointState>) =>
|
||||||
patchState(store, () => ({
|
patchState(store, state => {
|
||||||
name: name ?? '',
|
const updatedState: Partial<StartingPointState> = Object.fromEntries(
|
||||||
gender: gender ?? Gender.Undefined,
|
Object.entries({ name, gender, afterHighSchool }).filter(([, value]) => !!value)
|
||||||
afterHighSchool: afterHighSchool ?? AfterHighSchool.Undefined,
|
);
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
...updatedState,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
updateFormStatus: (formStatus: FormControlStatus) =>
|
||||||
|
patchState(store, state => ({
|
||||||
|
...state,
|
||||||
|
formStatus,
|
||||||
})),
|
})),
|
||||||
})),
|
})),
|
||||||
withHooks({
|
withHooks({
|
||||||
|
|
|
@ -71,6 +71,8 @@ export class StartingPoint implements OnInit {
|
||||||
.subscribe(({ name, gender, afterHighSchool }) =>
|
.subscribe(({ name, gender, afterHighSchool }) =>
|
||||||
this.state.updateState({ name, gender, afterHighSchool } as Partial<StartingPointState>)
|
this.state.updateState({ name, gender, afterHighSchool } as Partial<StartingPointState>)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.form.statusChanges.pipe(takeUntilDestroyed()).subscribe(status => this.state.updateFormStatus(status));
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(): void {}
|
ngOnInit(): void {}
|
||||||
|
|
Loading…
Add table
Reference in a new issue