fix(client): Store backend form value on submit

This commit is contained in:
Oliver Eyton-Williams
2019-05-21 17:27:08 +02:00
committed by mrugesh
parent 67028025d1
commit c99366fa71
2 changed files with 25 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import { Grid, Col, Row } from '@freecodecamp/react-bootstrap';
import { createSelector } from 'reselect'; import { createSelector } from 'reselect';
import { reduxForm } from 'redux-form'; import { reduxForm } from 'redux-form';
import { graphql } from 'gatsby'; import { graphql } from 'gatsby';
import normalizeUrl from 'normalize-url';
import { import {
executeChallenge, executeChallenge,
@ -11,6 +12,7 @@ import {
challengeTestsSelector, challengeTestsSelector,
consoleOutputSelector, consoleOutputSelector,
initTests, initTests,
updateBackendFormValues,
updateChallengeMeta, updateChallengeMeta,
updateProjectFormValues, updateProjectFormValues,
backendNS backendNS
@ -55,6 +57,7 @@ const propTypes = {
output: PropTypes.string, output: PropTypes.string,
tests: PropTypes.array, tests: PropTypes.array,
title: PropTypes.string, title: PropTypes.string,
updateBackendFormValues: PropTypes.func.isRequired,
updateChallengeMeta: PropTypes.func.isRequired, updateChallengeMeta: PropTypes.func.isRequired,
updateProjectFormValues: PropTypes.func.isRequired, updateProjectFormValues: PropTypes.func.isRequired,
...reduxFormPropTypes ...reduxFormPropTypes
@ -79,6 +82,7 @@ const mapDispatchToActions = {
challengeMounted, challengeMounted,
executeChallenge, executeChallenge,
initTests, initTests,
updateBackendFormValues,
updateChallengeMeta, updateChallengeMeta,
updateProjectFormValues updateProjectFormValues
}; };
@ -96,6 +100,7 @@ export class BackEnd extends Component {
super(props); super(props);
this.state = {}; this.state = {};
this.updateDimensions = this.updateDimensions.bind(this); this.updateDimensions = this.updateDimensions.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
} }
componentDidMount() { componentDidMount() {
@ -151,6 +156,13 @@ export class BackEnd extends Component {
} }
} }
handleSubmit(values) {
const { updateBackendFormValues, executeChallenge } = this.props;
values.solution = normalizeUrl(values.solution);
updateBackendFormValues(values);
executeChallenge();
}
render() { render() {
const { const {
data: { data: {
@ -174,6 +186,7 @@ export class BackEnd extends Component {
? 'Submit and go to my next challenge' ? 'Submit and go to my next challenge'
: "I've completed this challenge"; : "I've completed this challenge";
const blockNameTitle = `${blockName} - ${title}`; const blockNameTitle = `${blockName} - ${title}`;
return ( return (
<LearnLayout> <LearnLayout>
<Grid> <Grid>
@ -191,7 +204,7 @@ export class BackEnd extends Component {
formFields={formFields} formFields={formFields}
id={backendNS} id={backendNS}
options={options} options={options}
submit={executeChallenge} submit={this.handleSubmit}
/> />
) : ( ) : (
<ProjectForm <ProjectForm

View File

@ -49,6 +49,7 @@ export const types = createTypes(
'initTests', 'initTests',
'initConsole', 'initConsole',
'initLogs', 'initLogs',
'updateBackendFormValues',
'updateConsole', 'updateConsole',
'updateChallengeMeta', 'updateChallengeMeta',
'updateFile', 'updateFile',
@ -125,6 +126,9 @@ export const updateTests = createAction(types.updateTests);
export const initConsole = createAction(types.initConsole); export const initConsole = createAction(types.initConsole);
export const initLogs = createAction(types.initLogs); export const initLogs = createAction(types.initLogs);
export const updateBackendFormValues = createAction(
types.updateBackendFormValues
);
export const updateChallengeMeta = createAction(types.updateChallengeMeta); export const updateChallengeMeta = createAction(types.updateChallengeMeta);
export const updateFile = createAction(types.updateFile); export const updateFile = createAction(types.updateFile);
export const updateConsole = createAction(types.updateConsole); export const updateConsole = createAction(types.updateConsole);
@ -171,7 +175,8 @@ export const isResetModalOpenSelector = state => state[ns].modal.reset;
export const isBuildEnabledSelector = state => state[ns].isBuildEnabled; export const isBuildEnabledSelector = state => state[ns].isBuildEnabled;
export const successMessageSelector = state => state[ns].successMessage; export const successMessageSelector = state => state[ns].successMessage;
export const backendFormValuesSelector = state => state.form[backendNS] || {}; export const backendFormValuesSelector = state =>
state[ns].backendFormValues || {};
export const projectFormValuesSelector = state => export const projectFormValuesSelector = state =>
state[ns].projectFormValues || {}; state[ns].projectFormValues || {};
@ -187,7 +192,7 @@ export const challengeDataSelector = state => {
files: challengeFilesSelector(state) files: challengeFilesSelector(state)
}; };
} else if (challengeType === challengeTypes.backend) { } else if (challengeType === challengeTypes.backend) {
const { solution: { value: url } = {} } = backendFormValuesSelector(state); const { solution: url = {} } = backendFormValuesSelector(state);
challengeData = { challengeData = {
...challengeData, ...challengeData,
url url
@ -306,6 +311,10 @@ export const reducer = handleActions(
})), })),
consoleOut: '' consoleOut: ''
}), }),
[types.updateBackendFormValues]: (state, { payload }) => ({
...state,
backendFormValues: payload
}),
[types.updateProjectFormValues]: (state, { payload }) => ({ [types.updateProjectFormValues]: (state, { payload }) => ({
...state, ...state,
projectFormValues: payload projectFormValues: payload