Created share token hook.

This commit is contained in:
Mike Cao 2020-10-11 02:29:55 -07:00
parent 4119e80a9a
commit 5a73c224b7
22 changed files with 90 additions and 49 deletions

25
hooks/useShareToken.js Normal file
View file

@ -0,0 +1,25 @@
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { get } from 'lib/web';
import { setShareToken } from 'redux/actions/app';
export default function useShareToken(shareId) {
const dispatch = useDispatch();
const shareToken = useSelector(state => state.app.shareToken);
async function loadToken(id) {
const { data } = await get(`/api/share/${id}`);
if (data) {
dispatch(setShareToken(data));
}
}
useEffect(() => {
if (shareId) {
loadToken(shareId);
}
}, [shareId]);
return shareToken;
}