From 2ce9b4c3908c7ae254bf5e8d5e0c45a2d07eb9e2 Mon Sep 17 00:00:00 2001 From: nh602 Date: Wed, 28 Feb 2024 22:22:12 +0000 Subject: [PATCH] Passed HttpClient to services and changed error handling to be more graceful. --- frontend/src/index.jsx | 9 +++++++-- frontend/src/services/HttpClient.js | 1 - .../src/services/Vendors/VendorsRepository.js | 16 +++++++++------- frontend/src/services/Vendors/VendorsService.js | 4 ++++ 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/frontend/src/index.jsx b/frontend/src/index.jsx index 53acae8..468c58a 100644 --- a/frontend/src/index.jsx +++ b/frontend/src/index.jsx @@ -31,6 +31,8 @@ import VendorsService from './services/Vendors/VendorsService.js'; import {handleLoginVendor} from './services/handleLogin.js'; import {handleRegister} from './services/handleRegister.js'; +import HttpClient from './services/HttpClient.js'; + // Import configuration variables import config from './config.js'; @@ -47,9 +49,12 @@ if (config.environment == 'dev') { // Load base url for the backend const baseUrl = config.baseUrl; + // Create HttpClient + const httpClient = new HttpClient(baseUrl); + // Initilize Services - const eventsService = new EventsService(baseUrl); - const vendorsService = new VendorsService(baseUrl); + const eventsService = new EventsService(httpClient); + const vendorsService = new VendorsService(httpClient); eventService = eventsService; vendorService = vendorsService; diff --git a/frontend/src/services/HttpClient.js b/frontend/src/services/HttpClient.js index 6d802d9..0fd5fd6 100644 --- a/frontend/src/services/HttpClient.js +++ b/frontend/src/services/HttpClient.js @@ -4,7 +4,6 @@ class HttpClient { constructor(baseURL) { this.axiosInstance = axios.create({ baseURL: baseURL, - withCredentials: true, }); this.cookie = ''; } diff --git a/frontend/src/services/Vendors/VendorsRepository.js b/frontend/src/services/Vendors/VendorsRepository.js index 62c207a..1203375 100644 --- a/frontend/src/services/Vendors/VendorsRepository.js +++ b/frontend/src/services/Vendors/VendorsRepository.js @@ -8,8 +8,8 @@ export default class VendorsRepository { const response = await this.httpClient.axiosInstance.get('/vendors'); return response.data; } catch (error) { - console.error('Error fetching vendors:'); - throw error; + console.error('Error fetching vendors'); + return undefined; } } @@ -19,6 +19,7 @@ export default class VendorsRepository { return response.data; } catch (error) { console.error(`Error fetching vendor with ID ${vendorId}:`); + return undefined; } } @@ -29,6 +30,7 @@ export default class VendorsRepository { return response; } catch (error) { console.error('Error logging in vendor:'); + return undefined; } } @@ -38,7 +40,7 @@ export default class VendorsRepository { return response.data; } catch (error) { console.error('Error creating vendor:'); - throw error; + return undefined; } } @@ -48,7 +50,7 @@ export default class VendorsRepository { return response.data; } catch (error) { console.error('Error updating vendor:'); - throw error; + return undefined; } } @@ -58,7 +60,7 @@ export default class VendorsRepository { return response.data; } catch (error) { console.error(`Error updating vendor with ID ${vendorId}:`); - throw error; + return undefined; } } @@ -68,7 +70,7 @@ export default class VendorsRepository { return response.data; } catch (error) { console.error(`Error deleting vendor with ID ${vendorId}:`); - throw error; + return undefined; } } @@ -78,7 +80,7 @@ export default class VendorsRepository { return response.data; } catch (error) { console.error('Error logging out:'); - throw error; + return undefined; } } } diff --git a/frontend/src/services/Vendors/VendorsService.js b/frontend/src/services/Vendors/VendorsService.js index 57e4d13..5d8ba16 100644 --- a/frontend/src/services/Vendors/VendorsService.js +++ b/frontend/src/services/Vendors/VendorsService.js @@ -8,6 +8,10 @@ export default class VendorsService { async getVendors() { const vendorsData = await this.vendorsRepository.getAllVendors(); + if (vendorsData == undefined) { + return undefined; + } + return vendorsData.map((data) => new Vendor( data.vendor_id, data.name,