From 70a6330ce5b6a054131ec49c7e27d9e01258b2e3 Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Wed, 24 Apr 2024 03:34:53 +0500 Subject: [PATCH 01/14] update logic for categorizing expenses --- src/CONST.ts | 21 +++ src/libs/Permissions.ts | 2 +- src/libs/actions/Policy.ts | 122 ++++++++++++------ .../OnboardingWork/BaseOnboardingWork.tsx | 2 +- ...yForRefactorRequestParticipantsSelector.js | 45 ++----- 5 files changed, 117 insertions(+), 75 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index c2299f242b6b..9150c7d3fb70 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -1619,6 +1619,27 @@ const CONST = { DISABLE: 'disable', ENABLE: 'enable', }, + DEFAULT_CATEGORIES: [ + 'Advertising', + 'Benefits', + 'Car', + 'Equipment', + 'Fees', + 'Home Office', + 'Insurance', + 'Interest', + 'Labor', + 'Maintenance', + 'Materials', + 'Meals and Entertainment', + 'Office Supplies', + 'Other', + 'Professional Services', + 'Rent', + 'Taxes', + 'Travel', + 'Utilities', + ], OWNERSHIP_ERRORS: { NO_BILLING_CARD: 'noBillingCard', AMOUNT_OWED: 'amountOwed', diff --git a/src/libs/Permissions.ts b/src/libs/Permissions.ts index c79e9011386f..456f09c1535a 100644 --- a/src/libs/Permissions.ts +++ b/src/libs/Permissions.ts @@ -4,7 +4,7 @@ import type {IOUType} from '@src/CONST'; import type Beta from '@src/types/onyx/Beta'; function canUseAllBetas(betas: OnyxEntry): boolean { - return !!betas?.includes(CONST.BETAS.ALL); + return true; // !!betas?.includes(CONST.BETAS.ALL); } function canUseChronos(betas: OnyxEntry): boolean { diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index fb5fffdc17d2..6644998560d2 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -2075,6 +2075,69 @@ function createDraftInitialWorkspace(policyOwnerEmail = '', policyName = '', pol Onyx.update(optimisticData); } +function buildOptimisticPolicyCategories(policyID: string, categories: string[]) { + const optimisticCategoryMap = categories.reduce( + (acc, category) => ({ + ...acc, + [category]: { + name: category, + enabled: true, + errors: null, + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + }), + {}, + ); + + const successCategoryMap = categories.reduce( + (acc, category) => ({ + ...acc, + [category]: { + errors: null, + pendingAction: null, + }, + }), + {}, + ); + + const failureCategoryMap = categories.reduce( + (acc, category) => ({ + ...acc, + [category]: { + errors: ErrorUtils.getMicroSecondOnyxError('workspace.categories.createFailureMessage'), + pendingAction: null, + }, + }), + {}, + ); + + const onyxData: OnyxData = { + optimisticData: [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`, + value: optimisticCategoryMap, + }, + ], + successData: [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`, + value: successCategoryMap, + }, + ], + failureData: [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`, + value: failureCategoryMap, + }, + ], + }; + + return onyxData; +} + /** * Optimistically creates a new workspace and default workspace chats * @@ -2083,7 +2146,7 @@ function createDraftInitialWorkspace(policyOwnerEmail = '', policyName = '', pol * @param [policyName] custom policy name we will use for created workspace * @param [policyID] custom policy id we will use for created workspace */ -function createWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policyName = '', policyID = generatePolicyID()): string { +function createWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policyName = '', policyID = generatePolicyID()): CreateWorkspaceParams { const workspaceName = policyName || generateDefaultWorkspaceName(policyOwnerEmail); const {customUnits, customUnitID, customUnitRateID, outputCurrency} = buildOptimisticCustomUnits(); @@ -2103,6 +2166,8 @@ function createWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policyName expenseCreatedReportActionID, } = ReportUtils.buildOptimisticWorkspaceChats(policyID, workspaceName); + const optimisticCategoriesData = buildOptimisticPolicyCategories(policyID, CONST.POLICY.DEFAULT_CATEGORIES); + const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.SET, @@ -2293,6 +2358,18 @@ function createWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policyName }, ]; + if (optimisticCategoriesData.optimisticData) { + optimisticData.push(...optimisticCategoriesData.optimisticData); + } + + if (optimisticCategoriesData.failureData) { + failureData.push(...optimisticCategoriesData.failureData); + } + + if (optimisticCategoriesData.successData) { + successData.push(...optimisticCategoriesData.successData); + } + const params: CreateWorkspaceParams = { policyID, announceChatReportID, @@ -2311,7 +2388,7 @@ function createWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policyName API.write(WRITE_COMMANDS.CREATE_WORKSPACE, params, {optimisticData, successData, failureData}); - return adminsChatReportID; + return params; } function openWorkspaceReimburseView(policyID: string) { @@ -3085,46 +3162,7 @@ function setWorkspaceCategoryEnabled(policyID: string, categoriesToUpdate: Recor } function createPolicyCategory(policyID: string, categoryName: string) { - const onyxData: OnyxData = { - optimisticData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`, - value: { - [categoryName]: { - name: categoryName, - enabled: true, - errors: null, - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, - }, - }, - ], - successData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`, - value: { - [categoryName]: { - errors: null, - pendingAction: null, - }, - }, - }, - ], - failureData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`, - value: { - [categoryName]: { - errors: ErrorUtils.getMicroSecondOnyxError('workspace.categories.createFailureMessage'), - pendingAction: null, - }, - }, - }, - ], - }; + const onyxData = buildOptimisticPolicyCategories(policyID, [categoryName]); const parameters = { policyID, diff --git a/src/pages/OnboardingWork/BaseOnboardingWork.tsx b/src/pages/OnboardingWork/BaseOnboardingWork.tsx index 1236a8178796..151c1bb35ea2 100644 --- a/src/pages/OnboardingWork/BaseOnboardingWork.tsx +++ b/src/pages/OnboardingWork/BaseOnboardingWork.tsx @@ -43,7 +43,7 @@ function BaseOnboardingWork({currentUserPersonalDetails, shouldUseNativeStyles, const work = values.work.trim(); - const adminsChatReportID = Policy.createWorkspace(undefined, true, work); + const {adminsChatReportID} = Policy.createWorkspace(undefined, true, work); Report.completeOnboarding( onboardingPurposeSelected, diff --git a/src/pages/iou/request/MoneyTemporaryForRefactorRequestParticipantsSelector.js b/src/pages/iou/request/MoneyTemporaryForRefactorRequestParticipantsSelector.js index 762198090467..6ca810438af2 100644 --- a/src/pages/iou/request/MoneyTemporaryForRefactorRequestParticipantsSelector.js +++ b/src/pages/iou/request/MoneyTemporaryForRefactorRequestParticipantsSelector.js @@ -9,11 +9,8 @@ import lodashValues from 'lodash/values'; import PropTypes from 'prop-types'; import React, {memo, useCallback, useEffect, useMemo} from 'react'; import {useOnyx} from 'react-native-onyx'; -import BlockingView from '@components/BlockingViews/BlockingView'; import Button from '@components/Button'; import FormHelpMessage from '@components/FormHelpMessage'; -import * as Illustrations from '@components/Icon/Illustrations'; -import OfflineIndicator from '@components/OfflineIndicator'; import {usePersonalDetails} from '@components/OnyxProvider'; import {useOptionsList} from '@components/OptionListContextProvider'; import ReferralProgramCTA from '@components/ReferralProgramCTA'; @@ -26,15 +23,12 @@ import useNetwork from '@hooks/useNetwork'; import usePermissions from '@hooks/usePermissions'; import useScreenWrapperTranstionStatus from '@hooks/useScreenWrapperTransitionStatus'; import useThemeStyles from '@hooks/useThemeStyles'; -import useWindowDimensions from '@hooks/useWindowDimensions'; import * as DeviceCapabilities from '@libs/DeviceCapabilities'; -import Navigation from '@libs/Navigation/Navigation'; import * as OptionsListUtils from '@libs/OptionsListUtils'; -import variables from '@styles/variables'; +import * as Policy from '@userActions/Policy'; import * as Report from '@userActions/Report'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import ROUTES from '@src/ROUTES'; const propTypes = { /** Callback to request parent modal to go to next step, which should be split */ @@ -87,7 +81,6 @@ function MoneyTemporaryForRefactorRequestParticipantsSelector({participants, onF const offlineMessage = isOffline ? [`${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}`, {isTranslated: true}] : ''; const maxParticipantsReached = participants.length === CONST.REPORT.MAXIMUM_PARTICIPANTS; - const {isSmallScreenWidth} = useWindowDimensions(); const isIOUSplit = iouType === CONST.IOU.TYPE.SPLIT; const isCategorizeOrShareAction = [CONST.IOU.ACTION.CATEGORIZE, CONST.IOU.ACTION.SHARE].includes(action); @@ -335,30 +328,20 @@ function MoneyTemporaryForRefactorRequestParticipantsSelector({participants, onF ); }, [handleConfirmSelection, participants.length, isDismissed, referralContentType, shouldShowSplitBillErrorMessage, styles, translate]); - const renderEmptyWorkspaceView = () => ( - <> - -