From 43bf31200717808cc1e9a03810fcb091403174ef Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 10 Mar 2020 00:17:32 +0100 Subject: [PATCH 01/21] Register page now uses api functions and can scan from vinmonopolet qr codes. Re-did formatting and styling on page. --- api/wineinfo.js | 31 ++ src/api.js | 160 +++++++ src/components/RegisterPage.vue | 815 +++++++++++++++++--------------- src/ui/ScanToVinmonopolet.vue | 107 +++++ src/ui/TextToast.vue | 99 ++++ 5 files changed, 834 insertions(+), 378 deletions(-) create mode 100644 api/wineinfo.js create mode 100644 src/api.js create mode 100644 src/ui/ScanToVinmonopolet.vue create mode 100644 src/ui/TextToast.vue diff --git a/api/wineinfo.js b/api/wineinfo.js new file mode 100644 index 0000000..237de19 --- /dev/null +++ b/api/wineinfo.js @@ -0,0 +1,31 @@ +const express = require("express"); +const path = require("path"); +const router = express.Router(); +const fetch = require('node-fetch') + +const mustBeAuthenticated = require(path.join(__dirname + "/../middleware/mustBeAuthenticated")) + +router.use((req, res, next) => { + next(); +}); + +router.route("/wineinfo/:ean").get(async (req, res) => { + const vinmonopoletResponse = await fetch("https://app.vinmonopolet.no/vmpws/v2/vmp/products/barCodeSearch/" + req.params.ean) + .then(resp => resp.json()) + + if (vinmonopoletResponse.errors != null) { + return vinmonopoletResponse.errors.map(error => { + if (error.type == "UnknownProductError") { + return res.status(404).json({ + message: error.message + }) + } else { + return next() + } + }) + } + + res.send(vinmonopoletResponse); +}); + +module.exports = router; diff --git a/src/api.js b/src/api.js new file mode 100644 index 0000000..04502b5 --- /dev/null +++ b/src/api.js @@ -0,0 +1,160 @@ +const BASE_URL = __APIURL__ || "http://localhost:30030/"; + +const statistics = () => { + const url = new URL('/api/purchase/statistics', BASE_URL) + + return fetch(url.href) + .then(resp => resp.json()) +} + +const colorStatistics = () => { + const url = new URL("/api/purchase/statistics/color", BASE_URL) + + return fetch(url.href) + .then(resp => resp.json()) +} + +const highscoreStatistics = () => { + const url = new URL("/api/highscore/statistics", BASE_URL) + + return fetch(url.href) + .then(resp => resp.json()) +} + +const overallWineStatistics = () => { + const url = new URL("/api/wines/statistics/overall", BASE_URL) + + return fetch(url.href) + .then(resp => resp.json()) +} + + +const prelottery = () => { + const url = new URL("/api/wines/prelottery", BASE_URL) + + return fetch(url.href) + .then(resp => resp.json()) +} + +const log = (sendObject) => { + const url = new URL("/api/log", BASE_URL) + + const options = { + headers: { + "Content-Type": "application/json" + }, + method: "POST", + body: JSON.stringify(sendObject) + } + + return fetch(url.href, options) + .then(resp => resp.json()) +} + +const logWines = (wines) => { + const url = new URL("/api/log/wines", BASE_URL) + + const options = { + headers: { + "Content-Type": "application/json" + }, + method: "POST", + body: JSON.stringify(wines) + } + + return fetch(url.href, options) + .then(resp => resp.json()) +} + +const wineSchema = () => { + const url = new URL("/api/log/schema", BASE_URL) + + return fetch(url.href) + .then(resp => resp.json()) +} + +const barcodeToVinmonopolet = (id) => { + const url = new URL("/api/wineinfo/" + id, BASE_URL) + + return fetch(url.href) + .then(async (resp) => { + if (!resp.ok) { + if (resp.status == 404) { + throw await resp.json() + } + } else { + return resp.json() + } + }) +} + +const handleErrors = async (resp) => { + if ([400, 409].includes(resp.status)) { + throw await resp.json() + } else { + console.error("Unexpected error occured when login/register user:", resp) + throw await resp.json() + } +} + +const login = (username, password) => { + const url = new URL("/login", BASE_URL) + const options = { + headers: { + "Content-Type": "application/json" + }, + method: "POST", + redirect: "follow", + body: JSON.stringify({ username, password }) + } + + return fetch(url.href, options) + .then(resp => { + if (resp.ok) { + if (resp.bodyUsed) + return resp.json() + else + return resp + } else { + return handleErrors(resp) + } + }) +} + +const register = (username, password) => { + const url = new URL("/register", BASE_URL) + const options = { + headers: { + "Content-Type": "application/json" + }, + method: "POST", + redirect: 'follow', + body: JSON.stringify({ username, password }) + } + + return fetch(url.href, options) + .then(resp => { + if (resp.ok) { + if (resp.bodyUsed) + return resp.json() + else + return resp + } else { + return handleErrors(resp) + } + }) +} + +export { + statistics, + colorStatistics, + highscoreStatistics, + overallWineStatistics, + prelottery, + log, + logWines, + wineSchema, + barcodeToVinmonopolet, + login, + register +} diff --git a/src/components/RegisterPage.vue b/src/components/RegisterPage.vue index b9c5f96..761cbd5 100644 --- a/src/components/RegisterPage.vue +++ b/src/components/RegisterPage.vue @@ -1,382 +1,439 @@