Skip to content

Commit

Permalink
refactor: improve code efficiency and readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jajjibhai008 committed Sep 21, 2023
1 parent 5aecd8f commit ac6f555
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/components/EnterpriseApp/data/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export const BUDGET_STATUSES = {
expired: 'Expired',
upcoming: 'Upcoming',
};

export const OFFER_TYPES = {
ecommerce: 'ecommerce',
subsidy: 'subsidy',
};
9 changes: 5 additions & 4 deletions src/components/EnterpriseSubsidiesContext/data/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { camelCaseObject } from '@edx/frontend-platform/utils';
import EcommerceApiService from '../../../data/services/EcommerceApiService';
import LicenseManagerApiService from '../../../data/services/LicenseManagerAPIService';
import SubsidyApiService from '../../../data/services/EnterpriseSubsidyApiService';
import { OFFER_TYPES } from '../../EnterpriseApp/data/constants';

export const useEnterpriseOffers = ({ enablePortalLearnerCreditManagementScreen, enterpriseId }) => {
const [offers, setOffers] = useState([]);
Expand Down Expand Up @@ -39,9 +40,9 @@ export const useEnterpriseOffers = ({ enablePortalLearnerCreditManagementScreen,

for (let i = 0; i < enterpriseResults.length; i++) {
const subsidy = enterpriseResults[i];
const source = 'subsidyApi';
const { isActive } = subsidy; // Always check isActive for enterprise subsidies
const isCurrent = isActive; // You can adjust this based on your specific requirements
const source = OFFER_TYPES.subsidy;
const { isActive } = subsidy;
const isCurrent = isActive;
const activeSubsidyData = {
id: subsidy.uuid,
name: subsidy.title,
Expand All @@ -58,7 +59,7 @@ export const useEnterpriseOffers = ({ enablePortalLearnerCreditManagementScreen,

for (let i = 0; i < ecommerceResults.length; i++) {
const subsidy = ecommerceResults[i];
const source = 'ecommerceApi';
const source = OFFER_TYPES.ecommerce;
const { isCurrent } = subsidy;
const activeSubsidyData = {
id: subsidy.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useCoupons, useCustomerAgreement, useEnterpriseOffers } from '../hooks'
import EcommerceApiService from '../../../../data/services/EcommerceApiService';
import LicenseManagerApiService from '../../../../data/services/LicenseManagerAPIService';
import SubsidyApiService from '../../../../data/services/EnterpriseSubsidyApiService';
import { OFFER_TYPES } from '../../../EnterpriseApp/data/constants';

jest.mock('@edx/frontend-platform/config', () => ({
getConfig: jest.fn(() => ({
Expand Down Expand Up @@ -51,7 +52,7 @@ describe('useEnterpriseOffers', () => {
start: '2021-05-15T19:56:09Z',
end: '2100-05-15T19:56:09Z',
isCurrent: true,
source: 'ecommerceApi',
source: OFFER_TYPES.ecommerce,
}];

SubsidyApiService.getSubsidyByCustomerUUID.mockResolvedValueOnce({
Expand Down Expand Up @@ -132,15 +133,15 @@ describe('useEnterpriseOffers', () => {
start: '2021-05-15T19:56:09Z',
end: '2100-05-15T19:56:09Z',
isCurrent: true,
source: 'subsidyApi',
source: OFFER_TYPES.subsidy,
},
{
id: 'uuid',
name: 'offer-name',
start: '2021-05-15T19:56:09Z',
end: '2100-05-15T19:56:09Z',
isCurrent: true,
source: 'ecommerceApi',
source: OFFER_TYPES.ecommerce,
},
];

Expand Down Expand Up @@ -215,15 +216,15 @@ describe('useEnterpriseOffers', () => {
start: '2005-05-15T19:56:09Z',
end: '2006-05-15T19:56:09Z',
isCurrent: false,
source: 'subsidyApi',
source: OFFER_TYPES.subsidy,
},
{
id: 'offer-2',
name: 'offer-name-2',
start: '2006-05-15T19:56:09Z',
end: '2099-05-15T19:56:09Z',
isCurrent: true,
source: 'subsidyApi',
source: OFFER_TYPES.subsidy,
},
];

Expand Down
3 changes: 2 additions & 1 deletion src/components/learner-credit-management/BudgetCard-V2.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {

import { useOfferSummary } from './data/hooks';
import SubBudgetCard from './Budgetcard-V3';
import { OFFER_TYPES } from '../EnterpriseApp/data/constants';

const LoadingCards = () => (
<Stack gap={4}>
Expand Down Expand Up @@ -54,7 +55,7 @@ const BudgetCard = ({
<Stack gap={4} className='mt-4'>
{isLoadingOfferSummary ? (
<LoadingCards />
) : offerType === 'ecommerceApi' ? (
) : offerType === OFFER_TYPES.ecommerce ? (
<SubBudgetCard
id={offerSummary.offerId}
start={start}
Expand Down
4 changes: 2 additions & 2 deletions src/components/learner-credit-management/data/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export const transformOfferSummary = (offerSummary) => {
const redeemedFunds = budgets[i].amountOfPolicySpent && parseFloat(budgets[i].amountOfPolicySpent);
const remainingFunds = budgets[i].remainingBalance && parseFloat(budgets[i].remainingBalance);
// Create an object with key-value pairs
const budgetEntry = {
const updatedBudgetDetail = {
redeemedFunds,
remainingFunds,
...budgets[i],
};
budgetsSummary.push(budgetEntry);
budgetsSummary.push(updatedBudgetDetail);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import '@testing-library/jest-dom/extend-expect';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import BudgetCard from '../BudgetCard-V2';
import { useOfferSummary, useOfferRedemptions } from '../data/hooks';
import { OFFER_TYPES } from '../../EnterpriseApp/data/constants';

jest.mock('../data/hooks');
useOfferSummary.mockReturnValue({
Expand Down Expand Up @@ -115,13 +116,13 @@ describe('<BudgetCard />', () => {
expect(firstElementWithTestId).toHaveTextContent(formattedString);
});

it('renders SubBudgetCard when offerType is ecommerceApi', () => {
it('renders SubBudgetCard when offerType is ecommerce', () => {
const mockOffer = {
id: mockEnterpriseOfferId,
name: mockOfferDisplayName,
start: '2022-01-01',
end: '2023-01-01',
offerType: 'ecommerceApi',
offerType: OFFER_TYPES.ecommerce,
};
const mockOfferRedemption = {
created: '2022-02-01',
Expand Down Expand Up @@ -166,7 +167,7 @@ describe('<BudgetCard />', () => {
expect(screen.getByTestId('view-budget')).toBeInTheDocument();
});

it('renders SubBudgetCard when offerType is not ecommerceApi', () => {
it('renders SubBudgetCard when offerType is not ecommerce', () => {
const mockOffer = {
id: mockEnterpriseOfferId,
name: mockOfferDisplayName,
Expand Down

0 comments on commit ac6f555

Please sign in to comment.