From fbbaf853dcc135e43a7500bf56287098a84edca6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 29 May 2021 16:56:31 -0500 Subject: add getResponses --- src/web/src/lib/search.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/web') diff --git a/src/web/src/lib/search.js b/src/web/src/lib/search.js index c71dba96..47f431aa 100644 --- a/src/web/src/lib/search.js +++ b/src/web/src/lib/search.js @@ -4,6 +4,10 @@ export const search = ({ id, searchText }) => { return api.post(`/searches`, { id, searchText }); }; -export const getStatus = async ({ id }) => { +export const getStatus = async ({ id, includeResponses = false }) => { + return (await api.get(`/searches/${encodeURIComponent(id)}?includeResponses=${includeResponses}`)).data; +}; + +export const getResponses = async ({ id }) => { return (await api.get(`/searches/${encodeURIComponent(id)}`)).data; }; \ No newline at end of file -- cgit v1.2.3 From c9295fdaa1e0c004a406f1f7b5fbf9e181afbc54 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 29 May 2021 17:03:54 -0500 Subject: actually get responses --- src/web/src/lib/search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/web') diff --git a/src/web/src/lib/search.js b/src/web/src/lib/search.js index 47f431aa..2179dcd2 100644 --- a/src/web/src/lib/search.js +++ b/src/web/src/lib/search.js @@ -9,5 +9,5 @@ export const getStatus = async ({ id, includeResponses = false }) => { }; export const getResponses = async ({ id }) => { - return (await api.get(`/searches/${encodeURIComponent(id)}`)).data; + return (await api.get(`/searches/${encodeURIComponent(id)}/responses`)).data; }; \ No newline at end of file -- cgit v1.2.3 From a4280e899dd5d7b8b4cef6cb33fe48a375055e98 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 29 May 2021 17:04:28 -0500 Subject: dont rely on POST response for search state --- src/web/src/components/Search/Search.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src/web') diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js index 48e05775..f1caee54 100644 --- a/src/web/src/components/Search/Search.js +++ b/src/web/src/components/Search/Search.js @@ -50,13 +50,9 @@ class Search extends Component { const searchPhrase = this.inputtext.inputRef.current.value; const searchId = uuidv4(); - this.setState({ searchPhrase, searchId, searchState: 'pending' }, () => { - search.search({ id: searchId, searchText: searchPhrase }) - .then(response => this.setState({ results: response.data })) - .then(() => this.setState({ searchState: 'complete' }, () => { - this.saveState(); - this.setSearchText(); - })) + this.setState({ searchPhrase, searchId, searchState: 'pending' }, async () => { + this.saveState(); + search.search({ id: searchId, searchText: searchPhrase }); }); } -- cgit v1.2.3 From 762833d3fed517fd1acad9aa5ac469a6f894bc13 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 29 May 2021 17:05:49 -0500 Subject: dont save results to localstorage --- src/web/src/components/Search/Search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/web') diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js index f1caee54..94b36051 100644 --- a/src/web/src/components/Search/Search.js +++ b/src/web/src/components/Search/Search.js @@ -72,7 +72,7 @@ class Search extends Component { saveState = () => { try { - localStorage.setItem('soulseek-example-search-state', JSON.stringify(this.state)); + localStorage.setItem('soulseek-example-search-state', JSON.stringify({ ...this.state, results: [] })); } catch(error) { console.log(error); } -- cgit v1.2.3 From 61375627fb3a82bdf53cc989339d69f39dca0165 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 29 May 2021 17:06:03 -0500 Subject: promisify loadstate --- src/web/src/components/Search/Search.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/web') diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js index 94b36051..ab1257e9 100644 --- a/src/web/src/components/Search/Search.js +++ b/src/web/src/components/Search/Search.js @@ -79,7 +79,8 @@ class Search extends Component { } loadState = () => { - this.setState(JSON.parse(localStorage.getItem('soulseek-example-search-state')) || initialState); + return new Promise((resolve) => + this.setState(JSON.parse(localStorage.getItem('soulseek-example-search-state')) || initialState, resolve)); } componentDidMount = () => { -- cgit v1.2.3 From 0a4f7c5e6c6dd09ac4457460cbff0e33f0ab934f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 29 May 2021 17:06:38 -0500 Subject: fetch results on mount --- src/web/src/components/Search/Search.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'src/web') diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js index ab1257e9..ccb9c874 100644 --- a/src/web/src/components/Search/Search.js +++ b/src/web/src/components/Search/Search.js @@ -83,9 +83,9 @@ class Search extends Component { this.setState(JSON.parse(localStorage.getItem('soulseek-example-search-state')) || initialState, resolve)); } - componentDidMount = () => { - this.fetchStatus(); - this.loadState(); + componentDidMount = async () => { + await this.loadState(); + this.fetchResults(); this.setState({ interval: window.setInterval(this.fetchStatus, 500) }, () => this.setSearchText()); @@ -104,12 +104,25 @@ class Search extends Component { document.removeEventListener("keyup", this.keyUp, false); } - fetchStatus = () => { - if (this.state.searchState === 'pending') { - search.getStatus({ id: this.state.searchId }) - .then(response => this.setState({ - searchStatus: response - })); + fetchResults = async () => { + const { searchId } = this.state; + + if (!!searchId) { + this.setState({ fetching: true }, async () => { + try { + const responses = await search.getResponses({ id: searchId }); + + this.setState({ + results: responses, + fetching: false + }, this.saveState); + } catch (error) { + console.log(error); + this.clear(); + } + }); + } + } } } -- cgit v1.2.3 From 25ddbc2a75cec8cc3a2b5d2b1be7b86b733a2b1e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 29 May 2021 17:08:36 -0500 Subject: display loading status --- src/web/src/components/Search/Search.js | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'src/web') diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js index ccb9c874..639cc5d8 100644 --- a/src/web/src/components/Search/Search.js +++ b/src/web/src/components/Search/Search.js @@ -18,7 +18,7 @@ import { const initialState = { searchPhrase: '', - searchId: '', + searchId: undefined, searchState: 'idle', searchStatus: { responseCount: 0, @@ -31,6 +31,7 @@ const initialState = { hideNoFreeSlots: true, hiddenResults: [], hideLocked: true, + fetching: false, }; const sortOptions = { @@ -123,6 +124,25 @@ class Search extends Component { }); } } + + fetchStatus = async () => { + const { searchState, searchId } = this.state; + + if (searchState === 'pending') { + this.setState({ fetching: true }, async () => { + const response = await search.getStatus({ id: searchId }); + + if (response.isComplete) { + this.setState({ + searchState: 'complete' + }, this.fetchResults); + } else { + this.setState({ + searchStatus: response, + fetching: false + }, this.saveState); + } + }); } } @@ -131,7 +151,7 @@ class Search extends Component { } sortAndFilterResults = () => { - const { results, hideNoFreeSlots, resultSort, hideLocked, hiddenResults } = this.state; + const { results = [], hideNoFreeSlots, resultSort, hideLocked, hiddenResults = [] } = this.state; const { field, order } = sortOptions[resultSort]; return results @@ -164,8 +184,8 @@ class Search extends Component { } render = () => { - let { searchState, searchStatus, results, displayCount, resultSort, hideNoFreeSlots, hideLocked, hiddenResults } = this.state; - let pending = searchState === 'pending'; + let { searchState, searchStatus, results = [], displayCount, resultSort, hideNoFreeSlots, hideLocked, hiddenResults = [], fetching } = this.state; + let pending = fetching || searchState === 'pending'; const sortedAndFilteredResults = this.sortAndFilterResults(); @@ -195,7 +215,8 @@ class Search extends Component { inline='centered' size='big' > - Found {searchStatus.fileCount} files {searchStatus.lockedFileCount > 0 ? `(plus ${searchStatus.lockedFileCount} locked) ` : ''}from {searchStatus.responseCount} users + {searchState === 'pending' ? Found {searchStatus.fileCount} files {searchStatus.lockedFileCount > 0 ? `(plus ${searchStatus.lockedFileCount} locked) ` : ''}from {searchStatus.responseCount} users + : 'Fetching results'} :
-- cgit v1.2.3 From 4533681fbfc20e1e608f7ddf8ce4a105e281d481 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 29 May 2021 17:10:04 -0500 Subject: tweak verbiage --- src/web/src/components/Search/Search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/web') diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js index 639cc5d8..60720c1a 100644 --- a/src/web/src/components/Search/Search.js +++ b/src/web/src/components/Search/Search.js @@ -216,7 +216,7 @@ class Search extends Component { size='big' > {searchState === 'pending' ? Found {searchStatus.fileCount} files {searchStatus.lockedFileCount > 0 ? `(plus ${searchStatus.lockedFileCount} locked) ` : ''}from {searchStatus.responseCount} users - : 'Fetching results'} + : 'Loading results...'} :
-- cgit v1.2.3