From 9e5b09bc1a3164c4f63ab65888816dc27d3e2a82 Mon Sep 17 00:00:00 2001 From: xianshenglu Date: Wed, 27 Apr 2022 23:51:39 +0800 Subject: [PATCH] feat(TestRun): start test and download when user clicks start button --- screens/TestRunScreen/components/TestPage.tsx | 25 ++++++++++++++++--- .../hooks/useTestRunningStatus.ts | 24 ++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 screens/TestRunScreen/hooks/useTestRunningStatus.ts diff --git a/screens/TestRunScreen/components/TestPage.tsx b/screens/TestRunScreen/components/TestPage.tsx index 978e567..113bc4a 100644 --- a/screens/TestRunScreen/components/TestPage.tsx +++ b/screens/TestRunScreen/components/TestPage.tsx @@ -1,6 +1,6 @@ import { StyleSheet, Button, TextInput } from "react-native"; import { Text, View } from "@/components/Themed"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import { responseTestService } from "@/services/ResponseTest.service"; import { downloadTestService } from "@/services/DownloadTest.service"; import { TableHeader } from "@/components/Table/TableHeader"; @@ -9,7 +9,7 @@ import { useTableData } from "../hooks/useTableData"; import { useTestIpCount } from "../hooks/useTestIpCount"; import { TableRows } from "@/components/Table/TableRows"; import { initialTestPageTableHeaderCols, MyTableHeaderColumn } from "../model"; -import { RequestStatus } from "@/typings"; +import { useTestRunningStatus } from "../hooks/useTestRunningStatus"; export default function TestPage({ path }: { path: string }) { const { testIpCount, setTestIpCount, getIpList } = useTestIpCount(); @@ -37,13 +37,32 @@ export default function TestPage({ path }: { path: string }) { changeTableHeadersSortType, } = useTableHeader(initialTestPageTableHeaderCols); + const { testRunningStatus, nextTestRunningStatus } = useTestRunningStatus(); + function onReset() { responseTestService.stop(); downloadTestService.stop(); resetTableData(); resetTableHeader(); - initTableData(getIpList()); + const newIpList = getIpList(); + initTableData(newIpList); + nextTestRunningStatus(); } + + useEffect(() => { + // in the future may need to add a status check + startResponseSpeedTest( + getSelectedIpList(), + Number(testIpCoCurrentCount), + testUrl + ); + startDownloadSpeedTest( + getSelectedIpList(), + Number(testIpCoCurrentCount), + testUrl + ); + }, [testRunningStatus]); + function onSort( colId: MyTableHeaderColumn["id"], sortType: MyTableHeaderColumn["sort"] diff --git a/screens/TestRunScreen/hooks/useTestRunningStatus.ts b/screens/TestRunScreen/hooks/useTestRunningStatus.ts new file mode 100644 index 0000000..3521b00 --- /dev/null +++ b/screens/TestRunScreen/hooks/useTestRunningStatus.ts @@ -0,0 +1,24 @@ +import { useState } from "react"; +export enum TestRunningStatus { + Uninitialized = "Uninitialized", + Running = "Running", +} +const nextTestRunningStatusMap = { + [TestRunningStatus.Uninitialized]: TestRunningStatus.Running as const, + [TestRunningStatus.Running]: TestRunningStatus.Uninitialized as const, +}; +export function useTestRunningStatus() { + const [testRunningStatus, setTestRunningStatus] = + useState<`${TestRunningStatus}`>(TestRunningStatus.Uninitialized); + + function nextTestRunningStatus() { + setTestRunningStatus((prev) => { + return nextTestRunningStatusMap[prev]; + }); + } + + return { + testRunningStatus, + nextTestRunningStatus, + }; +}