import { patchState, signalStore, withMethods, withState } from '@ngrx/signals'; import { PrivacyState } from './privacy.types'; import { Router } from '@angular/router'; import { inject } from '@angular/core'; import { PrivacyService } from './privacy-service'; import { AuthService } from '../../auth/auth-service'; import { HttpErrorResponse } from '@angular/common/http'; const initialState: PrivacyState = { twoFactor: { enabled: false, qrCode: null, recoveryCodes: [], }, validationErrors: null, isLoading: false, }; export const PrivayStore = signalStore( { providedIn: 'root' }, withState(initialState), withMethods((store) => { const router = inject(Router); const authService = inject(AuthService); return { toggleTwoFA: (password: string) => { patchState(store, { isLoading: true }); authService.confirmPassword({ password: password }).subscribe({ next: () => { const privacyService = inject(PrivacyService); if (store.twoFactor().enabled) { privacyService.disableTwoFA(); patchState(store, (state) => { }) } }, error: (error: HttpErrorResponse) => { if (error.status === 422) { patchState(store, { validationErrors: error.error.errors, isLoading: false }); } }, }); }, }; }), );