Skip to content

Commit

Permalink
Merge pull request #52269 from shubham1206agra/per-diem-3
Browse files Browse the repository at this point in the history
Feature: Per Diem Rates Settings Page
  • Loading branch information
Gonals authored Nov 22, 2024
2 parents 2b31276 + 0e677a7 commit dfa69e6
Show file tree
Hide file tree
Showing 21 changed files with 166 additions and 37 deletions.
4 changes: 4 additions & 0 deletions src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,10 @@ const ROUTES = {
route: 'settings/workspaces/:policyID/per-diem',
getRoute: (policyID: string) => `settings/workspaces/${policyID}/per-diem` as const,
},
WORKSPACE_PER_DIEM_SETTINGS: {
route: 'settings/workspaces/:policyID/per-diem/settings',
getRoute: (policyID: string) => `settings/workspaces/${policyID}/per-diem/settings` as const,
},
RULES_CUSTOM_NAME: {
route: 'settings/workspaces/:policyID/rules/name',
getRoute: (policyID: string) => `settings/workspaces/${policyID}/rules/name` as const,
Expand Down
1 change: 1 addition & 0 deletions src/SCREENS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ const SCREENS = {
RULES_MAX_EXPENSE_AGE: 'Rules_Max_Expense_Age',
RULES_BILLABLE_DEFAULT: 'Rules_Billable_Default',
PER_DIEM: 'Per_Diem',
PER_DIEM_SETTINGS: 'Per_Diem_Settings',
},

EDIT_REQUEST: {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2526,6 +2526,7 @@ const translations = {
return 'Member';
}
},
defaultCategory: 'Default category',
},
perDiem: {
subtitle: 'Set per diem rates to control daily employee spend. ',
Expand Down Expand Up @@ -4091,7 +4092,6 @@ const translations = {
unit: 'Unit',
taxFeatureNotEnabledMessage: 'Taxes must be enabled on the workspace to use this feature. Head over to ',
changePromptMessage: ' to make that change.',
defaultCategory: 'Default category',
deleteDistanceRate: 'Delete distance rate',
areYouSureDelete: () => ({
one: 'Are you sure you want to delete this rate?',
Expand Down
2 changes: 1 addition & 1 deletion src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,7 @@ const translations = {
return 'Miembro';
}
},
defaultCategory: 'Categoría predeterminada',
},
perDiem: {
subtitle: 'Establece las tasas per diem para controlar los gastos diarios de los empleados. ',
Expand Down Expand Up @@ -4136,7 +4137,6 @@ const translations = {
unit: 'Unidad',
taxFeatureNotEnabledMessage: 'Los impuestos deben estar activados en el área de trabajo para poder utilizar esta función. Dirígete a ',
changePromptMessage: ' para hacer ese cambio.',
defaultCategory: 'Categoría predeterminada',
deleteDistanceRate: 'Eliminar tasa de distancia',
areYouSureDelete: () => ({
one: '¿Estás seguro de que quieres eliminar esta tasa?',
Expand Down
6 changes: 6 additions & 0 deletions src/libs/API/parameters/EnableDistanceRequestTaxParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type EnableDistanceRequestTaxParams = {
policyID: string;
customUnit: string;
};

export default EnableDistanceRequestTaxParams;
7 changes: 7 additions & 0 deletions src/libs/API/parameters/SetCustomUnitDefaultCategoryParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type SetCustomUnitDefaultCategoryParams = {
policyID: string;
customUnitID: string;
category: string;
};

export default SetCustomUnitDefaultCategoryParams;

This file was deleted.

3 changes: 2 additions & 1 deletion src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ export type {default as EnablePolicyTaxesParams} from './EnablePolicyTaxesParams
export type {default as OpenPolicyMoreFeaturesPageParams} from './OpenPolicyMoreFeaturesPageParams';
export type {default as CreatePolicyDistanceRateParams} from './CreatePolicyDistanceRateParams';
export type {default as SetPolicyDistanceRatesUnitParams} from './SetPolicyDistanceRatesUnitParams';
export type {default as SetPolicyDistanceRatesDefaultCategoryParams} from './SetPolicyDistanceRatesDefaultCategoryParams';
export type {default as EnableDistanceRequestTaxParams} from './EnableDistanceRequestTaxParams';
export type {default as SetCustomUnitDefaultCategoryParams} from './SetCustomUnitDefaultCategoryParams';
export type {default as UpdatePolicyDistanceRateValueParams} from './UpdatePolicyDistanceRateValueParams';
export type {default as SetPolicyDistanceRatesEnabledParams} from './SetPolicyDistanceRatesEnabledParams';
export type {default as DeletePolicyDistanceRatesParams} from './DeletePolicyDistanceRatesParams';
Expand Down
6 changes: 3 additions & 3 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ const WRITE_COMMANDS = {
REQUEST_WORKSPACE_OWNER_CHANGE: 'RequestWorkspaceOwnerChange',
ADD_BILLING_CARD_AND_REQUEST_WORKSPACE_OWNER_CHANGE: 'AddBillingCardAndRequestPolicyOwnerChange',
SET_POLICY_DISTANCE_RATES_UNIT: 'SetPolicyDistanceRatesUnit',
SET_POLICY_DISTANCE_RATES_DEFAULT_CATEGORY: 'SetPolicyDistanceRatesDefaultCategory',
SET_CUSTOM_UNIT_DEFAULT_CATEGORY: 'SetCustomUnitDefaultCategory',
ENABLE_DISTANCE_REQUEST_TAX: 'EnableDistanceRequestTax',
UPDATE_POLICY_DISTANCE_RATE_VALUE: 'UpdatePolicyDistanceRateValue',
UPDATE_POLICY_DISTANCE_TAX_RATE_VALUE: 'UpdateDistanceTaxRate',
Expand Down Expand Up @@ -688,8 +688,8 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.RENAME_POLICY_TAX]: Parameters.RenamePolicyTaxParams;
[WRITE_COMMANDS.UPDATE_POLICY_TAX_CODE]: Parameters.UpdatePolicyTaxCodeParams;
[WRITE_COMMANDS.SET_POLICY_DISTANCE_RATES_UNIT]: Parameters.SetPolicyDistanceRatesUnitParams;
[WRITE_COMMANDS.SET_POLICY_DISTANCE_RATES_DEFAULT_CATEGORY]: Parameters.SetPolicyDistanceRatesDefaultCategoryParams;
[WRITE_COMMANDS.ENABLE_DISTANCE_REQUEST_TAX]: Parameters.SetPolicyDistanceRatesDefaultCategoryParams;
[WRITE_COMMANDS.SET_CUSTOM_UNIT_DEFAULT_CATEGORY]: Parameters.SetCustomUnitDefaultCategoryParams;
[WRITE_COMMANDS.ENABLE_DISTANCE_REQUEST_TAX]: Parameters.EnableDistanceRequestTaxParams;
[WRITE_COMMANDS.REPORT_EXPORT]: Parameters.ReportExportParams;
[WRITE_COMMANDS.MARK_AS_EXPORTED]: Parameters.MarkAsExportedParams;
[WRITE_COMMANDS.REQUEST_EXPENSIFY_CARD_LIMIT_INCREASE]: Parameters.RequestExpensifyCardLimitIncreaseParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ const SettingsModalStackNavigator = createModalStackNavigator<SettingsNavigatorP
[SCREENS.WORKSPACE.RULES_MAX_EXPENSE_AMOUNT]: () => require<ReactComponentModule>('../../../../pages/workspace/rules/RulesMaxExpenseAmountPage').default,
[SCREENS.WORKSPACE.RULES_MAX_EXPENSE_AGE]: () => require<ReactComponentModule>('../../../../pages/workspace/rules/RulesMaxExpenseAgePage').default,
[SCREENS.WORKSPACE.RULES_BILLABLE_DEFAULT]: () => require<ReactComponentModule>('../../../../pages/workspace/rules/RulesBillableDefaultPage').default,
[SCREENS.WORKSPACE.PER_DIEM_SETTINGS]: () => require<ReactComponentModule>('../../../../pages/workspace/perDiem/WorkspacePerDiemSettingsPage').default,
});

const EnablePaymentsStackNavigator = createModalStackNavigator<EnablePaymentsNavigatorParamList>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ const FULL_SCREEN_TO_RHP_MAPPING: Partial<Record<FullScreenName, string[]>> = {
SCREENS.WORKSPACE.RULES_MAX_EXPENSE_AGE,
SCREENS.WORKSPACE.RULES_BILLABLE_DEFAULT,
],
[SCREENS.WORKSPACE.PER_DIEM]: [SCREENS.WORKSPACE.PER_DIEM_SETTINGS],
};

export default FULL_SCREEN_TO_RHP_MAPPING;
3 changes: 3 additions & 0 deletions src/libs/Navigation/linkingConfig/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,9 @@ const config: LinkingOptions<RootStackParamList>['config'] = {
[SCREENS.WORKSPACE.RULES_BILLABLE_DEFAULT]: {
path: ROUTES.RULES_BILLABLE_DEFAULT.route,
},
[SCREENS.WORKSPACE.PER_DIEM_SETTINGS]: {
path: ROUTES.WORKSPACE_PER_DIEM_SETTINGS.route,
},
},
},
[SCREENS.RIGHT_MODAL.PRIVATE_NOTES]: {
Expand Down
3 changes: 3 additions & 0 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,9 @@ type SettingsNavigatorParamList = {
[SCREENS.WORKSPACE.RULES_BILLABLE_DEFAULT]: {
policyID: string;
};
[SCREENS.WORKSPACE.PER_DIEM_SETTINGS]: {
policyID: string;
};
} & ReimbursementAccountNavigatorParamList;

type NewChatNavigatorParamList = {
Expand Down
26 changes: 13 additions & 13 deletions src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
SetPolicyCategoryMaxAmountParams,
SetPolicyCategoryReceiptsRequiredParams,
SetPolicyCategoryTaxParams,
SetPolicyDistanceRatesDefaultCategoryParams,
SetWorkspaceCategoryDescriptionHintParams,
UpdatePolicyCategoryGLCodeParams,
} from '@libs/API/parameters';
Expand All @@ -28,13 +27,13 @@ import {translateLocal} from '@libs/Localize';
import Log from '@libs/Log';
import enhanceParameters from '@libs/Network/enhanceParameters';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import {navigateWhenEnableFeature, removePendingFieldsFromCustomUnit} from '@libs/PolicyUtils';
import {navigateWhenEnableFeature} from '@libs/PolicyUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, PolicyCategories, PolicyCategory, RecentlyUsedCategories, Report} from '@src/types/onyx';
import type {ApprovalRule, CustomUnit, ExpenseRule} from '@src/types/onyx/Policy';
import type {ApprovalRule, ExpenseRule} from '@src/types/onyx/Policy';
import type {PolicyCategoryExpenseLimitType} from '@src/types/onyx/PolicyCategory';
import type {OnyxData} from '@src/types/onyx/Request';

Expand Down Expand Up @@ -1015,15 +1014,15 @@ function enablePolicyCategories(policyID: string, enabled: boolean) {
}
}

function setPolicyDistanceRatesDefaultCategory(policyID: string, currentCustomUnit: CustomUnit, newCustomUnit: CustomUnit) {
function setPolicyCustomUnitDefaultCategory(policyID: string, customUnitID: string, oldCategory: string | undefined, category: string) {
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
customUnits: {
[newCustomUnit.customUnitID]: {
...newCustomUnit,
[customUnitID]: {
defaultCategory: category,
pendingFields: {defaultCategory: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE},
},
},
Expand All @@ -1037,7 +1036,7 @@ function setPolicyDistanceRatesDefaultCategory(policyID: string, currentCustomUn
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
customUnits: {
[newCustomUnit.customUnitID]: {
[customUnitID]: {
pendingFields: {defaultCategory: null},
},
},
Expand All @@ -1051,8 +1050,8 @@ function setPolicyDistanceRatesDefaultCategory(policyID: string, currentCustomUn
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
customUnits: {
[currentCustomUnit.customUnitID]: {
...currentCustomUnit,
[customUnitID]: {
defaultCategory: oldCategory,
errorFields: {defaultCategory: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage')},
pendingFields: {defaultCategory: null},
},
Expand All @@ -1061,12 +1060,13 @@ function setPolicyDistanceRatesDefaultCategory(policyID: string, currentCustomUn
},
];

const params: SetPolicyDistanceRatesDefaultCategoryParams = {
const params = {
policyID,
customUnit: JSON.stringify(removePendingFieldsFromCustomUnit(newCustomUnit)),
customUnitID,
category,
};

API.write(WRITE_COMMANDS.SET_POLICY_DISTANCE_RATES_DEFAULT_CATEGORY, params, {optimisticData, successData, failureData});
API.write(WRITE_COMMANDS.SET_CUSTOM_UNIT_DEFAULT_CATEGORY, params, {optimisticData, successData, failureData});
}

function downloadCategoriesCSV(policyID: string, onDownloadFailed: () => void) {
Expand Down Expand Up @@ -1364,7 +1364,7 @@ export {
setPolicyCategoryGLCode,
clearCategoryErrors,
enablePolicyCategories,
setPolicyDistanceRatesDefaultCategory,
setPolicyCustomUnitDefaultCategory,
deleteWorkspaceCategories,
buildOptimisticPolicyCategories,
setPolicyCategoryReceiptsRequired,
Expand Down
13 changes: 12 additions & 1 deletion src/libs/actions/Policy/PerDiem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, Report} from '@src/types/onyx';
import type {ErrorFields} from '@src/types/onyx/OnyxCommon';
import type {OnyxData} from '@src/types/onyx/Request';

const allPolicies: OnyxCollection<Policy> = {};
Expand Down Expand Up @@ -119,4 +120,14 @@ function openPolicyPerDiemPage(policyID?: string) {
API.read(READ_COMMANDS.OPEN_POLICY_PER_DIEM_RATES_PAGE, params);
}

export {enablePerDiem, openPolicyPerDiemPage};
function clearPolicyPerDiemRatesErrorFields(policyID: string, customUnitID: string, updatedErrorFields: ErrorFields) {
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {
customUnits: {
[customUnitID]: {
errorFields: updatedErrorFields,
},
},
});
}

export {enablePerDiem, openPolicyPerDiemPage, clearPolicyPerDiemRatesErrorFields};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useMemo, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import CategorySelectorModal from '@components/CategorySelector/CategorySelectorModal';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionList';
Expand All @@ -12,7 +13,6 @@ import Navigation from '@libs/Navigation/Navigation';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import CategorySelectorModal from '@pages/workspace/distanceRates/CategorySelector/CategorySelectorModal';
import type {WithPolicyConnectionsProps} from '@pages/workspace/withPolicyConnections';
import withPolicyConnections from '@pages/workspace/withPolicyConnections';
import ToggleSettingOptionRow from '@pages/workspace/workflows/ToggleSettingsOptionRow';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, {useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import FullPageOfflineBlockingView from '@components/BlockingViews/FullPageOfflineBlockingView';
import CategorySelector from '@components/CategorySelector';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import ScreenWrapper from '@components/ScreenWrapper';
Expand All @@ -28,7 +29,6 @@ import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {CustomUnit} from '@src/types/onyx/Policy';
import CategorySelector from './CategorySelector';
import UnitSelector from './UnitSelector';

type PolicyDistanceRatesSettingsPageProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.DISTANCE_RATES_SETTINGS>;
Expand Down Expand Up @@ -61,14 +61,11 @@ function PolicyDistanceRatesSettingsPage({route}: PolicyDistanceRatesSettingsPag
};

const setNewCategory = (category: ListItem) => {
if (!category.searchText || !customUnit) {
if (!category.searchText || !customUnit || defaultCategory === category.searchText) {
return;
}

Category.setPolicyDistanceRatesDefaultCategory(policyID, customUnit, {
...customUnit,
defaultCategory: defaultCategory === category.searchText ? '' : category.searchText,
});
Category.setPolicyCustomUnitDefaultCategory(policyID, customUnitID, customUnit.defaultCategory, category.searchText);
};

const clearErrorFields = (fieldName: keyof CustomUnit) => {
Expand Down Expand Up @@ -125,7 +122,7 @@ function PolicyDistanceRatesSettingsPage({route}: PolicyDistanceRatesSettingsPag
>
<CategorySelector
policyID={policyID}
label={translate('workspace.distanceRates.defaultCategory')}
label={translate('workspace.common.defaultCategory')}
defaultValue={defaultCategory}
wrapperStyle={[styles.ph5, styles.mt3]}
setNewCategory={setNewCategory}
Expand Down
5 changes: 2 additions & 3 deletions src/pages/workspace/perDiem/WorkspacePerDiemPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import * as Link from '@userActions/Link';
import * as Modal from '@userActions/Modal';
import * as PerDiem from '@userActions/Policy/PerDiem';
import CONST from '@src/CONST';
// import ROUTES from '@src/ROUTES';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {PendingAction} from '@src/types/onyx/OnyxCommon';
import type {Rate} from '@src/types/onyx/Policy';
Expand Down Expand Up @@ -216,8 +216,7 @@ function WorkspacePerDiemPage({route}: WorkspacePerDiemPageProps) {
);

const openSettings = () => {
// TODO: Uncomment this when the import feature is ready
// Navigation.navigate(ROUTES.WORKSPACE_PER_DIEM_RATES_SETTINGS.getRoute(policyID));
Navigation.navigate(ROUTES.WORKSPACE_PER_DIEM_SETTINGS.getRoute(policyID));
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
Loading

0 comments on commit dfa69e6

Please sign in to comment.