From b9aea18d343cb3a54560bab7c197ea546019b565 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Fri, 12 Nov 2021 15:59:44 +0100 Subject: [PATCH] add fetch of config with admin key --- src/app/app-routing.module.ts | 2 +- .../components/header/header.component.html | 6 +++ src/app/core/services/api.service.ts | 44 +++++++++++---- src/app/core/services/poll.service.ts | 28 +++++++--- .../administration-routing.module.ts | 7 +++ .../administration.component.ts | 2 +- .../administration/administration.module.ts | 2 + .../consultation/consultation.component.html | 16 ++++++ .../consultation/consultation.component.scss | 0 .../consultation.component.spec.ts | 24 +++++++++ .../consultation/consultation.component.ts | 54 +++++++++++++++++++ .../steps/step-four/step-four.component.html | 2 +- .../steps/step-four/step-four.component.ts | 7 ++- .../success/success.component.html | 2 +- .../success/success.component.scss | 2 +- .../consultation-routing.module.ts | 1 + .../consultation/consultation.component.html | 2 +- src/app/routes-framadate.ts | 7 --- src/assets/i18n/FR.json | 2 +- src/environments/environment.ts | 3 +- 20 files changed, 180 insertions(+), 33 deletions(-) create mode 100644 src/app/features/administration/consultation/consultation.component.html create mode 100644 src/app/features/administration/consultation/consultation.component.scss create mode 100644 src/app/features/administration/consultation/consultation.component.spec.ts create mode 100644 src/app/features/administration/consultation/consultation.component.ts diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 4a0e9f19..43b0fa8e 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -3,7 +3,7 @@ import { RouterModule } from '@angular/router'; import { routes } from './routes-framadate'; @NgModule({ - imports: [RouterModule.forRoot(routes, { useHash: true })], + imports: [RouterModule.forRoot(routes, { useHash: true, enableTracing: true })], exports: [RouterModule], }) export class AppRoutingModule {} diff --git a/src/app/core/components/header/header.component.html b/src/app/core/components/header/header.component.html index deeeee0f..b75a47f1 100644 --- a/src/app/core/components/header/header.component.html +++ b/src/app/core/components/header/header.component.html @@ -123,4 +123,10 @@ + + test admin link to edit poll + diff --git a/src/app/core/services/api.service.ts b/src/app/core/services/api.service.ts index 05ce4cc8..4bc8df9a 100644 --- a/src/app/core/services/api.service.ts +++ b/src/app/core/services/api.service.ts @@ -136,7 +136,9 @@ export class ApiService { } } - ////////// + /** + * get all polls published by the API + */ public async getAllAvailablePolls(): Promise { // TODO: used for facilities in DEV, should be removed in production try { @@ -147,10 +149,32 @@ export class ApiService { } } - public async getPollByCustomUrl(slug: string): Promise { + /** + * get one poll by its admin key + * @param admin_key + */ + public async getPollByAdminKey(admin_key: string): Promise { try { - console.log('fetch API : asking for poll with custom_url=' + slug); - const response: AxiosResponse = await this.axiosInstance.get(`${this.pollsEndpoint}/${slug}`); + console.log('fetch API : asking for poll with admin_key=' + admin_key); + const response: AxiosResponse = await this.axiosInstance.get( + `${this.pollsEndpoint}/admin/${admin_key}` + ); + return response && response.data && !Array.isArray(response.data) ? response.data : undefined; + } catch (error) { + if (error.response?.status === 404) { + return undefined; + } else { + ApiService.handleError(error); + } + } + } + + public async getPollByCustomUrl(custom_url: string): Promise { + try { + console.log('fetch API : asking for poll with custom_url=' + custom_url); + const response: AxiosResponse = await this.axiosInstance.get( + `${this.pollsEndpoint}/${custom_url}` + ); return response && response.data && !Array.isArray(response.data) ? response.data : undefined; } catch (error) { @@ -162,12 +186,12 @@ export class ApiService { } } - public async getPollByCustomUrlWithHash(slug: string, hash: string): Promise { + public async getPollByCustomUrlWithHash(custom_url: string, hash: string): Promise { try { const response: AxiosResponse = await this.axiosInstance.get( - `${this.pollsEndpoint}/${slug}/pass/${hash}` + `${this.pollsEndpoint}/${custom_url}/pass/${hash}` ); - console.log('fetch API : asking for poll with custom_url=' + slug, { response }); + console.log('fetch API : asking for poll with custom_url=' + custom_url, { response }); return response && response.data && !Array.isArray(response.data) ? response.data : undefined; } catch (error) { @@ -181,11 +205,11 @@ export class ApiService { } } - public async getSlug(slug: string): Promise { + public async getSlug(custom_url: string): Promise { try { // TODO: scenario should be : if we can get this custom_url, it exists. if not, it doesn't. It's just a GET. const response: AxiosResponse = await this.axiosInstance.get( - `${this.pollsEndpoint}${this.slugsEndpoint}/${slug}` + `${this.pollsEndpoint}${this.slugsEndpoint}/${custom_url}` ); if (response?.status !== 404) { return false; @@ -201,7 +225,7 @@ export class ApiService { //////////// // UPDATE // - + //////////// public async sendUpdateVoteStack(vote_stack: Stack) { try { return await this.axiosInstance.patch( diff --git a/src/app/core/services/poll.service.ts b/src/app/core/services/poll.service.ts index 0a11859a..da254d98 100644 --- a/src/app/core/services/poll.service.ts +++ b/src/app/core/services/poll.service.ts @@ -65,6 +65,7 @@ export class PollService implements Resolve { ) { this.createFormGroup(); + // fill in the next 3 days of the calendar date picker this.calendar = [ this.DateUtilitiesService.addDaysToDate(1, new Date()), this.DateUtilitiesService.addDaysToDate(2, new Date()), @@ -80,7 +81,7 @@ export class PollService implements Resolve { } /** - * add example values to the form + * add example values to the form for demo env */ setDemoValues(): void { this.addChoice('orange'); @@ -125,8 +126,6 @@ export class PollService implements Resolve { whoModifiesAnswers: ['', [Validators.required]], whoCanChangeAnswers: ['', [Validators.required]], isAboutDate: [true, [Validators.required]], - // startDateInterval: ['', [Validators.required]], - // endDateInterval: ['', [Validators.required]], expiresDaysDelay: [60, []], maxCountOfAnswers: [300, []], isZeroKnoledge: [false, [Validators.required]], @@ -148,6 +147,9 @@ export class PollService implements Resolve { return form; } + /** + * set default configs to the form + */ public patchFormDefaultValues() { this.form.patchValue({ title: 'mon titre de sondage', @@ -169,6 +171,9 @@ export class PollService implements Resolve { this.setDefaultDatesForInterval(); } + /** + * get a new slug from form title and creation date + */ public updateSlug(): void { console.log('this.form.value', this.form.value); this.form.patchValue({ custom_url: this.makeSlug(this.form) }); @@ -176,6 +181,7 @@ export class PollService implements Resolve { /** * auto fetch a poll when route is looking for one in the administration pattern + * DO NOT USE - needs refacto * @param route * @param state */ @@ -259,7 +265,7 @@ export class PollService implements Resolve { * update poll and parse its fields * @param poll */ - public updateCurrentPoll(poll: Poll): void { + public updateCurrentPoll(poll: Poll): Poll { console.log('this.storageService.vote_stack.id', this.storageService.vote_stack.id); if (!this.storageService.vote_stack.id || this.storageService.vote_stack.poll_custom_url !== poll.custom_url) { @@ -272,8 +278,9 @@ export class PollService implements Resolve { // this.storageService.setChoicesForVoteStack(poll.choices); } - this.toastService.display('sondage bien mis à jour', 'success'); this._poll.next(poll); + this.toastService.display(`sondage ${poll.title} bien mis à jour`, 'success'); + return poll; } /** @@ -577,7 +584,9 @@ export class PollService implements Resolve { public getAdministrationUrlFromForm(): string { // admin_key is filled after creation - return `${environment.frontDomain}#/admin/${this.admin_key}/consultation`; + // example http://localhost:4200/#/administration/8Ubcg2YI99f69xz946cn4O64bQAeb + + return `${environment.frontDomain}#/administration/${this.admin_key}`; } public getParticipationUrl(): string { @@ -648,6 +657,13 @@ export class PollService implements Resolve { return array_dates; } + patchFormWithPoll(poll: Poll) { + this.form.patchValue({ + ...poll, + isAboutDate: poll.kind == 'date', + }); + } + /** * @description convert to API version 1 data transition object */ diff --git a/src/app/features/administration/administration-routing.module.ts b/src/app/features/administration/administration-routing.module.ts index 05b6a355..4676f048 100644 --- a/src/app/features/administration/administration-routing.module.ts +++ b/src/app/features/administration/administration-routing.module.ts @@ -7,12 +7,15 @@ import { StepThreeComponent } from './form/steps/step-three/step-three.component import { StepFourComponent } from './form/steps/step-four/step-four.component'; import { StepFiveComponent } from './form/steps/step-five/step-five.component'; import { StepOneComponent } from './form/steps/step-one/step-one.component'; +import { SuccessComponent } from './success/success.component'; +import { AdminConsultationComponent } from './consultation/consultation.component'; const routes: Routes = [ { path: '', component: AdministrationComponent, }, + { path: 'key/:admin_key', component: AdminConsultationComponent }, { path: 'step', children: [ @@ -23,6 +26,10 @@ const routes: Routes = [ { path: '5', component: StepFiveComponent }, ], }, + { + path: 'success', + component: SuccessComponent, + }, ]; @NgModule({ diff --git a/src/app/features/administration/administration.component.ts b/src/app/features/administration/administration.component.ts index 54bdcd71..8d8d2286 100644 --- a/src/app/features/administration/administration.component.ts +++ b/src/app/features/administration/administration.component.ts @@ -20,7 +20,7 @@ export class AdministrationComponent implements OnInit, OnDestroy { ngOnInit(): void { this.routeSubscription = this.route.data.subscribe((data: { poll: Poll }) => { - console.log('data', data); + console.log('routeSubscription data', data); if (data.poll) { this.poll = data.poll; } diff --git a/src/app/features/administration/administration.module.ts b/src/app/features/administration/administration.module.ts index b0faa2a9..70e204ed 100644 --- a/src/app/features/administration/administration.module.ts +++ b/src/app/features/administration/administration.module.ts @@ -27,6 +27,7 @@ import { IntervalComponent } from './form/date/interval/interval.component'; import { DayListComponent } from './form/date/list/day/day-list.component'; import { PickerComponent } from './form/date/picker/picker.component'; import { TimeListComponent } from './form/date/list/time/time-list.component'; +import { AdminConsultationComponent } from './consultation/consultation.component'; @NgModule({ declarations: [ @@ -50,6 +51,7 @@ import { TimeListComponent } from './form/date/list/time/time-list.component'; DayListComponent, PickerComponent, TimeListComponent, + AdminConsultationComponent, ], imports: [ AdministrationRoutingModule, diff --git a/src/app/features/administration/consultation/consultation.component.html b/src/app/features/administration/consultation/consultation.component.html new file mode 100644 index 00000000..e21f1a2f --- /dev/null +++ b/src/app/features/administration/consultation/consultation.component.html @@ -0,0 +1,16 @@ +
+

Consulter le sondage

+ +
+

{{ form.value.title }}

+ +
+

{{ poll.title }}

+ + Créé le {{ poll.created_at | date }} par {{ poll.owner.pseudo }}, {{ poll.owner.email }} +
+
+
diff --git a/src/app/features/administration/consultation/consultation.component.scss b/src/app/features/administration/consultation/consultation.component.scss new file mode 100644 index 00000000..e69de29b diff --git a/src/app/features/administration/consultation/consultation.component.spec.ts b/src/app/features/administration/consultation/consultation.component.spec.ts new file mode 100644 index 00000000..7ee3e6f8 --- /dev/null +++ b/src/app/features/administration/consultation/consultation.component.spec.ts @@ -0,0 +1,24 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ConsultationComponent } from './consultation.component'; + +describe('ConsultationComponent', () => { + let component: ConsultationComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ConsultationComponent], + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ConsultationComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/features/administration/consultation/consultation.component.ts b/src/app/features/administration/consultation/consultation.component.ts new file mode 100644 index 00000000..cfba138c --- /dev/null +++ b/src/app/features/administration/consultation/consultation.component.ts @@ -0,0 +1,54 @@ +import { Component, OnInit } from '@angular/core'; +import { PollService } from '../../../core/services/poll.service'; +import { ActivatedRoute, ParamMap, Router } from '@angular/router'; +import { ApiService } from '../../../core/services/api.service'; +import { FormGroup } from '@angular/forms'; +import { Poll } from '../../../core/models/poll.model'; + +@Component({ + selector: 'app-admin-consultation', + templateUrl: './consultation.component.html', + styleUrls: ['./consultation.component.scss'], +}) +export class AdminConsultationComponent implements OnInit { + private admin_key: string; + public form: FormGroup; + public poll: any; + + constructor( + private pollService: PollService, + private apiService: ApiService, + private _Activatedroute: ActivatedRoute, + private router: Router + ) { + this.poll = this.pollService._poll.getValue(); + this.form = this.pollService.form; + } + + ngOnInit(): void { + this._Activatedroute.paramMap.subscribe((params: ParamMap) => { + this.admin_key = params.get('admin_key'); + if (!this.admin_key) { + this.router.navigate('page-not-found'); + } + this.apiService.getPollByAdminKey(this.admin_key).then( + (res) => { + this.pollService.updateCurrentPoll(res.poll); + this.patchFormLoadedWithPoll(); + this.form = this.pollService.form; + this.poll = this.pollService._poll.getValue(); + console.log('formulaire patché', this.pollService.form, this.pollService.poll); + }, + (err) => { + if (!this.admin_key) { + this.router.navigate('page-not-found'); + } + } + ); + }); + } + + patchFormLoadedWithPoll() { + this.pollService.patchFormWithPoll(this.pollService._poll.getValue()); + } +} diff --git a/src/app/features/administration/form/steps/step-four/step-four.component.html b/src/app/features/administration/form/steps/step-four/step-four.component.html index 6fd7d762..0fa9ca8d 100644 --- a/src/app/features/administration/form/steps/step-four/step-four.component.html +++ b/src/app/features/administration/form/steps/step-four/step-four.component.html @@ -1,7 +1,7 @@
- +
- vous êtes admin de ce sondage + vous êtes admin de ce sondage et pouvez le modifier
diff --git a/src/app/routes-framadate.ts b/src/app/routes-framadate.ts index 1a9ca878..f2d7d619 100644 --- a/src/app/routes-framadate.ts +++ b/src/app/routes-framadate.ts @@ -20,13 +20,6 @@ export const routes: Routes = [ data: { animation: 'AdminPage' }, loadChildren: () => import('./features/administration/administration.module').then((m) => m.AdministrationModule), - // resolve: { poll: PollService }, - }, - { - path: 'admin/:admin_key', - loadChildren: () => - import('./features/administration/administration.module').then((m) => m.AdministrationModule), - // resolve: { poll: PollService }, }, { path: 'poll/:custom_url/consultation', diff --git a/src/assets/i18n/FR.json b/src/assets/i18n/FR.json index 77a8fbaf..08a03b79 100644 --- a/src/assets/i18n/FR.json +++ b/src/assets/i18n/FR.json @@ -73,7 +73,7 @@ "continue": "Voyons ce que ça donne" }, "resume": { - "title": "Et c'est tout pour nous !", + "title": "Félicitations !", "admins": "Côté administrateur-ice-eux", "users": "Côté sondés", "links_mail": "Recevoir les liens par e-mail" diff --git a/src/environments/environment.ts b/src/environments/environment.ts index b7b112db..95c54cfb 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -16,7 +16,8 @@ export const environment = { autofill_participation: true, // autofill: false, showDemoWarning: false, - autoSendNewPoll: true, + // autoSendNewPoll: true, + autoSendNewPoll: false, interval_days_default: 7, expiresDaysDelay: 60, maxCountOfAnswers: 150,