Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit ead7ac8

Browse files
committed
Fixed: billing account was reset in UI after reopening working period's details.
1 parent 9a4057c commit ead7ac8

File tree

6 files changed

+71
-65
lines changed

6 files changed

+71
-65
lines changed

src/routes/WorkPeriods/components/PeriodDetails/index.jsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ import styles from "./styles.module.scss";
2626
* @param {Object} props.details working period details object
2727
* @param {boolean} props.isDisabled whether the details are disabled
2828
* @param {boolean} props.isFailed whether the payments for the period has failed
29+
* @param {Object} props.period working period basic data object
2930
* @returns {JSX.Element}
3031
*/
31-
const PeriodDetails = ({ className, details, isDisabled, isFailed }) => {
32+
const PeriodDetails = ({
33+
className,
34+
details,
35+
isDisabled,
36+
isFailed,
37+
period,
38+
}) => {
3239
const dispatch = useDispatch();
40+
const { id: periodId, rbId, jobId, billingAccountId } = period;
3341
const {
34-
periodId,
35-
rbId,
36-
jobId,
37-
billingAccountId,
3842
billingAccounts,
3943
billingAccountsError,
4044
billingAccountsIsDisabled,
@@ -153,10 +157,6 @@ const PeriodDetails = ({ className, details, isDisabled, isFailed }) => {
153157
PeriodDetails.propTypes = {
154158
className: PT.string,
155159
details: PT.shape({
156-
periodId: PT.string.isRequired,
157-
rbId: PT.string.isRequired,
158-
jobId: PT.string.isRequired,
159-
billingAccountId: PT.number.isRequired,
160160
billingAccounts: PT.arrayOf(
161161
PT.shape({
162162
label: PT.string.isRequired,
@@ -172,6 +172,12 @@ PeriodDetails.propTypes = {
172172
}).isRequired,
173173
isDisabled: PT.bool.isRequired,
174174
isFailed: PT.bool.isRequired,
175+
period: PT.shape({
176+
id: PT.string.isRequired,
177+
rbId: PT.string.isRequired,
178+
jobId: PT.string.isRequired,
179+
billingAccountId: PT.number.isRequired,
180+
}).isRequired,
175181
};
176182

177183
export default memo(PeriodDetails);

src/routes/WorkPeriods/components/PeriodItem/index.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ const PeriodItem = ({
214214
{details && (
215215
<PeriodDetails
216216
className={styles.periodDetails}
217+
period={item}
217218
details={details}
218219
isDisabled={isDisabled}
219220
isFailed={!!reasonFailed}
@@ -252,6 +253,7 @@ PeriodItem.propTypes = {
252253
id: PT.oneOfType([PT.number, PT.string]).isRequired,
253254
jobId: PT.string,
254255
rbId: PT.string.isRequired,
256+
billingAccountId: PT.number.isRequired,
255257
projectId: PT.oneOfType([PT.number, PT.string]).isRequired,
256258
userHandle: PT.string.isRequired,
257259
teamName: PT.oneOfType([PT.number, PT.string]).isRequired,
@@ -268,10 +270,6 @@ PeriodItem.propTypes = {
268270
paymentTotal: PT.number.isRequired,
269271
}).isRequired,
270272
details: PT.shape({
271-
periodId: PT.string.isRequired,
272-
rbId: PT.string.isRequired,
273-
jobId: PT.string.isRequired,
274-
billingAccountId: PT.number.isRequired,
275273
billingAccounts: PT.arrayOf(
276274
PT.shape({
277275
label: PT.string.isRequired,

src/store/actions/workPeriods.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,25 @@ export const loadWorkPeriodDetailsError = (periodId, message) => ({
9292
/**
9393
* Creates an action denoting successful load of billing accounts.
9494
*
95-
* @param {string} periodId working period id
95+
* @param {Object} period working period basic data object
9696
* @param {Array} accounts billing accounts
9797
* @returns {Object}
9898
*/
99-
export const loadBillingAccountsSuccess = (periodId, accounts) => ({
99+
export const loadBillingAccountsSuccess = (period, accounts) => ({
100100
type: ACTION_TYPE.WP_LOAD_BILLING_ACCOUNTS_SUCCESS,
101-
payload: { periodId, accounts },
101+
payload: { period, accounts },
102102
});
103103

104104
/**
105105
* Creates an action denoting an error while loading billing accounts.
106106
*
107-
* @param {string} periodId working period id
107+
* @param {Object} period working period basic data object
108108
* @param {string} message error message
109109
* @returns {Object}
110110
*/
111-
export const loadBillingAccountsError = (periodId, message) => ({
111+
export const loadBillingAccountsError = (period, message) => ({
112112
type: ACTION_TYPE.WP_LOAD_BILLING_ACCOUNTS_ERROR,
113-
payload: { periodId, message, id: nextErrorId++ },
113+
payload: { period, message, id: nextErrorId++ },
114114
});
115115

116116
/**

src/store/reducers/workPeriods.js

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,13 @@ const initPeriodData = (period) => {
4949
return data;
5050
};
5151

52-
const initPeriodDetails = (period, cancelSource = cancelSourceDummy) => ({
53-
periodId: period.id,
54-
rbId: period.rbId,
52+
const initPeriodDetails = (
53+
billingAccountId = 0,
54+
cancelSource = cancelSourceDummy
55+
) => ({
5556
cancelSource,
56-
jobId: period.jobId,
57-
billingAccountId: period.billingAccountId || 0,
5857
billingAccounts: [
59-
{ value: period.billingAccountId || 0, label: BILLING_ACCOUNTS_LOADING },
58+
{ value: billingAccountId, label: BILLING_ACCOUNTS_LOADING },
6059
],
6160
billingAccountsError: null,
6261
billingAccountsIsDisabled: true,
@@ -191,7 +190,10 @@ const actionHandlers = {
191190
{ period, cancelSource }
192191
) => {
193192
const periodsDetails = { ...state.periodsDetails };
194-
periodsDetails[period.id] = initPeriodDetails(period, cancelSource);
193+
periodsDetails[period.id] = initPeriodDetails(
194+
period.billingAccountId,
195+
cancelSource
196+
);
195197
return {
196198
...state,
197199
periodsDetails,
@@ -201,7 +203,7 @@ const actionHandlers = {
201203
state,
202204
{ periodId, details }
203205
) => {
204-
const periodsDetails = { ...state.periodsDetails };
206+
const periodsDetails = state.periodsDetails;
205207
let periodDetails = periodsDetails[periodId];
206208
// period details object must already be initialized
207209
if (!periodDetails) {
@@ -232,7 +234,7 @@ const actionHandlers = {
232234
return {
233235
...state,
234236
periodsData: [periodsData],
235-
periodsDetails,
237+
periodsDetails: { ...periodsDetails },
236238
};
237239
},
238240
[ACTION_TYPE.WP_LOAD_PERIOD_DETAILS_ERROR]: (
@@ -250,16 +252,26 @@ const actionHandlers = {
250252
},
251253
[ACTION_TYPE.WP_LOAD_BILLING_ACCOUNTS_SUCCESS]: (
252254
state,
253-
{ periodId, accounts }
255+
{ period, accounts }
254256
) => {
255-
const periodsDetails = { ...state.periodsDetails };
256-
let periodDetails = periodsDetails[periodId];
257+
const periodsDetails = state.periodsDetails;
258+
let periodDetails = periodsDetails[period.id];
257259
if (!periodDetails) {
258260
// Period details may be removed at this point so we must handle this case.
259261
return state;
260262
}
263+
let accountId = period.billingAccountId;
264+
let hasAssignedAccount = false;
265+
for (let account of accounts) {
266+
if (account.value === accountId) {
267+
hasAssignedAccount = true;
268+
break;
269+
}
270+
}
271+
if (accountId > 0 && !hasAssignedAccount) {
272+
accounts.unshift(createAssignedBillingAccountOption(accountId));
273+
}
261274
let billingAccountsIsDisabled = false;
262-
let accountId = periodDetails.billingAccountId;
263275
if (!accounts.length) {
264276
accounts.push({ value: accountId, label: BILLING_ACCOUNTS_NONE });
265277
billingAccountsIsDisabled = true;
@@ -274,24 +286,24 @@ const actionHandlers = {
274286
if (!periodDetails.periodsIsLoading) {
275287
periodDetails.cancelSource = null;
276288
}
277-
periodsDetails[periodId] = periodDetails;
289+
periodsDetails[period.id] = periodDetails;
278290
return {
279291
...state,
280-
periodsDetails,
292+
periodsDetails: { ...periodsDetails },
281293
};
282294
},
283295
[ACTION_TYPE.WP_LOAD_BILLING_ACCOUNTS_ERROR]: (
284296
state,
285-
{ periodId, message }
297+
{ period, message }
286298
) => {
287-
const periodsDetails = { ...state.periodsDetails };
288-
let periodDetails = periodsDetails[periodId];
299+
const periodsDetails = state.periodsDetails;
300+
let periodDetails = periodsDetails[period.id];
289301
if (!periodDetails) {
290302
return state;
291303
}
292304
let billingAccounts = [];
293305
let billingAccountsIsDisabled = true;
294-
let accountId = periodDetails.billingAccountId;
306+
let accountId = period.billingAccountId;
295307
if (accountId) {
296308
billingAccounts.push(createAssignedBillingAccountOption(accountId));
297309
billingAccountsIsDisabled = false;
@@ -308,27 +320,26 @@ const actionHandlers = {
308320
if (!periodDetails.periodsIsLoading) {
309321
periodDetails.cancelSource = null;
310322
}
311-
periodsDetails[periodId] = periodDetails;
323+
periodsDetails[period.id] = periodDetails;
312324
return {
313325
...state,
314-
periodsDetails,
326+
periodsDetails: { ...periodsDetails },
315327
};
316328
},
317329
[ACTION_TYPE.WP_SET_BILLING_ACCOUNT]: (state, { periodId, accountId }) => {
318-
let periodsDetails = state.periodsDetails;
319-
const periodDetails = periodsDetails[periodId];
320-
if (!periodDetails) {
321-
return state;
330+
const periods = state.periods;
331+
for (let i = 0, len = periods.length; i < len; i++) {
332+
let period = periods[i];
333+
if (period.id === periodId) {
334+
periods[i] = { ...period, billingAccountId: accountId };
335+
break;
336+
}
322337
}
323-
periodsDetails[periodId] = {
324-
...periodDetails,
325-
billingAccountId: accountId,
326-
};
327-
periodsDetails = { ...periodsDetails };
328338
state = {
329339
...state,
330-
periodsDetails,
340+
periods: [...periods],
331341
};
342+
// updating reasons for which the period's selection may be disabled
332343
const periodsDisabledMap = state.periodsDisabled[0];
333344
const oldReasonsDisabled = periodsDisabledMap.get(periodId);
334345
const reasonsDisabled = removeReasonDisabled(

src/store/thunks/workPeriods.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,13 @@ export const toggleWorkPeriodDetails =
156156
);
157157
bilAccsPromise
158158
.then((data) => {
159-
const periodsDetails = selectors.getWorkPeriodsDetails(getState());
160-
const periodDetails = periodsDetails[period.id];
161-
const billingAccountId =
162-
(periodDetails && periodDetails.billingAccountId) ||
163-
period.billingAccountId;
164-
const accounts = normalizeBillingAccounts(data, billingAccountId);
165-
dispatch(actions.loadBillingAccountsSuccess(period.id, accounts));
159+
const accounts = normalizeBillingAccounts(data);
160+
dispatch(actions.loadBillingAccountsSuccess(period, accounts));
166161
})
167162
.catch((error) => {
168163
if (!axios.isCancel(error)) {
169164
dispatch(
170-
actions.loadBillingAccountsError(period.id, error.toString())
165+
actions.loadBillingAccountsError(period, error.toString())
171166
);
172167
}
173168
});

src/utils/workPeriods.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export function findReasonsDisabled(period) {
3131
return reasons.length ? reasons : undefined;
3232
}
3333

34+
export function createAlerts(period, bookingEndDate) {}
35+
3436
export function addReasonDisabled(reasons, reason) {
3537
if (!reasons) {
3638
return [reason];
@@ -171,15 +173,12 @@ export function normalizePaymentStatus(paymentStatus) {
171173
* billing account.
172174
*
173175
* @param {Array} accounts array of billing accounts received for specific project
174-
* @param {number} accountId resource booking's billing account id
175176
* @returns {Array}
176177
*/
177-
export function normalizeBillingAccounts(accounts, accountId = -1) {
178+
export function normalizeBillingAccounts(accounts) {
178179
const accs = [];
179-
let hasSelectedAccount = false;
180180
for (let acc of accounts) {
181181
const value = +acc.tcBillingAccountId;
182-
hasSelectedAccount = hasSelectedAccount || value === accountId;
183182
const endDate = acc.endDate
184183
? moment(acc.endDate).format("DD MMM YYYY")
185184
: "";
@@ -188,9 +187,6 @@ export function normalizeBillingAccounts(accounts, accountId = -1) {
188187
label: `${acc.name} (${value})` + (endDate ? ` - ${endDate}` : ""),
189188
});
190189
}
191-
if (!hasSelectedAccount && accountId > 0) {
192-
accs.unshift(createAssignedBillingAccountOption(accountId));
193-
}
194190
return accs;
195191
}
196192

0 commit comments

Comments
 (0)