Skip to content

Commit

Permalink
fix(hooks): make useActivity and useAdaptiveCard work if an adapter i…
Browse files Browse the repository at this point in the history
…sn't yet present on the context
  • Loading branch information
patricia0817 authored and cipak committed Mar 3, 2022
1 parent 44b2fb2 commit 5bb40d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
32 changes: 21 additions & 11 deletions src/components/hooks/useActivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,29 @@ import {AdapterContext} from './contexts';
*/
export default function useActivity(activityID) {
const [activity, setActivity] = useState({});
const {activitiesAdapter} = useContext(AdapterContext);
const adapter = useContext(AdapterContext);
const activitiesAdapter = adapter && adapter.activitiesAdapter;

useEffect(() => {
const onError = (error) => {
throw error;
};
const subscription = activitiesAdapter.getActivity(activityID).subscribe(setActivity, onError);

return () => {
subscription.unsubscribe();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
let cleanup;

if (!activitiesAdapter || !activityID) {
setActivity({});
cleanup = undefined;
} else {
const onError = (error) => {
throw error;
};
const subscription = activitiesAdapter
.getActivity(activityID).subscribe(setActivity, onError);

cleanup = () => {
subscription.unsubscribe();
};
}

return cleanup;
}, [activitiesAdapter, activityID]);

return activity;
}
3 changes: 2 additions & 1 deletion src/components/hooks/useAdaptiveCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {AdapterContext} from './contexts';
*/
export default function useAdaptiveCard(activityID) {
const [card, setCard] = useState({});
const {activitiesAdapter} = useContext(AdapterContext);
const adapter = useContext(AdapterContext);
const activitiesAdapter = adapter && adapter.activitiesAdapter;

useEffect(() => {
let cleanup;
Expand Down

0 comments on commit 5bb40d0

Please sign in to comment.