From 30ee5e4a67c86df0f3bf7457ee06cbd9e7044d72 Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Tue, 10 May 2022 14:57:46 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Search=20page=20refactoring?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/actions/airdcpp.actions.tsx | 378 ++++++++++---------- src/client/components/Library/SearchBar.tsx | 1 - 2 files changed, 189 insertions(+), 190 deletions(-) diff --git a/src/client/actions/airdcpp.actions.tsx b/src/client/actions/airdcpp.actions.tsx index d2938b2..3af75d8 100644 --- a/src/client/actions/airdcpp.actions.tsx +++ b/src/client/actions/airdcpp.actions.tsx @@ -33,84 +33,84 @@ function sleep(ms: number): Promise { export const search = (data: SearchData, ADCPPSocket: any, credentials: any) => - async (dispatch) => { - try { - if (!ADCPPSocket.isConnected()) { - await ADCPPSocket.connect( - credentials.username, - credentials.password, - true, - ); - } - const instance: SearchInstance = await ADCPPSocket.post("search"); - dispatch({ - type: AIRDCPP_SEARCH_IN_PROGRESS, - }); - - // We want to get notified about every new result in order to make the user experience better - await ADCPPSocket.addListener( - `search`, - "search_result_added", - async (groupedResult) => { - // ...add the received result in the UI - // (it's probably a good idea to have some kind of throttling for the UI updates as there can be thousands of results) - - dispatch({ - type: AIRDCPP_SEARCH_RESULTS_ADDED, - groupedResult, - }); - }, - instance.id, + async (dispatch) => { + try { + if (!ADCPPSocket.isConnected()) { + await ADCPPSocket.connect( + credentials.username, + credentials.password, + true, ); - - // We also want to update the existing items in our list when new hits arrive for the previously listed files/directories - await ADCPPSocket.addListener( - `search`, - "search_result_updated", - async (groupedResult) => { - // ...update properties of the existing result in the UI - dispatch({ - type: AIRDCPP_SEARCH_RESULTS_UPDATED, - groupedResult, - }); - }, - instance.id, - ); - - // We need to show something to the user in case the search won't yield any results so that he won't be waiting forever) - // Wait for 5 seconds for any results to arrive after the searches were sent to the hubs - await ADCPPSocket.addListener( - `search`, - "search_hub_searches_sent", - async (searchInfo) => { - await sleep(5000); - - // Check the number of received results (in real use cases we should know that even without calling the API) - const currentInstance = await ADCPPSocket.get( - `search/${instance.id}`, - ); - if (currentInstance.result_count === 0) { - // ...nothing was received, show an informative message to the user - console.log("No more search results."); - } - - // The search can now be considered to be "complete" - // If there's an "in progress" indicator in the UI, that could also be disabled here - dispatch({ - type: AIRDCPP_HUB_SEARCHES_SENT, - searchInfo, - instance, - }); - }, - instance.id, - ); - // Finally, perform the actual search - await ADCPPSocket.post(`search/${instance.id}/hub_search`, data); - } catch (error) { - console.log(error); - throw error; } - }; + const instance: SearchInstance = await ADCPPSocket.post("search"); + dispatch({ + type: AIRDCPP_SEARCH_IN_PROGRESS, + }); + + // We want to get notified about every new result in order to make the user experience better + await ADCPPSocket.addListener( + `search`, + "search_result_added", + async (groupedResult) => { + // ...add the received result in the UI + // (it's probably a good idea to have some kind of throttling for the UI updates as there can be thousands of results) + + dispatch({ + type: AIRDCPP_SEARCH_RESULTS_ADDED, + groupedResult, + }); + }, + instance.id, + ); + + // We also want to update the existing items in our list when new hits arrive for the previously listed files/directories + await ADCPPSocket.addListener( + `search`, + "search_result_updated", + async (groupedResult) => { + // ...update properties of the existing result in the UI + dispatch({ + type: AIRDCPP_SEARCH_RESULTS_UPDATED, + groupedResult, + }); + }, + instance.id, + ); + + // We need to show something to the user in case the search won't yield any results so that he won't be waiting forever) + // Wait for 5 seconds for any results to arrive after the searches were sent to the hubs + await ADCPPSocket.addListener( + `search`, + "search_hub_searches_sent", + async (searchInfo) => { + await sleep(5000); + + // Check the number of received results (in real use cases we should know that even without calling the API) + const currentInstance = await ADCPPSocket.get( + `search/${instance.id}`, + ); + if (currentInstance.result_count === 0) { + // ...nothing was received, show an informative message to the user + console.log("No more search results."); + } + + // The search can now be considered to be "complete" + // If there's an "in progress" indicator in the UI, that could also be disabled here + dispatch({ + type: AIRDCPP_HUB_SEARCHES_SENT, + searchInfo, + instance, + }); + }, + instance.id, + ); + // Finally, perform the actual search + await ADCPPSocket.post(`search/${instance.id}/hub_search`, data); + } catch (error) { + console.log(error); + throw error; + } + }; export const downloadAirDCPPItem = ( @@ -120,133 +120,133 @@ export const downloadAirDCPPItem = ADCPPSocket: any, credentials: any, ): void => - async (dispatch) => { - try { - if (!ADCPPSocket.isConnected()) { - await ADCPPSocket.connect( - `${credentials.username}`, - `${credentials.password}`, - true, - ); - } - let bundleDBImportResult = {}; - const downloadResult = await ADCPPSocket.post( - `search/${instanceId}/results/${resultId}/download`, + async (dispatch) => { + try { + if (!ADCPPSocket.isConnected()) { + await ADCPPSocket.connect( + `${credentials.username}`, + `${credentials.password}`, + true, ); - let downloadStatus = undefined; - // download status check - await ADCPPSocket.addListener(`queue`, "queue_file_status", (status) => { - if (status.status.completed) { - downloadStatus = status; - - dispatch({ - type: LS_SINGLE_IMPORT, - meta: { remote: true }, - data: { downloadStatus }, - }); - } - }); - - let bundleId; - let directoryIds; - if (!isNil(downloadResult.bundle_info)) { - bundleId = downloadResult.bundle_info.id; - } - if (!isNil(downloadResult.directory_download_ids)) { - directoryIds = downloadResult.directory_download_ids.map( - (item) => item.id, - ); - } - - if (!isNil(downloadResult)) { - bundleDBImportResult = await axios({ - method: "POST", - url: `${LIBRARY_SERVICE_BASE_URI}/applyAirDCPPDownloadMetadata`, - headers: { - "Content-Type": "application/json; charset=utf-8", - }, - data: { - resultId, - comicObjectId, - searchInstanceId: instanceId, - bundleId, - directoryIds, - }, - }); - dispatch({ - type: AIRDCPP_RESULT_DOWNLOAD_INITIATED, - downloadResult: downloadResult, - bundleDBImportResult, - }); - dispatch({ - type: IMS_COMIC_BOOK_DB_OBJECT_FETCHED, - comicBookDetail: bundleDBImportResult.data, - IMS_inProgress: false, - }); - } - } catch (error) { - throw error; } - }; + let bundleDBImportResult = {}; + const downloadResult = await ADCPPSocket.post( + `search/${instanceId}/results/${resultId}/download`, + ); + let downloadStatus = undefined; + // download status check + await ADCPPSocket.addListener(`queue`, "queue_file_status", (status) => { + if (status.status.completed) { + downloadStatus = status; -export const getDownloadProgress = - (comicObjectId: string, ADCPPSocket: any, credentials: any): void => - async (dispatch) => { - try { - if (!ADCPPSocket.isConnected()) { - await ADCPPSocket.connect( - `${credentials.username}`, - `${credentials.password}`, - true, - ); + dispatch({ + type: LS_SINGLE_IMPORT, + meta: { remote: true }, + data: { downloadStatus, comicObjectId }, + }); } - await ADCPPSocket.addListener( - `queue`, - "queue_bundle_tick", - async (downloadProgressData) => { - dispatch({ - type: AIRDCPP_DOWNLOAD_PROGRESS_TICK, - downloadProgressData, - }); - }, + }); + + let bundleId; + let directoryIds; + if (!isNil(downloadResult.bundle_info)) { + bundleId = downloadResult.bundle_info.id; + } + if (!isNil(downloadResult.directory_download_ids)) { + directoryIds = downloadResult.directory_download_ids.map( + (item) => item.id, ); - } catch (error) { - throw error; } - }; -export const getBundlesForComic = - (comicObjectId: string, ADCPPSocket: any, credentials: any) => - async (dispatch) => { - try { - if (!ADCPPSocket.isConnected()) { - await ADCPPSocket.connect( - `${credentials.username}`, - `${credentials.password}`, - true, - ); - } - const comicObject = await axios({ + if (!isNil(downloadResult)) { + bundleDBImportResult = await axios({ method: "POST", - url: `${LIBRARY_SERVICE_BASE_URI}/getComicBookById`, + url: `${LIBRARY_SERVICE_BASE_URI}/applyAirDCPPDownloadMetadata`, headers: { "Content-Type": "application/json; charset=utf-8", }, data: { - id: `${comicObjectId}`, + resultId, + comicObjectId, + searchInstanceId: instanceId, + bundleId, + directoryIds, }, }); - // get only the bundles applicable for the comic - const filteredBundles = comicObject.data.acquisition.directconnect.map( - async ({ bundleId }) => { - return await ADCPPSocket.get(`queue/bundles/${bundleId}`); - }, - ); dispatch({ - type: AIRDCPP_BUNDLES_FETCHED, - bundles: await Promise.all(filteredBundles), + type: AIRDCPP_RESULT_DOWNLOAD_INITIATED, + downloadResult: downloadResult, + bundleDBImportResult, + }); + dispatch({ + type: IMS_COMIC_BOOK_DB_OBJECT_FETCHED, + comicBookDetail: bundleDBImportResult.data, + IMS_inProgress: false, }); - } catch (error) { - throw error; } - }; + } catch (error) { + throw error; + } + }; + +export const getDownloadProgress = + (comicObjectId: string, ADCPPSocket: any, credentials: any): void => + async (dispatch) => { + try { + if (!ADCPPSocket.isConnected()) { + await ADCPPSocket.connect( + `${credentials.username}`, + `${credentials.password}`, + true, + ); + } + await ADCPPSocket.addListener( + `queue`, + "queue_bundle_tick", + async (downloadProgressData) => { + dispatch({ + type: AIRDCPP_DOWNLOAD_PROGRESS_TICK, + downloadProgressData, + }); + }, + ); + } catch (error) { + throw error; + } + }; + +export const getBundlesForComic = + (comicObjectId: string, ADCPPSocket: any, credentials: any) => + async (dispatch) => { + try { + if (!ADCPPSocket.isConnected()) { + await ADCPPSocket.connect( + `${credentials.username}`, + `${credentials.password}`, + true, + ); + } + const comicObject = await axios({ + method: "POST", + url: `${LIBRARY_SERVICE_BASE_URI}/getComicBookById`, + headers: { + "Content-Type": "application/json; charset=utf-8", + }, + data: { + id: `${comicObjectId}`, + }, + }); + // get only the bundles applicable for the comic + const filteredBundles = comicObject.data.acquisition.directconnect.map( + async ({ bundleId }) => { + return await ADCPPSocket.get(`queue/bundles/${bundleId}`); + }, + ); + dispatch({ + type: AIRDCPP_BUNDLES_FETCHED, + bundles: await Promise.all(filteredBundles), + }); + } catch (error) { + throw error; + } + }; diff --git a/src/client/components/Library/SearchBar.tsx b/src/client/components/Library/SearchBar.tsx index 36d39fe..2afb71b 100644 --- a/src/client/components/Library/SearchBar.tsx +++ b/src/client/components/Library/SearchBar.tsx @@ -8,7 +8,6 @@ import { searchIssue } from "../../actions/fileops.actions"; export const SearchBar = (): ReactElement => { const dispatch = useDispatch(); const handleSubmit = useCallback((e) => { - console.log(e); dispatch( searchIssue( {