umami/hooks/useShareToken.js

28 lines
673 B
JavaScript
Raw Normal View History

2020-10-11 11:29:55 +02:00
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
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 { setShareToken } from 'redux/actions/app';
export default function useShareToken(shareId) {
2021-01-14 09:34:51 +01:00
const { basePath } = useRouter();
2020-10-11 11:29:55 +02:00
const dispatch = useDispatch();
const shareToken = useSelector(state => state.app.shareToken);
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) {
dispatch(setShareToken(data));
}
}
useEffect(() => {
if (shareId) {
loadToken(shareId);
}
}, [shareId]);
return shareToken;
}