/* ScanAp.tsx - ESP3D WebUI component file Copyright (c) 2021 Luc LEBOSSE. All rights reserved. This code is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with This code; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import { Fragment, FunctionalComponent } from "preact" import { useState, useEffect } from "preact/hooks" import { ButtonImg, Loading } from "./../Controls" import { useTargetCommands } from "../../hooks" import { useUiContextFn, useModalsContext, useToastsContext } from "../../contexts" import { T } from "./../Translations" import { Lock, CheckCircle } from "preact-feather" interface AccessPoint { SSID: string SIGNAL: number IS_PROTECTED: boolean } interface ScanApListProps { id: string setValue: (value: string) => void refreshfn: (fn: () => void) => void } const ScanApList: FunctionalComponent = ({ id, setValue, refreshfn }) => { const { modals } = useModalsContext() const { toasts } = useToastsContext() const [isLoading, setIsLoading] = useState(true) const [APList, setApList] = useState([]) const { targetCommands } = useTargetCommands() const ScanNetworks = () => { setIsLoading(true) const callbacks = { onSuccess: (result: string) => { setIsLoading(false) const jsonResult = JSON.parse(result) if ( jsonResult.cmd != 410 || jsonResult.status == "error" || !jsonResult.data ) { toasts.addToast({ content: T("S194"), type: "error" }) } else { setApList(jsonResult.data) } }, onFail: (error: string) => { setIsLoading(false) toasts.addToast({ content: error, type: "error" }) setApList([]) }, } targetCommands("[ESP410]json=yes", undefined, { echo: false }, callbacks) } useEffect(() => { ScanNetworks() refreshfn(ScanNetworks) }, []) return ( {isLoading && } {!isLoading && ( {APList.map((e) => { const SSID = e.SSID.replace("'", "'").replace( """, '"' ) return ( ) })}
{T("SSID")} {T("signal")} {T("S49")} {T("S48")}
{SSID} {e.SIGNAL}% {e.IS_PROTECTED ? : ""} } onClick={() => { useUiContextFn.haptic() setValue(SSID) modals.removeModal( modals.getModalIndex(id) ) }} />
)}
) } export { ScanApList }