-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Violations): create useMoneyRequestViewErrors and move violation…
…s into errors prop of inputs
- Loading branch information
1 parent
ce67e8a
commit a4844ce
Showing
4 changed files
with
102 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import MoneyRequestView from './MoneyRequestView'; | ||
|
||
export default MoneyRequestView; |
70 changes: 70 additions & 0 deletions
70
src/components/ReportActionItem/MoneyRequestView/useMoneyRequestViewErrors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import {useCallback} from 'react'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import usePermissions from '@hooks/usePermissions'; | ||
import type {MoneyRequestField} from '@hooks/useViolations'; | ||
import useViolations from '@hooks/useViolations'; | ||
import ViolationsUtils from '@libs/ViolationsUtils'; | ||
import type {TranslationPaths} from '@src/languages/types'; | ||
import type {TransactionViolation} from '@src/types/onyx'; | ||
|
||
type FieldsWithErrors = Exclude<MoneyRequestField, 'receipt'>; | ||
|
||
type FieldCheck = { | ||
isError: boolean; | ||
translationPath: TranslationPaths; | ||
}; | ||
|
||
type FieldChecks = Partial<Record<FieldsWithErrors, FieldCheck>>; | ||
|
||
type UseMoneyRequestViewErrorsParams = { | ||
transactionViolations: TransactionViolation[]; | ||
hasErrors: boolean; | ||
isEmptyMerchant: boolean; | ||
transactionDate: string; | ||
transactionAmount: number; | ||
}; | ||
|
||
function useMoneyRequestViewErrors(params: UseMoneyRequestViewErrorsParams) { | ||
const {transactionViolations, hasErrors, isEmptyMerchant, transactionDate, transactionAmount} = params; | ||
|
||
const {translate} = useLocalize(); | ||
const {canUseViolations} = usePermissions(); | ||
const {getViolationsForField} = useViolations(transactionViolations); | ||
|
||
const getErrorForField = useCallback( | ||
(field: Exclude<MoneyRequestField, 'receipt'>) => { | ||
const fieldChecks: FieldChecks = { | ||
amount: { | ||
isError: transactionAmount === 0, | ||
translationPath: 'common.error.enterAmount', | ||
}, | ||
merchant: { | ||
isError: isEmptyMerchant, | ||
translationPath: 'common.error.enterMerchant', | ||
}, | ||
date: { | ||
isError: transactionDate === '', | ||
translationPath: 'common.error.enterDate', | ||
}, | ||
}; | ||
|
||
const {isError, translationPath} = fieldChecks[field] ?? {}; | ||
|
||
if (hasErrors && isError && translationPath) { | ||
return translate(translationPath); | ||
} | ||
|
||
if (canUseViolations) { | ||
const violations = getViolationsForField(field); | ||
return ViolationsUtils.getViolationTranslation(violations[0], translate); | ||
} | ||
|
||
return ''; | ||
}, | ||
[canUseViolations, hasErrors, getViolationsForField, translate, transactionAmount, isEmptyMerchant, transactionDate], | ||
); | ||
|
||
return {getErrorForField}; | ||
} | ||
|
||
export default useMoneyRequestViewErrors; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters