Skip to content

Commit

Permalink
Merge pull request #294 from aevo98765/tech-debt/#293/author-informat…
Browse files Browse the repository at this point in the history
…ion-duplication

Tech debt/#293/author information duplication
  • Loading branch information
nerdalert authored Oct 28, 2024
2 parents 8c9d979 + 3e437c8 commit 5a7a109
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 126 deletions.
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 @@ -371,8 +372,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

0 comments on commit 5a7a109

Please sign in to comment.