2020-10-11 11:29:55 +02:00
|
|
|
import { useEffect } from 'react';
|
2022-02-23 07:47:59 +01:00
|
|
|
import useStore, { setShareToken } from 'store/app';
|
2022-03-02 06:13:15 +01:00
|
|
|
import useApi from './useApi';
|
2022-02-23 07:47:59 +01:00
|
|
|
|
|
|
|
const selector = state => state.shareToken;
|
2020-10-11 11:29:55 +02:00
|
|
|
|
2023-05-18 08:20:06 +02:00
|
|
|
export function useShareToken(shareId) {
|
2022-02-23 07:47:59 +01:00
|
|
|
const shareToken = useStore(selector);
|
2022-03-02 06:13:15 +01:00
|
|
|
const { get } = useApi();
|
2020-10-11 11:29:55 +02:00
|
|
|
|
|
|
|
async function loadToken(id) {
|
2023-01-11 17:33:43 +01:00
|
|
|
const data = await get(`/share/${id}`);
|
2020-10-11 11:29:55 +02:00
|
|
|
|
|
|
|
if (data) {
|
2022-02-23 07:47:59 +01:00
|
|
|
setShareToken(data);
|
2020-10-11 11:29:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (shareId) {
|
|
|
|
loadToken(shareId);
|
|
|
|
}
|
|
|
|
}, [shareId]);
|
|
|
|
|
|
|
|
return shareToken;
|
|
|
|
}
|
2023-05-18 08:20:06 +02:00
|
|
|
|
|
|
|
export default useShareToken;
|