Skip to content

Commit

Permalink
Eoxia#10 [JS] Add utils JS function for API
Browse files Browse the repository at this point in the history
  • Loading branch information
eoxia-amandine committed Aug 2, 2024
1 parent c16902d commit a24904b
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 121 deletions.
79 changes: 79 additions & 0 deletions assets/js/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Utils JS functions
*
*/

/**
* WordPress dependencies
*/
import { useState, useEffect } from 'react';

/**
* Clean URL
*
* @param url
* @returns {*}
*/
const cleanUrl = (url) => {
let cleanedUrl = url.replace(/:\//g, '://');
cleanedUrl = cleanedUrl.replace(/([^:]\/)\/+/g, '$1');
cleanedUrl = cleanedUrl.replace(/^\/+|\/+$/g, '');
return cleanedUrl;
};

/**
* Get datas from DigiRisk module on Dolibarr
*
* @param route
* @param params
* @returns {{data: unknown, error: unknown}}
*/
export const digiriskApiGet = (route, params) => {
const [data, setData] = useState(null);
const [error, setError] = useState(null);

useEffect(() => {
const fetchData = async () => {
if (!params) {
return;
}
const { eoblocks_dolibarr_url: baseUrlApi, eoblocks_dolibarr_api_key: apiKey } = params;
if (!baseUrlApi || !apiKey || !route) {
setError('Missing API key or base URL or route');
return;
}

let digiriskUrlApi = `${baseUrlApi}/api/index.php/${route}?DOLAPIKEY=${apiKey}`;
digiriskUrlApi = cleanUrl(digiriskUrlApi);

try {
const response = await fetch(digiriskUrlApi);

if (!response.ok) {
if (response.status === 401) {
setError('Unauthorized: Wrong API Key or Unauthorized');
} else if (response.status === 404) {
setError('Not Found: Wrong Dolibarr URL');
} else {
setError(`Error: ${response.status}`);
}
return;
}

const data = await response.json();

if (data.error) {
setError('Error in API response');
return;
}

setData(data);
} catch (err) {
setError(err.message);
}
};
fetchData();
}, [route, params]);

return { data, error };
}
2 changes: 1 addition & 1 deletion blocks/build/digirisk-list-risk/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-i18n'), 'version' => '654d2362268332228fac');
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-i18n'), 'version' => 'a991e2130badc69da2e5');
2 changes: 1 addition & 1 deletion blocks/build/digirisk-list-risk/index.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

149 changes: 98 additions & 51 deletions blocks/build/digirisk-list-risk/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion blocks/build/digirisk-list-risk/index.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit a24904b

Please sign in to comment.