-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.jsx
106 lines (94 loc) · 3.46 KB
/
App.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import "./App.css";
import "./components/CommonStyles.css";
import { useEffect, useState } from "react";
import { addTokenListener, removeTokenListener } from "./api/auth";
import ObjectViewerComponent from "./components/ObjectViewerComponent";
import { isTelegramAPISupported } from "./api/telegram";
import { i18n } from "./api/i18n";
import { ButtonComponent } from "./components/ButtonComponents";
function App() {
const isSupported = isTelegramAPISupported();
const onScanQRClicked = () => {
window.Telegram.WebApp.showScanQrPopup({});
};
const onCloseClicked = () => {
window.Telegram.WebApp.close();
};
const [scannedObject, setScannedObject] = useState(null);
const [tokenState, setTokenState] = useState({
loaded: false,
token: null
});
useEffect(() => {
const onTokenChanged = (event) => {
setTokenState({
loaded: true,
token: event.detail
});
};
addTokenListener(onTokenChanged);
return () => {
removeTokenListener(onTokenChanged);
};
}, []);
useEffect(() => {
const onBackClicked = () => {
window.Telegram.WebApp.BackButton.isVisible = false;
setScannedObject(null);
};
window.Telegram.WebApp.BackButton.onClick(onBackClicked);
return () => {
window.Telegram.WebApp.BackButton.offClick(onBackClicked);
};
}, []);
useEffect(() => {
const QR_EVENT = "qrTextReceived";
const onQRTextReceived = (event) => {
window.Telegram.WebApp.closeScanQrPopup();
window.Telegram.WebApp.BackButton.isVisible = true;
setScannedObject(event.data);
};
window.Telegram.WebApp.onEvent(QR_EVENT, onQRTextReceived);
return () => {
window.Telegram.WebApp.offEvent(QR_EVENT, onQRTextReceived);
};
}, []);
const getUI = () => {
if (!isSupported) {
return (
<div className="scanner-wrapper">
<span>{i18n.t("MESSAGE_NOT_SUPPORTED_LINE_1")}</span>
<span>{i18n.t("MESSAGE_NOT_SUPPORTED_LINE_2")}</span>
<ButtonComponent onClick={onCloseClicked}>{i18n.t("BUTTON_CLOSE_APP")}</ButtonComponent>
</div>
);
}
else if (!tokenState.loaded) {
return (<div className="preloader" />);
} else if (tokenState.token == null) {
return (
<div className="scanner-wrapper">
<span>{i18n.t("MESSAGE_NO_TICKET_LINE_1")}</span>
<span>{i18n.t("MESSAGE_NO_TICKET_LINE_2")}</span>
<ButtonComponent onClick={onCloseClicked}>{i18n.t("BUTTON_CLOSE_APP")}</ButtonComponent>
</div>
);
} else if (scannedObject == null) {
return (
<div className="scanner-wrapper">
<span>{i18n.t("MESSAGE_WELCOME_LINE_1")}</span>
<span>{i18n.t("MESSAGE_WELCOME_LINE_2")}</span>
<ButtonComponent onClick={onScanQRClicked}>{i18n.t("BUTTON_SCAN_QR")}</ButtonComponent>
</div>
);
} else {
return <ObjectViewerComponent accessToken={tokenState.token} objectCode={scannedObject} />;
}
};
return (
<div className="App">
{getUI()}
</div>
);
}
export default App;