Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: modal actions #68

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/components/ModalActions/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';
import PropTypes from 'prop-types';

import { Button, StatefulButton } from '@edx/paragon';

import { MutationStatus } from 'data/services/lms/constants';
import { useCloseModal } from 'hooks';

const ModalActions = (props) => {
// const { secondary } = props;
const closeModal = useCloseModal();
const { primary } = props;
const secondary = props.secondary && {
...props.secondary,
onClick: closeModal,
};
const className = 'w-100';
const disabledStates = [MutationStatus.loading];
console.log(props);
const genButton = (variant, btnProps) => (btnProps.state
? <StatefulButton {...btnProps} {...{ className, disabledStates, variant }} />
: <Button {...btnProps} {...{ className, variant }} />);

return (
<div>
{secondary && genButton('outline-primary', secondary)}
{primary && genButton('primary', primary)}
</div>
);
};
const actionProps = PropTypes.shape({
onClick: PropTypes.func,
state: PropTypes.string,
disabledStates: PropTypes.arrayOf(PropTypes.string),
labels: PropTypes.objectOf(PropTypes.node),
});
ModalActions.defaultProps = {
secondary: null,
primary: null,
};
ModalActions.propTypes = {
secondary: actionProps,
primary: actionProps,
};
export default ModalActions;
5 changes: 5 additions & 0 deletions src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export const nullMethod = () => ({});
export const useActiveView = () => useLocation().pathname.split('/')[1];
export const useIsEmbedded = () => useLocation().pathname.split('/')[2] === 'embedded';

export const useCloseModal = () => {
const postMessage = (data) => window.parent.postMessage(data, document.referrer);
return () => postMessage({ type: 'plugin.modal-close' });
};

export const useOpenModal = () => {
const postMessage = (data) => window.parent.postMessage(data, document.referrer);
const viewUrl = useViewUrl();
Expand Down
57 changes: 0 additions & 57 deletions src/views/SubmissionView/Actions.jsx

This file was deleted.

25 changes: 21 additions & 4 deletions src/views/SubmissionView/hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback, useEffect } from 'react';

import { StrictDict, useKeyedState } from '@edx/react-unit-test-utils';
import { useIntl } from '@edx/frontend-platform/i18n';
import {
usePageData,
usePrompts,
Expand All @@ -12,6 +13,7 @@ import {
useSubmitResponse, useSaveResponse, useUploadFiles, useDeleteFile,
} from 'data/services/lms/hooks/actions';
import { MutationStatus } from 'data/services/lms/constants';
import messages from './messages';

export const stateKeys = StrictDict({
textResponses: 'textResponses',
Expand Down Expand Up @@ -87,6 +89,7 @@ const useSubmissionViewData = () => {
const textResponses = useTextResponses();
const uploadedFiles = useUploadedFiles();
const { showDuringResponse } = useRubricConfig();
const { formatMessage } = useIntl();

const submitResponseHandler = useCallback(() => {
submitResponseMutation.mutate({
Expand All @@ -97,11 +100,25 @@ const useSubmissionViewData = () => {

return {
actionsProps: {
submitResponse: {
handler: submitResponseHandler,
status: submitResponseMutation.status,
primary: {
onClick: submitResponseHandler,
state: submitResponseMutation.status,
disabledStates: [MutationStatus.loading],
labels: {
[MutationStatus.idle]: formatMessage(messages.submissionActionSubmit),
[MutationStatus.loading]: formatMessage(messages.submissionActionSubmitting),
[MutationStatus.success]: formatMessage(messages.submissionActionSubmitted),
},
},
secondary: {
onClick: textResponses.saveResponse.handler,
state: textResponses.saveResponse.status,
disabledStates: [MutationStatus.loading],
labels: {
[MutationStatus.idle]: formatMessage(messages.saveActionSave),
[MutationStatus.loading]: formatMessage(messages.saveActionSaving),
},
},
saveResponse: textResponses.saveResponse,
},
formProps: {
textResponses: textResponses.formProps,
Expand Down
27 changes: 11 additions & 16 deletions src/views/SubmissionView/index.jsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
import React from 'react';
import PropTypes from 'prop-types';

import { Col, Row } from '@edx/paragon';

import Rubric from 'components/Rubric';
import ProgressBar from 'components/ProgressBar';
import { useIsPageDataLoaded } from 'data/services/lms/hooks/selectors';
import Actions from 'components/ModalActions';

import Content from './Content';
import Actions from './Actions';
import useSubmissionViewData from './hooks';

import './index.scss';

export const SubmissionView = () => {
const { actionsProps, formProps, showRubric } = useSubmissionViewData();
return (
<>
<div className="assessment-content-layout mr-auto ml-auto">
<div className="content-wrapper">
<Row className="flex-nowrap m-0">
<Col className="p-0">
<Content {...formProps} />
</Col>
{showRubric && <Rubric />}
</Row>
</div>
<div className="assessment-content-layout mr-auto ml-auto">
<div className="content-wrapper">
<Row className="flex-nowrap m-0">
<Col className="p-0">
<Content {...formProps} />
<Actions {...actionsProps} />
</Col>
{showRubric && <Rubric />}
</Row>
</div>
<Actions {...actionsProps} />
</>
</div>
);
};

Expand Down