From 4eef4c4220a4b8523e6f7a430afda12c563edaae Mon Sep 17 00:00:00 2001 From: tykayn Date: Wed, 11 Nov 2020 12:14:01 +0100 Subject: [PATCH] add fields to config of poll --- angular.json | 3 +- src/app/core/models/configuration.model.ts | 6 ++ src/app/core/models/poll.model.ts | 4 +- src/app/core/services/api.service.ts | 13 ++++- src/app/core/services/poll.service.ts | 58 ++++++++++++++++++- .../administration/form/form.component.ts | 4 +- src/proxy.conf.json | 7 +++ 7 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 src/proxy.conf.json diff --git a/angular.json b/angular.json index a9606a6f..2f0d87cf 100644 --- a/angular.json +++ b/angular.json @@ -73,7 +73,8 @@ "serve": { "builder": "@angular-devkit/build-angular:dev-server", "options": { - "browserTarget": "framadate:build" + "browserTarget": "framadate:build", + "proxyConfig": "src/proxy.conf.json" }, "configurations": { "production": { diff --git a/src/app/core/models/configuration.model.ts b/src/app/core/models/configuration.model.ts index bc17a948..8ec766f2 100644 --- a/src/app/core/models/configuration.model.ts +++ b/src/app/core/models/configuration.model.ts @@ -5,11 +5,17 @@ export class PollConfiguration { constructor( public isAboutDate: boolean = false, public isProtectedByPassword: boolean = false, + public password: string = '', public isOwnerNotifiedByEmailOnNewVote: boolean = false, public isOwnerNotifiedByEmailOnNewComment: boolean = false, public isMaybeAnswerAvailable: boolean = false, public areResultsPublic: boolean = true, + public isAllowingtoChangeOwnAnswers: boolean = true, + public whoCanChangeAnswers: string = 'everybody', public dateCreated: Date = new Date(Date.now()), + public expiresDaysDelay: number = environment.poll.defaultConfig.expiracyInDays, + public expiracyAfterLastModificationInDays: number = environment.poll.defaultConfig + .expiracyAfterLastModificationInDays, public expires: Date = DateService.addDaysToDate( environment.poll.defaultConfig.expiracyInDays, new Date(Date.now()) diff --git a/src/app/core/models/poll.model.ts b/src/app/core/models/poll.model.ts index 288822ab..8d616c3f 100644 --- a/src/app/core/models/poll.model.ts +++ b/src/app/core/models/poll.model.ts @@ -13,7 +13,9 @@ export class Poll { public description?: string, public configuration: PollConfiguration = new PollConfiguration(), public comments: Comment[] = [], - public choices: Choice[] = [] + public choices: Choice[] = [], + public dateChoices: Choice[] = [], + public timeChoices: Choice[] = [] ) {} public getAdministrationUrl(): string { diff --git a/src/app/core/services/api.service.ts b/src/app/core/services/api.service.ts index 9198a331..28dc9c31 100644 --- a/src/app/core/services/api.service.ts +++ b/src/app/core/services/api.service.ts @@ -30,15 +30,24 @@ export class ApiService { this.axiosInstance = axios.create({ baseURL: apiBaseHref }); this.axiosInstance.defaults.timeout = 2500; this.axiosInstance.defaults.headers.post['Content-Type'] = 'application/json'; + this.axiosInstance.defaults.headers.post['Accept'] = 'application/json'; + this.axiosInstance.defaults.headers.post['Charset'] = 'UTF-8'; + this.axiosInstance.defaults.headers.post['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'; + this.axiosInstance.defaults.headers.post['Access-Control-Allow-Origin'] = '*'; + console.log('this.axiosInstance.defaults.headers', this.axiosInstance.defaults.headers); } ////////////////////// // CREATE OR UPDATE // ////////////////////// - public async createPoll(poll: Poll): Promise { + public async createPoll(poll: any): Promise { + const dataToSend = poll; + console.log('poll', poll); try { console.log('currentApiRoutes', currentApiRoutes); - return await this.axiosInstance.post(`${apiBaseHref}${currentApiRoutes['api_new_poll']}`, poll); + return await this.axiosInstance.post(`${apiBaseHref}${currentApiRoutes['api_new_poll']}`, { + data: dataToSend, + }); } catch (error) { ApiService.handleError(error); } diff --git a/src/app/core/services/poll.service.ts b/src/app/core/services/poll.service.ts index 9647cb9a..0cb0c71e 100644 --- a/src/app/core/services/poll.service.ts +++ b/src/app/core/services/poll.service.ts @@ -12,6 +12,13 @@ import { UserService } from './user.service'; import { UuidService } from './uuid.service'; import { Form } from '@angular/forms'; import { PollConfig } from '../../features/old-stuff/config/PollConfig'; +import { environment } from '../../../environments/environment'; +import { + DateChoice, + defaultAnswers, + otherDefaultDates, + PollAnswer, +} from '../../features/old-stuff/config/defaultConfigs'; @Injectable({ providedIn: 'root', @@ -168,6 +175,55 @@ export class PollService implements Resolve { this.uuidService.getUUID(), form.controls.title.value ); - return newpoll; + /** + * convert to API version 1 config poll + */ + const apiV1Poll = { + menuVisible: true, + expiracyDateDefaultInDays: newpoll.configuration.expiresDaysDelay, + deletionDateAfterLastModification: newpoll.configuration.expiracyAfterLastModificationInDays, + pollType: newpoll.configuration.isAboutDate ? 'dates' : 'classic', // classic or dates + title: newpoll.title, + description: newpoll.description, + myName: newpoll.owner.pseudo, + myComment: '', + isAdmin: true, // when we create a poll, we are admin on it + myVoteStack: {}, + myTempVoteStack: 0, + myEmail: newpoll.owner.email, + myPolls: [], // list of retrieved polls from the backend api + /* + date specific poll, we have the choice to setup different hours (timeList) for all possible dates (dateList), or use the same hours for all dates + */ + allowSeveralHours: 'true', + // access + visibility: newpoll.configuration.areResultsPublic, // visible to one with the link: + voteChoices: newpoll.configuration.isMaybeAnswerAvailable ? 'yes, maybe, no' : 'yes', // possible answers to a vote choice: only "yes", "yes, maybe, no" + creationDate: new Date(), + expirationDate: '', // expiracy date + voteStackId: null, // id of the vote stack to update + pollId: null, // id of the current poll when created. data given by the backend api + pollSlug: null, // id of the current poll when created. data given by the backend api + currentPoll: null, // current poll selected with createPoll or getPoll of ConfigService + passwordAccess: newpoll.configuration.isProtectedByPassword, + password: newpoll.configuration.password, + customUrl: newpoll.slug, // custom slug in the url, must be unique + customUrlIsUnique: null, // given by the backend + urlSlugPublic: null, + urlPublic: null, + urlAdmin: null, + adminKey: '', // key to change config of the poll + owner_modifier_token: '', // key to change a vote stack + canModifyAnswers: newpoll.configuration.isAllowingtoChangeOwnAnswers, // bool for the frontend selector + whoModifiesAnswers: newpoll.configuration.whoCanChangeAnswers, // everybody, self, nobody (: just admin) + whoCanChangeAnswers: newpoll.configuration.whoCanChangeAnswers, // everybody, self, nobody (: just admin) + dateList: newpoll.dateChoices, // sets of days as strings, config to set identical time for days in a special days poll + timeList: newpoll.timeChoices, // ranges of time expressed as strings + + answers: newpoll.choices, + // modals + displayConfirmVoteModalAdmin: false, + }; + return apiV1Poll; } } diff --git a/src/app/features/administration/form/form.component.ts b/src/app/features/administration/form/form.component.ts index 2c0b6c1a..020d2ac7 100644 --- a/src/app/features/administration/form/form.component.ts +++ b/src/app/features/administration/form/form.component.ts @@ -54,7 +54,9 @@ export class FormComponent implements OnInit { public createPoll(): void { console.log('this.form', this.form); - this.apiService.createPoll(this.pollService.newPollFromForm(this.form)); + const newpoll = this.pollService.newPollFromForm(this.form); + console.log('newpoll', newpoll); + this.apiService.createPoll(newpoll); // if (this.form.valid) { // console.log('Le sondage est correctement rempli, prêt à enregistrer.'); // const newpoll = this.pollService.newPollFromForm(this.form); diff --git a/src/proxy.conf.json b/src/proxy.conf.json new file mode 100644 index 00000000..709cdb6c --- /dev/null +++ b/src/proxy.conf.json @@ -0,0 +1,7 @@ +{ + "/api/*": { + "target": "http://localhost:8000", + "secure": false, + "logLevel": "debug" + } +}