-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #366 from openedx/knguyen2/ent-7958
feat: adds budget distribution mode selection component in provisioning tool
- Loading branch information
Showing
12 changed files
with
244 additions
and
31 deletions.
There are no files selected for viewing
5 changes: 4 additions & 1 deletion
5
...ation/Provisioning/ProvisioningForm/PolicyForm/ProvisioningFormPerLearnerCapContainer.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/Configuration/Provisioning/ProvisioningForm/PolicyForm/ProvisioningFormPolicyType.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { | ||
Form, | ||
} from '@edx/paragon'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import React, { useState } from 'react'; | ||
import PROVISIONING_PAGE_TEXT from '../../data/constants'; | ||
import useProvisioningContext from '../../data/hooks'; | ||
import { indexOnlyPropType, selectProvisioningContext } from '../../data/utils'; | ||
|
||
const ProvisioningFormPolicyType = ({ index }) => { | ||
const { perLearnerCap, setPolicyType, setInvalidPolicyFields } = useProvisioningContext(); | ||
const { POLICY_TYPE } = PROVISIONING_PAGE_TEXT.FORM; | ||
const [formData, showInvalidField] = selectProvisioningContext('formData', 'showInvalidField'); | ||
const { policies } = showInvalidField; | ||
const isPolicyTypeDefinedAndFalse = policies[index]?.policyType === false; | ||
|
||
const [value, setValue] = useState(null); | ||
|
||
const handleChange = (e) => { | ||
const policyTypeValue = e.target.value; | ||
if (policyTypeValue === POLICY_TYPE.OPTIONS.LEARNER_SELECTS.DESCRIPTION) { | ||
setPolicyType({ | ||
policyType: POLICY_TYPE.OPTIONS.LEARNER_SELECTS.VALUE, | ||
accessMethod: POLICY_TYPE.OPTIONS.LEARNER_SELECTS.ACCESS_METHOD, | ||
}, index); | ||
} else if (policyTypeValue === POLICY_TYPE.OPTIONS.ADMIN_SELECTS.DESCRIPTION) { | ||
setPolicyType({ | ||
policyType: POLICY_TYPE.OPTIONS.ADMIN_SELECTS.VALUE, | ||
accessMethod: POLICY_TYPE.OPTIONS.ADMIN_SELECTS.ACCESS_METHOD, | ||
}, index); | ||
perLearnerCap({ | ||
perLearnerCap: false, | ||
}, index); | ||
setInvalidPolicyFields({ perLearnerCap: true }, index); | ||
} | ||
setValue(policyTypeValue); | ||
setInvalidPolicyFields({ policyType: true }, index); | ||
}; | ||
|
||
return ( | ||
<article className="mt-4.5"> | ||
<div> | ||
<h3>{POLICY_TYPE.TITLE}</h3> | ||
</div> | ||
<Form.Group | ||
className="mt-3.5" | ||
> | ||
<Form.Label className="mb-2.5">{POLICY_TYPE.LABEL}</Form.Label> | ||
<Form.RadioSet | ||
name={`display-policy-type-${index}`} | ||
onChange={handleChange} | ||
value={value || formData.policies[index]?.policyType} | ||
> | ||
{ | ||
Object.values(POLICY_TYPE.OPTIONS).map(({ DESCRIPTION }) => ( | ||
<Form.Radio | ||
value={DESCRIPTION} | ||
type="radio" | ||
key={uuidv4()} | ||
data-testid={DESCRIPTION} | ||
isInvalid={isPolicyTypeDefinedAndFalse} | ||
> | ||
{DESCRIPTION} | ||
</Form.Radio> | ||
)) | ||
} | ||
</Form.RadioSet> | ||
{isPolicyTypeDefinedAndFalse && ( | ||
<Form.Control.Feedback | ||
type="invalid" | ||
> | ||
{POLICY_TYPE.ERROR} | ||
</Form.Control.Feedback> | ||
)} | ||
</Form.Group> | ||
</article> | ||
); | ||
}; | ||
|
||
ProvisioningFormPolicyType.propTypes = indexOnlyPropType; | ||
|
||
export default ProvisioningFormPolicyType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...ration/Provisioning/ProvisioningForm/PolicyForm/tests/ProvisioningFormPolicyType.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* eslint-disable react/prop-types */ | ||
import { renderWithRouter } from '@edx/frontend-enterprise-utils'; | ||
import { fireEvent, screen } from '@testing-library/react'; | ||
import { ProvisioningContext, initialStateValue } from '../../../../testData/Provisioning'; | ||
import PROVISIONING_PAGE_TEXT, { INITIAL_CATALOG_QUERIES } from '../../../data/constants'; | ||
import ProvisioningFormPolicyType from '../ProvisioningFormPolicyType'; | ||
|
||
const { POLICY_TYPE } = PROVISIONING_PAGE_TEXT.FORM; | ||
|
||
const ProvisioningFormPolicyTypeWrapper = ({ | ||
value = initialStateValue, | ||
index = 0, | ||
}) => ( | ||
<ProvisioningContext value={value}> | ||
<ProvisioningFormPolicyType index={index} /> | ||
</ProvisioningContext> | ||
); | ||
|
||
describe('ProvisioningFormPolicyType', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
it('renders', () => { | ||
const updatedInitialState = { | ||
...initialStateValue, | ||
multipleFunds: false, | ||
formData: { | ||
...initialStateValue.formData, | ||
policies: INITIAL_CATALOG_QUERIES.defaultQuery, | ||
}, | ||
}; | ||
renderWithRouter( | ||
<ProvisioningFormPolicyTypeWrapper | ||
value={updatedInitialState} | ||
index={0} | ||
/>, | ||
); | ||
|
||
expect(screen.getByText(POLICY_TYPE.TITLE)).toBeTruthy(); | ||
expect(screen.getByText(POLICY_TYPE.LABEL)).toBeTruthy(); | ||
|
||
const policyTypeOptions = Object.keys(POLICY_TYPE.OPTIONS); | ||
const policyTypeButtons = []; | ||
// Retrieves a list of input elements based on test ids | ||
for (let i = 0; i < policyTypeOptions.length; i++) { | ||
policyTypeButtons.push(screen.getByTestId(POLICY_TYPE.OPTIONS[policyTypeOptions[i]].DESCRIPTION)); | ||
} | ||
|
||
// Clicks on each input element and checks if it is checked | ||
for (let i = 0; i < policyTypeButtons.length; i++) { | ||
fireEvent.click(policyTypeButtons[i]); | ||
expect(policyTypeButtons[i].checked).toBeTruthy(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.