Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: email update #2158

Merged
merged 10 commits into from
Apr 19, 2024
18 changes: 13 additions & 5 deletions packages/scaffold/src/views/w3m-update-email-wallet-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,20 @@ export class W3mUpdateEmailWalletView extends LitElement {
throw new Error('w3m-update-email-wallet: Email connector not found')
}

await emailConnector.provider.updateEmail({ email: this.email })
const response = await emailConnector.provider.updateEmail({ email: this.email })
EventsController.sendEvent({ type: 'track', event: 'EMAIL_EDIT' })
RouterController.replace('UpdateEmailPrimaryOtp', {
email: this.initialEmail,
newEmail: this.email
})

if (response.action === 'VERIFY_SECONDARY_OTP') {
RouterController.push('UpdateEmailSecondaryOtp', {
email: this.initialEmail,
newEmail: this.email
})
} else {
RouterController.push('UpdateEmailPrimaryOtp', {
email: this.initialEmail,
newEmail: this.email
})
}
} catch (error) {
SnackController.showError(error)
this.loading = false
Expand Down
12 changes: 7 additions & 5 deletions packages/wallet/src/W3mFrameProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type IsConnectedResolver = Resolver<W3mFrameTypes.Responses['FrameIsConnectedRes
type GetChainIdResolver = Resolver<W3mFrameTypes.Responses['FrameGetChainIdResponse']>
type SwitchChainResolver = Resolver<W3mFrameTypes.Responses['FrameSwitchNetworkResponse']>
type RpcRequestResolver = Resolver<W3mFrameTypes.RPCResponse>
type UpdateEmailResolver = Resolver<undefined>
type UpdateEmailResolver = Resolver<W3mFrameTypes.Responses['FrameUpdateEmailResponse']>
type UpdateEmailPrimaryOtpResolver = Resolver<undefined>
type UpdateEmailSecondaryOtpResolver = Resolver<
W3mFrameTypes.Responses['FrameUpdateEmailSecondaryOtpResolver']
Expand Down Expand Up @@ -109,7 +109,7 @@ export class W3mFrameProvider {
case W3mFrameConstants.FRAME_SESSION_UPDATE:
return this.onSessionUpdate(event)
case W3mFrameConstants.FRAME_UPDATE_EMAIL_SUCCESS:
return this.onUpdateEmailSuccess()
return this.onUpdateEmailSuccess(event)
case W3mFrameConstants.FRAME_UPDATE_EMAIL_ERROR:
return this.onUpdateEmailError(event)
case W3mFrameConstants.FRAME_UPDATE_EMAIL_PRIMARY_OTP_SUCCESS:
Expand Down Expand Up @@ -210,7 +210,7 @@ export class W3mFrameProvider {
W3mFrameHelpers.checkIfAllowedToTriggerEmail()
this.w3mFrame.events.postAppEvent({ type: W3mFrameConstants.APP_UPDATE_EMAIL, payload })

return new Promise((resolve, reject) => {
return new Promise<W3mFrameTypes.Responses['FrameUpdateEmailResponse']>((resolve, reject) => {
this.updateEmailResolver = { resolve, reject }
})
}
Expand Down Expand Up @@ -523,8 +523,10 @@ export class W3mFrameProvider {
}
}

private onUpdateEmailSuccess() {
this.updateEmailResolver?.resolve(undefined)
private onUpdateEmailSuccess(
event: Extract<W3mFrameTypes.FrameEvent, { type: '@w3m-frame/UPDATE_EMAIL_SUCCESS' }>
) {
this.updateEmailResolver?.resolve(event.payload)
this.setNewLastEmailLoginTime()
}

Expand Down
5 changes: 4 additions & 1 deletion packages/wallet/src/W3mFrameSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export const AppSetPreferredAccountRequest = z.object({ type: z.string() })
export const FrameConnectEmailResponse = z.object({
action: z.enum(['VERIFY_DEVICE', 'VERIFY_OTP'])
})
export const FrameUpdateEmailResponse = z.object({
action: z.enum(['VERIFY_PRIMARY_OTP', 'VERIFY_SECONDARY_OTP'])
})
export const FrameGetUserResponse = z.object({
email: z.string().email(),
address: z.string(),
Expand Down Expand Up @@ -406,7 +409,7 @@ export const W3mFrameSchema = {

.or(z.object({ type: zType('FRAME_UPDATE_EMAIL_ERROR'), payload: zError }))

.or(z.object({ type: zType('FRAME_UPDATE_EMAIL_SUCCESS') }))
.or(z.object({ type: zType('FRAME_UPDATE_EMAIL_SUCCESS'), payload: FrameUpdateEmailResponse }))

.or(z.object({ type: zType('FRAME_UPDATE_EMAIL_PRIMARY_OTP_ERROR'), payload: zError }))

Expand Down
4 changes: 3 additions & 1 deletion packages/wallet/src/W3mFrameTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ import {
RpcEthChainId,
FrameSwitchNetworkResponse,
AppSyncDappDataRequest,
FrameGetSmartAccountEnabledNetworksResponse
FrameGetSmartAccountEnabledNetworksResponse,
FrameUpdateEmailResponse
} from './W3mFrameSchema.js'
import type { W3mFrameRpcConstants } from './W3mFrameConstants.js'

Expand Down Expand Up @@ -86,6 +87,7 @@ export namespace W3mFrameTypes {
FrameGetSmartAccountEnabledNetworksResponse: z.infer<
typeof FrameGetSmartAccountEnabledNetworksResponse
>
FrameUpdateEmailResponse: z.infer<typeof FrameUpdateEmailResponse>
}

export interface Network {
Expand Down
Loading