Skip to content

Commit

Permalink
chore: promise utils refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
JeevaRamu0104 committed Jan 17, 2024
1 parent a98a6c1 commit 684419d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
50 changes: 27 additions & 23 deletions src/screens/HyperSwitch/UserManagement/InviteUsers.res
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,15 @@ let make = () => {
->Js.Json.object_
})

let inviteUserReq = async (body, emailPasswordsArray) => {
try {
let url = getURL(~entityName=USERS, ~userType=#INVITE, ~methodType=Post, ())
let response = await updateDetails(url, body, Post)
if !magicLink {
let passwordFromResponse = response->getDictFromJsonObject->getString("password", "")
emailPasswordsArray->Array.push(
[
(
"email",
body
->LogicUtils.getDictFromJsonObject
->LogicUtils.getString("email", "")
->Js.Json.string,
),
("password", passwordFromResponse->Js.Json.string),
]->LogicUtils.getJsonFromArrayOfJson,
)
}
} catch {
| _ => ()
}
let inviteUserReq = (body, emailPasswordsArray) => {
// try {
let url = getURL(~entityName=USERS, ~userType=#INVITE, ~methodType=Post, ())
let response = updateDetails(url, body, Post)

response
// } catch {
// | _ => Js.Json.null
// }
}

let inviteListOfUsers = async values => {
Expand All @@ -133,8 +120,25 @@ let make = () => {
inviteUserReq(body, emailPasswordsArray)
})

let _ = await PromiseUtils.allSettledPolyfill(arrayOfPromises)
let response = await PromiseUtils.allSettledPolyfill(arrayOfPromises)
Js.log(response)

// Js.log2(res, "res")/
// if !magicLink {
// let passwordFromResponse = response->getDictFromJsonObject->getString("password", "")
// emailPasswordsArray->Array.push(
// [
// (
// "email",
// body
// ->LogicUtils.getDictFromJsonObject
// ->LogicUtils.getString("email", "")
// ->Js.Json.string,
// ),
// ("password", passwordFromResponse->Js.Json.string),
// ]->LogicUtils.getJsonFromArrayOfJson,
// )
// }
showToast(
~message=magicLink
? `Invite(s) sent successfully via Email`
Expand Down
8 changes: 6 additions & 2 deletions src/utils/PromiseUtils.res
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// 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<promise<unit>>) => {
let allSettledPolyfill = (arr: array<promise<Js.Json.t>>) => {
let res = arr->Array.map(promise =>
promise->Promise.then(val => {
promise
->Promise.then(val => {
Promise.resolve(val)
})
->Promise.catch(_err => {
Js.Json.null->Js.Promise.resolve
})
)
Promise.all(res)
}

0 comments on commit 684419d

Please sign in to comment.