Skip to content

Commit

Permalink
feat: update org name (#1669)
Browse files Browse the repository at this point in the history
Co-authored-by: prajwalnl0 <[email protected]>
  • Loading branch information
gitanjli525 and prajwalnl0 authored Nov 13, 2024
1 parent 6b1887a commit 8b53637
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/screens/APIUtils/APIUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ let useGetURL = () => {
/* MERCHANT ACCOUNT DETAILS (Get and Post) */
| MERCHANT_ACCOUNT => `accounts/${merchantId}`

/* ORGANIZATION UPDATE */
| UPDATE_ORGANIZATION =>
switch methodType {
| Put =>
switch id {
| Some(id) => `organization/${id}`
| None => `organization`
}
| _ => ""
}

/* CUSTOMERS DETAILS */
| CUSTOMERS =>
switch methodType {
Expand Down
1 change: 1 addition & 0 deletions src/screens/APIUtils/APIUtilsTypes.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ type entityName =
| CONNECTOR
| ROUTING
| MERCHANT_ACCOUNT
| UPDATE_ORGANIZATION
| REFUNDS
| REFUND_FILTERS
| DISPUTES
Expand Down
85 changes: 84 additions & 1 deletion src/screens/OMPSwitch/OMPSwitchHelper.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ListBaseComp = {
@react.component
let make = (~heading, ~subHeading, ~arrow) => {
let make = (~heading, ~subHeading, ~arrow, ~showEditIcon=false, ~onEditClick=_ => ()) => {
<div
className="flex items-center justify-between text-sm text-center text-white font-medium rounded hover:bg-opacity-80 bg-sidebar-blue cursor-pointer">
<div className="flex flex-col items-start px-2 py-2 w-5/6">
Expand All @@ -9,6 +9,9 @@ module ListBaseComp = {
<p className="fs-10"> {subHeading->React.string} </p>
</div>
</div>
<RenderIf condition={showEditIcon}>
<Icon name="pencil-alt" size=10 onClick=onEditClick className="mt-4 mr-1" />
</RenderIf>
<div className="px-2 py-2">
<Icon
className={arrow
Expand Down Expand Up @@ -177,3 +180,83 @@ let generateDropdownOptions: array<OMPSwitchTypes.ompListTypes> => array<
})
options
}

module EditOrgName = {
@react.component
let make = (~showModal, ~setShowModal, ~orgList, ~orgId, ~getOrgList) => {
open LogicUtils
open APIUtils
let getURL = useGetURL()
let updateDetails = useUpdateMethod()
let showToast = ToastState.useShowToast()
let initialValues =
[
("organization_name", OMPSwitchUtils.currentOMPName(orgList, orgId)->JSON.Encode.string),
]->Dict.fromArray

let validateForm = (values: JSON.t) => {
let errors = Dict.make()
let organizationName =
values->getDictFromJsonObject->getString("organization_name", "")->String.trim
let regexForOrganizationName = "^([a-z]|[A-Z]|[0-9]|_|\\s)+$"

let errorMessage = if organizationName->isEmptyString {
"Organization name cannot be empty"
} else if organizationName->String.length > 64 {
"Organization name cannot exceed 64 characters"
} else if !RegExp.test(RegExp.fromString(regexForOrganizationName), organizationName) {
"Organization name should not contain special characters"
} else {
""
}

if errorMessage->isNonEmptyString {
Dict.set(errors, "organization_name", errorMessage->JSON.Encode.string)
}

errors->JSON.Encode.object
}

let orgName = FormRenderer.makeFieldInfo(
~label="Org Name",
~name="organization_name",
~placeholder=`Eg: Hyperswitch`,
~customInput=InputFields.textInput(),
~isRequired=true,
)

let onSubmit = async (values, _) => {
try {
let url = getURL(~entityName=UPDATE_ORGANIZATION, ~methodType=Put, ~id=Some(orgId))
let _ = await updateDetails(url, values, Put)
let _ = await getOrgList()
showToast(~message="Updated organization name!", ~toastType=ToastSuccess)
} catch {
| _ => showToast(~message="Failed to update organization name!", ~toastType=ToastError)
}
setShowModal(_ => false)
Nullable.null
}

<>
<Modal modalHeading="Edit Org name" showModal setShowModal modalClass="w-1/4 m-auto">
<Form initialValues={initialValues->JSON.Encode.object} onSubmit validate={validateForm}>
<div className="flex flex-col gap-12 h-full w-full">
<FormRenderer.DesktopRow>
<FormRenderer.FieldRenderer
fieldWrapperClass="w-full"
field={orgName}
labelClass="!text-black font-medium !-ml-[0.5px]"
/>
</FormRenderer.DesktopRow>
<div className="flex justify-end w-full pr-5 pb-3">
<FormRenderer.SubmitButton
text="Submit changes" buttonSize={Small} loadingText="Processing..."
/>
</div>
</div>
</Form>
</Modal>
</>
}
}
14 changes: 13 additions & 1 deletion src/screens/OMPSwitch/OrgSwitch.res
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ let make = () => {
let fetchDetails = useGetMethod()
let showToast = ToastState.useShowToast()
let orgSwitch = OMPSwitchHooks.useOrgSwitch()
let {userHasAccess} = GroupACLHooks.useUserGroupACLHook()
let {userInfo: {orgId}} = React.useContext(UserInfoProvider.defaultContext)
let (orgList, setOrgList) = Recoil.useRecoilState(HyperswitchAtom.orgListAtom)
let (showSwitchingOrg, setShowSwitchingOrg) = React.useState(_ => false)
let (showModal, setShowModal) = React.useState(_ => false)
let (arrow, setArrow) = React.useState(_ => false)

let getOrgList = async () => {
Expand Down Expand Up @@ -44,6 +46,11 @@ let make = () => {
}
}

let onEditClick = e => {
setShowModal(_ => true)
e->ReactEvent.Mouse.stopPropagation
}

let input: ReactFinalForm.fieldRenderPropsInput = {
name: "name",
onBlur: _ => (),
Expand Down Expand Up @@ -77,7 +84,11 @@ let make = () => {
customSelectStyle="md:bg-blue-840 hover:bg-popover-background-hover rounded"
searchable=false
baseComponent={<ListBaseComp
heading="Org" subHeading={currentOMPName(orgList, orgId)} arrow
heading="Org"
subHeading={currentOMPName(orgList, orgId)}
arrow
showEditIcon={userHasAccess(~groupAccess=OrganizationManage) === Access}
onEditClick
/>}
baseComponentCustomStyle="border-blue-820 rounded bg-popover-background rounded text-white"
optionClass="text-gray-200 text-fs-14"
Expand All @@ -88,6 +99,7 @@ let make = () => {
customScrollStyle
shouldDisplaySelectedOnTop=true
/>
<EditOrgName showModal setShowModal orgList orgId getOrgList />
<LoaderModal
showModal={showSwitchingOrg}
setShowModal={setShowSwitchingOrg}
Expand Down

0 comments on commit 8b53637

Please sign in to comment.