Skip to content

Commit

Permalink
fix: update toggle state based on api response
Browse files Browse the repository at this point in the history
  • Loading branch information
SundasNoreen committed Sep 20, 2023
1 parent 6f238bd commit 84c0c80
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
33 changes: 19 additions & 14 deletions src/pages-and-resources/discussions/app-list/AppList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import React, {
} from 'react';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import {
CardGrid, Container, breakpoints, Form, ActionRow, AlertModal, Button,
CardGrid, Container, breakpoints, Form, ActionRow, AlertModal, Button, Spinner,
} from '@edx/paragon';
import { useDispatch, useSelector } from 'react-redux';
import Responsive from 'react-responsive';
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
import { useModels } from '../../../generic/model-store';
import {
selectApp, LOADED, LOADING,
updateValidationStatus,
selectApp, LOADED, LOADING, SAVING, updateValidationStatus,
} from '../data/slice';
import AppCard from './AppCard';
import messages from './messages';
Expand All @@ -27,9 +26,9 @@ const AppList = ({ intl }) => {
const dispatch = useDispatch();
const { courseId } = useContext(PagesAndResourcesContext);
const {
appIds, featureIds, status, activeAppId, selectedAppId, enabled, postingRestrictions,
appIds, featureIds, status, saveStatus, activeAppId, selectedAppId, enabled, postingRestrictions,
} = useSelector(state => state.discussions);
const [discussionEnabled, setDiscussionEnabled] = useState(enabled);
const [toggleDiscussionEnabledAlert, setToggleDiscussionEnabledAlert] = useState(enabled);
const apps = useModels('apps', appIds);
const features = useModels('features', featureIds);
const isGlobalStaff = getAuthenticatedUser().administrator;
Expand All @@ -54,7 +53,7 @@ const AppList = ({ intl }) => {
}, [selectedAppId, activeAppId]);

useEffect(() => {
setDiscussionEnabled(enabled);
setToggleDiscussionEnabledAlert(enabled);
}, [enabled]);

useEffect(() => {
Expand All @@ -80,19 +79,20 @@ const AppList = ({ intl }) => {
}, [courseId, selectedAppId, postingRestrictions]);

const handleClose = useCallback(() => {
setDiscussionEnabled(enabled);
setToggleDiscussionEnabledAlert(enabled);
}, [enabled]);

const handleOk = useCallback(() => {
setDiscussionEnabled(false);
setToggleDiscussionEnabledAlert(false);
updateSettings(false);
}, [updateSettings]);

const handleChange = useCallback((e) => {
const toggleVal = e.target.checked;
setDiscussionEnabled(!toggleVal);
if (!toggleVal) {
updateSettings(!toggleVal);
} else {
setToggleDiscussionEnabledAlert(!toggleVal);
}
}, [updateSettings]);

Expand Down Expand Up @@ -131,10 +131,10 @@ const AppList = ({ intl }) => {
className="text-primary-500 align-items-center"
labelClassName="line-height-24"
onChange={handleChange}
checked={!discussionEnabled}
checked={!enabled}
data-testId="hide-discussion"
>
Hide discussion tab
{intl.formatMessage(messages.hideDiscussionTab)}
</Form.Switch>
</div>
<CardGrid
Expand All @@ -161,14 +161,19 @@ const AppList = ({ intl }) => {
</Responsive>
<AlertModal
title={intl.formatMessage(messages.hideDiscussionTabTitle)}
isOpen={enabled && !discussionEnabled}
isOpen={enabled && !toggleDiscussionEnabledAlert}
onClose={handleClose}
isBlocking
className="hide-discussion-modal"
footerNode={(
<ActionRow>
<Button variant="link" className="text-decoration-none bg-black" onClick={handleClose}>Cancel</Button>
<Button variant="primary" className="bg-primary-500 ml-1 rounded-0" onClick={handleOk}>OK</Button>
<Button variant="link" className="text-decoration-none bg-black" onClick={handleClose}>
{intl.formatMessage(messages.hideDiscussionCancelButton)}
</Button>
<Button variant="primary" className="bg-primary-500 ml-1 rounded-0" onClick={handleOk}>
{intl.formatMessage(messages.hideDiscussionOkButton)}
{saveStatus === SAVING && <Spinner animation="border" size="sm" className="ml-2" />}
</Button>
</ActionRow>
)}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('AppList', () => {
});

test.each([
{ title: 'OK', description: 'Enable the toggle state by clicking on OK button' },
{ title: 'Ok', description: 'Enable the toggle state by clicking on OK button' },
{ title: 'Cancel', description: 'Disable the toggle state by clicking on Cancel button' },
])('%s of the hide discussion tab', async ({ title }) => {
renderComponent();
Expand Down
5 changes: 5 additions & 0 deletions src/pages-and-resources/discussions/app-list/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ const messages = defineMessages({
defaultMessage: 'Commonly requested',
description: 'The type of a discussions feature.',
},
hideDiscussionTab: {
id: 'authoring.discussions.hide-discussion-tab',
defaultMessage: 'Hide discussion tab',
description: 'Title message to hide discussion tab',
},
hideDiscussionTabTitle: {
id: 'authoring.discussions.hide-tab-title',
defaultMessage: 'Hide the discussion tab?',
Expand Down

0 comments on commit 84c0c80

Please sign in to comment.