umami/hooks/useShareToken.js

28 lines
596 B
JavaScript
Raw Normal View History

2020-10-11 11:29:55 +02:00
import { useEffect } from 'react';
2021-01-14 09:34:51 +01:00
import { useRouter } from 'next/router';
2020-10-11 11:29:55 +02:00
import { get } from 'lib/web';
import useStore, { setShareToken } from 'store/app';
const selector = state => state.shareToken;
2020-10-11 11:29:55 +02:00
export default function useShareToken(shareId) {
2021-01-14 09:34:51 +01:00
const { basePath } = useRouter();
const shareToken = useStore(selector);
2020-10-11 11:29:55 +02:00
async function loadToken(id) {
2021-01-14 09:34:51 +01:00
const { data } = await get(`${basePath}/api/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;
}