Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tech debt/#293/author information duplication #294

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,37 @@ import { HelperText } from '@patternfly/react-core/dist/dynamic/components/Helpe
import { HelperTextItem } from '@patternfly/react-core/dist/dynamic/components/HelperText';
import ExclamationCircleIcon from '@patternfly/react-icons/dist/dynamic/icons/exclamation-circle-icon';
import { ValidatedOptions } from '@patternfly/react-core/dist/esm/helpers/constants';
import { KnowledgeFormData } from '..';
import { checkKnowledgeFormCompletion } from '../validation';
import { checkSkillFormCompletion } from './Skill/validation';
import { checkKnowledgeFormCompletion } from './Knowledge/validation';

export enum FormType {
Knowledge,
Skill
}

interface Props {
formType: FormType;
reset: boolean;
knowledgeFormData: KnowledgeFormData;
formData: object;
setDisableAction: React.Dispatch<React.SetStateAction<boolean>>;
email: string;
setEmail: React.Dispatch<React.SetStateAction<string>>;
name: string;
setName: React.Dispatch<React.SetStateAction<string>>;
}
const AuthorInformation: React.FC<Props> = ({ reset, knowledgeFormData, setDisableAction, email, setEmail, name, setName }) => {
const AuthorInformation: React.FC<Props> = ({ formType, reset, formData, setDisableAction, email, setEmail, name, setName }) => {
const [validEmail, setValidEmail] = useState<ValidatedOptions>();
const [validName, setValidName] = useState<ValidatedOptions>();

const validateEmail = (email: string) => {
const re = /\S+@\S+\.\S+/;
if (re.test(email)) {
setValidEmail(ValidatedOptions.success);
setDisableAction(!checkKnowledgeFormCompletion(knowledgeFormData));
if (formType === FormType.Knowledge) {
setDisableAction(!checkKnowledgeFormCompletion(formData));
return;
}
setDisableAction(!checkSkillFormCompletion(formData));
return;
}
setDisableAction(true);
Expand All @@ -36,7 +46,11 @@ const AuthorInformation: React.FC<Props> = ({ reset, knowledgeFormData, setDisab
const validateName = (name: string) => {
if (name.length > 0) {
setValidName(ValidatedOptions.success);
setDisableAction(!checkKnowledgeFormCompletion(knowledgeFormData));
if (formType === FormType.Knowledge) {
setDisableAction(!checkKnowledgeFormCompletion(formData));
return;
}
setDisableAction(!checkSkillFormCompletion(formData));
return;
}
setDisableAction(true);
Expand Down
6 changes: 4 additions & 2 deletions src/components/Contribute/Knowledge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { ActionGroup } from '@patternfly/react-core/dist/dynamic/components/Form
import { Form } from '@patternfly/react-core/dist/dynamic/components/Form';
import { getGitHubUsername } from '../../../utils/github';
import { useSession } from 'next-auth/react';
import AuthorInformation from './AuthorInformation/AuthorInformation';
import AuthorInformation from '../AuthorInformation';
import { FormType } from '../AuthorInformation';
import KnowledgeInformation from './KnowledgeInformation/KnowledgeInformation';
import FilePathInformation from './FilePathInformation/FilePathInformation';
import DocumentInformation from './DocumentInformation/DocumentInformation';
Expand Down Expand Up @@ -481,8 +482,9 @@ export const KnowledgeForm: React.FunctionComponent<KnowledgeFormProps> = ({ kno

<Form className="form-k">
<AuthorInformation
formType={FormType.Knowledge}
reset={reset}
knowledgeFormData={knowledgeFormData}
formData={knowledgeFormData}
setDisableAction={setDisableAction}
email={email}
setEmail={setEmail}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Contribute/Knowledge/validation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const validateFields = (
return true;
};

export const checkKnowledgeFormCompletion = (knowledgeFormData: KnowledgeFormData): boolean => {
export const checkKnowledgeFormCompletion = (knowledgeFormData: object): boolean => {
// Helper function to check if a value is non-empty
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isNonEmpty = (value: any): boolean => {
Expand Down

This file was deleted.

6 changes: 4 additions & 2 deletions src/components/Contribute/Skill/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { ActionGroup } from '@patternfly/react-core/dist/dynamic/components/Form
import { Form } from '@patternfly/react-core/dist/dynamic/components/Form';
import { getGitHubUsername } from '../../../utils/github';
import { useSession } from 'next-auth/react';
import AuthorInformation from './AuthorInformation/AuthorInformation';
import AuthorInformation from '../AuthorInformation';
import { FormType } from '../AuthorInformation';
import FilePathInformation from './FilePathInformation/FilePathInformation';
import AttributionInformation from './AttributionInformation/AttributionInformation';
import Submit from './Submit/Submit';
Expand Down Expand Up @@ -364,8 +365,9 @@ export const SkillForm: React.FunctionComponent<SkillFormProps> = ({ skillEditFo
)}
<Form className="form-s">
<AuthorInformation
formType={FormType.Knowledge}
reset={reset}
skillFormData={skillFormData}
formData={skillFormData}
setDisableAction={setDisableAction}
email={email}
setEmail={setEmail}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Contribute/Skill/validation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const validateFields = (

const optionalKeys = ['context', 'isContextValid', 'validationError', 'questionValidationError', 'answerValidationError'];

export const checkSkillFormCompletion = (skillFormData: SkillFormData): boolean => {
export const checkSkillFormCompletion = (skillFormData: object): boolean => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isObject = (value: Record<string, any>): boolean => {
return value && typeof value === 'object' && !Array.isArray(value);
Expand Down