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

feat: updated organization import form for api changes #1375

Merged
merged 1 commit into from
Jan 9, 2025
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
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;
}

const organizationsApi = cadtApi.injectEndpoints({
Expand Down Expand Up @@ -59,22 +64,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 { 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';
3 changes: 2 additions & 1 deletion src/renderer/translations/tokens/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,6 @@
"api-host-loaded-from-configuration": "API host loaded from configuration",
"not-specified": "not specified",
"unable-to-load-staging-data": "Unable to load staging data",
"new-unit": "New Unit"
"new-unit": "New Unit",
"import-as-home-organization": "Import as home organization"
}
Loading