Skip to content

Commit

Permalink
Merge pull request #364 from vaskocuturilo/dv000302_fix_plate_service
Browse files Browse the repository at this point in the history
DV-000302: Fix PlateService;
  • Loading branch information
vaskocuturilo authored Aug 17, 2024
2 parents 83eb1a6 + 90f8d07 commit 3034974
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
10 changes: 7 additions & 3 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,13 @@ function App() {
</button>
</div>
</div>
<JSONPretty id="json-pretty" style={{fontSize: "1.1em", textAlign: 'left'}}
data={getResult} mainStyle="padding:1em"
valueStyle="font-size:1.5em"></JSONPretty>
<JSONPretty
id="json-pretty"
style={{fontSize: "1.1em", textAlign: 'left'}}
data={getResult}
mainStyle="padding:1em"
valueStyle="font-size:1.5em">
</JSONPretty>
</div>
<button className="btn btn-primary" onClick={clearGetOutputPrivatePlates}
data-cy="clear_button">Clear
Expand Down
1 change: 0 additions & 1 deletion frontend/src/api/http-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ apiClient.interceptors.request.use(

apiClient.interceptors.response.use(
(response) => {
// Handle successful responses
return response;
},
(error) => {
Expand Down
51 changes: 42 additions & 9 deletions frontend/src/service/PlateService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,22 @@ export class PlateService {
this.regionRef = regionRef;
}

// Template method
getErrorMessage(err) {
const errorMessagesByCode = {
'ECONNABORTED': 'Request timed out. Please try again later.',
};

const errorMessagesByStatus = {
404: 'Region not found. Please check the region and try again.',
500: 'Server error. Please try again later.',
};

return errorMessagesByCode[err?.code] ||
(!err?.response ? 'No Server Response. Please check your network connection or try again later.' :
errorMessagesByStatus[err.response.status] ||
`An error occurred: ${err.response.status}. Please try again.`);
}

async getPlates(endpoint) {
const regionValue = this.getRegionRef.current.value;
const selectValue = this.regionRef.current.value;
Expand All @@ -24,15 +39,33 @@ export class PlateService {

try {
const res = await this.apiClient.get(endpoint);
const result = { information: res.data };
this.setGetResult(this.formatResponse(result));
this.getRegionRef.current.value = '';
this.setIsShown(true);
} catch (err) {
if (!err?.response) {
alert("No Server Response");
return;
if (res && res.data) {
const data = res.data

if (!data) {
alert('No information found in the response.');
return;
}

const result = Array.isArray(data)
? data.map(item => {
if (!item.description || !item.region) {
throw new Error('Incomplete data in one of the items.');
}
return [item.description, item.region];
})
: [data.description, data.region];
this.setGetResult(this.formatResponse(result));
this.getRegionRef.current.value = '';
this.setIsShown(true);
}
} catch
(err) {
const message = this.getErrorMessage(err);
alert(message);

console.error("Error fetching plates by region:", err);

this.setGetResult(this.formatResponse(err.response?.data || err));
this.getRegionRef.current.value = '';
}
Expand Down

0 comments on commit 3034974

Please sign in to comment.