import React from "react"
import * as antd from "antd"
import classnames from "classnames"
import NFCModel from "comty.js/models/nfc"
import { Icons } from "components/Icons"
import StepsContext from "./context"
import CheckRegister from "./steps/check_register"
import DataEditor from "./steps/data_editor"
import TagWritter from "./steps/tag_writter"
import Success from "./steps/success"
import "./index.less"
const RegisterNewTagSteps = [
CheckRegister,
DataEditor,
TagWritter,
Success,
]
const RegisterNewTag = (props) => {
const [step, setStep] = React.useState(0)
const [stepsValues, setStepsValues] = React.useState({
...props.tagData ?? {}
})
const nextStep = () => {
setStep((step) => step + 1)
}
const prevStep = () => {
setStep((step) => step - 1)
}
const finish = () => {
if (typeof props.onFinish === "function") {
props.onFinish()
}
if (typeof props.close === "function") {
props.close()
}
}
// create a react context for the steps
const StepsContextValue = {
next: nextStep,
prev: prevStep,
values: stepsValues,
setValue: (key, value) => {
setStepsValues((stepsValues) => {
return {
...stepsValues,
[key]: value
}
})
},
onFinish: finish,
nfcReader: app.cores.nfc.instance(),
close: props.close
}
if (props.tagData) {
return
}
if (app.cores.nfc.incompatible) {
return
}
return 0
}
)}
>
}
className={classnames(
"tap-share-register-header-back",
{
["hidden"]: step === 0
}
)}
/>
Register new tag
{
React.createElement(RegisterNewTagSteps[step])
}
}
const TagItem = (props) => {
return
{props.tag.alias}
{props.tag.serial}
}
onClick={props.onEdit}
/>
}
danger
disabled
/>
}
class OwnTags extends React.Component {
state = {
loading: true,
error: null,
data: null,
}
loadData = async () => {
this.setState({
loading: true,
})
const result = await NFCModel.getOwnTags()
.catch((err) => {
console.error(err)
this.setState({
error: err.message,
loading: false,
data: null
})
return false
})
if (!result) {
return false
}
this.setState({
loading: false,
data: result,
error: null
})
}
handleTagRead = async (error, tag) => {
if (error) {
console.error(error)
return false
}
const ownedTag = this.state.data.find((ownedTag) => {
return ownedTag.serial === tag.serialNumber
})
if (!ownedTag && app.DrawerController.drawersLength() === 0) {
app.message.error("This tag is not registered or you don't have permission to edit it.")
return false
}
return OpenTagEditor({
tag: ownedTag
})
}
componentDidMount = async () => {
await this.loadData()
app.cores.nfc.subscribe(this.handleTagRead)
}
componentWillUnmount = () => {
app.cores.nfc.unsubscribe(this.handleTagRead)
}
render() {
if (this.state.loading) {
return
}
if (!this.state.data) {
return
}
return
{
this.state.data.length === 0 &&
}
{
this.state.data.length > 0 && this.state.data.map((tag) => {
return
{
OpenTagEditor({
tag
})
}}
/>
})
}
}
}
const OpenTagEditor = ({ tag, onFinish = () => app.navigation.softReload() } = {}) => {
app.DrawerController.open("tag_register", RegisterNewTag, {
componentProps: {
tagData: tag,
onFinish: onFinish,
}
})
}
const TapShareRender = () => {
return
{
!app.cores.nfc.scanning && app.isMobile &&
}
Registered Tags
{
app.cores.nfc.scanning &&
You can quickly edit your tags by tapping them.
}
{
app.isMobile &&
}
onClick={() => OpenTagEditor()}
>
Add new
}
}
export default {
id: "tap_share",
icon: "MdNfc",
label: "Tap Share",
group: "app",
render: TapShareRender
}