fix(client): update store after any submission (#41329)

This commit is contained in:
Shaun Hamilton
2021-03-04 21:01:18 +00:00
committed by GitHub
parent cd40d47363
commit 36f58bcfdf
5 changed files with 21 additions and 9 deletions

View File

@ -476,6 +476,7 @@
"url-not-image": "URL must link directly to an image file", "url-not-image": "URL must link directly to an image file",
"use-valid-url": "Please use a valid URL", "use-valid-url": "Please use a valid URL",
"editor-url": "Remember to submit the Live App URL.", "editor-url": "Remember to submit the Live App URL.",
"http-url": "An unsecure (http) URL cannot be used.",
"own-work-url": "Remember to submit your own work.", "own-work-url": "Remember to submit your own work.",
"publicly-visible-url": "Remember to submit a publicly visible app URL." "publicly-visible-url": "Remember to submit a publicly visible app URL."
}, },

View File

@ -15,7 +15,8 @@ import {
editorValidator, editorValidator,
localhostValidator, localhostValidator,
composeValidators, composeValidators,
fCCValidator fCCValidator,
httpValidator
} from './FormValidators'; } from './FormValidators';
const propTypes = { const propTypes = {
@ -39,7 +40,8 @@ function FormFields(props) {
placeholders = {}, placeholders = {},
required = [], required = [],
types = {}, types = {},
isEditorLinkAllowed = false isEditorLinkAllowed = false,
isLocalLinkAllowed = false
} = options; } = options;
const nullOrWarning = (value, error, isURL, name) => { const nullOrWarning = (value, error, isURL, name) => {
@ -54,7 +56,8 @@ function FormFields(props) {
const validationWarning = composeValidators( const validationWarning = composeValidators(
name === 'githubLink' || isEditorLinkAllowed ? null : editorValidator, name === 'githubLink' || isEditorLinkAllowed ? null : editorValidator,
fCCValidator, fCCValidator,
localhostValidator httpValidator,
isLocalLinkAllowed ? null : localhostValidator
)(value); )(value);
const message = error || validationError || validationWarning; const message = error || validationError || validationWarning;
return message ? ( return message ? (

View File

@ -5,6 +5,7 @@ import { Trans } from 'react-i18next';
const editorRegex = /repl\.it\/(@|join\/)|glitch\.com\/edit\/#!|codesandbox\.io\/s\/|github\.com/; const editorRegex = /repl\.it\/(@|join\/)|glitch\.com\/edit\/#!|codesandbox\.io\/s\/|github\.com/;
const fCCRegex = /codepen\.io\/freecodecamp|freecodecamp\.rocks|github\.com\/freecodecamp/i; const fCCRegex = /codepen\.io\/freecodecamp|freecodecamp\.rocks|github\.com\/freecodecamp/i;
const localhostRegex = /localhost:/; const localhostRegex = /localhost:/;
const httpRegex = /http(?!s|([^s]+?localhost))/;
export const editorValidator = value => export const editorValidator = value =>
editorRegex.test(value) ? <Trans>validation.editor-url</Trans> : null; editorRegex.test(value) ? <Trans>validation.editor-url</Trans> : null;
@ -17,5 +18,8 @@ export const localhostValidator = value =>
<Trans>validation.publicly-visible-url</Trans> <Trans>validation.publicly-visible-url</Trans>
) : null; ) : null;
export const httpValidator = value =>
httpRegex.test(value) ? <Trans>validation.http-url</Trans> : null;
export const composeValidators = (...validators) => value => export const composeValidators = (...validators) => value =>
validators.reduce((error, validator) => error ?? validator?.(value), null); validators.reduce((error, validator) => error ?? validator?.(value), null);

View File

@ -3,7 +3,8 @@ import {
localhostValidator, localhostValidator,
editorValidator, editorValidator,
composeValidators, composeValidators,
fCCValidator fCCValidator,
httpValidator
} from './FormValidators'; } from './FormValidators';
export { default as BlockSaveButton } from './BlockSaveButton.js'; export { default as BlockSaveButton } from './BlockSaveButton.js';
@ -16,13 +17,14 @@ const normalizeOptions = {
}; };
export function formatUrlValues(values, options) { export function formatUrlValues(values, options) {
const { isEditorLinkAllowed, types } = options; const { isEditorLinkAllowed, isLocalLinkAllowed, types } = options;
const validatedValues = { values: {}, errors: [], invalidValues: [] }; const validatedValues = { values: {}, errors: [], invalidValues: [] };
const urlValues = Object.keys(values).reduce((result, key) => { const urlValues = Object.keys(values).reduce((result, key) => {
let value = values[key]; let value = values[key];
const nullOrWarning = composeValidators( const nullOrWarning = composeValidators(
fCCValidator, fCCValidator,
localhostValidator, httpValidator,
isLocalLinkAllowed ? null : localhostValidator,
key === 'githubLink' || isEditorLinkAllowed ? null : editorValidator key === 'githubLink' || isEditorLinkAllowed ? null : editorValidator
)(value); )(value);
if (nullOrWarning) { if (nullOrWarning) {

View File

@ -31,9 +31,9 @@ export class SolutionForm extends Component {
handleSubmit(validatedValues) { handleSubmit(validatedValues) {
// Do not execute challenge, if errors // Do not execute challenge, if errors
if (validatedValues.errors.length === 0) { if (validatedValues.errors.length === 0) {
// updates values on store
this.props.updateSolutionForm(validatedValues.values);
if (validatedValues.invalidValues.length === 0) { if (validatedValues.invalidValues.length === 0) {
// updates values on server
this.props.updateSolutionForm(validatedValues.values);
this.props.onSubmit({ isShouldCompletionModalOpen: true }); this.props.onSubmit({ isShouldCompletionModalOpen: true });
} else { } else {
this.props.onSubmit({ isShouldCompletionModalOpen: false }); this.props.onSubmit({ isShouldCompletionModalOpen: false });
@ -59,7 +59,8 @@ export class SolutionForm extends Component {
githubLink: 'url' githubLink: 'url'
}, },
required: ['solution'], required: ['solution'],
isEditorLinkAllowed: false isEditorLinkAllowed: false,
isLocalLinkAllowed: false
}; };
const buttonCopy = isSubmitting const buttonCopy = isSubmitting
@ -79,6 +80,7 @@ export class SolutionForm extends Component {
case backend: case backend:
formFields = solutionField; formFields = solutionField;
options.isLocalLinkAllowed = true;
solutionLink = solutionLink + 'https://project-name.camperbot.repl.co/'; solutionLink = solutionLink + 'https://project-name.camperbot.repl.co/';
break; break;