aboutsummaryrefslogtreecommitdiffstats
path: root/src/App.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/App.js')
-rw-r--r--src/App.js173
1 files changed, 124 insertions, 49 deletions
diff --git a/src/App.js b/src/App.js
index 87b88da..3d93bd6 100644
--- a/src/App.js
+++ b/src/App.js
@@ -1,7 +1,9 @@
1import React from 'react'; 1import { Button, Heading, Link, Pane, Paragraph } from 'evergreen-ui';
2import React, { useEffect, useRef, useState } from 'react';
2import { useTranslation } from 'react-i18next'; 3import { useTranslation } from 'react-i18next';
3import logo from '../src/images/wifi.png'; 4import logo from '../src/images/wifi.png';
4import { Card } from './components/Card'; 5import { Settings } from './components/Settings';
6import { WifiCard } from './components/WifiCard';
5import './style.css'; 7import './style.css';
6 8
7/* List of languages that require RTL direction (alphabetic order). */ 9/* List of languages that require RTL direction (alphabetic order). */
@@ -10,65 +12,138 @@ const RTL_LANGUAGES = ['ar', 'fa-IR'];
10function App() { 12function App() {
11 const html = document.querySelector('html'); 13 const html = document.querySelector('html');
12 const { t, i18n } = useTranslation(); 14 const { t, i18n } = useTranslation();
15 const firstLoad = useRef(true);
16 const [settings, setSettings] = useState({
17 // Network SSID name
18 ssid: '',
19 // Network password
20 password: '',
21 // Settings: Network encryption mode
22 encryptionMode: 'WPA',
23 // Settings: Hide password on the printed card
24 hidePassword: false,
25 // Settings: Portrait orientation
26 portrait: false,
27 });
28 const [errors, setErrors] = useState({
29 ssidError: '',
30 passwordError: '',
31 });
13 32
14 const changeLanguage = (language) => { 33 const onChangeLanguage = (language) => {
15 html.style.direction = RTL_LANGUAGES.includes(language) ? 'rtl' : 'ltr'; 34 html.style.direction = RTL_LANGUAGES.includes(language) ? 'rtl' : 'ltr';
16 i18n.changeLanguage(language); 35 i18n.changeLanguage(language);
17 }; 36 };
18 37
19 /* handle the edge case of the initial render requiring RTL direction */ 38 const onPrint = () => {
20 if (RTL_LANGUAGES.includes(i18n.language)) { 39 if (!settings.ssid.length) {
21 html.style.direction = 'rtl'; 40 setErrors({
22 } 41 ...errors,
42 ssidError: t('wifi.alert.name'),
43 });
44 return;
45 }
46
47 if (settings.ssid.length > 0) {
48 if (settings.password.length < 8 && settings.encryptionMode === 'WPA') {
49 setErrors({
50 ...errors,
51 passwordError: t('wifi.alert.password.length.8'),
52 });
53 } else if (
54 settings.password.length < 5 &&
55 settings.encryptionMode === 'WEP'
56 ) {
57 setErrors({
58 ...errors,
59 passwordError: t('wifi.alert.password.length.5'),
60 });
61 } else {
62 document.title = 'WiFi Card - ' + settings.ssid;
63 window.print();
64 }
65 }
66 };
67
68 const onSSIDChange = (ssid) => {
69 setErrors({ ...errors, ssidError: '' });
70 setSettings({ ...settings, ssid });
71 };
72 const onPasswordChange = (password) => {
73 setErrors({ ...errors, passwordError: '' });
74 setSettings({ ...settings, password });
75 };
76 const onEncryptionModeChange = (encryptionMode) => {
77 setErrors({ ...errors, passwordError: '' });
78 setSettings({ ...settings, encryptionMode });
79 };
80 const onOrientationChange = (portrait) => {
81 setSettings({ ...settings, portrait });
82 };
83 const onHidePasswordChange = (hidePassword) => {
84 setSettings({ ...settings, hidePassword });
85 };
86 const onFirstLoad = () => {
87 firstLoad.current = false;
88 };
89
90 useEffect(() => {
91 /* handle the edge case of the initial render requiring RTL direction */
92 if (RTL_LANGUAGES.includes(i18n.language)) {
93 html.style.direction = 'rtl';
94 }
95 });
23 96
24 return ( 97 return (
25 <div className="App"> 98 <Pane>
26 <h1> 99 <Pane display="flex">
27 <img alt="icon" src={logo} width="32" height="32" /> 100 <img alt="icon" src={logo} width="32" height="32" />
28 &nbsp; {t('title')} 101 <Heading size={900} paddingLeft={16}>
29 </h1> 102 {t('title')}
103 </Heading>
104 </Pane>
105
106 <Pane>
107 <Paragraph marginTop={12}>{t('desc.use')}</Paragraph>
30 108
31 <div> 109 <Paragraph marginTop={12}>
32 <label>{t('select')}</label> 110 {t('desc.privacy')}{' '}
33 <select 111 <Link href="https://github.com/bndw/wifi-card">
34 value={i18n.language} 112 {t('desc.source')}
35 onChange={(e) => changeLanguage(e.target.value)} 113 </Link>
36 > 114 .
37 <option value="en-US">English</option> 115 </Paragraph>
38 <option value="ar">Arabic - العربية</option> 116 </Pane>
39 <option value="ca">Catalan - Català</option>
40 <option value="zh-HK">Chinese Hong Kong - 简体中文</option>
41 <option value="zh-CN">Chinese Simplified - 简体中文</option>
42 <option value="nl-NL">Dutch - Nederlands</option>
43 <option value="fr-FR">French - Français</option>
44 <option value="de-DE">German - Deutsch</option>
45 <option value="hi-IN">Hindi - हिन्दी</option>
46 <option value="id-ID">Indonesian</option>
47 <option value="it-IT">Italian</option>
48 <option value="ja">Japanese - 日本語</option>
49 <option value="ko">Korean - 한국어</option>
50 <option value="no-NB">Norwegian - Norsk</option>
51 <option value="oc">Occitan</option>
52 <option value="fa-IR">Persian Iran - فارسی</option>
53 <option value="pl-PL">Polish - Polski</option>
54 <option value="pt">Portuguese - Português</option>
55 <option value="pt-BR">Portuguese - Português brasileiro</option>
56 <option value="ru-RU">Russian - Русский</option>
57 <option value="es">Spanish - Español</option>
58 <option value="tr-TR">Turkish - Türkçe</option>
59 <option value="uk-UA">Ukrainian - Українська</option>
60 </select>
61 </div>
62 117
63 <p className="tag">{t('desc.use')}</p> 118 <WifiCard
119 direction={RTL_LANGUAGES.includes(i18n.language) ? 'rtl' : 'ltr'}
120 settings={settings}
121 ssidError={errors.ssidError}
122 passwordError={errors.passwordError}
123 onSSIDChange={onSSIDChange}
124 onPasswordChange={onPasswordChange}
125 />
64 126
65 <p className="tag"> 127 <Settings
66 {t('desc.privacy')}{' '} 128 settings={settings}
67 <a href="https://github.com/bndw/wifi-card">{t('desc.source')}</a>. 129 firstLoad={firstLoad}
68 </p> 130 onFirstLoad={onFirstLoad}
131 onLanguageChange={onChangeLanguage}
132 onEncryptionModeChange={onEncryptionModeChange}
133 onOrientationChange={onOrientationChange}
134 onHidePasswordChange={onHidePasswordChange}
135 />
69 136
70 <Card direction={RTL_LANGUAGES.includes(i18n.language) ? 'rtl' : 'ltr'} /> 137 <Button
71 </div> 138 id="print"
139 appearance="primary"
140 height={40}
141 marginRight={16}
142 onClick={onPrint}
143 >
144 {t('button.print')}
145 </Button>
146 </Pane>
72 ); 147 );
73} 148}
74 149