forked from Eoxia/eo-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Eoxia#10 [JS] Add utils JS function for API
- Loading branch information
1 parent
c16902d
commit a24904b
Showing
16 changed files
with
194 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.