-
Notifications
You must be signed in to change notification settings - Fork 125
/
submitRepresentativeReport.js
78 lines (67 loc) · 2.12 KB
/
submitRepresentativeReport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import * as Sentry from '@sentry/browser';
import {
REPORT_STARTED,
REPORT_FAILED,
REPORT_COMPLETE,
} from '../../utils/actionTypes';
import RepresentativeFinderApi from '../../api/RepresentativeFinderApi';
/**
* Check for existing reports for a given rep (to avoid multiple reports with same id)
* Then, pass new report to POST request
* @param {Object} localStorageArray all reports that have been pushed to storage
*/
export const submitRepresentativeReport = newReport => {
return async dispatch => {
try {
dispatch({
type: REPORT_STARTED,
payload: {
reportSubmissionInProgress: true,
},
});
const storedReports = JSON.parse(localStorage.getItem('vaReports')) || [];
const updatedReports = [...storedReports];
// look for existing reports for a given representative
const index = storedReports.findIndex(
storedReport =>
storedReport.representativeId === newReport.representativeId,
);
if (index !== -1) {
updatedReports[index].reports = {
...storedReports[index].reports,
...newReport.reports,
};
} else {
updatedReports.push(newReport);
}
// send to api, update local storage + redux state on successful response
const reportResponse = await RepresentativeFinderApi.reportResult(
newReport,
);
if (reportResponse.errors?.length > 0) {
dispatch({
type: REPORT_FAILED,
payload: { reportSubmissionInProgress: false },
error: reportResponse.errors,
});
} else {
localStorage.setItem('vaReports', JSON.stringify(updatedReports));
dispatch({
type: REPORT_COMPLETE,
payload: { updatedReports },
});
}
} catch (error) {
Sentry.withScope(scope => {
scope.setExtra('error', error);
Sentry.captureMessage('Error submitting representative report');
});
dispatch({
type: REPORT_FAILED,
payload: { reportSubmissionInProgress: false },
error: error.message,
});
throw error;
}
};
};