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

[NCL-7898] Refactor useForm to add boolean value for preBuildSyncEnable #249

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/components/DemoPage/DemoPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export const DemoPage = () => {
type="text"
id="inputFieldA"
name="inputFieldA"
value={fields.inputFieldA.value}
value={fields.inputFieldA.value as string}
autoComplete="off"
onChange={(text) => {
onChange('inputFieldA', text);
Expand All @@ -278,7 +278,7 @@ export const DemoPage = () => {
<TextArea
id="textAreaA"
name="textAreaA"
value={fields.textAreaA.value}
value={fields.textAreaA.value as string}
onChange={(text) => {
onChange('textAreaA', text);
}}
Expand Down
12 changes: 6 additions & 6 deletions src/components/ProjectCreateEditPage/ProjectCreateEditPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export const ProjectCreateEditPage = ({ isEditPage = false }: IProjectCreateEdit
type="text"
id={projectEntityAttributes.name.id}
name={projectEntityAttributes.name.id}
value={fields.name.value}
value={fields.name.value as string}
autoComplete="off"
onChange={(name) => {
onChange('name', name);
Expand All @@ -177,7 +177,7 @@ export const ProjectCreateEditPage = ({ isEditPage = false }: IProjectCreateEdit
<TextArea
id={projectEntityAttributes.description.id}
name={projectEntityAttributes.description.id}
value={fields.description.value}
value={fields.description.value as string}
onChange={(description) => {
onChange('description', description);
}}
Expand All @@ -199,7 +199,7 @@ export const ProjectCreateEditPage = ({ isEditPage = false }: IProjectCreateEdit
id={projectEntityAttributes.projectUrl.id}
name={projectEntityAttributes.projectUrl.id}
autoComplete="off"
value={fields.projectUrl.value}
value={fields.projectUrl.value as string}
onChange={(url) => {
onChange('projectUrl', url);
}}
Expand All @@ -220,7 +220,7 @@ export const ProjectCreateEditPage = ({ isEditPage = false }: IProjectCreateEdit
id={projectEntityAttributes.issueTrackerUrl.id}
name={projectEntityAttributes.issueTrackerUrl.id}
autoComplete="off"
value={fields.issueTrackerUrl.value}
value={fields.issueTrackerUrl.value as string}
onChange={(url) => {
onChange('issueTrackerUrl', url);
}}
Expand All @@ -232,7 +232,7 @@ export const ProjectCreateEditPage = ({ isEditPage = false }: IProjectCreateEdit
id={projectEntityAttributes.engineeringTeam.id}
name={projectEntityAttributes.engineeringTeam.id}
autoComplete="off"
value={fields.engineeringTeam.value}
value={fields.engineeringTeam.value as string}
onChange={(engineeringTeam) => {
onChange('engineeringTeam', engineeringTeam);
}}
Expand All @@ -244,7 +244,7 @@ export const ProjectCreateEditPage = ({ isEditPage = false }: IProjectCreateEdit
id={projectEntityAttributes.technicalLeader.id}
name={projectEntityAttributes.technicalLeader.id}
autoComplete="off"
value={fields.technicalLeader.value}
value={fields.technicalLeader.value as string}
onChange={(technicalLeader) => {
onChange('technicalLeader', technicalLeader);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Form,
FormGroup,
FormHelperText,
Switch,
TextInput,
} from '@patternfly/react-core';
import { useEffect, useState } from 'react';
Expand Down Expand Up @@ -40,14 +41,16 @@ const createFormConfig = {
isRequired: true,
validators: [{ validator: validateScmUrl, errorMessage: 'Invalid SCM URL format.' }],
},
// preBuildSyncEnabled: {},
preBuildSyncEnabled: {
value: true,
},
};

const editFormConfig = {
externalUrl: {
validators: [{ validator: validateScmUrl, errorMessage: 'Invalid SCM URL format.' }],
},
// preBuildSyncEnabled: {},
preBuildSyncEnabled: {},
};

const flexDirectionColumn: FlexProps['direction'] = { default: 'column' };
Expand Down Expand Up @@ -83,7 +86,7 @@ export const ScmRepositoryCreateEditPage = ({ isEditPage = false }: IScmReposito
serviceData: {
data: {
scmUrl: data.scmUrl.value,
// preBuildSyncEnabled: data.preBuildSyncEnabled.value,
preBuildSyncEnabled: data.preBuildSyncEnabled.value,
},
},
})
Expand Down Expand Up @@ -124,7 +127,7 @@ export const ScmRepositoryCreateEditPage = ({ isEditPage = false }: IScmReposito
setId(scmRepository.id);
reinitialize({
externalUrl: scmRepository.externalUrl,
// preBuildSyncEnabled: scmRepository.preBuildSyncEnabled,
preBuildSyncEnabled: scmRepository.preBuildSyncEnabled,
});
});
} else {
Expand Down Expand Up @@ -161,7 +164,7 @@ export const ScmRepositoryCreateEditPage = ({ isEditPage = false }: IScmReposito
type="text"
id="scmUrl"
name="scmUrl"
value={fields.scmUrl.value}
value={fields.scmUrl.value as string}
autoComplete="off"
onChange={(scmUrl) => {
onChange('scmUrl', scmUrl);
Expand Down Expand Up @@ -196,7 +199,7 @@ export const ScmRepositoryCreateEditPage = ({ isEditPage = false }: IScmReposito
type="text"
id="externalUrl"
name="externalUrl"
value={fields.externalUrl.value}
value={fields.externalUrl.value as string}
autoComplete="off"
onChange={(externalUrl) => {
onChange('externalUrl', externalUrl);
Expand All @@ -205,7 +208,7 @@ export const ScmRepositoryCreateEditPage = ({ isEditPage = false }: IScmReposito
</FormGroup>
</>
)}
{/* <FormGroup
<FormGroup
label="Pre-build Sync"
fieldId="preBuildSyncEnabled"
helperText={
Expand All @@ -217,12 +220,14 @@ export const ScmRepositoryCreateEditPage = ({ isEditPage = false }: IScmReposito
<Switch
id="preBuildSyncEnabled"
name="preBuildSyncEnabled"
isChecked={fields.preBuildSyncEnabled.value?.toLowerCase() === 'true'}
label="Pre-build Sync Enabled"
labelOff="Pre-build Sync Disabled"
isChecked={fields.preBuildSyncEnabled?.value as boolean}
onChange={(preBuildSyncEnabled) => {
onChange('preBuildSyncEnabled', preBuildSyncEnabled);
}}
/>
</FormGroup> */}
</FormGroup>
<ActionGroup>
<Button
variant="primary"
Expand Down
23 changes: 13 additions & 10 deletions src/hooks/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TextInputProps } from '@patternfly/react-core';
import { useCallback, useEffect, useState } from 'react';

interface IFieldValues {
[key: string]: string | undefined;
[key: string]: string | boolean | undefined;
}

interface IValidator {
Expand All @@ -11,7 +11,7 @@ interface IValidator {
}

interface IField {
value?: string;
value?: string | boolean;
errorMessages?: string[];
state?: TextInputProps['validated'];
isRequired?: boolean;
Expand Down Expand Up @@ -98,7 +98,7 @@ export const useForm = (initFields: Omit<Omit<IFields, 'errorMessages'>, 'state'
// also delete old error messages, new checks are going to be done
const newField = {
...fields[fieldName],
value: fieldValue ? fieldValue : '',
value: typeof fieldValue === 'boolean' ? fieldValue : fieldValue ? fieldValue : '',
DnsZhou marked this conversation as resolved.
Show resolved Hide resolved
errorMessages: [],
state: 'default' as TextInputProps['validated'],
};
Expand All @@ -111,7 +111,7 @@ export const useForm = (initFields: Omit<Omit<IFields, 'errorMessages'>, 'state'
// validate field state and change error messages / state
const validate = (field: IField) => {
if (field.isRequired) {
if (!field.value?.trim()) {
if (typeof field.value === 'string' && !field.value?.trim()) {
addError(field, 'Field must be filled.');
}
setState(field);
Expand Down Expand Up @@ -152,15 +152,18 @@ export const useForm = (initFields: Omit<Omit<IFields, 'errorMessages'>, 'state'
const fieldsCopy = { ...fields };
for (const key in fieldsCopy) {
// trim just strings
fieldsCopy[key].value = typeof fieldsCopy[key].value === 'string' ? fieldsCopy[key].value?.trim() : fieldsCopy[key].value;
fieldsCopy[key].value =
typeof fieldsCopy[key].value === 'string' ? (fieldsCopy[key].value as string)?.trim() : fieldsCopy[key].value;
// reset state to 'default' (valid inputs wont be highlighted)
fieldsCopy[key].state = 'default';
}

submitCallback(fieldsCopy).catch((error: Error) => {
// error is displayed by the ServiceContainer
console.error(error);
});
submitCallback(fieldsCopy);

// submitCallback(fieldsCopy).catch((error: Error) => {
// // error is displayed by the ServiceContainer
// console.error(error);
// });
// .catch((error: any) => {
// FUTURE IMPLEMENTATION (backend error):
// const fieldsCopy = { ...fields };
Expand Down Expand Up @@ -197,7 +200,7 @@ export const useForm = (initFields: Omit<Omit<IFields, 'errorMessages'>, 'state'
// are all required inputs filled?
const areRequiredFilled = () => {
for (const key in fields) {
if (fields[key].isRequired && !fields[key].value?.trim()) {
if (fields[key].isRequired && typeof fields[key].value === 'string' && !(fields[key].value as string)?.trim()) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useServiceContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const useServiceContainer = (service: Function): IServiceContainer => {
// Convert undefined to null
// undefined is reserved for "service not executed yet"
if (response.data === undefined) {
setData(null);
setData(typeof response.data === 'boolean' ? false : null);
DnsZhou marked this conversation as resolved.
Show resolved Hide resolved
} else {
setData(response.data);
}
Expand Down
6 changes: 4 additions & 2 deletions src/utils/patchHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { IFields } from 'hooks/useForm';
* @param data - form state
* @returns Object of non-empty form values
*/
export const transformFormToValues = (data: IFields): { [key: string]: string | null } => {
return Object.fromEntries(Object.entries(data).map(([k, v]) => [k, v.value ? v.value : null]));
export const transformFormToValues = (data: IFields): { [key: string]: string | boolean | null } => {
return Object.fromEntries(
Object.entries(data).map(([k, v]) => [k, v.value ? v.value : typeof v.value === 'boolean' ? false : null])
DnsZhou marked this conversation as resolved.
Show resolved Hide resolved
);
};

// original Angular implementation:
Expand Down