diff --git a/src/screens/HyperSwitch/SwitchMerchant/SwitchMerchant.res b/src/screens/HyperSwitch/SwitchMerchant/SwitchMerchant.res index 0c24b62f4..f8c8f3ca0 100644 --- a/src/screens/HyperSwitch/SwitchMerchant/SwitchMerchant.res +++ b/src/screens/HyperSwitch/SwitchMerchant/SwitchMerchant.res @@ -318,16 +318,9 @@ let make = (~userRole, ~isAddMerchantEnabled=false) => { } else { <> - - {
-

{"Switching merchant..."->React.string}

-
} -
+ } } diff --git a/src/screens/HyperSwitch/UserManagement/InviteUsers.res b/src/screens/HyperSwitch/UserManagement/InviteUsers.res index 9823d77ac..f8e9b5375 100644 --- a/src/screens/HyperSwitch/UserManagement/InviteUsers.res +++ b/src/screens/HyperSwitch/UserManagement/InviteUsers.res @@ -5,6 +5,7 @@ module InviteEmailForm = { open LogicUtils open APIUtils let fetchDetails = useGetMethod() + let {magicLink} = HyperswitchAtom.featureFlagAtom->Recoil.useRecoilValueFromAtom let (roleListData, setRoleListData) = React.useState(_ => []) let emailList = @@ -53,7 +54,7 @@ module InviteEmailForm = {
{emailList->React.string}
- +
{ let fetchDetails = useGetMethod() let updateDetails = useUpdateMethod() let showToast = ToastState.useShowToast() + let {magicLink} = HyperswitchAtom.featureFlagAtom->Recoil.useRecoilValueFromAtom let {permissionInfo, setPermissionInfo} = React.useContext(GlobalProvider.defaultContext) let (screenState, setScreenState) = React.useState(_ => PageLoaderWrapper.Loading) let (roleTypeValue, setRoleTypeValue) = React.useState(_ => "merchant_view_only") let (roleDict, setRoleDict) = React.useState(_ => Dict.make()) + let (loaderForInviteUsers, setLoaderForInviteUsers) = React.useState(_ => false) let initialValues = React.useMemo0(() => { [("roleType", ["merchant_view_only"->Js.Json.string]->Js.Json.array)] @@ -85,36 +88,70 @@ let make = () => { ->Js.Json.object_ }) - let inviteUserReq = async (body, index) => { - try { - let url = getURL(~entityName=USERS, ~userType=#INVITE, ~methodType=Post, ()) - let _ = await updateDetails(url, body, Post) - if index === 0 { - showToast(~message=`Invite(s) sent successfully via Email`, ~toastType=ToastSuccess, ()) - } - } catch { - | _ => () - } + let inviteUserReq = body => { + let url = getURL(~entityName=USERS, ~userType=#INVITE, ~methodType=Post, ()) + let response = updateDetails(url, body, Post) + response } let inviteListOfUsers = async values => { + if !magicLink { + setLoaderForInviteUsers(_ => true) + } let valDict = values->getDictFromJsonObject let role = valDict->getStrArray("roleType")->LogicUtils.getValueFromArray(0, "") + let emailList = valDict->getStrArray("emailList") + let emailPasswordsArray = [] - valDict - ->getStrArray("emailList") - ->Array.forEachWithIndex((ele, index) => { + let arrayOfPromises = emailList->Array.map(ele => { let body = [ ("email", ele->String.toLowerCase->Js.Json.string), ("name", ele->getNameFromEmail->Js.Json.string), ("role_id", role->Js.Json.string), - ] - ->Dict.fromArray - ->Js.Json.object_ - let _ = inviteUserReq(body, index) + ]->LogicUtils.getJsonFromArrayOfJson + inviteUserReq(body) }) - await HyperSwitchUtils.delay(400) + + let response = await PromiseUtils.allSettledPolyfill(arrayOfPromises) + if !magicLink { + response->Array.forEachWithIndex((ele, index) => { + switch Js.Json.classify(ele) { + | Js.Json.JSONObject(jsonDict) => { + let passwordFromResponse = jsonDict->getString("password", "") + emailPasswordsArray->Array.push( + [ + ("email", emailList[index]->Option.getWithDefault("")->Js.Json.string), + ("password", passwordFromResponse->Js.Json.string), + ]->LogicUtils.getJsonFromArrayOfJson, + ) + } + | _ => () + } + }) + } + + showToast( + ~message=magicLink + ? `Invite(s) sent successfully via Email` + : `The user accounts have been successfully created. The file with their credentials has been downloaded.`, + ~toastType=ToastSuccess, + (), + ) + + if !magicLink && emailPasswordsArray->Array.length > 0 { + DownloadUtils.download( + ~fileName=`invited-users.txt`, + ~content=emailPasswordsArray->Js.Json.array->Js.Json.stringifyWithSpace(3), + ~fileType="application/json", + ) + } + + if !magicLink { + setLoaderForInviteUsers(_ => false) + } else { + await HyperSwitchUtils.delay(400) + } RescriptReactRouter.push("/users") Js.Nullable.null } @@ -222,5 +259,12 @@ let make = () => { + + + } diff --git a/src/utils/LoaderModal.res b/src/utils/LoaderModal.res new file mode 100644 index 000000000..48799a00d --- /dev/null +++ b/src/utils/LoaderModal.res @@ -0,0 +1,16 @@ +@react.component +let make = (~showModal, ~setShowModal, ~text) => { + + +
+ +
{text->React.string}
+
+
+
+} diff --git a/src/utils/PromiseUtils.res b/src/utils/PromiseUtils.res new file mode 100644 index 000000000..3309ebc7b --- /dev/null +++ b/src/utils/PromiseUtils.res @@ -0,0 +1,21 @@ +// Pollyfill for Promise.allSettled() +// Promise.allSettled() takes an iterable of promises and returns a single promise that is fulfilled with an array of promise settlement result + +let allSettledPolyfill = (arr: array>) => { + arr + ->Array.map(promise => + promise + ->Promise.then(val => { + Promise.resolve(val) + }) + ->Promise.catch(err => { + switch err { + | Js.Exn.Error(e) => + let err = Js.Exn.message(e)->Belt.Option.getWithDefault("Failed to Fetch!") + err->Js.Json.string + | _ => Js.Json.null + }->Promise.resolve + }) + ) + ->Promise.all +}