Skip to content

Commit 21d1b22

Browse files
long74100iloveagent57
authored andcommitted
fix: use coupon details from overview instead of fetching coupons
1 parent c3951ae commit 21d1b22

File tree

3 files changed

+17
-74
lines changed

3 files changed

+17
-74
lines changed

src/components/subsidy-request-modals/ApproveCouponCodeRequestModal.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { connect } from 'react-redux';
1212
import { Info } from '@edx/paragon/icons';
1313
import { logError } from '@edx/frontend-platform/logging';
1414
import Skeleton from 'react-loading-skeleton';
15+
import { camelCaseObject } from '@edx/frontend-platform/utils';
1516
import { useApplicableCoupons } from './data/hooks';
1617
import EnterpriseAccessApiService from '../../data/services/EnterpriseAccessApiService';
1718
import { formatTimestamp } from '../../utils';
@@ -202,7 +203,7 @@ ApproveCouponCodeRequestModal.propTypes = {
202203
};
203204

204205
const mapStateToProps = state => ({
205-
coupons: state.coupons.data,
206+
coupons: state.coupons.data ? camelCaseObject(state.coupons.data) : { results: [] },
206207
});
207208

208209
export default connect(mapStateToProps)(ApproveCouponCodeRequestModal);

src/components/subsidy-request-modals/data/hooks.js

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { camelCaseObject } from '@edx/frontend-platform/utils';
33
import { logError } from '@edx/frontend-platform/logging';
44
import moment from 'moment';
55
import EnterpriseCatalogApiService from '../../../data/services/EnterpriseCatalogApiService';
6-
import EcommerceApiService from '../../../data/services/EcommerceApiService';
76

87
/**
98
* This hook returns the enterprise's catalogs that contain the given course runs.
@@ -85,59 +84,28 @@ export const useApplicableCoupons = ({
8584
}) => {
8685
const {
8786
applicableCatalogs,
88-
isLoading: isLoadingApplicableCatalogs,
89-
error: fetchApplicableCatalogsError,
87+
isLoading,
88+
error,
9089
} = useApplicableCatalogs({ enterpriseId, courseRunIds });
91-
const [isLoading, setIsLoading] = useState(false);
9290
const [applicableCoupons, setApplicableCoupons] = useState([]);
93-
const [couponDetails, setCouponDetails] = useState([]);
94-
const [fetchCouponsError, setFetchCouponsError] = useState();
95-
96-
useEffect(() => {
97-
const fetchCouponDetails = async () => {
98-
setIsLoading(true);
99-
try {
100-
// We need to fetch individual coupons to get the associated enterprise catalog
101-
const details = (await Promise.all(
102-
coupons.results.map(coupon => EcommerceApiService.fetchCoupon(coupon.id)),
103-
)).map(
104-
(resp, index) => camelCaseObject({
105-
...resp.data,
106-
...coupons.results[index],
107-
}),
108-
);
109-
110-
setCouponDetails(details);
111-
} catch (err) {
112-
logError(err);
113-
setFetchCouponsError(err);
114-
} finally {
115-
setIsLoading(false);
116-
}
117-
};
118-
119-
if (coupons?.results?.length > 0) {
120-
fetchCouponDetails();
121-
}
122-
}, [coupons?.results?.length]);
12391

12492
useEffect(() => {
125-
if (couponDetails) {
93+
if (applicableCatalogs.length > 0 && coupons.results.length > 0) {
12694
const now = moment();
127-
const applicableCoups = couponDetails.filter(
95+
const applicableCoups = coupons.results.filter(
12896
coupon => applicableCatalogs.includes(
129-
coupon.enterpriseCustomerCatalog,
97+
coupon.enterpriseCatalogUuid,
13098
) && moment(coupon.endDate) > now && coupon.numUnassigned > 0,
13199
);
132100

133101
setApplicableCoupons(applicableCoups);
134102
}
135-
}, [applicableCatalogs, couponDetails]);
103+
}, [applicableCatalogs, coupons.results]);
136104

137105
return {
138106
applicableCatalogs,
139107
applicableCoupons,
140-
isLoading: isLoadingApplicableCatalogs || isLoading,
141-
error: fetchApplicableCatalogsError || fetchCouponsError,
108+
isLoading,
109+
error,
142110
};
143111
};

src/components/subsidy-request-modals/tests/hooks.test.jsx

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { renderHook } from '@testing-library/react-hooks/dom';
22
import moment from 'moment';
33
import { useApplicableCatalogs, useApplicableSubscriptions, useApplicableCoupons } from '../data/hooks';
44
import EnterpriseCatalogApiService from '../../../data/services/EnterpriseCatalogApiService';
5-
import EcommerceApiService from '../../../data/services/EcommerceApiService';
65

76
const TEST_ENTERPRISE_UUID = 'test-enterprise-uuid';
87
const TEST_COURSE_RUN_IDS = ['edx+101'];
@@ -16,15 +15,6 @@ jest.mock('../../../data/services/EnterpriseCatalogApiService', () => ({
1615
})),
1716
}));
1817

19-
jest.mock('../../../data/services/EcommerceApiService', () => ({
20-
fetchCoupon: jest.fn((couponId) => ({
21-
data: {
22-
id: couponId,
23-
max_uses: 3,
24-
},
25-
})),
26-
}));
27-
2818
describe('useApplicableCatalogs', () => {
2919
afterEach(() => jest.clearAllMocks());
3020

@@ -125,20 +115,23 @@ describe('useApplicableCoupons', () => {
125115
results: [{
126116
id: 1,
127117
numUnassigned: 1,
128-
enterpriseCustomerCatalog: TEST_CATALOG_UUID,
118+
enterpriseCatalogUuid: TEST_CATALOG_UUID,
129119
endDate: moment().add(1, 'days').toISOString(),
120+
maxUses: 3,
130121
},
131122
{
132123
id: 2,
133124
numUnassigned: 1,
134-
enterpriseCustomerCatalog: 'abc',
125+
enterpriseCatalogUuid: 'abc',
135126
endDate: moment().add(1, 'days').toISOString(),
127+
maxUses: 3,
136128
},
137129
{
138130
id: 3,
139131
numUnassigned: 3,
140-
enterpriseCustomerCatalog: TEST_CATALOG_UUID,
132+
enterpriseCatalogUuid: TEST_CATALOG_UUID,
141133
endDate: moment().subtract(1, 'days').toISOString(),
134+
maxUses: 3,
142135
}],
143136
};
144137

@@ -159,14 +152,9 @@ describe('useApplicableCoupons', () => {
159152
},
160153
);
161154

162-
expect(EcommerceApiService.fetchCoupon).toHaveBeenCalledTimes(3);
163-
164155
const { applicableCoupons } = result.current;
165156
expect(applicableCoupons.length).toEqual(1);
166-
expect(applicableCoupons[0]).toEqual({
167-
...couponOrders.results[0],
168-
maxUses: 3,
169-
});
157+
expect(applicableCoupons[0].id).toEqual(couponOrders.results[0].id);
170158
});
171159

172160
it('should handle errors fetching applicable catalogs', async () => {
@@ -182,18 +170,4 @@ describe('useApplicableCoupons', () => {
182170
await waitForNextUpdate();
183171
expect(result.current.error).toEqual(error);
184172
});
185-
186-
it('should handle errors fetching coupons', async () => {
187-
const error = new Error('something went wrong fetching coupons');
188-
EcommerceApiService.fetchCoupon.mockRejectedValueOnce(error);
189-
190-
const { result, waitForNextUpdate } = renderHook(() => useApplicableCoupons({
191-
enterpriseId: TEST_ENTERPRISE_UUID,
192-
courseRunIds: TEST_COURSE_RUN_IDS,
193-
coupons: couponOrders,
194-
}));
195-
196-
await waitForNextUpdate();
197-
expect(result.current.error).toEqual(error);
198-
});
199173
});

0 commit comments

Comments
 (0)