Skip to content

Commit fe31261

Browse files
author
Patricio Vargas
committed
modal working
1 parent 64348c4 commit fe31261

File tree

9 files changed

+78
-47
lines changed

9 files changed

+78
-47
lines changed

src/App.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.App {
2+
position: relative;
23
display: flex;
34
align-items: center;
45
justify-content: center;
@@ -28,6 +29,6 @@
2829
}
2930

3031
.App-logo {
31-
height: 10vmin;
32+
height: 20vmin;
3233
pointer-events: none;
3334
}

src/App.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import logo from './logo.png';
1+
import nfc from './nfc.svg';
22
import './App.css';
33
import Scan from './containers/Scan';
44
import Write from './containers/Write';
@@ -8,19 +8,19 @@ function App() {
88
const [isScan, setScan] = useState(false);
99
const [isWrite, setWrite] = useState(false);
1010

11-
const onHandleScan =()=>{
12-
setScan(!isScan);
11+
const onHandleScan = () =>{
12+
setScan(true);
1313
setWrite(false)
1414
}
1515

16-
const onHandleWrite =()=>{
16+
const onHandleWrite = () =>{
1717
setScan(false);
18-
setWrite(!isWrite)
18+
setWrite(true)
1919
}
2020

2121
return (
2222
<div className="App">
23-
<img src={logo} className="App-logo" alt="logo" />
23+
<img src={nfc} className="App-logo" alt="logo" />
2424
<h1>NFC Tool</h1>
2525
<div className="App-container">
2626
<button onClick={onHandleScan} className="App-button">Scan</button>

src/components/Scanner/Scanner.css

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.scanner {
2+
position: absolute;
3+
z-index: 1;
4+
top: calc(50vh - 150px);
5+
border-radius: 15px;
6+
background-color: #fff;
7+
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
8+
}
9+
10+
.scanner-exit {
11+
text-align: right;
12+
padding-right: 16px;
13+
font-weight: 800;
14+
cursor: pointer;
15+
}
16+
17+
.scanner-container {
18+
height: 300px;
19+
width: 250px;
20+
display: flex;
21+
justify-content: center;
22+
align-items: center;
23+
flex-direction: column;
24+
}
25+
26+
.scanner-image {
27+
height: 20vmin;
28+
}

src/components/Scanner/Scanner.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import React from 'react';
2+
import './Scanner.css'
3+
import Spinner from '../../spinner.gif';
24

3-
const Scanner = ({scanned}) => {
5+
const Scanner = ({status}) => {
46
return (
57
<div className="scanner">
8+
<p className="scanner-exit">X</p>
69
<div className="scanner-container">
7-
<div className="scanner-image" />
8-
<div className="scanner-text">
9-
{scanned}
10-
</div>
10+
<img src={Spinner} alt="spinning log" className="scanner-image"/>
11+
<p className="scanner-text">
12+
Scanning...
13+
</p>
1114
</div>
1215
</div>
1316
);
File renamed without changes.

src/containers/Scan.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import React, { useEffect, useState } from 'react';
1+
import React, { useCallback, useEffect, useState } from 'react';
22
import Scanner from '../components/Scanner/Scanner';
33

44
const Scan = () => {
5-
const [isScanned, setiSScanned] = useState('Scanning...');
5+
const [status, setStatus] = useState('scanning');
66
const [message, setMessage] = useState('');
77
const [serialNumber, setSerialNumber] = useState('');
88

9-
const scan = () => {
9+
const scan = useCallback(async() => {
1010
if ('NDEFReader' in window) {
11-
const ndef = new window.NDEFReader();
12-
13-
ndef.scan().then(() => {
11+
try {
12+
const ndef = new window.NDEFReader();
13+
await ndef.scan();
14+
1415
console.log("Scan started successfully.");
1516
ndef.onreadingerror = () => {
1617
console.log("Cannot read data from the NFC tag. Try another one?");
@@ -19,20 +20,18 @@ const Scan = () => {
1920
ndef.onreading = event => {
2021
console.log("NDEF message read.");
2122
onReading(event);
22-
setiSScanned('Scanned');
23+
setStatus('scanned');
2324
};
24-
}).catch(error => {
25+
26+
} catch(error){
2527
console.log(`Error! Scan failed to start: ${error}.`);
26-
});
28+
};
2729
}
28-
}
30+
},[]);
2931

3032
const onReading = ({message, serialNumber}) => {
3133
setSerialNumber(serialNumber);
3234
for (const record of message.records) {
33-
console.log("Record type: " + record.recordType);
34-
console.log("MIME type: " + record.mediaType);
35-
console.log("Record id: " + record.id);
3635
switch (record.recordType) {
3736
case "text":
3837
const textDecoder = new TextDecoder(record.encoding);
@@ -47,12 +46,19 @@ const Scan = () => {
4746
}
4847
};
4948

50-
scan();
51-
return (
49+
useEffect(() => {
50+
scan();
51+
}, [scan]);
52+
53+
54+
return(
5255
<>
53-
<p>Serial Number: {serialNumber}</p>
54-
<p>Message: {message}</p>
55-
<Scanner scanned={isScanned}></Scanner>
56+
{status === 'scanned' ?
57+
<div>
58+
<p>Serial Number: {serialNumber}</p>
59+
<p>Message: {message}</p>
60+
</div>
61+
: <Scanner status={status}></Scanner> }
5662
</>
5763
);
5864
};

src/containers/Write.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
import React from 'react';
1+
import React, { useCallback, useEffect } from 'react';
22

33
const Write = () => {
44

5-
const onWrite = async() => {
6-
const abortController = new AbortController();
7-
abortController.signal.onabort = event => {
8-
alert('STOP');
9-
};
5+
const onWrite = useCallback(async() => {
106
try {
117
const ndef = new window.NDEFReader();
12-
await ndef.write(
13-
{
14-
records: [{ recordType: "text", data: "17" }]
15-
},
16-
{signal: abortController.signal}
17-
);
18-
alert(`${16} saved!`);
19-
abortController.abort();
8+
await ndef.write({records: [{ recordType: "text", data: "18" }]});
9+
alert(`${18} saved!`);
2010
} catch (error) {
2111
console.log(error);
22-
abortController.abort();
2312
}
24-
};
13+
}, []);
14+
15+
useEffect(() => {
16+
onWrite();
17+
}, [onWrite])
2518

26-
onWrite();
2719
return (
2820
"Write"
2921
);

src/nfc.svg

Lines changed: 1 addition & 0 deletions
Loading

src/spinner.gif

88 KB
Loading

0 commit comments

Comments
 (0)