Skip to content

Commit

Permalink
feat: updated organization import form for api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
wwills2 committed Jan 2, 2025
1 parent 89764a6 commit e68a073
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
17 changes: 10 additions & 7 deletions src/renderer/api/cadt/v1/organizations/organizations.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ interface GetOrgnaizationsMapResponse {

interface CreateOrganizationResponse {
message: string;
orgId: string;
orgUid: string;
}

interface CreateOrganizationParams {
orgUid: string;
isHome: boolean;
}

export interface CreateOrganizatonParams {
Expand Down Expand Up @@ -65,22 +70,20 @@ const organizationsApi = cadtApi.injectEndpoints({
invalidatesTags: [organizationsTag],
}),

importOrganization: builder.mutation<CreateOrganizationResponse, string>({
query: (orgUid: string) => ({
importOrganization: builder.mutation<CreateOrganizationResponse, CreateOrganizationParams>({
query: (createOrgParams) => ({
url: `/v1/organizations`,
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: { orgUid },
body: createOrgParams,
}),
invalidatesTags: [organizationsTag],
}),

deleteOrganization: builder.mutation<any, string>({
query: (orgUid: string) => ({
url: `/v1/organizations`,
url: `/v1/organizations/${orgUid}`,
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
body: { orgUid },
}),
invalidatesTags: [organizationsTag],
}),
Expand Down
23 changes: 18 additions & 5 deletions src/renderer/components/blocks/forms/ImportOrganizationForm.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
import React, { useCallback } from 'react';
import { ErrorMessage, Field, Form, Formik } from 'formik';
import * as yup from 'yup';
import { FloatingLabel, FormButton } from '@/components';
import { CheckBox, FloatingLabel, FormButton, Label } from '@/components';
import { FormattedMessage, IntlShape, useIntl } from 'react-intl';

const validationSchema = yup.object({
orgUid: yup.string().length(64).required('OrgUid is required'),
});

export interface ImportOrganizationProps {
orgUid: string;
isHome: boolean;
}

interface FormProps {
onSubmit: (orgUid: string) => Promise<any>;
onSubmit: (params: ImportOrganizationProps) => Promise<any>;
}

const ImportOrganizationForm: React.FC<FormProps> = ({ onSubmit }) => {
const intl: IntlShape = useIntl();

const handleSubmit = useCallback(
async (values: { orgUid: string }, { setSubmitting }) => {
await onSubmit(values.orgUid);
async (values: ImportOrganizationProps, { setSubmitting }) => {
await onSubmit(values);
setSubmitting(false);
},
[onSubmit],
); // Include onSuccess in the dependencies array

return (
<>
<Formik initialValues={{ orgUid: '' }} validationSchema={validationSchema} onSubmit={handleSubmit}>
<Formik initialValues={{ orgUid: '', isHome: true }} validationSchema={validationSchema} onSubmit={handleSubmit}>
{({ errors, touched, isSubmitting }) => (
<Form>
<div className="mb-4">
Expand All @@ -42,6 +47,14 @@ const ImportOrganizationForm: React.FC<FormProps> = ({ onSubmit }) => {
)}
</Field>
{touched.orgUid && <ErrorMessage name="orgUid" component="div" className="text-red-600" />}
<div className="flex space-x-2.5">
<Field name="isHome">{({ field }) => <CheckBox id="isHome" checked {...field} />}</Field>
<Label htmlFor="isHome">
<p className="text-gray-600">
<FormattedMessage id="import-as-home-organization" />
</p>
</Label>
</div>
</div>
<FormButton isSubmitting={isSubmitting} formikErrors={errors}>
<FormattedMessage id="submit" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Card, CreateOrganizationForm, Modal, Spinner, Tabs } from '@/components';
import { Card, CreateOrganizationForm, ImportOrganizationProps, Modal, Spinner, Tabs } from '@/components';
import { FormattedMessage } from 'react-intl';
import { CreateOrganizatonParams, useCreateOrganizationMutation, useImportOrganizationMutation } from '@/api';
import { ImportOrganizationForm } from '@/components/blocks/forms/ImportOrganizationForm';
Expand Down Expand Up @@ -31,8 +31,9 @@ const CreateOrganizationModal: React.FC<CreateOrganizationModalProps> = ({
}
};

const handleSubmitImportOrg = async (orgName: string) => {
const createOrgResult: any = await triggerImportOrganization(orgName);
const handleSubmitImportOrg = async (importOrgFormValues: ImportOrganizationProps) => {
const { orgUid, isHome } = importOrgFormValues;
const createOrgResult: any = await triggerImportOrganization({ orgUid, isHome });
if (createOrgResult?.data.success) {
onClose();
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/renderer/components/proxy/CheckBox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Checkbox as FlowbiteCheckBox, CheckboxProps } from 'flowbite-react';

function CheckBox({ children, ...props }: CheckboxProps) {
return <FlowbiteCheckBox {...props}>{children}</FlowbiteCheckBox>;
}

export { CheckBox };
1 change: 1 addition & 0 deletions src/renderer/components/proxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export * from './FloatingLabel';
export * from './Select';
export * from './Badge';
export * from './Popover';
export * from './CheckBox';
5 changes: 3 additions & 2 deletions src/renderer/translations/tokens/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@
"name-is-required": "Name is required",
"prefix-is-required": "Prefix is required",
"unable-to-load-staging-data": "Unable to load staging data",
"new-unit": "New Unit",
"cannot-connect-to-registry-api-with-current-settings": "Cannot connect to registry API with current settings",
"please-disconnect-to-edit-the-api-url-and-api-key": "Please disconnect to edit the API URL and API key"
"please-disconnect-to-edit-the-api-url-and-api-key": "Please disconnect to edit the API URL and API key",
"new-unit": "New Unit",
"import-as-home-organization": "Import as home organization"
}

0 comments on commit e68a073

Please sign in to comment.