|
1 | | -import React, { useReducer } from "react"; |
| 1 | +import React, { useReducer, useEffect, useRef } from "react"; |
2 | 2 |
|
3 | | -import fetchNDilation from "../../lib/fetchNDilation"; |
4 | 3 | import { useWindowContext } from "../WindowContext"; |
5 | 4 | import DilationForm from "./DilationForm"; |
6 | 5 | import Result from "./Result"; |
| 6 | +import WasmLoader from "../WasmLoader"; |
7 | 7 |
|
8 | | -const initialState = { isLoading: false, dilation: null, error: null }; |
| 8 | +const initialState = { |
| 9 | + isLoading: false, |
| 10 | + dilation: null, |
| 11 | + validationError: false, |
| 12 | + runtimeError: false, |
| 13 | + ready: false, |
| 14 | +}; |
9 | 15 |
|
10 | 16 | function reducer(state, action) { |
11 | 17 | switch (action.type) { |
12 | | - case "FETCH_START": |
13 | | - return { isLoading: true, dilation: null, error: null }; |
14 | | - case "FETCH_OK": |
15 | | - return { isLoading: false, dilation: action.payload, error: null }; |
16 | | - case "FETCH_NOT_OK": |
17 | | - return { isLoading: false, dilation: null, error: action.payload }; |
18 | | - case "FETCH_ERROR": |
19 | | - return { isLoading: false, dilation: null, error: action.payload }; |
| 18 | + case "WASM_INITIALIZED": |
| 19 | + return { ...state, ready: true }; |
| 20 | + case "CALCULATE_START": |
| 21 | + return { |
| 22 | + ...state, |
| 23 | + isLoading: true, |
| 24 | + dilation: null, |
| 25 | + runtimeError: false, |
| 26 | + validationError: false, |
| 27 | + }; |
| 28 | + case "CALCULATE_OK": |
| 29 | + return { |
| 30 | + ...state, |
| 31 | + isLoading: false, |
| 32 | + dilation: action.payload, |
| 33 | + error: null, |
| 34 | + }; |
| 35 | + case "VALIDATION_ERROR": |
| 36 | + return { |
| 37 | + ...state, |
| 38 | + isLoading: false, |
| 39 | + dilation: null, |
| 40 | + runtimeError: false, |
| 41 | + validationError: true, |
| 42 | + }; |
| 43 | + case "CALCULATE_ERROR": |
| 44 | + return { |
| 45 | + ...state, |
| 46 | + ready: false, |
| 47 | + isLoading: false, |
| 48 | + dilation: null, |
| 49 | + runtimeError: true, |
| 50 | + validationError: false, |
| 51 | + }; |
20 | 52 | default: |
21 | 53 | return state; |
22 | 54 | } |
23 | 55 | } |
24 | 56 |
|
25 | | -const createOnSubmitHandler = (fetch, dispatch) => async (matrix, degree) => { |
| 57 | +const createOnSubmitHandler = (unitaryNDilationAsync, dispatch) => async ( |
| 58 | + matrix, |
| 59 | + degree |
| 60 | +) => { |
26 | 61 | try { |
27 | | - dispatch({ type: "FETCH_START" }); |
28 | | - const response = await fetchNDilation(fetch, matrix, degree); |
29 | | - |
30 | | - const body = await response.json(); |
| 62 | + dispatch({ type: "CALCULATE_START" }); |
| 63 | + const { error, value } = await unitaryNDilationAsync(matrix, degree); |
31 | 64 |
|
32 | | - if (response.ok) { |
33 | | - dispatch({ type: "FETCH_OK", payload: body.value }); |
| 65 | + if (!error) { |
| 66 | + dispatch({ type: "CALCULATE_OK", payload: value }); |
34 | 67 | } else { |
35 | | - dispatch({ type: "FETCH_NOT_OK", payload: body }); |
| 68 | + dispatch({ type: "VALIDATION_ERROR", payload: error }); |
36 | 69 | } |
37 | 70 | } catch (e) { |
38 | | - dispatch({ type: "FETCH_ERROR", payload: e }); |
| 71 | + dispatch({ type: "CALCULATE_ERROR", payload: e }); |
39 | 72 | } |
40 | 73 | }; |
41 | 74 |
|
42 | 75 | const Calculator = () => { |
43 | | - const [{ isLoading, dilation, error }, dispatch] = useReducer( |
44 | | - reducer, |
45 | | - initialState |
46 | | - ); |
| 76 | + const [ |
| 77 | + { isLoading, dilation, validationError, runtimeError }, |
| 78 | + dispatch, |
| 79 | + ] = useReducer(reducer, initialState); |
| 80 | + const onSubmitHandler = useRef(null); |
47 | 81 | const window = useWindowContext(); |
48 | 82 |
|
| 83 | + useEffect(() => { |
| 84 | + if (!onSubmitHandler.current) { |
| 85 | + const unitaryNDilationAsync = (matrix, degree) => |
| 86 | + Promise.resolve(window.UnitaryNDilation(matrix, degree)); |
| 87 | + onSubmitHandler.current = createOnSubmitHandler( |
| 88 | + unitaryNDilationAsync, |
| 89 | + dispatch |
| 90 | + ); |
| 91 | + dispatch({ type: "WASM_INITIALIZED" }); |
| 92 | + } |
| 93 | + }, [window && window.UnitaryNDilation]); |
| 94 | + |
49 | 95 | return ( |
50 | 96 | <div> |
51 | | - <DilationForm |
52 | | - onSubmit={ |
53 | | - window && createOnSubmitHandler(window.fetch, dispatch) |
54 | | - } |
55 | | - /> |
| 97 | + <WasmLoader /> |
| 98 | + <DilationForm onSubmit={onSubmitHandler.current} /> |
56 | 99 | <Result |
57 | 100 | isLoading={isLoading} |
58 | | - errorDetails={error} |
| 101 | + validationError={validationError} |
| 102 | + runtimeError={runtimeError} |
59 | 103 | dilation={dilation} |
60 | 104 | /> |
61 | 105 | </div> |
|
0 commit comments