|
|
|
|
@ -5,14 +5,11 @@ import { |
|
|
|
|
useCallback, |
|
|
|
|
} from 'react' |
|
|
|
|
|
|
|
|
|
import sumBy from 'lodash/sumBy' |
|
|
|
|
import filter from 'lodash/filter' |
|
|
|
|
import without from 'lodash/without' |
|
|
|
|
import includes from 'lodash/includes' |
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
getSubscriptions, |
|
|
|
|
buyMatchSubscriptions, |
|
|
|
|
buyMatchSubscription, |
|
|
|
|
} from 'requests' |
|
|
|
|
|
|
|
|
|
import { SportTypes } from 'config' |
|
|
|
|
@ -26,7 +23,7 @@ export const useSubscriptions = (goTo: (step: Steps) => void) => { |
|
|
|
|
const { fetchLexics } = useLexicsFetcher() |
|
|
|
|
const [selectedPeriod, setSelectedPeriod] = useState(SubscriptionType.Month) |
|
|
|
|
const [subscriptionsList, setSubscriptionsList] = useState<MatchSubscriptions>([]) |
|
|
|
|
const [selectedSubscriptions, setSelectedSubscriptions] = useState<MatchSubscriptions>([]) |
|
|
|
|
const [selectedSubscription, setSelectedSubscription] = useState<MatchSubscription | null>(null) |
|
|
|
|
|
|
|
|
|
const fetchSubscriptions = useCallback((sport: SportTypes, id: number) => { |
|
|
|
|
getSubscriptions(sport, id) |
|
|
|
|
@ -40,40 +37,29 @@ export const useSubscriptions = (goTo: (step: Steps) => void) => { |
|
|
|
|
[selectedPeriod, subscriptionsList], |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const totalPrice = useMemo( |
|
|
|
|
() => sumBy(selectedSubscriptions, (subscription) => subscription.price), |
|
|
|
|
[selectedSubscriptions], |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const onSubscriptionSelect = (selected: MatchSubscription) => { |
|
|
|
|
const newSubscriptions = includes(selectedSubscriptions, selected) |
|
|
|
|
? without(selectedSubscriptions, selected) |
|
|
|
|
: [...selectedSubscriptions, selected] |
|
|
|
|
setSelectedSubscriptions(newSubscriptions) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const resetSubscriptions = useCallback(() => { |
|
|
|
|
setSelectedPeriod(SubscriptionType.Month) |
|
|
|
|
setSelectedSubscriptions([]) |
|
|
|
|
setSelectedSubscription(null) |
|
|
|
|
setSubscriptionsList([]) |
|
|
|
|
}, []) |
|
|
|
|
|
|
|
|
|
const subscribeToMatches = (e: MouseEvent) => { |
|
|
|
|
e.stopPropagation() |
|
|
|
|
buyMatchSubscriptions(selectedSubscriptions) |
|
|
|
|
.then(() => goTo(Steps.Success)) |
|
|
|
|
.catch(() => goTo(Steps.Error)) |
|
|
|
|
if (selectedSubscription) { |
|
|
|
|
buyMatchSubscription(selectedSubscription) |
|
|
|
|
.then(() => goTo(Steps.Success)) |
|
|
|
|
.catch(() => goTo(Steps.Error)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
fetchSubscriptions, |
|
|
|
|
onPeriodSelect: setSelectedPeriod, |
|
|
|
|
onSubscriptionSelect, |
|
|
|
|
onSubscriptionSelect: setSelectedSubscription, |
|
|
|
|
resetSubscriptions, |
|
|
|
|
selectedPeriod, |
|
|
|
|
selectedSubscriptions, |
|
|
|
|
selectedSubscription, |
|
|
|
|
subscribeToMatches, |
|
|
|
|
subscriptions, |
|
|
|
|
totalPrice, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|