sdfxs

Untitled

Jun 17th, 2021
840
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { AppConfig, AppLoginType, Email, PhoneNumber } from '@shared/entities';
  2. import { Routing } from '@shared/features/Routing';
  3. import { AnalyticsService, AnalyticsStore } from '@shared/modules';
  4. import { AuthApi, AuthService, Clock, GoogleAnalytics, ModalRoute, Storage } from '@shared/services';
  5. import { CurrencyStore, MetadataStore, SettingsStore } from '@shared/stores';
  6. import { inject, injectable, named } from 'inversify';
  7. import { action, computed, makeObservable, observable, reaction } from 'mobx';
  8.  
  9. import { AuthModals } from './AuthModals';
  10. import { LoginViewModel, RecoveryViewModel, RegistrationViewModel } from './formModel';
  11. import { EmailLoginContainer, LoginContainer, PhoneNumberLoginContainer } from './LoginContainer';
  12.  
  13. const SAVED_COUNTRY_KEY = 'phone_country';
  14.  
  15. export type FormViewModelType = RegistrationViewModel | RecoveryViewModel | LoginViewModel;
  16.  
  17. @injectable()
  18. export class AuthViewModel {
  19.   @observable.ref
  20.   public login: LoginContainer | undefined = undefined;
  21.  
  22.   private readonly recoveryViewModel: RecoveryViewModel;
  23.   private readonly registrationViewModel: RegistrationViewModel;
  24.   private readonly loginViewModel: LoginViewModel;
  25.  
  26.   public constructor(
  27.     @inject(SettingsStore) private readonly settingsStore: SettingsStore,
  28.     @inject(AuthApi) private readonly api: AuthApi,
  29.     @inject(AuthService) private readonly authService: AuthService,
  30.     @inject(Clock) private readonly clock: Clock,
  31.     @inject(CurrencyStore) private readonly currencyStore: CurrencyStore,
  32.     @inject(GoogleAnalytics) private readonly googleAnalytics: GoogleAnalytics,
  33.     @inject(AnalyticsService) analyticsService: AnalyticsService,
  34.     @inject(AnalyticsStore) analyticsStore: AnalyticsStore,
  35.     @inject(Routing) private readonly routing: Routing,
  36.     @inject(MetadataStore) metadataStore: MetadataStore,
  37.     @inject(Storage) @named(Storage.LOCAL) private readonly storage: Storage,
  38.     @inject(Storage) @named(Storage.SESSION) private readonly sessionStorage: Storage,
  39.   ) {
  40.     makeObservable(this);
  41.     this.recoveryViewModel = new RecoveryViewModel(
  42.       computed(() => this.login),
  43.       this.api,
  44.       this.authService,
  45.       this.clock,
  46.       this.onFinishRecovery,
  47.       this.currencyStore,
  48.       this.googleAnalytics,
  49.       analyticsService,
  50.       sessionStorage,
  51.     );
  52.     this.registrationViewModel = new RegistrationViewModel(
  53.       computed(() => this.login),
  54.       this.api,
  55.       this.authService,
  56.       this.clock,
  57.       this.onFinishRegistration,
  58.       this.currencyStore,
  59.       this.googleAnalytics,
  60.       analyticsService,
  61.       analyticsStore,
  62.       this.routing,
  63.       this.sessionStorage,
  64.       metadataStore,
  65.     );
  66.     this.loginViewModel = new LoginViewModel(
  67.       computed(() => this.login),
  68.       this.api,
  69.       this.authService,
  70.       this.goToRegistration,
  71.       this.goToRecovery,
  72.       this.onLogin,
  73.       this.googleAnalytics,
  74.     );
  75.  
  76.     reaction(
  77.       () => this.authService.authorized,
  78.       (authorized) => {
  79.         if (authorized) this.closeModal();
  80.       },
  81.       { fireImmediately: true },
  82.     );
  83.  
  84.     reaction(
  85.       () => this.settingsStore.appConfig,
  86.       action((appConfig) => {
  87.         if (appConfig) this.onConfig(appConfig);
  88.       }),
  89.     );
  90.  
  91.     reaction(
  92.       () => this.activeModal,
  93.       action((activeModal) => {
  94.         if (activeModal === AuthModals.REGISTRATION) {
  95.           console.log('AAAAAAAAAAAAAAAA A A A A AA  AA  A A A AA A A A  ');
  96.           console.log(window.location.search);
  97.           const params = new URLSearchParams(window.location.search);
  98.           console.log(params.get('acctype'));
  99.         }
  100.       }),
  101.       { fireImmediately: true },
  102.     );
  103.   }
  104.  
  105.   @action
  106.   private onConfig = (config: AppConfig): void => {
  107.     this.login?.dispose();
  108.     switch (config.loginType) {
  109.       case AppLoginType.PHONE: {
  110.         this.login = new PhoneNumberLoginContainer(
  111.           new PhoneNumber(this.storage.get(SAVED_COUNTRY_KEY) ?? config.defaultCountry),
  112.         );
  113.         break;
  114.       }
  115.       case AppLoginType.EMAIL:
  116.       default: {
  117.         this.login = new EmailLoginContainer(new Email());
  118.         break;
  119.       }
  120.     }
  121.   };
  122.  
  123.   @computed
  124.   public get formViewModel(): FormViewModelType | undefined {
  125.     switch (this.activeModal) {
  126.       case AuthModals.RECOVERY:
  127.         return this.recoveryViewModel;
  128.       case AuthModals.REGISTRATION:
  129.         return this.registrationViewModel;
  130.       case AuthModals.LOGIN:
  131.         return this.loginViewModel;
  132.       default:
  133.         return undefined;
  134.     }
  135.   }
  136.  
  137.   @computed
  138.   public get activeModal(): AuthModals | undefined {
  139.     const route = this.activeRoute;
  140.     const routing = this.routing.auth;
  141.  
  142.     if (route === routing.registration) return AuthModals.REGISTRATION;
  143.     if (route === routing.login) return AuthModals.LOGIN;
  144.     if (route === routing.recovery) return AuthModals.RECOVERY;
  145.  
  146.     return undefined;
  147.   }
  148.  
  149.   public goToRegistration = (): void => {
  150.     this.googleAnalytics.event({ category: 'registration', action: 'registration_initiated' });
  151.     this.routing.auth.registration.open();
  152.   };
  153.  
  154.   public goToLogin = (): void => {
  155.     this.googleAnalytics.event({ category: 'login', action: 'login_initiated' });
  156.     this.routing.auth.login.open();
  157.   };
  158.  
  159.   public goToRecovery = (): void => {
  160.     this.routing.auth.recovery.open();
  161.   };
  162.  
  163.   public goToMainPage = (): void => {
  164.     this.routing.shared.main.push();
  165.   };
  166.  
  167.   public closeModal = (): void => {
  168.     this.activeRoute?.close();
  169.   };
  170.  
  171.   @computed
  172.   private get activeRoute(): ModalRoute | undefined {
  173.     const routing = this.routing.auth;
  174.     if (routing.registration.isAt()) return routing.registration;
  175.     if (routing.login.isAt()) return routing.login;
  176.     if (routing.recovery.isAt()) return routing.recovery;
  177.     return undefined;
  178.   }
  179.  
  180.   private onFinishRecovery = (): void => this.onDone(this.closeModal);
  181.  
  182.   private onFinishRegistration = (): void => this.onDone(this.goToMainPage);
  183.  
  184.   private onLogin = (): void => this.onDone();
  185.  
  186.   @action
  187.   private resetForm = (): void => {
  188.     this.login?.reset();
  189.     [this.registrationViewModel, this.recoveryViewModel, this.loginViewModel].forEach((viewModel) => viewModel.reset());
  190.   };
  191.  
  192.   @action
  193.   private onDone = (onDoneRedirect?: () => void): void => {
  194.     if (this.login?.type === AppLoginType.PHONE) {
  195.       this.storage.set(SAVED_COUNTRY_KEY, this.login.phoneNumber.selectedCountry);
  196.     }
  197.     this.resetForm();
  198.     onDoneRedirect?.();
  199.   };
  200. }
  201.  
RAW Paste Data