Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#10 [JS] Add utils JS function for API #12

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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