Files
freeCodeCamp/client/src/templates/Challenges/projects/solution-form.tsx
awu43 dd5d2919be feat(client): ts-migrate client/utils/** (#42823)
* rename js files

* update imports and references

* migrate build-challenges

* migrate challenge-types

* migrate utils/index

* migrate state-management

* install @types/psl for tags

* migrate tags

* migrate tags.test

* migrate challenge-page-creator

* migrate utils/gatsby/index

* migrate layout-selector

* migrate layout-selector.test

* revert challenge-types

Curriculum can't handle TS or modules

* convert arrow functions

* revert build-challenges

* revert utils/gatsby/index

* revert challenge-page-creator

* revert challenge-types reference

* Delete state-management

Deleted in #42960

* Disable render-result-naming-convention (for now)

* update layout-selector.test comment

* reorder imports in build-challenges

* change ts-ignore to ts-expect-error
2021-08-09 10:30:31 +02:00

135 lines
3.6 KiB
TypeScript

import React, { Component } from 'react';
import { withTranslation } from 'react-i18next';
import type { WithTranslation } from 'react-i18next';
import {
backend,
backEndProject,
frontEndProject,
pythonProject
} from '../../../../utils/challenge-types';
import { Form } from '../../../components/formHelpers';
interface SubmitProps {
isShouldCompletionModalOpen: boolean;
}
interface FormProps extends WithTranslation {
challengeType: number;
description?: string;
onSubmit: (arg0: SubmitProps) => void;
updateSolutionForm: (arg0: Record<string, unknown>) => void;
}
interface ValidatedValues {
errors: string[];
invalidValues: string[];
values: Record<string, unknown>;
}
export class SolutionForm extends Component<FormProps> {
constructor(props: FormProps) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
componentDidMount(): void {
this.props.updateSolutionForm({});
}
handleSubmit = (validatedValues: ValidatedValues): void => {
// Do not execute challenge, if errors
if (validatedValues.errors.length === 0) {
// updates values on store
this.props.updateSolutionForm(validatedValues.values);
if (validatedValues.invalidValues.length === 0) {
this.props.onSubmit({ isShouldCompletionModalOpen: true });
} else {
this.props.onSubmit({ isShouldCompletionModalOpen: false });
}
}
};
render(): JSX.Element {
const { 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 buttonCopy = t('learn.i-completed');
const options = {
types: {
solution: 'url',
githubLink: 'url'
},
required: ['solution'],
isEditorLinkAllowed: false,
isLocalLinkAllowed: false
};
let formFields = solutionField;
let solutionLink = 'ex: ';
let solutionFormID = 'front-end-form';
switch (challengeType) {
case frontEndProject:
formFields = solutionField;
solutionLink =
solutionLink + 'https://codepen.io/camperbot/full/oNvPqqo';
break;
case backend:
formFields = solutionField;
options.isLocalLinkAllowed = true;
solutionLink = solutionLink + 'https://project-name.camperbot.repl.co/';
break;
case backEndProject:
formFields = backEndProjectFields;
solutionLink = solutionLink + 'https://project-name.camperbot.repl.co/';
solutionFormID = 'back-end-form';
break;
case pythonProject:
formFields = solutionField;
options.isEditorLinkAllowed = true;
solutionLink =
solutionLink +
(description?.includes('Colaboratory')
? 'https://colab.research.google.com/drive/1i5EmInTWi1RFvFr2_aRXky96YxY6sbWy'
: 'https://replit.com/@camperbot/hello');
break;
default:
formFields = solutionField;
solutionLink =
solutionLink + 'https://codepen.io/camperbot/full/oNvPqqo';
}
return (
<Form
buttonText={`${buttonCopy}`}
formFields={formFields}
id={solutionFormID}
options={{
...options,
placeholders: {
solution: solutionLink,
githubLink: 'ex: https://github.com/camperbot/hello'
}
}}
submit={this.handleSubmit}
/>
);
}
}
export default withTranslation()(SolutionForm);