forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from infinitered/trevorcoleman/violations/viol…
…ation-utils Trevorcoleman/violations/violation utils
- Loading branch information
Showing
9 changed files
with
361 additions
and
4,124 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 was deleted.
Oops, something went wrong.
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,88 @@ | ||
import {useCallback, useMemo} from 'react'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import {TransactionViolation, ViolationName} from '@src/types/onyx'; | ||
|
||
/** | ||
* Map from Violation Names to the field where that violation can occur | ||
*/ | ||
const violationFields: Record<ViolationName, ViolationField> = { | ||
allTagLevelsRequired: 'tag', | ||
autoReportedRejectedExpense: 'amount', | ||
billableExpense: 'billable', | ||
cashExpenseWithNoReceipt: 'receipt', | ||
categoryOutOfPolicy: 'category', | ||
conversionSurcharge: 'amount', | ||
customUnitOutOfPolicy: 'amount', | ||
duplicatedTransaction: 'merchant', | ||
fieldRequired: 'category', | ||
futureDate: 'date', | ||
invoiceMarkup: 'amount', | ||
maxAge: 'date', | ||
missingCategory: 'category', | ||
missingComment: 'comment', | ||
missingTag: 'tag', | ||
modifiedAmount: 'amount', | ||
modifiedDate: 'date', | ||
nonExpensiworksExpense: 'merchant', | ||
overAutoApprovalLimit: 'amount', | ||
overCategoryLimit: 'amount', | ||
overLimit: 'amount', | ||
overLimitAttendee: 'amount', | ||
perDayLimit: 'amount', | ||
receiptNotSmartScanned: 'receipt', | ||
receiptRequired: 'receipt', | ||
rter: 'merchant', | ||
smartscanFailed: 'receipt', | ||
someTagLevelsRequired: 'tag', | ||
tagOutOfPolicy: 'tag', | ||
taxAmountChanged: 'tax', | ||
taxOutOfPolicy: 'tax', | ||
taxRateChanged: 'tax', | ||
taxRequired: 'tax', | ||
}; | ||
|
||
/** | ||
* Names of Fields where violations can occur | ||
*/ | ||
type ViolationField = 'amount' | 'billable' | 'category' | 'comment' | 'date' | 'merchant' | 'receipt' | 'tag' | 'tax'; | ||
|
||
type ViolationsMap = Map<ViolationField, TransactionViolation[]>; | ||
|
||
function useViolations(violations: TransactionViolation[]) { | ||
const {translate} = useLocalize(); | ||
|
||
const violationsByField = useMemo((): ViolationsMap => { | ||
const violationGroups = new Map<ViolationField, TransactionViolation[]>(); | ||
|
||
for (const violation of violations) { | ||
const field = violationFields[violation.name]; | ||
const existingViolations = violationGroups.get(field) ?? []; | ||
violationGroups.set(field, [...existingViolations, violation]); | ||
} | ||
|
||
return violationGroups; | ||
}, [violations]); | ||
|
||
const hasViolations = useCallback( | ||
(field: ViolationField) => { | ||
const fieldViolations: TransactionViolation[] = violationsByField.get(field) ?? []; | ||
return Boolean(fieldViolations.length > 0); | ||
}, | ||
[violationsByField], | ||
); | ||
|
||
const getViolationsForField = useCallback( | ||
(field: ViolationField) => { | ||
const fieldViolations: TransactionViolation[] = violationsByField.get(field) ?? []; | ||
return fieldViolations.map((violation) => translate(`violations.${violation.name}`)); | ||
}, | ||
[translate, violationsByField], | ||
); | ||
|
||
return { | ||
hasViolations, | ||
getViolationsForField, | ||
}; | ||
} | ||
|
||
export {useViolations, violationFields}; |
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
Oops, something went wrong.