Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NewGrafon committed Dec 19, 2023
1 parent e680a06 commit caa7169
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';

import { routes } from './app.routes';
import { CookieService } from 'ngx-cookie-service';

export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes)],
providers: [provideRouter(routes), CookieService],
};
34 changes: 26 additions & 8 deletions src/app/pages/auth/auth.component.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { TelegramService } from '../../services/telegram/telegram.service';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { ApiService } from '../../services/api/api.service';
import { IApiAuth } from '../../static/interfaces/auth.interface';
import { JsonPipe } from '@angular/common';

@Component({
selector: 'app-auth',
standalone: true,
imports: [ReactiveFormsModule],
imports: [ReactiveFormsModule, JsonPipe],
templateUrl: './auth.component.html',
styleUrl: './auth.component.scss',
})
export class AuthComponent {
export class AuthComponent implements OnInit {

private static instance: AuthComponent;
get Instance() {
return AuthComponent.instance;
}

authForm = new FormGroup({
id: new FormControl('', [Validators.required, Validators.pattern(/^\d+$/), Validators.min(0)]),
Expand Down Expand Up @@ -49,22 +55,34 @@ export class AuthComponent {
}
}

onSubmitting: boolean = false;
async onSubmit() {
this.telegram.MainButton.showProgress(false);
const _this = AuthComponent.instance;

if (_this.onSubmitting) {
return;
}
_this.onSubmitting = true;

_this.telegram.MainButton.showProgress(false);
const body: IApiAuth = {
id: this.authForm.controls.id.value as string,
password: this.authForm.controls.password.value as string,
id: _this.authForm.controls.id.value as string,
password: _this.authForm.controls.password.value as string,
};
const result = await _this.api.auth(body);

const result = await this.api.auth(body);
_this.telegram.MainButton.hideProgress();

this.telegram.MainButton.hideProgress();
_this.onSubmitting = false;
}

constructor(
private readonly telegram: TelegramService,
private readonly api: ApiService,
) {
}

ngOnInit() {
AuthComponent.instance = this;
}
}
19 changes: 18 additions & 1 deletion src/app/services/api/api.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { Injectable } from '@angular/core';
import ky from 'ky';
import { type IApiAuth, type IApiAuthResponse } from '../../static/interfaces/auth.interface';
import { CookieService } from 'ngx-cookie-service';
import { AuthComponent } from '../../pages/auth/auth.component';

@Injectable({
providedIn: 'root',
})
export class ApiService {
constructor(private readonly cookieService: CookieService) {
}

private readonly api = ky.create({
prefixUrl: '',
prefixUrl: 'api.toys-orders.ru/',
headers: {
authorization: this.cookieService.get('access_token')
},
retry: 0,
hooks: {
afterResponse: [
async (res) => {
const body = await res.json();
console.log(body);
}
]
}
});

public async auth(body: IApiAuth): Promise<IApiAuthResponse> {
Expand Down

0 comments on commit caa7169

Please sign in to comment.