Skip to content

Commit

Permalink
Fix conditional rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuuszzzzz committed Dec 22, 2023
1 parent c3378bd commit 2371230
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
4 changes: 1 addition & 3 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ function getPolicyBrickRoadIndicatorStatus(policy: OnyxEntry<Policy>, policyMemb
*/
function shouldShowPolicy(policy: OnyxEntry<Policy>, isOffline: boolean): boolean {
return (
!!policy &&
policy?.isPolicyExpenseChatEnabled &&
(isOffline || policy?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || Object.keys(policy.errors ?? {}).length > 0)
!!policy && policy?.isPolicyExpenseChatEnabled && (isOffline || policy?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || Object.keys(policy.errors ?? {}).length > 0)
);
}

Expand Down
11 changes: 5 additions & 6 deletions src/libs/WorkspacesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ function getWorkspacesBrickRoads(): Record<string, BrickRoad> {
* @returns a map where the keys are policyIDs and the values are truthy booleans if policy has unread content
*/
function getWorkspacesUnreadStatuses(): Record<string, boolean> {
if(!allReports) {
if (!allReports) {
return {};
}

const workspacesUnreadStatuses: Record<string, boolean> = {}
const workspacesUnreadStatuses: Record<string, boolean> = {};

Object.keys(allReports).forEach((report) => {
const policyID = allReports?.[report]?.policyID;
Expand All @@ -88,13 +88,12 @@ function getWorkspacesUnreadStatuses(): Record<string, boolean> {

const unreadStatus = ReportUtils.isUnread(policyReport);

if(unreadStatus) {
if (unreadStatus) {
workspacesUnreadStatuses[policyID] = true;
}
else {
} else {
workspacesUnreadStatuses[policyID] = false;
}
})
});

return workspacesUnreadStatuses;
}
Expand Down
50 changes: 29 additions & 21 deletions src/pages/WorkspaceSwitcherPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {getWorkspacesBrickRoads, getWorkspacesUnreadStatuses} from '@libs/WorkspacesUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import {getWorkspacesBrickRoads, getWorkspacesUnreadStatuses} from '@libs/WorkspacesUtils';
import * as App from '@userActions/App';
import * as Policy from '@userActions/Policy';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import SCREENS from '@src/SCREENS';
import ROUTES from '@src/ROUTES';
import * as App from '@userActions/App';
import Navigation from '@libs/Navigation/Navigation';
import SCREENS from '@src/SCREENS';
import WorkspaceCardCreateAWorkspace from './workspace/card/WorkspaceCardCreateAWorkspace';

const propTypes = {
Expand Down Expand Up @@ -71,31 +71,34 @@ function WorkspaceSwitcherPage({policies, activeWorkspaceID}) {
const brickRoadsForPolicies = useMemo(() => getWorkspacesBrickRoads(), []);
const unreadStatusesForPolicies = useMemo(() => getWorkspacesUnreadStatuses(), []);

const getIndicatorTypeForPolicy = useCallback((policyId) => {
if (policyId && policyId !== activeWorkspaceID) {
return brickRoadsForPolicies[policyId];
}
const getIndicatorTypeForPolicy = useCallback(
(policyId) => {
if (policyId && policyId !== activeWorkspaceID) {
return brickRoadsForPolicies[policyId];
}

if(_.values(brickRoadsForPolicies).includes(CONST.BRICK_ROAD.RBR)) {
return CONST.BRICK_ROAD.RBR;
}
if (_.values(brickRoadsForPolicies).includes(CONST.BRICK_ROAD.RBR)) {
return CONST.BRICK_ROAD.RBR;
}

if(_.values(brickRoadsForPolicies).includes(CONST.BRICK_ROAD.GBR)) {
return CONST.BRICK_ROAD.GBR;
}
if (_.values(brickRoadsForPolicies).includes(CONST.BRICK_ROAD.GBR)) {
return CONST.BRICK_ROAD.GBR;
}

return undefined
}, [activeWorkspaceID, brickRoadsForPolicies]);
return undefined;
},
[activeWorkspaceID, brickRoadsForPolicies],
);

const hasUnreadData = useCallback(
// TO DO: Implement checking if policy has some unread data
// eslint-disable-next-line no-unused-vars
(policyId) => {
if(policyId) {
if (policyId) {
return unreadStatusesForPolicies[policyId];
}
return _.some(_.values(unreadStatusesForPolicies), (status) => status)

return _.some(_.values(unreadStatusesForPolicies), (status) => status);
},
[unreadStatusesForPolicies],
);
Expand Down Expand Up @@ -214,7 +217,12 @@ function WorkspaceSwitcherPage({policies, activeWorkspaceID}) {
{translate('common.workspaces')}
</Text>
</View>
<PressableWithFeedback role={CONST.ROLE.BUTTON} onPress={() => {App.createWorkspaceWithPolicyDraftAndNavigateToIt()}}>
<PressableWithFeedback
role={CONST.ROLE.BUTTON}
onPress={() => {
App.createWorkspaceWithPolicyDraftAndNavigateToIt();
}}
>
{({hovered}) => (
<Icon
src={Expensicons.Plus}
Expand All @@ -231,7 +239,7 @@ function WorkspaceSwitcherPage({policies, activeWorkspaceID}) {
ref={inputCallbackRef}
sections={[usersWorkspacesSectionData]}
value={searchTerm}
shouldShowTextInput={usersWorkspaces.length < MINIMUM_WORKSPACES_TO_SHOW_SEARCH}
shouldShowTextInput={usersWorkspaces.length >= MINIMUM_WORKSPACES_TO_SHOW_SEARCH}
onChangeText={onChangeText}
selectedOptions={selectedOption ? [selectedOption] : []}
onSelectRow={selectPolicy}
Expand Down
4 changes: 3 additions & 1 deletion src/pages/workspace/card/WorkspaceCardCreateAWorkspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ function WorkspaceCardCreateAWorkspace() {
containerStyles={[styles.highlightBG]}
>
<Button
onPress={() => {App.createWorkspaceWithPolicyDraftAndNavigateToIt()}}
onPress={() => {
App.createWorkspaceWithPolicyDraftAndNavigateToIt();
}}
text={translate('workspace.emptyWorkspace.createAWorkspaceCTA')}
style={styles.mt5}
success
Expand Down

0 comments on commit 2371230

Please sign in to comment.