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

🐛 Decouple form actions from ApplicationForm #1776

Merged
merged 3 commits into from
Mar 22, 2024
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 @@ -91,7 +91,6 @@ import { useFetchTagsWithTagItems } from "@app/queries/tags";
// Relative components
import { ApplicationAssessmentStatus } from "../components/application-assessment-status";
import { ApplicationBusinessService } from "../components/application-business-service";
import { ApplicationForm } from "../components/application-form";
import { ImportApplicationsForm } from "../components/import-applications-form";
import { ConditionalRender } from "@app/components/ConditionalRender";
import { NoDataEmptyState } from "@app/components/NoDataEmptyState";
Expand All @@ -108,6 +107,7 @@ import { ApplicationIdentityForm } from "../components/application-identity-form
import { ApplicationReviewStatus } from "../components/application-review-status/application-review-status";
import { KebabDropdown } from "@app/components/KebabDropdown";
import { useFetchArchetypes } from "@app/queries/archetypes";
import { ApplicationFormModal } from "../components/application-form";

export const ApplicationsTable: React.FC = () => {
const { t } = useTranslation();
Expand Down Expand Up @@ -1103,21 +1103,12 @@ export const ApplicationsTable: React.FC = () => {
/>
)}
</Modal>
<Modal
title={
createUpdateApplications
? t("dialog.title.updateApplication")
: t("dialog.title.newApplication")
}
variant="medium"
isOpen={isCreateUpdateApplicationsModalOpen}
onClose={() => setSaveApplicationModalState(null)}
>
<ApplicationForm
{isCreateUpdateApplicationsModalOpen && (
<ApplicationFormModal
application={createUpdateApplications}
onClose={() => setSaveApplicationModalState(null)}
/>
</Modal>
)}
<SimpleDocumentViewerModal
title={`Analysis details for ${taskToView?.name}`}
documentId={taskToView?.task}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import {
TAG_CATEGORIES,
} from "@app/api/rest";
import mock from "@app/test-config/mockInstance";
import { ApplicationForm } from "../application-form";
import userEvent from "@testing-library/user-event";

import "@testing-library/jest-dom/extend-expect";
import "@testing-library/jest-dom";
import { BusinessService } from "@app/api/models";
import { ApplicationFormModal } from "../application-form-modal";

const data: any[] = [];
mock.onGet(`${BUSINESS_SERVICES}`).reply(200, data);
Expand All @@ -34,7 +34,9 @@ describe("Component: application-form", () => {

mock.onGet(`${BUSINESS_SERVICES}`).reply(200, businessServices);

render(<ApplicationForm application={null} onClose={mockChangeValue} />);
render(
<ApplicationFormModal application={null} onClose={mockChangeValue} />
);
const nameInput = await screen.findByLabelText("Name *");
fireEvent.change(nameInput, {
target: { value: "app-name" },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as React from "react";
import { Button, ButtonVariant, Modal } from "@patternfly/react-core";
import { ApplicationForm, useApplicationFormHook } from "./application-form";
import { Application } from "@app/api/models";
import { useTranslation } from "react-i18next";

export interface ApplicationFormModalProps {
application: Application | null;
onClose: () => void;
}

export const ApplicationFormModal: React.FC<ApplicationFormModalProps> = ({
application,
onClose,
}) => {
const { t } = useTranslation();
const formProps = useApplicationFormHook({ application, onClose });
return (
<Modal
title={
application
? t("dialog.title.updateApplication")
: t("dialog.title.newApplication")
}
variant="medium"
isOpen={true}
onClose={onClose}
actions={[
<Button
key="submit"
id="submit"
aria-label="submit"
variant={ButtonVariant.primary}
isDisabled={formProps.isSubmitDisabled}
onClick={formProps.onSubmit}
>
{!application ? t("actions.create") : t("actions.save")}
</Button>,
<Button
key="cancel"
id="cancel"
aria-label="cancel"
variant={ButtonVariant.link}
isDisabled={formProps.isCancelDisabled}
onClick={onClose}
>
{t("actions.cancel")}
</Button>,
]}
>
<ApplicationForm {...formProps} />
</Modal>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { useTranslation } from "react-i18next";
import { AxiosError } from "axios";
import { object, string } from "yup";
import {
ActionGroup,
Button,
ButtonVariant,
ExpandableSection,
Form,
Popover,
Expand Down Expand Up @@ -70,10 +67,10 @@ export interface ApplicationFormProps {
onClose: () => void;
}

export const ApplicationForm: React.FC<ApplicationFormProps> = ({
export const useApplicationFormHook = ({
application,
onClose,
}) => {
}: ApplicationFormProps) => {
const { t } = useTranslation();
const {
existingApplications,
Expand Down Expand Up @@ -316,9 +313,46 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
toString: () => `Subversion`,
},
];
return {
handleSubmit,
onValidSubmit,
setBasicExpanded,
isBasicExpanded,
control,
tagItems,
stakeholdersOptions,
setSourceCodeExpanded,
isSourceCodeExpanded,
kindOptions,
setBinaryExpanded,
isBinaryExpanded,
isSubmitDisabled: !isValid || isSubmitting || isValidating || !isDirty,
isCancelDisabled: isSubmitting || isValidating,
stakeholders,
businessServiceOptions,
onSubmit: handleSubmit(onValidSubmit),
};
};

export const ApplicationForm: React.FC<
ReturnType<typeof useApplicationFormHook>
> = ({
setBasicExpanded,
isBasicExpanded,
control,
tagItems,
stakeholdersOptions,
setSourceCodeExpanded,
isSourceCodeExpanded,
kindOptions,
setBinaryExpanded,
isBinaryExpanded,
stakeholders,
businessServiceOptions,
}) => {
const { t } = useTranslation();
return (
<Form onSubmit={handleSubmit(onValidSubmit)}>
<Form>
<ExpandableSection
toggleText={"Basic information"}
className="toggle"
Expand Down Expand Up @@ -623,27 +657,6 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
/>
</div>
</ExpandableSection>
<ActionGroup>
<Button
type="submit"
id="submit"
aria-label="submit"
variant={ButtonVariant.primary}
isDisabled={!isValid || isSubmitting || isValidating || !isDirty}
>
{!application ? t("actions.create") : t("actions.save")}
</Button>
<Button
type="button"
id="cancel"
aria-label="cancel"
variant={ButtonVariant.link}
isDisabled={isSubmitting || isValidating}
onClick={onClose}
>
{t("actions.cancel")}
</Button>
</ActionGroup>
</Form>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { ApplicationForm } from "./application-form";
export { ApplicationFormModal } from "./application-form-modal";
Loading