umami/hooks/useShareToken.js

27 lines
528 B
JavaScript
Raw Normal View History

2020-10-11 11:29:55 +02:00
import { useEffect } from 'react';
import useStore, { setShareToken } from 'store/app';
2022-03-02 06:13:15 +01:00
import useApi from './useApi';
const selector = state => state.shareToken;
2020-10-11 11:29:55 +02:00
export default function useShareToken(shareId) {
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) {
const data = await get(`/share/${id}`);
2020-10-11 11:29:55 +02:00
if (data) {
setShareToken(data);
2020-10-11 11:29:55 +02:00
}
}
useEffect(() => {
if (shareId) {
loadToken(shareId);
}
}, [shareId]);
return shareToken;
}