-
Notifications
You must be signed in to change notification settings - Fork 125
/
config.js
134 lines (114 loc) · 3.56 KB
/
config.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import environment from '@department-of-veterans-affairs/platform-utilities/environment';
import compact from 'lodash/compact';
import { RepresentativeType } from './constants';
import manifest from './manifest.json';
/* eslint-disable camelcase */
export const sortOptions = {
distance_asc: 'Distance (closest to farthest)',
first_name_asc: 'First name (A - Z)',
first_name_desc: 'First name (Z - A)',
last_name_asc: 'Last name (A - Z)',
last_name_desc: 'Last name (Z - A)',
};
export const searchAreaOptions = {
'5': '5 miles',
'10': '10 miles',
'25': '25 miles',
'50': '50 miles',
'100': '100 miles',
'200': '200 miles',
'Show all': 'Show all',
};
export const endpointOptions = {
fetchVSOReps: `/services/veteran/v0/vso_accredited_representatives`,
fetchOtherReps: `/services/veteran/v0/other_accredited_representatives`,
flagReps: `/representation_management/v0/flag_accredited_representatives`,
};
/*
* Toggle true for local development
*/
export const useStagingDataLocally = true;
const baseUrl =
useStagingDataLocally && environment.BASE_URL === 'http://localhost:3001'
? `https://staging-api.va.gov`
: `${environment.API_URL}`;
export const formatReportBody = newReport => {
const reportRequestBody = {
representative_id: newReport.representativeId,
flags: [],
};
for (const [flag_type, flagged_value] of Object.entries(newReport.reports)) {
if (flagged_value !== null) {
reportRequestBody.flags.push({
// convert 'phone' to snakecase 'phone_number' before pushing
flag_type: flag_type === 'phone' ? 'phone_number' : flag_type,
flagged_value,
});
}
}
return reportRequestBody;
};
/**
* Build requestUrl and settings for api calls
* * @param endpoint {String} eg '/vso_accredited_representatives'
* * @param method {String} 'GET' or 'POST' (optional - defaults to 'GET')
* * @param requestBody {String} optional
* @returns {requestUrl, apiSettings}
*/
export const getApi = (endpoint, method = 'GET', requestBody) => {
const requestUrl = `${baseUrl}${endpoint}`;
const csrfToken = localStorage.getItem('csrfToken');
let formattedReportBody;
if (method === 'POST') {
formattedReportBody = formatReportBody(requestBody);
}
const apiSettings = {
mode: 'cors',
method,
headers: {
'X-Key-Inflection': 'camel',
'Sec-Fetch-Mode': 'cors',
'Content-Type': 'application/json',
'X-CSRF-Token': csrfToken,
// Pull app name directly from manifest since this config is defined
// before startApp, and using window.appName here would result in
// undefined for all requests that use this config.
'Source-App-Name': manifest.entryName,
},
body: JSON.stringify(formattedReportBody) || null,
};
return { requestUrl, apiSettings };
};
/**
* Build parameters and URL for representative API calls
*
*/
export const resolveParamsWithUrl = ({
address,
lat,
long,
name,
page,
perPage = 10,
sort,
type = 'veteran_service_officer',
distance,
}) => {
const params = [
address ? `address=${address}` : null,
lat ? `lat=${lat}` : null,
long ? `long=${long}` : null,
name ? `name=${name}` : null,
`page=${page || 1}`,
`per_page=${perPage}`,
`sort=${sort}`,
`type=${type}`,
distance ? `distance=${distance}` : null,
];
return `?${compact([...params]).join('&')}`;
};
export const representativeTypes = {
[RepresentativeType.VETERAN_SERVICE_OFFICER]: 'veteran_service_officer',
[RepresentativeType.ATTORNEY]: 'attorney',
[RepresentativeType.CLAIM_AGENTS]: 'claim_agents',
};