| 64 | type JsonSearchAction = SearchReceiveWorkerEvent | SearchAction | ResetAction; |
| 65 | |
| 66 | function reducer( |
| 67 | state: JsonSearchState, |
| 68 | action: JsonSearchAction |
| 69 | ): JsonSearchState { |
| 70 | switch (state.status) { |
| 71 | case "initializing": { |
| 72 | if (action.type === "index-initialized") { |
| 73 | return { |
| 74 | ...state, |
| 75 | status: "idle", |
| 76 | results: undefined, |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | return state; |
| 81 | } |
| 82 | case "idle": { |
| 83 | if (action.type === "reset") { |
| 84 | return { |
| 85 | ...state, |
| 86 | query: undefined, |
| 87 | results: undefined, |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | if (action.type === "search") { |
| 92 | return { |
| 93 | ...state, |
| 94 | status: "searching", |
| 95 | query: action.payload.query, |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | return state; |
| 100 | } |
| 101 | case "searching": { |
| 102 | if (action.type === "reset") { |
| 103 | return { |
| 104 | ...state, |
| 105 | status: "idle", |
| 106 | query: undefined, |
| 107 | results: undefined, |
| 108 | }; |
| 109 | } |
| 110 | |
| 111 | if ( |
| 112 | action.type === "search-results" && |
| 113 | state.query === action.payload.query |
| 114 | ) { |
| 115 | return { |
| 116 | ...state, |
| 117 | status: "idle", |
| 118 | results: action.payload.results, |
| 119 | }; |
| 120 | } |
| 121 | |
| 122 | return state; |
| 123 | } |