From 9a07689dfeb736341b4f1b378e0ec758ea9cd0ff Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Sat, 6 Jul 2024 02:33:06 -0400 Subject: feat: Add NFC tag writing functionality to ChoreCard component, Add Email to sign up --- src/views/Modals/Inputs/WriteNFCModal.jsx | 109 ++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/views/Modals/Inputs/WriteNFCModal.jsx (limited to 'src/views/Modals/Inputs') diff --git a/src/views/Modals/Inputs/WriteNFCModal.jsx b/src/views/Modals/Inputs/WriteNFCModal.jsx new file mode 100644 index 0000000..d71a2a3 --- /dev/null +++ b/src/views/Modals/Inputs/WriteNFCModal.jsx @@ -0,0 +1,109 @@ +import { + Box, + Button, + Checkbox, + ListItem, + Modal, + ModalDialog, + Typography, +} from '@mui/joy' +import React, { useState } from 'react' + +function WriteNFCModal({ config }) { + const [nfcStatus, setNfcStatus] = useState('idle') // 'idle', 'writing', 'success', 'error' + const [errorMessage, setErrorMessage] = useState('') + const [isAutoCompleteWhenScan, setIsAutoCompleteWhenScan] = useState(false) + + const requestNFCAccess = async () => { + if ('NDEFReader' in window) { + // Assuming permission request is implicit in 'write' or 'scan' methods + setNfcStatus('idle') + } else { + alert('NFC is not supported by this browser.') + } + } + + const writeToNFC = async url => { + if ('NDEFReader' in window) { + try { + const ndef = new window.NDEFReader() + await ndef.write({ + records: [{ recordType: 'url', data: url }], + }) + setNfcStatus('success') + } catch (error) { + console.error('Error writing to NFC tag:', error) + setNfcStatus('error') + setErrorMessage('Error writing to NFC tag. Please try again.') + } + } else { + setNfcStatus('error') + setErrorMessage('NFC is not supported by this browser.') + } + } + + const handleClose = () => { + config.onClose() + setNfcStatus('idle') + setErrorMessage('') + } + const getURL = () => { + let url = config.url + if (isAutoCompleteWhenScan) { + url = url + '?auto_complete=true' + } + + return url + } + return ( + + + + {nfcStatus === 'success' ? 'Success!' : 'Write to NFC'} + + + {nfcStatus === 'success' ? ( + + URL written to NFC tag successfully! + + ) : ( + <> + + {nfcStatus === 'error' + ? errorMessage + : 'Press the button below to write to NFC.'} + + + setIsAutoCompleteWhenScan(e.target.checked)} + label='Auto-complete when scanned' + /> + + + + + + + )} + + + + + + + ) +} + +export default WriteNFCModal -- cgit