|
| 1 | +import { useCurrentBranch } from "@/entities/branches/ui/branches-provider"; |
| 2 | +import { IP_PREFIX_GENERIC } from "@/entities/ipam/constants"; |
| 3 | +import { createObject } from "@/entities/nodes/api/createObject"; |
| 4 | +import { updateObjectWithId } from "@/entities/nodes/api/updateObjectWithId"; |
| 5 | +import { IP_PREFIX_POOL } from "@/entities/resource-manager/constants"; |
| 6 | +import { getSchema } from "@/entities/schema/domain/get-schema"; |
| 7 | +import { useSchema } from "@/entities/schema/ui/hooks/useSchema"; |
| 8 | +import graphqlClient from "@/shared/api/graphql/graphqlClientApollo"; |
| 9 | +import DynamicForm from "@/shared/components/form/dynamic-form"; |
| 10 | +import { NodeFormProps } from "@/shared/components/form/node-form"; |
| 11 | +import { DynamicSelectFieldProps, FormFieldValue } from "@/shared/components/form/type"; |
| 12 | +import { getFormFieldsFromSchema } from "@/shared/components/form/utils/getFormFieldsFromSchema"; |
| 13 | +import { getCreateMutationFromFormData } from "@/shared/components/form/utils/mutations/getCreateMutationFromFormData"; |
| 14 | +import { ALERT_TYPES, Alert } from "@/shared/components/ui/alert"; |
| 15 | +import { stringifyWithoutQuotes } from "@/shared/utils/string"; |
| 16 | +import { gql } from "@apollo/client"; |
| 17 | +import { useMemo } from "react"; |
| 18 | +import { toast } from "react-toastify"; |
| 19 | + |
| 20 | +export interface IpPrefixPoolFormProps extends NodeFormProps {} |
| 21 | + |
| 22 | +export function IpPrefixPoolForm({ |
| 23 | + currentObject, |
| 24 | + isUpdate, |
| 25 | + onSuccess, |
| 26 | + onSubmit, |
| 27 | + ...props |
| 28 | +}: IpPrefixPoolFormProps) { |
| 29 | + const { schema: genericPrefixSchema, isGeneric } = useSchema(IP_PREFIX_GENERIC); |
| 30 | + const { currentBranch } = useCurrentBranch(); |
| 31 | + |
| 32 | + const fields = useMemo(() => { |
| 33 | + const schemaFields = getFormFieldsFromSchema({ |
| 34 | + ...props, |
| 35 | + initialObject: currentObject, |
| 36 | + isUpdate, |
| 37 | + }); |
| 38 | + |
| 39 | + if (!genericPrefixSchema || !isGeneric) return schemaFields; |
| 40 | + |
| 41 | + // Replace default_prefix_type (text) field with a select |
| 42 | + return schemaFields.map((field) => { |
| 43 | + if (field.name === "default_prefix_type") { |
| 44 | + const items = |
| 45 | + genericPrefixSchema.used_by?.map((kind) => { |
| 46 | + const { schema } = getSchema(kind); |
| 47 | + |
| 48 | + if (!schema) { |
| 49 | + return { |
| 50 | + key: kind, |
| 51 | + label: kind, |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + return { |
| 56 | + key: kind, |
| 57 | + label: ( |
| 58 | + <div className="flex items-center justify-between w-full"> |
| 59 | + <span>{schema.label}</span> |
| 60 | + <span className="text-xs text-gray-500">{schema.namespace}</span> |
| 61 | + </div> |
| 62 | + ), |
| 63 | + }; |
| 64 | + }) ?? []; |
| 65 | + |
| 66 | + const defaultValue = |
| 67 | + isUpdate && currentObject |
| 68 | + ? field.defaultValue |
| 69 | + : items.length === 1 |
| 70 | + ? { source: { type: "user" }, value: items[0]?.key } |
| 71 | + : field.defaultValue; |
| 72 | + |
| 73 | + return { |
| 74 | + ...field, |
| 75 | + type: "select", |
| 76 | + items, |
| 77 | + defaultValue, |
| 78 | + } as DynamicSelectFieldProps; |
| 79 | + } |
| 80 | + return field; |
| 81 | + }); |
| 82 | + }, [props, genericPrefixSchema, isGeneric, currentObject, isUpdate]); |
| 83 | + |
| 84 | + async function handleSubmit(data: Record<string, FormFieldValue>) { |
| 85 | + try { |
| 86 | + const newObject = getCreateMutationFromFormData(fields, data); |
| 87 | + |
| 88 | + if (!Object.keys(newObject).length) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const mutationString = |
| 93 | + isUpdate && currentObject |
| 94 | + ? updateObjectWithId({ |
| 95 | + kind: IP_PREFIX_POOL, |
| 96 | + data: stringifyWithoutQuotes({ |
| 97 | + id: currentObject.id, |
| 98 | + ...newObject, |
| 99 | + }), |
| 100 | + }) |
| 101 | + : createObject({ |
| 102 | + kind: IP_PREFIX_POOL, |
| 103 | + data: stringifyWithoutQuotes(newObject), |
| 104 | + }); |
| 105 | + |
| 106 | + const mutation = gql` |
| 107 | + ${mutationString} |
| 108 | + `; |
| 109 | + |
| 110 | + const result = await graphqlClient.mutate({ |
| 111 | + mutation, |
| 112 | + context: { |
| 113 | + branch: currentBranch.name, |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + const operationType = isUpdate ? "Update" : "Create"; |
| 118 | + const successMessage = isUpdate ? "IP prefix pool updated" : "IP prefix pool created"; |
| 119 | + toast(<Alert type={ALERT_TYPES.SUCCESS} message={successMessage} />, { |
| 120 | + toastId: `alert-success-ip-prefix-pool-${operationType}`, |
| 121 | + }); |
| 122 | + |
| 123 | + if (onSuccess) { |
| 124 | + const resultData = result?.data?.[`${IP_PREFIX_POOL}${operationType}`]; |
| 125 | + await onSuccess(resultData); |
| 126 | + } |
| 127 | + } catch (error: unknown) { |
| 128 | + console.error( |
| 129 | + `An error occurred while ${isUpdate ? "updating" : "creating"} the IP prefix pool:`, |
| 130 | + error |
| 131 | + ); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return ( |
| 136 | + <DynamicForm |
| 137 | + fields={fields} |
| 138 | + onSubmit={(formData: Record<string, FormFieldValue>) => |
| 139 | + onSubmit ? onSubmit({ formData, fields }) : handleSubmit(formData) |
| 140 | + } |
| 141 | + className="p-4 overflow-auto" |
| 142 | + /> |
| 143 | + ); |
| 144 | +} |
0 commit comments