mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
refactor(client): simplify form logic (#54613)
This commit is contained in:
committed by
GitHub
parent
7651802fea
commit
0b3238e0e8
@@ -117,35 +117,16 @@ class InternetSettings extends Component<InternetProps, InternetState> {
|
||||
|
||||
isFormPristine = () => {
|
||||
const { formValues, originalValues } = this.state;
|
||||
return (Object.keys(originalValues) as Array<keyof Socials>)
|
||||
.map(key => originalValues[key] === formValues[key])
|
||||
.every(bool => bool);
|
||||
};
|
||||
|
||||
isFormValid = (): boolean => {
|
||||
const { formValues, originalValues } = this.state;
|
||||
const valueReducer = (obj: Socials) => {
|
||||
return Object.values(obj).reduce(
|
||||
(acc, cur): boolean => (acc ? acc : cur !== ''),
|
||||
false
|
||||
) as boolean;
|
||||
};
|
||||
|
||||
const formHasValues = valueReducer(formValues);
|
||||
const OriginalHasValues = valueReducer(originalValues);
|
||||
|
||||
// check if user had values but wants to delete them all
|
||||
if (OriginalHasValues && !formHasValues) return true;
|
||||
|
||||
return (Object.keys(formValues) as Array<keyof Socials>).reduce(
|
||||
(bool: boolean, key: keyof Socials): boolean => {
|
||||
const maybeUrl = formValues[key];
|
||||
return maybeUrl ? isURL(maybeUrl) : bool;
|
||||
},
|
||||
false
|
||||
return (Object.keys(originalValues) as Array<keyof Socials>).every(
|
||||
key => originalValues[key] === formValues[key]
|
||||
);
|
||||
};
|
||||
|
||||
isFormValid = (): boolean =>
|
||||
Object.values(this.state.formValues).every(
|
||||
(value: string) => value === '' || isURL(value)
|
||||
);
|
||||
|
||||
handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!this.isFormPristine() && this.isFormValid()) {
|
||||
|
||||
Reference in New Issue
Block a user