@selfcommunity/react-core
Version:
React Core Components useful for integrating UI Community components (react-ui).
36 lines (35 loc) • 1.19 kB
JavaScript
import { useEffect, useState } from 'react';
import { SCOPE_SC_CORE } from '../constants/Errors';
import { UserService } from '@selfcommunity/api-services';
import { Logger } from '@selfcommunity/utils';
/**
:::info
This custom hook is used to fetch the list of user providers.
:::
* @param object
* @param object.id
* @param object.providers
*/
export default function useSCFetchUserProviders({ id, providers = null }) {
const [scUserProviders, setSCUserProviders] = useState(providers || []);
const [error, setError] = useState(null);
/**
* If id resolve the obj
*/
useEffect(() => {
if (providers !== null) {
return;
}
// Retrieve provider associations if doues not exists
UserService.getProviderAssociations(id)
.then((providers) => {
setSCUserProviders(providers);
})
.catch((err) => {
setError(`User with id ${id} not found`);
Logger.error(SCOPE_SC_CORE, `User with id ${id} not found`);
Logger.error(SCOPE_SC_CORE, err.message);
});
}, [id, providers]);
return { scUserProviders, setSCUserProviders, error };
}