Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search improvements + better loader #751

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

apepper
Copy link
Contributor

@apepper apepper commented Feb 12, 2025

Motivation: When someone clicks on "Load more" a spinner should be shown until all informations are available (more items in the load and the status of "hasMore").

See effect by artificially slowing down localStorageDataConnection#index:

await new Promise((resolve) => setTimeout(resolve, 1500))

Otherwise the previous limit will be used. E.g. if someone searches for "X" and clicks the "Load more" button three times and the searches for "Y", the limit would be higher then the configuredLimit.
It shows a loading spinner, if more data is loaded for a Data List or Data Column List.

I also included "hasMore" in it to trigger a loading for the "Load more" button as well.
@apepper apepper requested a review from dcsaszar February 12, 2025 10:59
@dcsaszar dcsaszar self-assigned this Feb 12, 2025
Copy link
Contributor

@dcsaszar dcsaszar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting stuff. As this is purely for aesthetic purposes we must be careful with naming. Simpler is better.

@@ -28,7 +28,15 @@ export const DataBatchContextProvider = connect(
const configuredLimit = dataScope.limit() ?? 20
const [limit, setLimit] = useState(configuredLimit)
const [initialLimit, setInitialLimit] = useState(configuredLimit)
const [search, setSearch] = useState('')
const [search, setSearchState] = useState('')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Unfortunate naming. By not following the useState naming convention we can confuse the reader. Let's please stick to the conventions. How about [query, setQuery]?

Comment on lines +33 to +39
const setSearch = useCallback(
(query: string) => {
setSearchState(query)
setLimit(configuredLimit)
},
[configuredLimit],
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • FYI did you consider resetting the limit as an effect of a query change? Would be more straightforward for a reader, but maybe this causes an issue I'm missing right now.

import { connect, DataScope } from 'scrivito'
import { Loading } from '../../Components/Loading'

export const CombinedLoader = connect(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  •  FYI Naming. A "combined loader" rings no bell even after checking the implementation. Idea: DataBatchLoadingIndicator?

<CombinedLoader
dataScope={dataScope}
hasMore={hasMore}
key={combinedLoaderKey}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • FYI Both instances of the Loader use the DataBatchContext and useData(). How about inlining the props logic instead of duplicating it in the parents?

@@ -2,10 +2,12 @@ import { createContext, useCallback, useState } from 'react'
import { connect, useData, Obj, Widget, ContentTag } from 'scrivito'

export const DataBatchContext = createContext<{
combinedLoaderKey: string
Copy link
Contributor

@dcsaszar dcsaszar Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Naming: The name looks like it is coupled to a specific purpose which makes it harder to understand if read in isolation. How about batchKey?

dataScope: DataScope
hasMore: () => boolean
}) {
// use side-effects to trigger loading
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  •  FYI This is misleading. Typically the loading is triggered by the components that show the content. What we do here is something like "to subscribe" to what we assume the components load ,so we can conditionally show the spinner. Or am I reading this wrong?

@@ -43,15 +45,17 @@ export const DataBatchContextProvider = connect(
setLimit(configuredLimit)
}

const key = [
const baseKeys = [
'DataBatchContextProvider',
content.id(),
attribute,
id,
tag,
search,
// limit is intentionally not included in the key. Otherwise the component would show a loading spinner on every "load more" click.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • FYI maybe by using dataKey and batchKey we could make the code clear enough to go without the comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants