6 changed files with 188 additions and 11 deletions
@ -1,18 +1,169 @@ |
|||
import {Component, Inject} from "@angular/core"; |
|||
import {AfterViewInit, Component, Inject} from "@angular/core"; |
|||
import {MAT_DIALOG_DATA} from "@angular/material/dialog"; |
|||
import {VipBuy} from "../../entities/VipBuy"; |
|||
import {AuthService} from "../../services/auth.service"; |
|||
import {PromocodeService} from "../../services/promocode.service"; |
|||
import {DialogRef} from "@angular/cdk/dialog"; |
|||
import {ActionService} from "../../services/action.service"; |
|||
|
|||
@Component({ |
|||
selector: "app-vip-buy-dialog", |
|||
template:` |
|||
<h1 mat-dialog-title style="color: black">Получение через промокод</h1> |
|||
<mat-dialog-content> |
|||
</mat-dialog-content> |
|||
<mat-dialog-actions> |
|||
<app-need-auth-to-continue *ngIf="!authService.isAuth()"></app-need-auth-to-continue> |
|||
<div *ngIf="authService.isAuth()"> |
|||
<p>Промокод одноразовый, так что если его успели активировать до тебя, ты лох</p> |
|||
<p *ngIf="text.length>0" [style]="{'color': text_color}">{{text}}</p> |
|||
<mat-form-field style="width: 100%" appearance="fill"> |
|||
<mat-label>Промокод</mat-label> |
|||
<input matInput placeholder="1488-228-VIP" [(ngModel)]="code"> |
|||
</mat-form-field> |
|||
<button (click)="acceptPromocode()" [disabled]="code.length==0" mat-button mat-raised-button style="width: 100%">Активировать промокод</button> |
|||
|
|||
<mat-accordion *ngIf="authService.isAdmin()"> |
|||
<mat-expansion-panel hideToggle> |
|||
<mat-expansion-panel-header> |
|||
<mat-panel-title> |
|||
Генерация промокодов |
|||
</mat-panel-title> |
|||
</mat-expansion-panel-header> |
|||
<div> |
|||
<mat-form-field appearance="fill" style="width: 100%"> |
|||
<mat-label>Генерировать код для</mat-label> |
|||
<mat-select [(ngModel)]="genV"> |
|||
<mat-option *ngFor="let v of genVariants" [value]="v"> |
|||
{{v}} |
|||
</mat-option> |
|||
</mat-select> |
|||
</mat-form-field> |
|||
<mat-form-field style="width: 100%" appearance="fill"> |
|||
<mat-label>Количество секунд</mat-label> |
|||
<input type="number" matInput placeholder="86400" [(ngModel)]="sec"> |
|||
</mat-form-field> |
|||
<mat-form-field style="width: 100%" appearance="fill"> |
|||
<mat-label>Приписка к коду</mat-label> |
|||
<input matInput placeholder="GOYDA" [(ngModel)]="append"> |
|||
</mat-form-field> |
|||
<button (click)="createPromocode()" mat-button mat-raised-button style="width: 100%;">Создать промокод</button> |
|||
</div> |
|||
</mat-expansion-panel> |
|||
<mat-expansion-panel hideToggle> |
|||
<mat-expansion-panel-header> |
|||
<mat-panel-title> |
|||
Действующие промокоды |
|||
</mat-panel-title> |
|||
</mat-expansion-panel-header> |
|||
<div> |
|||
<mat-list> |
|||
<mat-list-item style="padding-bottom: 5%">Промокодов {{currentPromocodes.length}} <button mat-button (click)="copyCodes()">Скопировать в буфер обмена</button></mat-list-item> |
|||
<mat-list-item style="padding-bottom: 5%" *ngFor="let c of currentPromocodes">{{c.code}} дает {{c.action}} на {{c.seconds}} секунд</mat-list-item> |
|||
</mat-list> |
|||
</div> |
|||
</mat-expansion-panel> |
|||
</mat-accordion> |
|||
|
|||
</mat-dialog-actions> |
|||
<button (click)="dialogRef.close()" mat-button mat-raised-button style="width: 100%; margin-bottom: 2%">Закрыть</button> |
|||
</div> |
|||
</mat-dialog-content> |
|||
` |
|||
}) |
|||
export class VipPromocodeDialog { |
|||
constructor(@Inject(MAT_DIALOG_DATA) public vip:VipBuy) {} |
|||
export class VipPromocodeDialog implements AfterViewInit { |
|||
code: string = ""; |
|||
text: string = ''; |
|||
text_color: string = 'black'; |
|||
|
|||
genVariants: string[] = ['VIP'] |
|||
genV: string = 'VIP'; |
|||
sec: number = 86400; |
|||
append: string = 'YALOX'; |
|||
|
|||
currentPromocodes:{code: string, action: string, seconds: number}[] = [] |
|||
constructor(@Inject(MAT_DIALOG_DATA) public vip:VipBuy, |
|||
public authService: AuthService, |
|||
private promocodeService: PromocodeService, |
|||
public dialogRef: DialogRef, |
|||
private actionService: ActionService) {} |
|||
|
|||
ngAfterViewInit(): void { |
|||
this.getCurrentPromocodes(); |
|||
} |
|||
|
|||
createPromocode() { |
|||
this.promocodeService.createVipPromocode(`${this.genV}:${this.sec}`, this.append).subscribe( |
|||
() => {}, |
|||
(err) => { |
|||
if (err.status == 201) { |
|||
this.actionService.showSnack('Код сгенерирован'); |
|||
this.getCurrentPromocodes(); |
|||
} |
|||
else |
|||
this.actionService.showSnack('Ошибка генерации кода'); |
|||
} |
|||
) |
|||
} |
|||
|
|||
getCurrentPromocodes() { |
|||
this.currentPromocodes = []; |
|||
this.promocodeService.getPromoCodes().subscribe( |
|||
(res) => { |
|||
const cl = Object.keys(res); |
|||
cl.forEach((code) => { |
|||
const s = res[code].split(":"); |
|||
this.currentPromocodes.push({code: code, action: s[0], seconds: parseInt(s[1])}); |
|||
}) |
|||
} |
|||
) |
|||
} |
|||
|
|||
copyCodes() { |
|||
let text = ''; |
|||
this.currentPromocodes.forEach((c) => { |
|||
text += `${c.code} дает ${c.action} на ${c.seconds} секунд\n`; |
|||
}) |
|||
this.actionService.copyToClipboard(text); |
|||
} |
|||
|
|||
acceptPromocode() { |
|||
this.promocodeService.acceptPromocode(this.code).subscribe( |
|||
(res) => { |
|||
this.text = 'Промокод активирован, твой ВИП был продлен'; |
|||
this.text_color = '#54911b'; |
|||
}, |
|||
(err) => { |
|||
switch (err.status) { |
|||
case 204: { |
|||
this.text = 'Промокод активирован, но тебе это ничего не даст'; |
|||
this.text_color = '#54911b'; |
|||
break; |
|||
} |
|||
case 201: { |
|||
this.text = 'Промокод активирован, випка была добавлена к тебе на аккаунт'; |
|||
this.text_color = '#54911b'; |
|||
break; |
|||
} |
|||
case 423: |
|||
case 404: { |
|||
this.text = 'Такого промокода не существует'; |
|||
this.text_color = '#bd2200'; |
|||
break; |
|||
} |
|||
case 410: { |
|||
this.text = 'Промокод был уже активирован'; |
|||
this.text_color = 'rgba(253,136,70,0.4)'; |
|||
break; |
|||
} |
|||
case 409: { |
|||
this.text = 'Ты уже активировал ранее промокод, подожди перед активацией нового'; |
|||
this.text_color = 'rgba(253,136,70,0.4)'; |
|||
break; |
|||
} |
|||
default: { |
|||
this.text = 'Ошибка сервера, попробуй еще раз'; |
|||
this.text_color = 'rgb(255,40,40)'; |
|||
} |
|||
} |
|||
} |
|||
) |
|||
} |
|||
} |
|||
|
@ -0,0 +1,24 @@ |
|||
import { Injectable } from '@angular/core'; |
|||
import {HttpClient} from "@angular/common/http"; |
|||
import {Observable} from "rxjs"; |
|||
|
|||
@Injectable({ |
|||
providedIn: 'root' |
|||
}) |
|||
export class PromocodeService { |
|||
static routeApi:string = "api/promocode"; |
|||
|
|||
constructor(private http: HttpClient) { } |
|||
|
|||
getPromoCodes():Observable<any> { |
|||
return this.http.get(PromocodeService.routeApi) |
|||
} |
|||
|
|||
createVipPromocode(action: string, append: string): Observable<any> { |
|||
return this.http.post(PromocodeService.routeApi, {}, {params: {action, append}}) |
|||
} |
|||
|
|||
acceptPromocode(code: string): Observable<any> { |
|||
return this.http.put(PromocodeService.routeApi, {}, {params: {code}}) |
|||
} |
|||
} |
Loading…
Reference in new issue