@veritree/services 0.8.1 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "0.8.1",
3
+ "version": "0.9.1",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -1,4 +1,4 @@
1
- import { getCookie } from './cookies';
1
+ import { getCookie } from "./cookies";
2
2
 
3
3
  const Api = {
4
4
  baseUrl: `${process.env.API_VERITREE_URL}/api`,
@@ -8,12 +8,12 @@ const Api = {
8
8
  return await this.unWrap(url);
9
9
  },
10
10
 
11
- async post(url, data, isMultipart = false) {
12
- return await this.unWrap(url, 'post', data, isMultipart);
11
+ async post(url, data) {
12
+ return await this.unWrap(url, "post", data);
13
13
  },
14
14
 
15
- async update(url, data, isMultipart = false) {
16
- return await this.unWrap(url, 'put', data, isMultipart);
15
+ async update(url, data) {
16
+ return await this.unWrap(url, "put", data);
17
17
  },
18
18
 
19
19
  /**
@@ -24,9 +24,9 @@ const Api = {
24
24
  * @param {object} data
25
25
  * @returns {object} envelope
26
26
  */
27
- async unWrap(url, method = 'get', data, isMultipart) {
28
- if (this.forceEnvelopeResponse) url = this.addEvenlopeArgToUrl(url); // TODO: remove when API is fully migrated to envelopes
29
- const config = this.getConfig(method, data, isMultipart);
27
+ async unWrap(url, method = "get", data) {
28
+ if (this.forceEnvelopeResponse) url = this.handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
29
+ const config = this.getConfig(method, data);
30
30
 
31
31
  try {
32
32
  const response = await fetch(url, config);
@@ -45,22 +45,24 @@ const Api = {
45
45
  * @param {object} body
46
46
  * @returns {object} data
47
47
  */
48
- getConfig(method, data, isMultipart) {
49
- const contentType = isMultipart ? 'multipart/form-data' : 'application/json';
50
- const token = `Bearer ${getCookie('access_token')}`;
48
+ getConfig(method, data) {
49
+ const isGet = method === "get";
50
+ const isFormData = this._isFormData(data);
51
+ const accessToken = `Bearer ${getCookie('access_token')}`;
51
52
 
52
53
  const config = {
53
54
  method,
54
55
  headers: {
55
- 'Content-Type': contentType,
56
- Authorization: token
56
+ Authorization: accessToken,
57
57
  },
58
58
  };
59
59
 
60
- if (method !== 'get' && !isMultipart) {
61
- config.body = JSON.stringify(data);
62
- } else {
63
- config.body = data;
60
+ if(!isFormData) {
61
+ config.headers['Content-Type'] = 'application/json';
62
+ }
63
+
64
+ if (!isGet) {
65
+ isFormData ? (config.body = data) : (config.body = JSON.stringify(data));
64
66
  }
65
67
 
66
68
  return config;
@@ -72,13 +74,18 @@ const Api = {
72
74
  * @param {string} url
73
75
  * @returns {string} url
74
76
  */
75
- addEvenlopeArgToUrl(url) {
76
- if (!url || url.includes('_result=1')) return;
77
- const urlHasArgs = url.includes('?');
78
- const urlEvenlopeArg = urlHasArgs ? '&_result=1' : '?_result=1';
77
+ handleEnvelopParam(url) {
78
+ if (!url || url.includes("_result=1")) return url;
79
+
80
+ const urlHasArgs = url.includes("?");
81
+ const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
79
82
 
80
83
  return `${url}${urlEvenlopeArg}`;
81
84
  },
85
+
86
+ _isFormData(data) {
87
+ return data instanceof FormData;
88
+ },
82
89
  };
83
90
 
84
91
  export default Api;
@@ -0,0 +1,7 @@
1
+ export const getSessionOrgId = () => {
2
+ return getSession().orgId;
3
+ };
4
+
5
+ export const getSession = () => {
6
+ return JSON.parse(localStorage.getItem('session'));
7
+ };
@@ -1,15 +1,18 @@
1
- import Api from '../helpers/api';
1
+ import Api from "../helpers/api";
2
+ import { getSession } from "../helpers/session";
2
3
 
3
- export const createFieldUpdatesApiService = (orgId, orgType) => {
4
+ export const createFieldUpdatesApiService = () => {
4
5
  const resource = `field-updates`;
6
+ const { orgId, orgType } = getSession();
5
7
 
6
8
  const get = {
7
- async all(countryId, regionId, subsiteId, page = 1, pageSize = 10) {
9
+ async all(countryId, regionId, subsiteId, page = 1, orderBy = 'date_planted-desc', pageSize = 10) {
8
10
  const params = `${_getParams(
9
11
  countryId,
10
12
  regionId,
11
13
  subsiteId,
12
14
  page,
15
+ orderBy,
13
16
  pageSize
14
17
  )}`;
15
18
  const url = `${_getUrl()}?${params}`;
@@ -19,34 +22,35 @@ export const createFieldUpdatesApiService = (orgId, orgType) => {
19
22
  };
20
23
 
21
24
  /**
22
- *
23
- * @param {string} fieldUpdateId
24
- * @param {object} data
25
- * @returns
25
+ *
26
+ * @param {string} fieldUpdateId
27
+ * @param {object} data
28
+ * @returns
26
29
  */
27
- const update = (fieldUpdateId, data, isMultipart = false) => {
30
+ const update = (fieldUpdateId, data) => {
28
31
  const url = `${_getUrl()}/${fieldUpdateId}?${_getParams()}`;
29
32
 
30
- return Api.update(url, data, isMultipart);
31
- }
33
+ return Api.post(url, data);
34
+ };
32
35
 
33
36
  const _getUrl = () => {
34
37
  return `${Api.baseUrl}/${resource}`;
35
38
  };
36
39
 
37
- const _getParams = (countryId, regionId, subsiteId, page, pageSize) => {
38
- const countryIdParam = countryId ? `&country_id=${countryId}` : '';
39
- const regionIdParam = regionId ? `&region_id=${regionId}` : '';
40
- const subsiteIdParam = subsiteId ? `&subsite_id=${subsiteId}` : '';
41
- const pageParam = page ? `&page=${page}` : '';
42
- const pageSizeParam = pageSize ? `&page_size=${pageSize}` : '';
43
- const params = `${countryIdParam}${regionIdParam}${subsiteIdParam}${pageParam}${pageSizeParam}`;
40
+ const _getParams = (countryId, regionId, subsiteId, page, orderBy, pageSize) => {
41
+ const countryIdParam = countryId ? `&country_id=${countryId}` : "";
42
+ const regionIdParam = regionId ? `&region_id=${regionId}` : "";
43
+ const subsiteIdParam = subsiteId ? `&subsite_id=${subsiteId}` : "";
44
+ const pageParam = page ? `&page=${page}` : "";
45
+ const orderByParam = orderBy ? `&orderBy=${orderBy}` : "";
46
+ const pageSizeParam = pageSize ? `&page_size=${pageSize}` : "";
47
+ const params = `${countryIdParam}${regionIdParam}${subsiteIdParam}${pageParam}${pageSizeParam}${orderByParam}`;
44
48
 
45
49
  return `org_id=${orgId}&org_type=${orgType}${params}`;
46
50
  };
47
51
 
48
52
  return {
49
53
  get,
50
- update
54
+ update,
51
55
  };
52
56
  };