From 2e6d74180423e9db49625e724dbfaa807c1f33a9 Mon Sep 17 00:00:00 2001 From: nicgirault Date: Fri, 18 Feb 2022 22:38:58 +0100 Subject: [PATCH] install new API and WIP setup first call with election creation --- package.json | 1 + services/api.js | 39 ++++++++++++++++++++------------------- yarn.lock | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 4ff0b41..2b3e94d 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "i18next-localstorage-backend": "^3.1.2", "i18next-text": "^0.5.6", "mailgun.js": "^3.3.2", + "mv-api-client": "^0.0.2", "next": "^10.2.0", "next-i18next": "^8.2.0", "plotly.js": "^2.8.3", diff --git a/services/api.js b/services/api.js index bb5489c..cea869d 100644 --- a/services/api.js +++ b/services/api.js @@ -1,3 +1,5 @@ +import { createConfiguration, PollApi } from "mv-api-client"; + const api = { urlServer: process.env.NEXT_PUBLIC_SERVER_URL || "https://demo.mieuxvoter.fr/api/", @@ -12,6 +14,9 @@ const api = { }, }; +const configuration = createConfiguration({ urlServer: api.urlServer }); +export const pollApi = new PollApi(configuration); + const sendInviteMail = (res) => { /** * Send an invitation mail using a micro-service with Netlify @@ -78,25 +83,21 @@ const createElection = ( console.log(endpoint.href); const onInvitationOnly = mails && mails.length > 0; - fetch(endpoint.href, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - title, - candidates, - on_invitation_only: onInvitationOnly, - num_grades: numGrades, - elector_emails: mails || [], - start_at: start, - finish_at: finish, - select_language: locale || "en", - front_url: window.location.origin, - restrict_results: restrictResult, - send_mail: false, - }), - }) + pollApi + .postPollCollection({ + subject: title, + proposals: candidates, + scope: onInvitationOnly ? "unlisted" : "public", + // TODO: map the old API properties with the new ones + // num_grades: numGrades, + // elector_emails: mails || [], + // start_at: start, + // finish_at: finish, + // select_language: locale || "en", + // front_url: window.location.origin, + // restrict_results: restrictResult, + // send_mail: false, + }) .then((response) => { if (!response.ok) { throw Error(response.statusText); diff --git a/yarn.lock b/yarn.lock index 1ab7833..98670fb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6905,6 +6905,11 @@ es6-object-assign@^1.1.0: resolved "https://registry.npmjs.org/es6-object-assign/-/es6-object-assign-1.1.0.tgz" integrity sha1-wsNYJlYkfDnqEHyx5mUrb58kUjw= +es6-promise@^4.2.4: + version "4.2.8" + resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a" + integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w== + es6-promisify@^6.0.0: version "6.1.1" resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-6.1.1.tgz#46837651b7b06bf6fff893d03f29393668d01621" @@ -10652,6 +10657,15 @@ mute-stream@0.0.7: resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab" integrity sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s= +mv-api-client@^0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/mv-api-client/-/mv-api-client-0.0.2.tgz#cab08faafdd38eaeb40087c5a849911e500bdfdf" + integrity sha512-Qy4x2dfn/jXxr07UWvqhkz00h57ZjSjAMrLYveSZS9rs9tbtTbCqCYKgLFqfU/8GjxkAHFSitbaeS4xW/7jk7w== + dependencies: + es6-promise "^4.2.4" + url-parse "^1.4.3" + whatwg-fetch "^3.0.0" + mz@^2.7.0: version "2.7.0" resolved "https://registry.yarnpkg.com/mz/-/mz-2.7.0.tgz#95008057a56cafadc2bc63dde7f9ff6955948e32" @@ -12417,6 +12431,11 @@ querystring@0.2.0, querystring@^0.2.0: resolved "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz" integrity sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA= +querystringify@^2.1.1: + version "2.2.0" + resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.2.0.tgz#3345941b4153cb9d082d8eee4cda2016a9aef7f6" + integrity sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ== + queue-microtask@^1.2.2: version "1.2.3" resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243" @@ -15062,6 +15081,14 @@ url-parse-lax@^3.0.0: dependencies: prepend-http "^2.0.0" +url-parse@^1.4.3: + version "1.5.7" + resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.5.7.tgz#00780f60dbdae90181f51ed85fb24109422c932a" + integrity sha512-HxWkieX+STA38EDk7CE9MEryFeHCKzgagxlGvsdS7WBImq9Mk+PGwiT56w82WI3aicwJA8REp42Cxo98c8FZMA== + dependencies: + querystringify "^2.1.1" + requires-port "^1.0.0" + url-to-options@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/url-to-options/-/url-to-options-1.0.1.tgz#1505a03a289a48cbd7a434efbaeec5055f5633a9" @@ -15389,6 +15416,11 @@ whatwg-encoding@^2.0.0: dependencies: iconv-lite "0.6.3" +whatwg-fetch@^3.0.0: + version "3.6.2" + resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.6.2.tgz#dced24f37f2624ed0281725d51d0e2e3fe677f8c" + integrity sha512-bJlen0FcuU/0EMLrdbJ7zOnW6ITZLrZMIarMUVmdKtsGvZna8vxKYaexICWPfZ8qwf9fzNq+UEIZrnSaApt6RA== + whatwg-url@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"