|
| 1 | +import { Button } from "@/components/buttons/button-primitive"; |
| 2 | +import { NodeFormProps } from "@/components/form/node-form"; |
| 3 | +import { FormFieldValue } from "@/components/form/type"; |
| 4 | +import { getCurrentFieldValue } from "@/components/form/utils/getFieldDefaultValue"; |
| 5 | +import { getCreateMutationFromFormDataOnly } from "@/components/form/utils/mutations/getCreateMutationFromFormData"; |
| 6 | +import { ALERT_TYPES, Alert } from "@/components/ui/alert"; |
| 7 | +import { Form, FormSubmit } from "@/components/ui/form"; |
| 8 | +import { ACCOUNT_ROLE_OBJECT, GLOBAL_PERMISSION_OBJECT } from "@/config/constants"; |
| 9 | +import graphqlClient from "@/graphql/graphqlClientApollo"; |
| 10 | +import { createObject } from "@/graphql/mutations/objects/createObject"; |
| 11 | +import { updateObjectWithId } from "@/graphql/mutations/objects/updateObjectWithId"; |
| 12 | +import { currentBranchAtom } from "@/state/atoms/branches.atom"; |
| 13 | +import { datetimeAtom } from "@/state/atoms/time.atom"; |
| 14 | +import { AttributeType, RelationshipType } from "@/utils/getObjectItemDisplayValue"; |
| 15 | +import { stringifyWithoutQuotes } from "@/utils/string"; |
| 16 | +import { gql } from "@apollo/client"; |
| 17 | +import { useAtomValue } from "jotai"; |
| 18 | +import { FieldValues, useForm } from "react-hook-form"; |
| 19 | +import { toast } from "react-toastify"; |
| 20 | + |
| 21 | +import DropdownField from "@/components/form/fields/dropdown.field"; |
| 22 | +import InputField from "@/components/form/fields/input.field"; |
| 23 | +import RelationshipField from "@/components/form/fields/relationship.field"; |
| 24 | +import { getRelationshipDefaultValue } from "@/components/form/utils/getRelationshipDefaultValue"; |
| 25 | +import { isRequired } from "@/components/form/utils/validation"; |
| 26 | +import { useSchema } from "@/hooks/useSchema"; |
| 27 | + |
| 28 | +interface NumberPoolFormProps extends Pick<NodeFormProps, "onSuccess"> { |
| 29 | + currentObject?: Record<string, AttributeType | RelationshipType>; |
| 30 | + onCancel?: () => void; |
| 31 | + onUpdateComplete?: () => void; |
| 32 | +} |
| 33 | + |
| 34 | +export const GlobalPermissionForm = ({ |
| 35 | + currentObject, |
| 36 | + onSuccess, |
| 37 | + onCancel, |
| 38 | + onUpdateComplete, |
| 39 | +}: NumberPoolFormProps) => { |
| 40 | + const branch = useAtomValue(currentBranchAtom); |
| 41 | + const date = useAtomValue(datetimeAtom); |
| 42 | + const { schema } = useSchema(GLOBAL_PERMISSION_OBJECT); |
| 43 | + |
| 44 | + const roles = getRelationshipDefaultValue({ |
| 45 | + relationshipData: currentObject?.roles?.value, |
| 46 | + }); |
| 47 | + |
| 48 | + const defaultValues = { |
| 49 | + name: getCurrentFieldValue("name", currentObject), |
| 50 | + action: getCurrentFieldValue("action", currentObject), |
| 51 | + decision: getCurrentFieldValue("decision", currentObject), |
| 52 | + roles, |
| 53 | + }; |
| 54 | + |
| 55 | + const form = useForm<FieldValues>({ |
| 56 | + defaultValues, |
| 57 | + }); |
| 58 | + |
| 59 | + const actionOptions = schema?.attributes |
| 60 | + ?.find((attribute) => { |
| 61 | + return attribute.name === "action"; |
| 62 | + }) |
| 63 | + ?.choices?.map((choice) => { |
| 64 | + return { |
| 65 | + ...choice, |
| 66 | + value: choice.name, |
| 67 | + }; |
| 68 | + }); |
| 69 | + |
| 70 | + const decisionOptions = [ |
| 71 | + { |
| 72 | + value: "allow", |
| 73 | + label: "Allow", |
| 74 | + }, |
| 75 | + { |
| 76 | + value: "deny", |
| 77 | + label: "Deny", |
| 78 | + }, |
| 79 | + ]; |
| 80 | + |
| 81 | + async function handleSubmit(data: Record<string, FormFieldValue>) { |
| 82 | + try { |
| 83 | + const newObject = getCreateMutationFromFormDataOnly(data, currentObject); |
| 84 | + |
| 85 | + if (!Object.keys(newObject).length) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const mutationString = currentObject |
| 90 | + ? updateObjectWithId({ |
| 91 | + kind: GLOBAL_PERMISSION_OBJECT, |
| 92 | + data: stringifyWithoutQuotes({ |
| 93 | + id: currentObject.id, |
| 94 | + ...newObject, |
| 95 | + }), |
| 96 | + }) |
| 97 | + : createObject({ |
| 98 | + kind: GLOBAL_PERMISSION_OBJECT, |
| 99 | + data: stringifyWithoutQuotes({ |
| 100 | + ...newObject, |
| 101 | + }), |
| 102 | + }); |
| 103 | + |
| 104 | + const mutation = gql` |
| 105 | + ${mutationString} |
| 106 | + `; |
| 107 | + |
| 108 | + const result = await graphqlClient.mutate({ |
| 109 | + mutation, |
| 110 | + context: { |
| 111 | + branch: branch?.name, |
| 112 | + date, |
| 113 | + }, |
| 114 | + }); |
| 115 | + |
| 116 | + toast(<Alert type={ALERT_TYPES.SUCCESS} message={"Object permission created"} />, { |
| 117 | + toastId: "alert-success-object-permission-created", |
| 118 | + }); |
| 119 | + |
| 120 | + if (onSuccess) await onSuccess(result?.data?.[`${GLOBAL_PERMISSION_OBJECT}Create`]); |
| 121 | + if (onUpdateComplete) await onUpdateComplete(); |
| 122 | + } catch (error: unknown) { |
| 123 | + console.error("An error occurred while creating the object: ", error); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return ( |
| 128 | + <div className={"bg-custom-white flex flex-col flex-1 overflow-auto p-4"}> |
| 129 | + <Form form={form} onSubmit={handleSubmit}> |
| 130 | + <InputField |
| 131 | + name="name" |
| 132 | + label="Name" |
| 133 | + rules={{ |
| 134 | + required: true, |
| 135 | + validate: { |
| 136 | + required: isRequired, |
| 137 | + }, |
| 138 | + }} |
| 139 | + /> |
| 140 | + |
| 141 | + <DropdownField |
| 142 | + name="action" |
| 143 | + label="Action" |
| 144 | + items={actionOptions} |
| 145 | + rules={{ required: true, validate: { required: isRequired } }} |
| 146 | + /> |
| 147 | + |
| 148 | + <DropdownField |
| 149 | + name="decision" |
| 150 | + label="Decision" |
| 151 | + items={decisionOptions} |
| 152 | + rules={{ required: true, validate: { required: isRequired } }} |
| 153 | + /> |
| 154 | + |
| 155 | + <RelationshipField |
| 156 | + name="roles" |
| 157 | + label="Roles" |
| 158 | + relationship={{ |
| 159 | + name: "roles", |
| 160 | + peer: ACCOUNT_ROLE_OBJECT, |
| 161 | + cardinality: "many", |
| 162 | + }} |
| 163 | + options={roles.value} |
| 164 | + /> |
| 165 | + |
| 166 | + <div className="text-right"> |
| 167 | + {onCancel && ( |
| 168 | + <Button variant="outline" className="mr-2" onClick={onCancel}> |
| 169 | + Cancel |
| 170 | + </Button> |
| 171 | + )} |
| 172 | + |
| 173 | + <FormSubmit>{currentObject ? "Update" : "Create"}</FormSubmit> |
| 174 | + </div> |
| 175 | + </Form> |
| 176 | + </div> |
| 177 | + ); |
| 178 | +}; |
0 commit comments