Skip to content

Commit

Permalink
Fix useRewards hook that didn't work with async components (#84)
Browse files Browse the repository at this point in the history
Co-authored-by: jakub.szewczyk <jakub.szewczyk@tivix.com>
  • Loading branch information
thedevelobear and jakub.szewczyk authored Mar 28, 2022
1 parent 669e6c0 commit 98adb14
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-rewards",
"version": "2.0.2",
"version": "2.0.3",
"description": "This package lets you easily add micro-interactions to your app and reward users with the rain of confetti, emoji or balloons.",
"author": "thedevelobear",
"license": "MIT",
Expand Down
10 changes: 10 additions & 0 deletions src/functions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ export const generatePhysics = (
differentiator,
};
};

export const getContainerById = (id: string) => {
const container = document.getElementById(id);
if (!container) {
console.error(
`Element with an ID of ${id} could not be found. Please provide a valid ID.`
);
}
return container;
};
46 changes: 19 additions & 27 deletions src/hooks/useReward.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,13 @@
import { useLayoutEffect, useState } from 'react';
import { useState } from 'react';
import { confetti } from '../components/Confetti/Confetti';
import { emoji } from '../components/Emoji/Emoji';
import { balloons } from '../components/Balloons/Balloons';
import { UseRewardType } from './useReward.types';
import { getContainerById } from '../functions/helpers';

export const useReward: UseRewardType = (id, type, config) => {
const [container, setContainer] = useState<Element>();
const [isAnimating, setIsAnimating] = useState<boolean>(false);

const elementNotFoundMessage = `Element with an ID of ${id} could not be found. Please provide a valid ID.`;
const typeNotFoundMessage = `${type} is not a valid react-rewards type.`;

useLayoutEffect(() => {
const foundContainer = document.getElementById(id);
if (foundContainer) {
setContainer(foundContainer);
} else {
console.error(elementNotFoundMessage);
}
}, [id, elementNotFoundMessage]);

if (!container) {
return {
reward: () => {
console.error(elementNotFoundMessage);
},
isAnimating: false,
};
}

const internalAnimatingCallback = () => {
setIsAnimating(false);
};
Expand All @@ -37,27 +16,40 @@ export const useReward: UseRewardType = (id, type, config) => {
switch (type) {
case 'confetti': {
reward = () => {
const foundContainer = getContainerById(id);

if (!foundContainer) return;

setIsAnimating(true);
confetti(container, internalAnimatingCallback, config);
confetti(foundContainer, internalAnimatingCallback, config);
};
break;
}
case 'emoji': {
reward = () => {
const foundContainer = getContainerById(id);

if (!foundContainer) return;

setIsAnimating(true);
emoji(container, internalAnimatingCallback, config);
emoji(foundContainer, internalAnimatingCallback, config);
};
break;
}
case 'balloons': {
reward = () => {
const foundContainer = getContainerById(id);

if (!foundContainer) return;

setIsAnimating(true);
balloons(container, internalAnimatingCallback, config);
balloons(foundContainer, internalAnimatingCallback, config);
};
break;
}
default: {
reward = () => console.error(typeNotFoundMessage);
reward = () =>
console.error(`${type} is not a valid react-rewards type.`);
}
}
return { reward, isAnimating };
Expand Down

0 comments on commit 98adb14

Please sign in to comment.