From 1d42077ab4572b82a5c56eb398f0f175b07fb75c Mon Sep 17 00:00:00 2001 From: simeng-li Date: Thu, 19 Jan 2023 11:08:24 +0800 Subject: [PATCH] refactor(ui): optimize api call stack (#2988) --- packages/ui/src/apis/interaction.ts | 99 +++++++++++++++-------------- 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/packages/ui/src/apis/interaction.ts b/packages/ui/src/apis/interaction.ts index 5194d0a58..4d80b49ad 100644 --- a/packages/ui/src/apis/interaction.ts +++ b/packages/ui/src/apis/interaction.ts @@ -114,27 +114,29 @@ export const verifyForgotPasswordVerificationCodeIdentifier = async ( }; export const signInWithVerifiedIdentifier = async () => { - await api.delete(`${interactionPrefix}/profile`); - - await api.put(`${interactionPrefix}/event`, { - json: { - event: InteractionEvent.SignIn, - }, - }); + await Promise.all([ + api.delete(`${interactionPrefix}/profile`), + api.put(`${interactionPrefix}/event`, { + json: { + event: InteractionEvent.SignIn, + }, + }), + ]); return api.post(`${interactionPrefix}/submit`).json(); }; export const registerWithVerifiedIdentifier = async (payload: SendVerificationCodePayload) => { - await api.put(`${interactionPrefix}/event`, { - json: { - event: InteractionEvent.Register, - }, - }); - - await api.put(`${interactionPrefix}/profile`, { - json: payload, - }); + await Promise.all([ + api.put(`${interactionPrefix}/event`, { + json: { + event: InteractionEvent.Register, + }, + }), + api.put(`${interactionPrefix}/profile`, { + json: payload, + }), + ]); return api.post(`${interactionPrefix}/submit`).json(); }; @@ -172,31 +174,33 @@ export const signInWithSocial = async (payload: SocialConnectorPayload) => { }; export const registerWithVerifiedSocial = async (connectorId: string) => { - await api.put(`${interactionPrefix}/event`, { - json: { - event: InteractionEvent.Register, - }, - }); - - await api.patch(`${interactionPrefix}/profile`, { - json: { - connectorId, - }, - }); + await Promise.all([ + api.put(`${interactionPrefix}/event`, { + json: { + event: InteractionEvent.Register, + }, + }), + api.patch(`${interactionPrefix}/profile`, { + json: { + connectorId, + }, + }), + ]); return api.post(`${interactionPrefix}/submit`).json(); }; export const bindSocialRelatedUser = async (payload: SocialEmailPayload | SocialPhonePayload) => { - await api.patch(`${interactionPrefix}/identifiers`, { - json: payload, - }); - - await api.patch(`${interactionPrefix}/profile`, { - json: { - connectorId: payload.connectorId, - }, - }); + await Promise.all([ + api.patch(`${interactionPrefix}/identifiers`, { + json: payload, + }), + api.patch(`${interactionPrefix}/profile`, { + json: { + connectorId: payload.connectorId, + }, + }), + ]); return api.post(`${interactionPrefix}/submit`).json(); }; @@ -204,17 +208,18 @@ export const bindSocialRelatedUser = async (payload: SocialEmailPayload | Social export const linkWithSocial = async (connectorId: string) => { // Sign-in with pre-verified email/phone identifier instead and replace the email/phone profile with connectorId. - await api.put(`${interactionPrefix}/event`, { - json: { - event: InteractionEvent.SignIn, - }, - }); - - await api.put(`${interactionPrefix}/profile`, { - json: { - connectorId, - }, - }); + await Promise.all([ + api.put(`${interactionPrefix}/event`, { + json: { + event: InteractionEvent.SignIn, + }, + }), + api.put(`${interactionPrefix}/profile`, { + json: { + connectorId, + }, + }), + ]); return api.post(`${interactionPrefix}/submit`).json(); };