Skip to content

Commit

Permalink
Merge pull request #156 from map3xyz/feat/on-expire
Browse files Browse the repository at this point in the history
feat(expire): add onExpire callback
  • Loading branch information
plondon authored Mar 13, 2023
2 parents d26b24c + 226e0b5 commit d25b337
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/components/CountdownTimer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Context } from '../../providers/Store';
const circumference = Math.PI * 20;

const CountdownTimer: React.FC<Props> = () => {
const [state] = useContext(Context);
const [state, _dispatch, { onExpire }] = useContext(Context);

if (!state.expiration) {
return null;
Expand All @@ -19,6 +19,7 @@ const CountdownTimer: React.FC<Props> = () => {
const completedPercentage = progressedTime / totalTime;
const [seconds, setSeconds] = useState(remainingTimeSeconds);
const [position, setPosition] = useState(0);
const [timer, setTimer] = useState<NodeJS.Timeout | null>(null);

const countdown = () => {
if (seconds <= 0) {
Expand All @@ -29,14 +30,23 @@ const CountdownTimer: React.FC<Props> = () => {

useEffect(() => {
countdown();
const interval = setInterval(countdown, 1000);
setTimer(setInterval(countdown, 1000));

return () => clearInterval(interval);
return () => {
if (timer) {
clearInterval(timer);
}
};
}, []);

useEffect(() => {
const position = circumference - circumference * completedPercentage;
setPosition(position);

if (seconds <= 0) {
onExpire?.();
clearInterval(timer!);
}
}, [seconds]);

const hours = Math.floor(seconds / 3600)
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface Map3InitConfig {
| Promise<{ address: string; memo?: string }>
| { address: string; memo?: string };
onClose?: () => void;
onExpire?: () => void;
onFailure?: (
error: string,
networkCode: string,
Expand Down
11 changes: 9 additions & 2 deletions src/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ root.render(
callbacks: {
onAddressRequested: async () => {
return {
address: 'q3cde4tvozni',
memo: '22749',
address: '2Muv9Zzxd9M51mte3JGKk6EJFVTs74HCi15',
};
},
onExpire: () => {
console.error('Expired');
},
},
selection: {
// expiration is now plus 30 seconds
expiration: new Date(Date.now() + 30 * 1000).getTime(),
rate: 12000,
},
style: {
theme: 'dark',
Expand Down
4 changes: 4 additions & 0 deletions src/providers/Store/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ export const Store: React.FC<
handleAuthorizeTransaction,
handleOrderFeeCalculation,
onAddressRequested,
onExpire,
onFailure,
onOrderCreated,
onSuccess,
Expand Down Expand Up @@ -615,6 +616,7 @@ export const Store: React.FC<
handleAuthorizeTransaction,
handleOrderFeeCalculation,
onAddressRequested,
onExpire,
onFailure,
onOrderCreated,
onSuccess,
Expand Down Expand Up @@ -652,6 +654,7 @@ export const Context = createContext<
) =>
| Promise<{ address: string; memo?: string }>
| { address: string; memo?: string };
onExpire?: () => void;
onFailure?: (
error: string,
networkCode: string,
Expand All @@ -675,6 +678,7 @@ export const Context = createContext<
new Promise((resolve) => resolve({})),
onAddressRequested: /* istanbul ignore next */ () =>
new Promise((resolve) => resolve({ address: '' })),
onExpire: /* istanbul ignore next */ () => {},
onFailure: /* istanbul ignore next */ () => {},
onOrderCreated: /* istanbul ignore next */ () => {},
onSuccess: /* istanbul ignore next */ () => {},
Expand Down

0 comments on commit d25b337

Please sign in to comment.