Added token expired handling to mercure binding

This commit is contained in:
Alejandro Celaya 2020-04-18 12:25:47 +02:00
parent ed40b79c8d
commit a485d0b507
5 changed files with 13 additions and 8 deletions

View file

@ -1,6 +1,6 @@
import { EventSourcePolyfill as EventSource } from 'event-source-polyfill'; import { EventSourcePolyfill as EventSource } from 'event-source-polyfill';
export const bindToMercureTopic = (mercureInfo, topic, onMessage) => () => { export const bindToMercureTopic = (mercureInfo, topic, onMessage, onTokenExpired) => () => {
const { mercureHubUrl, token, loading, error } = mercureInfo; const { mercureHubUrl, token, loading, error } = mercureInfo;
if (loading || error) { if (loading || error) {
@ -17,9 +17,7 @@ export const bindToMercureTopic = (mercureInfo, topic, onMessage) => () => {
}); });
es.onmessage = ({ data }) => onMessage(JSON.parse(data)); es.onmessage = ({ data }) => onMessage(JSON.parse(data));
es.onerror = ({ status }) => status === 401 && onTokenExpired();
// TODO Handle errors and get a new token
es.onerror = () => {};
return () => es.close(); return () => es.close();
}; };

View file

@ -31,6 +31,7 @@ const propTypes = {
shortUrlsList: PropTypes.arrayOf(shortUrlType), shortUrlsList: PropTypes.arrayOf(shortUrlType),
selectedServer: serverType, selectedServer: serverType,
createNewVisit: PropTypes.func, createNewVisit: PropTypes.func,
loadMercureInfo: PropTypes.func,
mercureInfo: MercureInfoType, mercureInfo: MercureInfoType,
}; };
@ -47,6 +48,7 @@ const ShortUrlsList = (ShortUrlsRow) => {
shortUrlsList, shortUrlsList,
selectedServer, selectedServer,
createNewVisit, createNewVisit,
loadMercureInfo,
mercureInfo, mercureInfo,
}) => { }) => {
const { orderBy } = shortUrlsListParams; const { orderBy } = shortUrlsListParams;
@ -114,7 +116,10 @@ const ShortUrlsList = (ShortUrlsRow) => {
return resetShortUrlParams; return resetShortUrlParams;
}, []); }, []);
useEffect(bindToMercureTopic(mercureInfo, 'https://shlink.io/new-visit', createNewVisit), [ mercureInfo ]); useEffect(
bindToMercureTopic(mercureInfo, 'https://shlink.io/new-visit', createNewVisit, loadMercureInfo),
[ mercureInfo ]
);
return ( return (
<React.Fragment> <React.Fragment>

View file

@ -32,7 +32,7 @@ const provideServices = (bottle, connect) => {
bottle.serviceFactory('ShortUrlsList', ShortUrlsList, 'ShortUrlsRow'); bottle.serviceFactory('ShortUrlsList', ShortUrlsList, 'ShortUrlsRow');
bottle.decorator('ShortUrlsList', connect( bottle.decorator('ShortUrlsList', connect(
[ 'selectedServer', 'shortUrlsListParams', 'mercureInfo' ], [ 'selectedServer', 'shortUrlsListParams', 'mercureInfo' ],
[ 'listShortUrls', 'resetShortUrlParams', 'createNewVisit' ] [ 'listShortUrls', 'resetShortUrlParams', 'createNewVisit', 'loadMercureInfo' ]
)); ));
bottle.serviceFactory('ShortUrlsRow', ShortUrlsRow, 'ShortUrlsRowMenu', 'ColorGenerator', 'useStateFlagTimeout'); bottle.serviceFactory('ShortUrlsRow', ShortUrlsRow, 'ShortUrlsRowMenu', 'ColorGenerator', 'useStateFlagTimeout');

View file

@ -33,6 +33,7 @@ const propTypes = {
cancelGetShortUrlVisits: PropTypes.func, cancelGetShortUrlVisits: PropTypes.func,
matchMedia: PropTypes.func, matchMedia: PropTypes.func,
createNewVisit: PropTypes.func, createNewVisit: PropTypes.func,
loadMercureInfo: PropTypes.func,
mercureInfo: MercureInfoType, mercureInfo: MercureInfoType,
}; };
@ -59,6 +60,7 @@ const ShortUrlVisits = ({ processStatsFromVisits, normalizeVisits }, OpenMapModa
cancelGetShortUrlVisits, cancelGetShortUrlVisits,
matchMedia = window.matchMedia, matchMedia = window.matchMedia,
createNewVisit, createNewVisit,
loadMercureInfo,
mercureInfo, mercureInfo,
}) => { }) => {
const [ startDate, setStartDate ] = useState(undefined); const [ startDate, setStartDate ] = useState(undefined);
@ -115,7 +117,7 @@ const ShortUrlVisits = ({ processStatsFromVisits, normalizeVisits }, OpenMapModa
loadVisits(); loadVisits();
}, [ startDate, endDate ]); }, [ startDate, endDate ]);
useEffect( useEffect(
bindToMercureTopic(mercureInfo, `https://shlink.io/new-visit/${shortCode}`, createNewVisit), bindToMercureTopic(mercureInfo, `https://shlink.io/new-visit/${shortCode}`, createNewVisit, loadMercureInfo),
[ mercureInfo ], [ mercureInfo ],
); );

View file

@ -12,7 +12,7 @@ const provideServices = (bottle, connect) => {
bottle.serviceFactory('ShortUrlVisits', ShortUrlVisits, 'VisitsParser', 'OpenMapModalBtn'); bottle.serviceFactory('ShortUrlVisits', ShortUrlVisits, 'VisitsParser', 'OpenMapModalBtn');
bottle.decorator('ShortUrlVisits', connect( bottle.decorator('ShortUrlVisits', connect(
[ 'shortUrlVisits', 'shortUrlDetail', 'mercureInfo' ], [ 'shortUrlVisits', 'shortUrlDetail', 'mercureInfo' ],
[ 'getShortUrlVisits', 'getShortUrlDetail', 'cancelGetShortUrlVisits', 'createNewVisit' ] [ 'getShortUrlVisits', 'getShortUrlDetail', 'cancelGetShortUrlVisits', 'createNewVisit', 'loadMercureInfo' ]
)); ));
// Services // Services