Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
e062817
Wrap unit picker in OfflineWithFeedback
amyevans Aug 10, 2022
ec308aa
Create new function that calls new API command
amyevans Aug 11, 2022
8379bff
Merge branch 'main' into amy-custom-unit
amyevans Aug 12, 2022
70853d4
Update data structure to be consistent
amyevans Aug 12, 2022
cebeae2
Fix old data structure references
amyevans Aug 12, 2022
a815699
Ensure object key is lowercase
amyevans Aug 12, 2022
639be34
Match data shape for customUnits when app loads
amyevans Aug 12, 2022
fe61de9
Fix remaining references for new data structure
amyevans Aug 12, 2022
005c288
Add removeUnitError function
amyevans Aug 12, 2022
029b4e5
Fix reference to policyID to properly remove error
amyevans Aug 12, 2022
aa5994f
Don't call the API if the values are the same
amyevans Aug 15, 2022
ef2284f
Add error handling for offline
amyevans Aug 15, 2022
0ef9adc
Match the backend by using microseconds
amyevans Aug 15, 2022
8d509b6
Clear any existing errors if we get success back
amyevans Aug 15, 2022
7e50489
Merge branch 'main' into amy-custom-unit
amyevans Aug 15, 2022
2f09607
Add red dot to Avatar component if errors present
amyevans Aug 15, 2022
58ccd81
Define policies prop types
amyevans Aug 15, 2022
7766095
Add indicator to Settings menu
amyevans Aug 15, 2022
016c1a6
Add error dot to Reimburse expenses menu item
amyevans Aug 15, 2022
af1393b
Move the wrapping component up a level for proper alignment
amyevans Aug 15, 2022
0044394
Add some padding for when it runs long
amyevans Aug 15, 2022
4d48174
Remove deprecated function
amyevans Aug 15, 2022
a556e6e
Ensure errors are cleared after success
amyevans Aug 15, 2022
2919f7e
Fix conflicts from merge / merge main
amyevans Aug 15, 2022
c25ff22
Merge branch 'main' into amy-custom-unit
amyevans Aug 18, 2022
923536e
Merge branch 'main' into amy-custom-unit
amyevans Aug 19, 2022
5732cbd
Fix props declaration
amyevans Aug 19, 2022
30155aa
Use custom unit name as key without lowercasing
amyevans Aug 19, 2022
7f832de
Update shape of customUnits in prop types
amyevans Aug 19, 2022
29c7b14
Use lodash helper function
amyevans Aug 19, 2022
d1d1c86
Remove assumption that distance is first key
amyevans Aug 19, 2022
b7726cb
Use constant value
amyevans Aug 19, 2022
534bce9
Don't bother to lowercase the object keys
amyevans Aug 19, 2022
7ab590c
Overwrite whatever values are passed
amyevans Aug 19, 2022
7278f76
Flip terms to increase speed
amyevans Aug 19, 2022
1d36698
Fix a terrible var name
amyevans Aug 19, 2022
8cf19a2
Localize the error string
amyevans Aug 19, 2022
3b74222
Simplify to update only one custom unit at a time
amyevans Aug 19, 2022
befc8de
Add Spanish translation
amyevans Aug 21, 2022
5268962
Merge branch 'main' into amy-custom-unit
amyevans Aug 22, 2022
678b8f0
Move custom unit data massaging from FE to API
amyevans Aug 23, 2022
0f1015f
Key by ID instead of name
amyevans Aug 23, 2022
9ad1e8c
Remove debug statements and fix JS style
amyevans Aug 23, 2022
196239c
Remove remaining instances of hardcoded Distance
amyevans Aug 23, 2022
4e662e5
Clear the pending action too on dismissal
amyevans Aug 23, 2022
dd24978
Merge branch 'main' into amy-custom-unit
amyevans Aug 23, 2022
a27c1c1
Merge branch 'main' into amy-custom-unit
amyevans Aug 24, 2022
6430b85
Fix prop type definition to remove warning
amyevans Aug 24, 2022
ba4f92a
Remove early return since calling this twice is intentional in Strict…
amyevans Aug 24, 2022
bbdfe91
Fix props for rates
amyevans Aug 24, 2022
6405894
Merge branch 'main' into amy-custom-unit
amyevans Aug 25, 2022
31f6cf5
Implement getDerivedStateFromProps to fix bug with unsynced UI betwee…
amyevans Aug 25, 2022
7cfec8e
Prevent crash if custom unit doesn't exist
amyevans Aug 25, 2022
61e40fa
Remove excess line
amyevans Aug 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/components/AvatarWithIndicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const propTypes = {
/** The employee list of all policies (coming from Onyx) */
policiesMemberList: PropTypes.objectOf(policyMemberPropType),

/** The list of this user's policies (coming from Onyx) */
policies: PropTypes.objectOf(PropTypes.shape({
/** The ID of the policy */
id: PropTypes.string,
})),

/** List of bank accounts */
bankAccountList: PropTypes.objectOf(bankAccountPropTypes),

Expand All @@ -41,6 +47,7 @@ const defaultProps = {
size: 'default',
tooltipText: '',
policiesMemberList: {},
policies: {},
bankAccountList: {},
cardList: {},
userWallet: {},
Expand All @@ -55,8 +62,11 @@ const AvatarWithIndicator = (props) => {
];

const hasPolicyMemberError = _.some(props.policiesMemberList, policyMembers => Policy.hasPolicyMemberError(policyMembers));
const hasCustomUnitsError = _.some(props.policies, policy => Policy.hasCustomUnitsError(policy));
const hasPaymentMethodError = PaymentMethods.hasPaymentMethodError(props.bankAccountList, props.cardList);
const hasWalletError = !_.isEmpty(props.userWallet.errors);
const shouldShowIndicator = hasPolicyMemberError || hasPaymentMethodError || hasWalletError || hasCustomUnitsError;

return (
<View style={[isLarge ? styles.avatarLarge : styles.sidebarAvatar]}>
<Tooltip text={props.tooltipText}>
Expand All @@ -65,7 +75,7 @@ const AvatarWithIndicator = (props) => {
source={props.source}
size={props.size}
/>
{(hasPolicyMemberError || hasPaymentMethodError || hasWalletError) && (
{shouldShowIndicator && (
<View style={StyleSheet.flatten(indicatorStyles)} />
)}
</Tooltip>
Expand All @@ -81,6 +91,9 @@ export default withOnyx({
policiesMemberList: {
key: ONYXKEYS.COLLECTION.POLICY_MEMBER_LIST,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
bankAccountList: {
key: ONYXKEYS.BANK_ACCOUNT_LIST,
},
Expand Down
1 change: 1 addition & 0 deletions src/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ export default {
captureNoVBACopyAfterEmail: ' and download the Expensify App to track cash expenses on the go.',
unlockNoVBACopy: 'Connect a bank account to reimburse your workspace members online.',
fastReimbursementsVBACopy: 'You\'re all set to reimburse receipts from your bank account!',
updateCustomUnitError: "Your changes couldn't be saved. The workspace was modified while you were offline, please try again.",
},
bills: {
manageYourBills: 'Manage your bills',
Expand Down
1 change: 1 addition & 0 deletions src/languages/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ export default {
captureNoVBACopyAfterEmail: ' y descarga la App de Expensify para controlar tus gastos en efectivo sobre la marcha.',
unlockNoVBACopy: 'Conecta una cuenta bancaria para reembolsar online a los miembros de tu espacio de trabajo.',
fastReimbursementsVBACopy: '¡Todo listo para reembolsar recibos desde tu cuenta bancaria!',
updateCustomUnitError: 'Los cambios no han podido ser guardados. El espacio de trabajo ha sido modificado mientras estabas desconectado, por favor inténtalo de nuevo.',
},
bills: {
manageYourBills: 'Gestiona tus facturas',
Expand Down
123 changes: 86 additions & 37 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from 'underscore';
import Onyx from 'react-native-onyx';
import lodashGet from 'lodash/get';
import * as DeprecatedAPI from '../deprecatedAPI';
import * as API from '../API';
import ONYXKEYS from '../../ONYXKEYS';
import * as PersonalDetails from './PersonalDetails';
import Growl from '../Growl';
Expand All @@ -13,6 +14,7 @@ import ROUTES from '../../ROUTES';
import * as OptionsListUtils from '../OptionsListUtils';
import * as Report from './Report';
import * as Pusher from '../Pusher/pusher';
import DateUtils from '../DateUtils';

const allPolicies = {};
Onyx.connect({
Expand Down Expand Up @@ -66,19 +68,6 @@ function getSimplifiedEmployeeList(employeeList) {
* @returns {Object}
*/
function getSimplifiedPolicyObject(fullPolicyOrPolicySummary, isFromFullPolicy) {
const customUnit = lodashGet(fullPolicyOrPolicySummary, 'value.customUnits[0]', undefined);
const customUnitValue = lodashGet(customUnit, 'attributes.unit', 'mi');
const customUnitRate = lodashGet(customUnit, 'rates[0]', {});
const customUnitSimplified = customUnit && {
id: customUnit.customUnitID,
name: customUnit.name,
value: customUnitValue,
rate: {
id: customUnitRate.customUnitRateID,
name: customUnitRate.name,
value: Number(customUnitRate.rate),
},
};
return {
isFromFullPolicy,
id: fullPolicyOrPolicySummary.id,
Expand All @@ -91,7 +80,7 @@ function getSimplifiedPolicyObject(fullPolicyOrPolicySummary, isFromFullPolicy)
// "GetFullPolicy" and "GetPolicySummaryList" returns different policy objects. If policy is retrieved by "GetFullPolicy",
// avatarUrl will be nested within the key "value"
avatarURL: fullPolicyOrPolicySummary.avatarURL || lodashGet(fullPolicyOrPolicySummary, 'value.avatarURL', ''),
customUnit: customUnitSimplified,
customUnits: lodashGet(fullPolicyOrPolicySummary, 'value.customUnits', {}),
};
}

Expand Down Expand Up @@ -430,6 +419,31 @@ function setWorkspaceErrors(policyID, errors) {
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {errors});
}

/**
* @param {String} policyID
* @param {Number} customUnitID
*/
function removeUnitError(policyID, customUnitID) {
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {
customUnits: {
[customUnitID]: {
errors: null,
pendingAction: null,
},
},
});
}

/**
* Checks if we have any errors stored within the policy custom units.
* @param {Object} policy
* @returns {Boolean}
*/
function hasCustomUnitsError(policy) {
const unitsWithErrors = _.filter(lodashGet(policy, 'customUnits', {}), customUnit => (lodashGet(customUnit, 'errors', null)));
return !_.isEmpty(unitsWithErrors);
}

/**
* @param {String} policyID
*/
Expand All @@ -439,30 +453,63 @@ function hideWorkspaceAlertMessage(policyID) {

/**
* @param {String} policyID
* @param {Object} values
* @param {Object} currentCustomUnit
* @param {Object} values The new custom unit values
*/
function setCustomUnit(policyID, values) {
DeprecatedAPI.Policy_CustomUnit_Update({
policyID: policyID.toString(),
customUnit: JSON.stringify(values),
lastModified: null,
})
.then((response) => {
if (response.jsonCode !== 200) {
throw new Error();
}

updateLocalPolicyValues(policyID, {
customUnit: {
id: values.customUnitID,
name: values.name,
value: values.attributes.unit,
function updateWorkspaceCustomUnit(policyID, currentCustomUnit, values) {
const optimisticData = [
{
onyxMethod: 'merge',
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
customUnits: {
[values.customUnitID]: {
...values,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
},
});
}).catch(() => {
// Show the user feedback
Growl.error(Localize.translateLocal('workspace.editor.genericFailureMessage'), 5000);
});
},
},
];

const successData = [
{
onyxMethod: 'merge',
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
customUnits: {
[values.customUnitID]: {
pendingAction: null,
errors: null,
},
},
},
},
];

const failureData = [
{
onyxMethod: 'merge',
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
customUnits: {
[currentCustomUnit.customUnitID]: {
customUnitID: currentCustomUnit.customUnitID,
name: currentCustomUnit.name,
attributes: currentCustomUnit.attributes,
errors: {
[DateUtils.getMicroseconds()]: Localize.translateLocal('workspace.reimburse.updateCustomUnitError'),
},
},
},
},
},
];

API.write('UpdateWorkspaceCustomUnit', {
policyID,
customUnit: JSON.stringify(values),
}, {optimisticData, successData, failureData});
}

/**
Expand Down Expand Up @@ -577,11 +624,13 @@ export {
uploadAvatar,
update,
setWorkspaceErrors,
removeUnitError,
hasCustomUnitsError,
hideWorkspaceAlertMessage,
deletePolicy,
createAndNavigate,
createAndGetPolicyList,
setCustomUnit,
updateWorkspaceCustomUnit,
setCustomUnitRate,
updateLastAccessedWorkspace,
subscribeToPolicyEvents,
Expand Down
25 changes: 15 additions & 10 deletions src/pages/settings/InitialSettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,21 @@ class InitialSettingsPage extends React.Component {
getMenuItems() {
const menuItems = _.chain(this.props.policies)
.filter(policy => policy && policy.type === CONST.POLICY.TYPE.FREE && policy.role === CONST.POLICY.ROLE.ADMIN)
.map(policy => ({
title: policy.name,
icon: policy.avatarURL ? policy.avatarURL : Expensicons.Building,
iconType: policy.avatarURL ? CONST.ICON_TYPE_AVATAR : CONST.ICON_TYPE_ICON,
action: () => Navigation.navigate(ROUTES.getWorkspaceInitialRoute(policy.id)),
iconStyles: policy.avatarURL ? [] : [styles.popoverMenuIconEmphasized],
iconFill: themeColors.iconReversed,
fallbackIcon: Expensicons.FallbackWorkspaceAvatar,
brickRoadIndicator: Policy.hasPolicyMemberError(lodashGet(this.props.policyMembers, `${ONYXKEYS.COLLECTION.POLICY_MEMBER_LIST}${policy.id}`, {})) ? 'error' : null,
}))
.map((policy) => {
const shouldShowErrorIndicator = Policy.hasCustomUnitsError(policy)
|| Policy.hasPolicyMemberError(lodashGet(this.props.policyMembers, `${ONYXKEYS.COLLECTION.POLICY_MEMBER_LIST}${policy.id}`, {}));

return {
title: policy.name,
icon: policy.avatarURL ? policy.avatarURL : Expensicons.Building,
iconType: policy.avatarURL ? CONST.ICON_TYPE_AVATAR : CONST.ICON_TYPE_ICON,
action: () => Navigation.navigate(ROUTES.getWorkspaceInitialRoute(policy.id)),
iconStyles: policy.avatarURL ? [] : [styles.popoverMenuIconEmphasized],
iconFill: themeColors.iconReversed,
fallbackIcon: Expensicons.FallbackWorkspaceAvatar,
brickRoadIndicator: shouldShowErrorIndicator ? 'error' : null,
};
})
.value();
menuItems.push(...this.getDefaultMenuItems());

Expand Down
2 changes: 2 additions & 0 deletions src/pages/workspace/WorkspaceInitialPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class WorkspaceInitialPage extends React.Component {
render() {
const policy = this.props.policy;
const hasMembersError = PolicyActions.hasPolicyMemberError(this.props.policyMemberList);
const hasCustomUnitsError = PolicyActions.hasCustomUnitsError(this.props.policy);
const menuItems = [
{
translationKey: 'workspace.common.settings',
Expand All @@ -92,6 +93,7 @@ class WorkspaceInitialPage extends React.Component {
translationKey: 'workspace.common.reimburse',
icon: Expensicons.Receipt,
action: () => Navigation.navigate(ROUTES.getWorkspaceReimburseRoute(policy.id)),
error: hasCustomUnitsError,
},
{
translationKey: 'workspace.common.bills',
Expand Down
Loading