Skip to content

Commit

Permalink
bug/8839-Chika-PaymentsQueryNotDependentOnAuthorizedServicesAPICall (#…
Browse files Browse the repository at this point in the history
…9043)

Co-authored-by: Tim Wright <[email protected]>
  • Loading branch information
cadibemma and timwright12 authored Jul 24, 2024
1 parent ee26c11 commit 7383041
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 8 additions & 0 deletions VAMobile/src/api/payments/getPayments.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { useQuery } from '@tanstack/react-query'
import { has } from 'underscore'

import { useAuthorizedServices } from 'api/authorizedServices/getAuthorizedServices'
import { PaymentsGetData } from 'api/types'
import { DEFAULT_PAGE_SIZE } from 'constants/common'
import { Params, get } from 'store/api'
import { DowntimeFeatureTypeConstants } from 'store/api/types'
import { useDowntime } from 'utils/hooks'
import { getFirstAndLastDayOfYear, groupPaymentsByDate } from 'utils/payments'

import { paymentsKeys } from './queryKeys'
Expand Down Expand Up @@ -35,8 +39,12 @@ const getPayments = async (year: string | undefined, page: number): Promise<Paym
* Returns a query for user payments history
*/
export const usePayments = (year: string | undefined, page: number, options?: { enabled?: boolean }) => {
const { data: authorizedServices } = useAuthorizedServices()
const paymentsInDowntime = useDowntime(DowntimeFeatureTypeConstants.payments)
const queryEnabled = options && has(options, 'enabled') ? options.enabled : true
return useQuery({
...options,
enabled: !!(authorizedServices?.paymentHistory && !paymentsInDowntime && queryEnabled),
queryKey: [paymentsKeys.payments, year, page],
queryFn: () => getPayments(year, page),
meta: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { StackScreenProps } from '@react-navigation/stack'

import { map } from 'underscore'

import { useAuthorizedServices } from 'api/authorizedServices/getAuthorizedServices'
import { usePayments } from 'api/payments'
import { PaymentsData } from 'api/types'
import {
Expand All @@ -29,6 +30,7 @@ import NoPaymentsScreen from './NoPayments/NoPaymentsScreen'
type PaymentHistoryScreenProps = StackScreenProps<PaymentsStackParamList, 'PaymentHistory'>

function PaymentHistoryScreen({ navigation }: PaymentHistoryScreenProps) {
const { data: userAuthorizedServices } = useAuthorizedServices()
const { t } = useTranslation(NAMESPACE.COMMON)
const theme = useTheme()
const navigateTo = useRouteNavigation()
Expand All @@ -37,12 +39,13 @@ function PaymentHistoryScreen({ navigation }: PaymentHistoryScreenProps) {
const [yearPickerOption, setYearPickerOption] = useState<yearsDatePickerOption>()
const [pickerOptions, setpickerOptions] = useState<Array<yearsDatePickerOption>>([])
const paymentsInDowntime = useDowntime(DowntimeFeatureTypeConstants.payments)
const accessToPaymentHistory = userAuthorizedServices?.paymentHistory
const {
data: payments,
isFetching: loading,
error: hasError,
refetch: refetchPayments,
} = usePayments(yearPickerOption?.label, page, { enabled: !paymentsInDowntime })
} = usePayments(yearPickerOption?.label, page)
const noPayments = payments?.meta.availableYears?.length === 0

type yearsDatePickerOption = {
Expand Down Expand Up @@ -147,7 +150,7 @@ function PaymentHistoryScreen({ navigation }: PaymentHistoryScreenProps) {
error={hasError}
onTryAgain={refetchPayments}
/>
) : noPayments ? (
) : noPayments || !accessToPaymentHistory ? (
<NoPaymentsScreen />
) : (
<>
Expand Down

0 comments on commit 7383041

Please sign in to comment.