From 923adeb960639ae9f12dc2800a5c2e0fae89dccf Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Tue, 26 Dec 2023 17:24:44 +0100 Subject: [PATCH] feat(price create form): on barcode scan, fetch product details from OFF (#73) * On barcode scan, fetch data from Open Food Facts * Cleanup --- src/services/api.js | 10 ++++++++++ src/views/AddPriceSingle.vue | 35 +++++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/services/api.js b/src/services/api.js index 8dec7c37ac..0bb43a7a5e 100644 --- a/src/services/api.js +++ b/src/services/api.js @@ -1,5 +1,6 @@ import { useAppStore } from '../store' +const OPENFOODFACTS_PRODUCT_URL = 'https://world.openfoodfacts.org/api/v2/product' const NOMINATIM_SEARCH_URL = 'https://nominatim.openstreetmap.org/search' const NOMINATIM_RESULT_TYPE_EXCLUDE_LIST = ['fuel', 'gas', 'casino'] @@ -77,6 +78,13 @@ export default { .then((response) => response.json()) }, + openfoodfactsProductSearch(code) { + return fetch(`${OPENFOODFACTS_PRODUCT_URL}/${code}.json`, { + method: 'GET', + }) + .then((response) => response.json()) + }, + openstreetmapNominatimSearch(q) { return fetch(`${NOMINATIM_SEARCH_URL}?q=${q}&addressdetails=1&format=json&limit=10`, { method: 'GET', @@ -84,4 +92,6 @@ export default { .then((response) => response.json()) .then((data) => data.filter(l => !NOMINATIM_RESULT_TYPE_EXCLUDE_LIST.includes(l.type))) }, + + } diff --git a/src/views/AddPriceSingle.vue b/src/views/AddPriceSingle.vue index 71d65639ae..5ebda9c6c0 100644 --- a/src/views/AddPriceSingle.vue +++ b/src/views/AddPriceSingle.vue @@ -53,6 +53,7 @@ + -

Product set! code: {{ addPriceSingleForm.product_code }}

-

Product missing

+

+ + Product set! + code: {{ addPriceSingleForm.product_code }} + (not found in Open Food Facts) + +

+

Product missing

Price

@@ -166,6 +173,7 @@ import { mapStores } from 'pinia' import { useAppStore } from '../store' import constants from '../constants' import api from '../services/api' +import PriceCard from '../components/PriceCard.vue' import BarcodeScanner from '../components/BarcodeScanner.vue' import LocationSelector from '../components/LocationSelector.vue' @@ -179,6 +187,7 @@ Compressor.setDefaults({ export default { components: { + PriceCard, BarcodeScanner, LocationSelector }, @@ -202,6 +211,7 @@ export default { createProofLoading: false, proofSuccessMessage: false, // product data + product: null, barcodeScanner: false, // price data currencyList: constants.CURRENCY_LIST, @@ -241,7 +251,6 @@ export default { }, mounted() { this.initPriceSingleForm() - console.log(import.meta.env) }, methods: { fieldRequired(v) { @@ -313,8 +322,14 @@ export default { showBarcodeScanner() { this.barcodeScanner = true }, - setProductCode(event) { - this.addPriceSingleForm.product_code = event + setProductCode(code) { + this.addPriceSingleForm.product_code = code + this.product = null + api + .openfoodfactsProductSearch(code) + .then((data) => { + this.product = data['product'] + }) }, showLocationSelector() { this.locationSelector = true @@ -322,11 +337,11 @@ export default { closeLocationSelector(event) { this.locationSelector = false }, - setLocationData(event) { - this.appStore.addRecentLocation(event) - this.locationSelectedDisplayName = event.display_name - this.addPriceSingleForm.location_osm_id = event.osm_id - this.addPriceSingleForm.location_osm_type = event.osm_type.toUpperCase() + setLocationData(location) { + this.appStore.addRecentLocation(location) + this.locationSelectedDisplayName = location.display_name + this.addPriceSingleForm.location_osm_id = location.osm_id + this.addPriceSingleForm.location_osm_type = location.osm_type.toUpperCase() }, isSelectedLocation(location) { return this.locationSelectedDisplayName && this.locationSelectedDisplayName == location.display_name