Skip to content

Instantly share code, notes, and snippets.

@tgmarinho
Last active June 13, 2021 23:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgmarinho/672e3281748a9f8081d84d5e3894bf0c to your computer and use it in GitHub Desktop.
Save tgmarinho/672e3281748a9f8081d84d5e3894bf0c to your computer and use it in GitHub Desktop.
ask permission to user
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;
};
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();
}
}, []);
};
@dutradotdev
Copy link

Não pode dar erro na linha 17? Pq teoricamente seria algo do tipo undefined.close() ou null.close()

@tgmarinho
Copy link
Author

tgmarinho commented Jun 12, 2021

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