77 lines
2.5 KiB
TypeScript
77 lines
2.5 KiB
TypeScript
import {Component, inject, input, signal, viewChild} from '@angular/core';
|
|
import {IonicModule, LoadingController, ToastController} from "@ionic/angular";
|
|
import {GetRandomChallengeDto, RandomchallengesService} from "../../services/api";
|
|
import {firstValueFrom} from "rxjs";
|
|
import {TooltipComponent} from "../tooltip/tooltip.component";
|
|
import {ProofFormComponent} from "../proof-form/proof-form.component";
|
|
import {SignOnFormComponent} from "../sign-on-form/sign-on-form.component";
|
|
|
|
@Component({
|
|
selector: 'app-challenge-card',
|
|
templateUrl: './challenge-card.component.html',
|
|
styleUrls: ['./challenge-card.component.scss'],
|
|
imports: [
|
|
IonicModule,
|
|
TooltipComponent,
|
|
ProofFormComponent
|
|
]
|
|
})
|
|
export class ChallengeCardComponent {
|
|
private randomChallengesService = inject(RandomchallengesService);
|
|
private toastCtrl = inject(ToastController);
|
|
private loadCtrl = inject(LoadingController);
|
|
|
|
action = input.required<string>();
|
|
tag = input.required<string>();
|
|
color = input.required<string>();
|
|
data = input.required<GetRandomChallengeDto>();
|
|
|
|
proof = viewChild<ProofFormComponent>('proofForm');
|
|
|
|
isModalOpen = false;
|
|
|
|
async setOpen(isOpen: boolean) {
|
|
this.isModalOpen = isOpen;
|
|
}
|
|
|
|
async sendProof(randomChallengeId: number) {
|
|
const file = this.proof().proofForm.value.proof;
|
|
|
|
if (!file) {
|
|
const toast = await this.toastCtrl.create({
|
|
message: 'Aucune preuve n\'a été déposée',
|
|
duration: 2000,
|
|
color: 'danger'
|
|
});
|
|
await toast.present();
|
|
return;
|
|
}
|
|
|
|
const loading = await this.loadCtrl.create({
|
|
message: 'Chargement...',
|
|
spinner: 'lines-sharp-small'
|
|
});
|
|
await loading.present();
|
|
|
|
try {
|
|
await firstValueFrom(this.randomChallengesService.patchProofEndpoint(randomChallengeId, file));
|
|
|
|
this.isModalOpen = false;
|
|
const toast = await this.toastCtrl.create({
|
|
message: 'La preuve de ta réussite a bien été déposée',
|
|
duration: 2000,
|
|
color: 'success'
|
|
});
|
|
await toast.present();
|
|
} catch {
|
|
this.isModalOpen = false;
|
|
const toast = await this.toastCtrl.create({
|
|
message: 'Tu as déjà déposé une preuve pour ce défi',
|
|
duration: 2000,
|
|
color: 'danger'
|
|
});
|
|
await toast.present();
|
|
}
|
|
await loading.dismiss();
|
|
}
|
|
} |