diff --git a/src/features/BuyMatchPopup/store/hooks/index.tsx b/src/features/BuyMatchPopup/store/hooks/index.tsx index 977e65f6..cfe146db 100644 --- a/src/features/BuyMatchPopup/store/hooks/index.tsx +++ b/src/features/BuyMatchPopup/store/hooks/index.tsx @@ -40,15 +40,11 @@ export const useBuyMatchPopup = () => { setSteps([Steps.Subscriptions]) } - const closePopup = () => { - setMatch(null) - setSteps([]) - } - const { fetchSubscriptions, onPeriodSelect, onSubscriptionSelect, + resetSubscriptions, selectedPeriod, selectedSubscriptions, subscribeToMatches, @@ -56,6 +52,12 @@ export const useBuyMatchPopup = () => { totalPrice, } = useSubscriptions(goTo) + const closePopup = () => { + setMatch(null) + setSteps([]) + resetSubscriptions() + } + useEffect(() => { if (match) { fetchSubscriptions() diff --git a/src/features/BuyMatchPopup/store/hooks/useSubscriptions.tsx b/src/features/BuyMatchPopup/store/hooks/useSubscriptions.tsx index 4a1be9ff..9b48c2c2 100644 --- a/src/features/BuyMatchPopup/store/hooks/useSubscriptions.tsx +++ b/src/features/BuyMatchPopup/store/hooks/useSubscriptions.tsx @@ -5,7 +5,6 @@ import { useCallback, } from 'react' -import map from 'lodash/map' import sumBy from 'lodash/sumBy' import filter from 'lodash/filter' import without from 'lodash/without' @@ -54,12 +53,12 @@ export const useSubscriptions = (goTo: (step: Steps) => void) => { const resetSubscriptions = useCallback(() => { setSelectedPeriod(SubscriptionType.Month) setSelectedSubscriptions([]) + setSubscriptionsList([]) }, []) const subscribeToMatches = (e: MouseEvent) => { e.stopPropagation() - const ids = map(selectedSubscriptions, ({ id }) => id) - buyMatchSubscriptions(ids) + buyMatchSubscriptions(selectedSubscriptions) .then(() => goTo(Steps.Success)) .catch(() => goTo(Steps.Error)) } diff --git a/src/requests/buyMatchSubscriptions.tsx b/src/requests/buyMatchSubscriptions.tsx index 24e649dd..77ccf96b 100644 --- a/src/requests/buyMatchSubscriptions.tsx +++ b/src/requests/buyMatchSubscriptions.tsx @@ -3,11 +3,19 @@ import map from 'lodash/map' import { API_ROOT } from 'config' import { callApi } from 'helpers' -const buyMatchSubscription = (subscriptionId: number) => { +import type { SubscriptionType } from 'features/BuyMatchPopup/types' + +type Subscription = { + id: number, + type: SubscriptionType, +} + +const buyMatchSubscription = ({ id, type }: Subscription) => { const config = { body: { + _p_interval: type, is_scheduled: 0, - subscription_plan: subscriptionId, + subscription_plan: id, }, } @@ -17,6 +25,6 @@ const buyMatchSubscription = (subscriptionId: number) => { }) } -export const buyMatchSubscriptions = (subscriptionIds: Array) => ( - Promise.all(map(subscriptionIds, buyMatchSubscription)) +export const buyMatchSubscriptions = (subscriptions: Array) => ( + Promise.all(map(subscriptions, buyMatchSubscription)) )