Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3aae657

Browse files
committed
chore: moving polling switching into useEffect
1 parent 336f440 commit 3aae657

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/components/Payroll/PayrollOverview/PayrollOverview.tsx

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { usePayrollsGetSuspense } from '@gusto/embedded-api/react-query/payrolls
33
import { useTranslation } from 'react-i18next'
44
import { useBankAccountsGetSuspense } from '@gusto/embedded-api/react-query/bankAccountsGet'
55
import { useEmployeesListSuspense } from '@gusto/embedded-api/react-query/employeesList'
6-
import { useState } from 'react'
6+
import { useEffect, useState } from 'react'
77
import { PayrollOverviewPresentation } from './PayrollOverviewPresentation'
88
import { componentEvents, PAYROLL_PROCESSING_STATUS } from '@/shared/constants'
99
import { BaseComponent, useBase, type BaseComponentInterface } from '@/components/Base'
@@ -39,22 +39,31 @@ export const Root = ({ companyId, payrollId, dictionary, onEvent }: PayrollOverv
3939
)
4040
const payrollData = data.payrollShow!
4141

42-
//If payroll is in submitting state and we are not polling yet, start polling
43-
if (
44-
payrollData.processingRequest?.status === PAYROLL_PROCESSING_STATUS.submitting &&
45-
!isPolling
46-
) {
47-
setIsPolling(true)
48-
}
49-
//If we are polling and payroll is in success state, stop polling, and emit event
50-
//Payroll is processed
51-
if (
52-
isPolling &&
53-
payrollData.processingRequest?.status === PAYROLL_PROCESSING_STATUS.submit_success
54-
) {
55-
onEvent(componentEvents.RUN_PAYROLL_PROCESSED)
56-
setIsPolling(false)
57-
}
42+
useEffect(() => {
43+
// Start polling when payroll is submitting and not already polling
44+
if (
45+
payrollData.processingRequest?.status === PAYROLL_PROCESSING_STATUS.submitting &&
46+
!isPolling
47+
) {
48+
setIsPolling(true)
49+
}
50+
// Stop polling and emit event when payroll is processed successfully
51+
if (
52+
isPolling &&
53+
payrollData.processingRequest?.status === PAYROLL_PROCESSING_STATUS.submit_success
54+
) {
55+
onEvent(componentEvents.RUN_PAYROLL_PROCESSED)
56+
setIsPolling(false)
57+
}
58+
// If we are polling and payroll is in failed state, stop polling, and emit failure event
59+
if (
60+
isPolling &&
61+
payrollData.processingRequest?.status === PAYROLL_PROCESSING_STATUS.processing_failed
62+
) {
63+
onEvent(componentEvents.RUN_PAYROLL_PROCESSING_FAILED)
64+
setIsPolling(false)
65+
}
66+
}, [payrollData.processingRequest?.status, isPolling, onEvent])
5867

5968
const { data: bankAccountData } = useBankAccountsGetSuspense({
6069
companyId,

src/shared/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export const runPayrollEvents = {
128128
RUN_PAYROLL_SELECTED: 'runPayroll/selected',
129129
RUN_PAYROLL_SUBMITTED: 'runPayroll/submitted',
130130
RUN_PAYROLL_PROCESSED: 'runPayroll/processed',
131+
RUN_PAYROLL_PROCESSING_FAILED: 'runPayroll/processingFailed',
131132
} as const
132133

133134
export const componentEvents = {

0 commit comments

Comments
 (0)