Last active
June 13, 2021 23:51
-
-
Save tgmarinho/672e3281748a9f8081d84d5e3894bf0c to your computer and use it in GitHub Desktop.
ask permission to user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useState } from "react"; | |
import { Alert, Button, Space } from "antd"; | |
import { getLocalStorage, setLocalStorage } from "@/utils/helpers"; | |
import { KEY_ALLOW_POPUP } from "@/utils/constants"; | |
/** | |
* It's necessary because when we are using simple create form | |
* Open a lot of times the popup to login/sign transactions | |
*/ | |
enum Status { | |
LOADING, | |
NOT_AUTHORIZED, | |
AUTHORIZED, | |
} | |
export const AllowPopup = () => { | |
const [authorized, setAuthorized] = useState<Status>(Status.LOADING); | |
useEffect(() => { | |
const status: Status = getLocalStorage( | |
KEY_ALLOW_POPUP, | |
Status.NOT_AUTHORIZED, | |
); | |
setAuthorized(status); | |
if (status === Status.NOT_AUTHORIZED) { | |
const popUp = window.open( | |
"https://all-access.wax.io", | |
"Link to Login/SignTransaction", | |
); | |
if (!!popUp) { | |
popUp.close(); | |
} | |
} | |
}, []); | |
const handleClick = (value: Status) => { | |
setAuthorized(value); | |
setLocalStorage(KEY_ALLOW_POPUP, value); | |
}; | |
return authorized === Status.NOT_AUTHORIZED ? ( | |
<Alert | |
message="Allow Pop-ups" | |
description="Please allow the site to show pop-ups always, it's necessary for login and sign Transactions." | |
type="info" | |
action={ | |
<Space direction="vertical"> | |
<Button | |
size="small" | |
type="primary" | |
onClick={() => handleClick(Status.AUTHORIZED)} | |
> | |
Ok | |
</Button> | |
</Space> | |
} | |
closable | |
/> | |
) : null; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from "react"; | |
export const useAllowPopup = () => { | |
const [isAutorized, setIsAutorized] = useState(); | |
useEffect(() => { | |
const windowName = "userConsole"; | |
const popUp = window.open( | |
"htttp://www.google.com", | |
windowName, | |
"width=1000, height=700, left=24, top=24, scrollbars, resizable", | |
); | |
if (popUp == null || typeof popUp == "undefined") { | |
alert( | |
"Please allow the site show popup, always, its necessary for signTransactions and login.", | |
); | |
} else { | |
popUp.close(); | |
} | |
}, []); | |
}; |
Ops pode sim, o certo era para ser ali no lugar do focus dar o close no popup, eu nem quero que ele apareça, só quero forçar o usuário permitir a URL como popup confiável. Pq quando eu de fato precisar dela, não quero que de erro no fluxo por "unable to open popup" que eu estava recebendo.
Vlw :)
vou dar uma boa mexina no código ainda, colocar no localStorage ou banco de dados, se o usuário não permitir, vou ficar enviando isso até ele aceitar hahahaha assim que aceitar salvo, e não mostro mais. Mas se ele remover a URL lá das configurações azar dele.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Não pode dar erro na linha 17? Pq teoricamente seria algo do tipo
undefined.close()
ounull.close()