Skip to content

Commit

Permalink
Merge pull request #41990 from cretadn22/dont-create-multiple-policy
Browse files Browse the repository at this point in the history
  • Loading branch information
francoisl authored May 15, 2024
2 parents e83f79c + 5f8e897 commit 82bacc9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ const ONYXKEYS = {
/** Onboarding Purpose selected by the user during Onboarding flow */
ONBOARDING_PURPOSE_SELECTED: 'onboardingPurposeSelected',

/** Onboarding policyID selected by the user during Onboarding flow */
ONBOARDING_POLICY_ID: 'onboardingPolicyID',

/** Onboarding Purpose selected by the user during Onboarding flow */
ONBOARDING_ADMINS_CHAT_REPORT_ID: 'onboardingAdminsChatReportID',

Expand Down Expand Up @@ -664,6 +667,7 @@ type OnyxValuesMapping = {
[ONYXKEYS.MAX_CANVAS_HEIGHT]: number;
[ONYXKEYS.MAX_CANVAS_WIDTH]: number;
[ONYXKEYS.ONBOARDING_PURPOSE_SELECTED]: string;
[ONYXKEYS.ONBOARDING_POLICY_ID]: string;
[ONYXKEYS.ONBOARDING_ADMINS_CHAT_REPORT_ID]: string;
[ONYXKEYS.IS_SEARCHING_FOR_REPORTS]: boolean;
[ONYXKEYS.LAST_VISITED_PATH]: string | undefined;
Expand Down
3 changes: 2 additions & 1 deletion src/libs/actions/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1611,10 +1611,11 @@ function clearAvatarErrors(policyID: string) {
* Optimistically update the general settings. Set the general settings as pending until the response succeeds.
* If the response fails set a general error message. Clear the error message when updating.
*/
function updateGeneralSettings(policyID: string, name: string, currency: string) {
function updateGeneralSettings(policyID: string, name: string, currencyValue?: string) {
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
const distanceUnit = Object.values(policy?.customUnits ?? {}).find((unit) => unit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE);
const customUnitID = distanceUnit?.customUnitID;
const currency = currencyValue ?? policy?.outputCurrency ?? CONST.CURRENCY.USD;

if (!policy) {
return;
Expand Down
6 changes: 5 additions & 1 deletion src/libs/actions/Welcome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ function setOnboardingAdminsChatReportID(adminsChatReportID?: string) {
Onyx.set(ONYXKEYS.ONBOARDING_ADMINS_CHAT_REPORT_ID, adminsChatReportID ?? null);
}

function setOnboardingPolicyID(policyID?: string) {
Onyx.set(ONYXKEYS.ONBOARDING_POLICY_ID, policyID ?? null);
}

Onyx.connect({
key: ONYXKEYS.NVP_ONBOARDING,
initWithStoredValues: false,
Expand Down Expand Up @@ -134,4 +138,4 @@ function resetAllChecks() {
isLoadingReportData = true;
}

export {onServerDataReady, isOnboardingFlowCompleted, setOnboardingPurposeSelected, resetAllChecks, setOnboardingAdminsChatReportID};
export {onServerDataReady, isOnboardingFlowCompleted, setOnboardingPurposeSelected, resetAllChecks, setOnboardingAdminsChatReportID, setOnboardingPolicyID};
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function BaseOnboardingPersonalDetails({currentUserPersonalDetails, shouldUseNat
);

Welcome.setOnboardingAdminsChatReportID();
Welcome.setOnboardingPolicyID();

Navigation.dismissModal();

Expand Down
17 changes: 13 additions & 4 deletions src/pages/OnboardingWork/BaseOnboardingWork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type {BaseOnboardingWorkOnyxProps, BaseOnboardingWorkProps} from './types

const OPEN_WORK_PAGE_PURPOSES = [CONST.ONBOARDING_CHOICES.MANAGE_TEAM];

function BaseOnboardingWork({shouldUseNativeStyles, onboardingPurposeSelected}: BaseOnboardingWorkProps) {
function BaseOnboardingWork({shouldUseNativeStyles, onboardingPurposeSelected, onboardingPolicyID}: BaseOnboardingWorkProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {isSmallScreenWidth} = useWindowDimensions();
Expand All @@ -41,11 +41,17 @@ function BaseOnboardingWork({shouldUseNativeStyles, onboardingPurposeSelected}:
return;
}
const work = values.work.trim();
const {adminsChatReportID} = Policy.createWorkspace(undefined, true, work);
Welcome.setOnboardingAdminsChatReportID(adminsChatReportID);
if (!onboardingPolicyID) {
const {adminsChatReportID, policyID} = Policy.createWorkspace(undefined, true, work);
Welcome.setOnboardingAdminsChatReportID(adminsChatReportID);
Welcome.setOnboardingPolicyID(policyID);
} else {
Policy.updateGeneralSettings(onboardingPolicyID, work);
}

Navigation.navigate(ROUTES.ONBOARDING_PERSONAL_DETAILS);
},
[onboardingPurposeSelected],
[onboardingPurposeSelected, onboardingPolicyID],
);

const validate = (values: FormOnyxValues<'onboardingWorkForm'>) => {
Expand Down Expand Up @@ -118,4 +124,7 @@ export default withOnyx<BaseOnboardingWorkProps, BaseOnboardingWorkOnyxProps>({
onboardingPurposeSelected: {
key: ONYXKEYS.ONBOARDING_PURPOSE_SELECTED,
},
onboardingPolicyID: {
key: ONYXKEYS.ONBOARDING_POLICY_ID,
},
})(BaseOnboardingWork);
3 changes: 3 additions & 0 deletions src/pages/OnboardingWork/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ type OnboardingWorkProps = Record<string, unknown>;
type BaseOnboardingWorkOnyxProps = {
/** Saved onboarding purpose selected by the user */
onboardingPurposeSelected: OnyxEntry<OnboardingPurposeType>;

/** Saved onboarding purpose selected by the user */
onboardingPolicyID: OnyxEntry<string>;
};

type BaseOnboardingWorkProps = BaseOnboardingWorkOnyxProps & {
Expand Down

0 comments on commit 82bacc9

Please sign in to comment.