2018-04-06 14:51:52 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import noop from 'lodash/noop';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { createSelector } from 'reselect';
|
2018-09-30 11:37:19 +01:00
|
|
|
import { Button, Modal } from '@freecodecamp/react-bootstrap';
|
2018-04-06 14:51:52 +01:00
|
|
|
|
2018-05-18 19:07:32 +01:00
|
|
|
import ga from '../../../analytics';
|
2018-04-08 23:19:50 +01:00
|
|
|
import GreenPass from './icons/GreenPass';
|
2018-04-06 14:51:52 +01:00
|
|
|
|
2018-07-06 10:45:08 +01:00
|
|
|
import { dasherize } from '../../../../utils';
|
|
|
|
|
2018-05-18 14:54:21 +01:00
|
|
|
import './completion-modal.css';
|
|
|
|
|
2018-04-06 14:51:52 +01:00
|
|
|
import {
|
|
|
|
closeModal,
|
|
|
|
submitChallenge,
|
|
|
|
isCompletionModalOpenSelector,
|
2018-07-06 10:45:08 +01:00
|
|
|
successMessageSelector,
|
|
|
|
challengeFilesSelector,
|
|
|
|
challengeMetaSelector
|
2018-04-08 23:19:50 +01:00
|
|
|
} from '../redux';
|
2018-04-06 14:51:52 +01:00
|
|
|
|
|
|
|
const mapStateToProps = createSelector(
|
2018-07-06 10:45:08 +01:00
|
|
|
challengeFilesSelector,
|
|
|
|
challengeMetaSelector,
|
2018-04-06 14:51:52 +01:00
|
|
|
isCompletionModalOpenSelector,
|
|
|
|
successMessageSelector,
|
2018-07-06 10:45:08 +01:00
|
|
|
(files, { title }, isOpen, message) => ({
|
|
|
|
files,
|
|
|
|
title,
|
2018-04-06 14:51:52 +01:00
|
|
|
isOpen,
|
|
|
|
message
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const mapDispatchToProps = function(dispatch) {
|
|
|
|
const dispatchers = {
|
|
|
|
close: () => dispatch(closeModal('completion')),
|
|
|
|
handleKeypress: e => {
|
|
|
|
if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
|
|
|
|
dispatch(submitChallenge());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
submitChallenge: () => {
|
|
|
|
dispatch(submitChallenge());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return () => dispatchers;
|
|
|
|
};
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
close: PropTypes.func.isRequired,
|
2018-07-06 10:45:08 +01:00
|
|
|
files: PropTypes.object.isRequired,
|
2018-04-06 14:51:52 +01:00
|
|
|
handleKeypress: PropTypes.func.isRequired,
|
|
|
|
isOpen: PropTypes.bool,
|
|
|
|
message: PropTypes.string,
|
2018-07-06 10:45:08 +01:00
|
|
|
submitChallenge: PropTypes.func.isRequired,
|
|
|
|
title: PropTypes.string
|
2018-04-06 14:51:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export class CompletionModal extends PureComponent {
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
close,
|
|
|
|
isOpen,
|
|
|
|
submitChallenge,
|
|
|
|
handleKeypress,
|
2018-07-06 10:45:08 +01:00
|
|
|
message,
|
|
|
|
files = {},
|
|
|
|
title
|
2018-04-06 14:51:52 +01:00
|
|
|
} = this.props;
|
2018-05-18 19:07:32 +01:00
|
|
|
if (isOpen) {
|
|
|
|
ga.modalview('/completion-modal');
|
|
|
|
}
|
2018-07-06 10:45:08 +01:00
|
|
|
const showDownloadButton = Object.keys(files).length;
|
|
|
|
const filesForDownload = Object.keys(files)
|
|
|
|
.map(key => files[key])
|
2018-09-13 22:09:12 +03:00
|
|
|
.reduce(
|
|
|
|
(allFiles, { path, contents }) => ({
|
|
|
|
...allFiles,
|
|
|
|
[path]: contents
|
|
|
|
}),
|
|
|
|
{}
|
|
|
|
);
|
2018-07-06 10:45:08 +01:00
|
|
|
const dashedName = dasherize(title);
|
2018-04-06 14:51:52 +01:00
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
animation={false}
|
2018-05-18 14:54:21 +01:00
|
|
|
bsSize='lg'
|
|
|
|
dialogClassName='challenge-success-modal'
|
2018-04-06 14:51:52 +01:00
|
|
|
keyboard={true}
|
|
|
|
onHide={close}
|
|
|
|
onKeyDown={isOpen ? handleKeypress : noop}
|
|
|
|
show={isOpen}
|
|
|
|
>
|
2018-05-18 14:54:21 +01:00
|
|
|
<Modal.Header
|
|
|
|
className='challenge-list-header fcc-modal'
|
|
|
|
closeButton={true}
|
|
|
|
>
|
|
|
|
<Modal.Title className='text-center'>{message}</Modal.Title>
|
2018-04-06 14:51:52 +01:00
|
|
|
</Modal.Header>
|
2018-05-18 14:54:21 +01:00
|
|
|
<Modal.Body className='completion-modal-body'>
|
|
|
|
<div className='success-icon-wrapper'>
|
|
|
|
<GreenPass />
|
2018-04-06 14:51:52 +01:00
|
|
|
</div>
|
|
|
|
</Modal.Body>
|
|
|
|
<Modal.Footer>
|
|
|
|
<Button
|
|
|
|
block={true}
|
|
|
|
bsSize='large'
|
|
|
|
bsStyle='primary'
|
|
|
|
onClick={submitChallenge}
|
|
|
|
>
|
|
|
|
Submit and go to next challenge (Ctrl + Enter)
|
|
|
|
</Button>
|
2018-09-13 22:09:12 +03:00
|
|
|
{showDownloadButton ? (
|
|
|
|
<Button
|
|
|
|
block={true}
|
|
|
|
bsSize='lg'
|
|
|
|
bsStyle='primary'
|
|
|
|
className='btn-primary-invert'
|
|
|
|
download={`${dashedName}.json`}
|
|
|
|
href={`data:text/json;charset=utf-8,${encodeURIComponent(
|
|
|
|
JSON.stringify(filesForDownload)
|
|
|
|
)}`}
|
|
|
|
>
|
|
|
|
Download my solution
|
|
|
|
</Button>
|
|
|
|
) : null}
|
2018-04-06 14:51:52 +01:00
|
|
|
</Modal.Footer>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CompletionModal.displayName = 'CompletionModal';
|
|
|
|
CompletionModal.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(CompletionModal);
|