2018-11-07 18:16:50 +00:00
|
|
|
import React, { Component } from 'react';
|
2018-04-13 15:33:03 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2020-12-16 02:02:52 -06:00
|
|
|
import { withTranslation } from 'react-i18next';
|
2018-04-13 15:33:03 +01:00
|
|
|
|
2019-06-11 18:46:36 +03:00
|
|
|
import { Form } from '../../../components/formHelpers';
|
2020-05-22 03:24:17 +05:30
|
|
|
import {
|
2020-09-04 14:44:26 +01:00
|
|
|
backend,
|
2020-05-22 03:24:17 +05:30
|
|
|
backEndProject,
|
|
|
|
frontEndProject,
|
|
|
|
pythonProject
|
|
|
|
} from '../../../../utils/challengeTypes';
|
2018-04-13 15:33:03 +01:00
|
|
|
|
|
|
|
const propTypes = {
|
2020-05-22 03:24:17 +05:30
|
|
|
challengeType: PropTypes.number,
|
|
|
|
description: PropTypes.string,
|
2018-05-09 13:27:42 +01:00
|
|
|
isSubmitting: PropTypes.bool,
|
2019-06-11 18:46:36 +03:00
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2020-12-16 02:02:52 -06:00
|
|
|
t: PropTypes.func.isRequired,
|
2020-05-07 17:19:06 +01:00
|
|
|
updateSolutionForm: PropTypes.func.isRequired
|
2018-04-13 15:33:03 +01:00
|
|
|
};
|
|
|
|
|
2020-05-07 17:19:06 +01:00
|
|
|
export class SolutionForm extends Component {
|
2018-06-20 11:59:20 +01:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
|
|
}
|
2018-05-24 19:45:38 +01:00
|
|
|
componentDidMount() {
|
2020-05-07 17:19:06 +01:00
|
|
|
this.props.updateSolutionForm({});
|
2018-06-20 11:59:20 +01:00
|
|
|
}
|
2021-02-01 13:34:04 +00:00
|
|
|
|
|
|
|
handleSubmit(validatedValues) {
|
|
|
|
// Do not execute challenge, if errors
|
|
|
|
if (validatedValues.errors.length === 0) {
|
2021-03-04 21:01:18 +00:00
|
|
|
// updates values on store
|
|
|
|
this.props.updateSolutionForm(validatedValues.values);
|
2021-02-01 13:34:04 +00:00
|
|
|
if (validatedValues.invalidValues.length === 0) {
|
|
|
|
this.props.onSubmit({ isShouldCompletionModalOpen: true });
|
|
|
|
} else {
|
|
|
|
this.props.onSubmit({ isShouldCompletionModalOpen: false });
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 11:59:20 +01:00
|
|
|
}
|
2021-02-01 13:34:04 +00:00
|
|
|
|
2018-04-13 15:33:03 +01:00
|
|
|
render() {
|
2020-12-16 02:02:52 -06:00
|
|
|
const { isSubmitting, challengeType, description, t } = this.props;
|
|
|
|
|
|
|
|
// back end challenges and front end projects use a single form field
|
|
|
|
const solutionField = [
|
|
|
|
{ name: 'solution', label: t('learn.solution-link') }
|
|
|
|
];
|
|
|
|
const backEndProjectFields = [
|
|
|
|
{ name: 'solution', label: t('learn.solution-link') },
|
|
|
|
{ name: 'githubLink', label: t('learn.github-link') }
|
|
|
|
];
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
types: {
|
|
|
|
solution: 'url',
|
|
|
|
githubLink: 'url'
|
|
|
|
},
|
2021-02-10 11:31:08 +00:00
|
|
|
required: ['solution'],
|
2021-03-04 21:01:18 +00:00
|
|
|
isEditorLinkAllowed: false,
|
|
|
|
isLocalLinkAllowed: false
|
2020-12-16 02:02:52 -06:00
|
|
|
};
|
|
|
|
|
2018-04-13 15:33:03 +01:00
|
|
|
const buttonCopy = isSubmitting
|
2020-12-16 02:02:52 -06:00
|
|
|
? t('learn.submit-and-go')
|
|
|
|
: t('learn.i-completed');
|
2020-05-22 03:24:17 +05:30
|
|
|
|
2020-09-04 14:44:26 +01:00
|
|
|
let formFields = solutionField;
|
|
|
|
let solutionLink = 'ex: ';
|
2020-05-22 03:24:17 +05:30
|
|
|
let solutionFormID = 'front-end-form';
|
|
|
|
|
|
|
|
switch (challengeType) {
|
|
|
|
case frontEndProject:
|
2020-09-04 14:44:26 +01:00
|
|
|
formFields = solutionField;
|
2020-05-22 03:24:17 +05:30
|
|
|
solutionLink =
|
|
|
|
solutionLink + 'https://codepen.io/camperbot/full/oNvPqqo';
|
|
|
|
break;
|
|
|
|
|
2020-09-04 14:44:26 +01:00
|
|
|
case backend:
|
|
|
|
formFields = solutionField;
|
2021-03-04 21:01:18 +00:00
|
|
|
options.isLocalLinkAllowed = true;
|
2020-09-04 14:44:26 +01:00
|
|
|
solutionLink = solutionLink + 'https://project-name.camperbot.repl.co/';
|
|
|
|
break;
|
|
|
|
|
2020-05-22 03:24:17 +05:30
|
|
|
case backEndProject:
|
2020-09-04 14:44:26 +01:00
|
|
|
formFields = backEndProjectFields;
|
2020-08-18 06:38:16 +09:00
|
|
|
solutionLink = solutionLink + 'https://project-name.camperbot.repl.co/';
|
2020-05-22 03:24:17 +05:30
|
|
|
solutionFormID = 'back-end-form';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case pythonProject:
|
2020-09-04 14:44:26 +01:00
|
|
|
formFields = solutionField;
|
2021-02-10 11:31:08 +00:00
|
|
|
options.isEditorLinkAllowed = true;
|
2020-05-22 03:24:17 +05:30
|
|
|
solutionLink =
|
|
|
|
solutionLink +
|
|
|
|
(description.includes('Colaboratory')
|
|
|
|
? 'https://colab.research.google.com/drive/1i5EmInTWi1RFvFr2_aRXky96YxY6sbWy'
|
2021-04-29 06:13:38 -04:00
|
|
|
: 'https://replit.com/@camperbot/hello');
|
2020-05-22 03:24:17 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-09-04 14:44:26 +01:00
|
|
|
formFields = solutionField;
|
2020-05-22 03:24:17 +05:30
|
|
|
solutionLink =
|
|
|
|
solutionLink + 'https://codepen.io/camperbot/full/oNvPqqo';
|
|
|
|
}
|
|
|
|
|
2018-04-13 15:33:03 +01:00
|
|
|
return (
|
|
|
|
<Form
|
2019-06-11 18:46:36 +03:00
|
|
|
buttonText={`${buttonCopy}`}
|
2020-09-04 14:44:26 +01:00
|
|
|
formFields={formFields}
|
2020-05-22 03:24:17 +05:30
|
|
|
id={solutionFormID}
|
2019-09-19 12:44:58 +05:30
|
|
|
options={{
|
|
|
|
...options,
|
|
|
|
placeholders: {
|
2020-09-08 19:46:24 +02:00
|
|
|
solution: solutionLink,
|
2020-05-07 17:19:06 +01:00
|
|
|
githubLink: 'ex: https://github.com/camperbot/hello'
|
2019-09-19 12:44:58 +05:30
|
|
|
}
|
|
|
|
}}
|
2018-04-13 15:33:03 +01:00
|
|
|
submit={this.handleSubmit}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 17:19:06 +01:00
|
|
|
SolutionForm.propTypes = propTypes;
|
2018-04-13 15:33:03 +01:00
|
|
|
|
2020-12-16 02:02:52 -06:00
|
|
|
export default withTranslation()(SolutionForm);
|