-
Notifications
You must be signed in to change notification settings - Fork 19
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { createContext, useState } from 'react' | ||
import { createContext, useCallback, useState } from 'react' | ||
import { connect, useData, Obj, Widget, ContentTag } from 'scrivito' | ||
|
||
export const DataBatchContext = createContext<{ | ||
combinedLoaderKey: string | ||
hasMore: () => boolean | ||
loadMore: () => void | ||
setSearch?: (query: string) => void | ||
}>({ | ||
combinedLoaderKey: '', | ||
hasMore: () => true, | ||
loadMore: () => { | ||
throw new Error('loadMore is not provided!') | ||
|
@@ -28,22 +30,32 @@ 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('') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
const setSearch = useCallback( | ||
(query: string) => { | ||
setSearchState(query) | ||
setLimit(configuredLimit) | ||
}, | ||
[configuredLimit], | ||
) | ||
Comment on lines
+35
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
if (initialLimit !== configuredLimit) { | ||
setInitialLimit(configuredLimit) | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
].join('-') | ||
] | ||
|
||
const combinedLoaderKey = [...baseKeys, limit].join('-') | ||
|
||
const transform = { limit, search } | ||
|
||
|
@@ -61,11 +73,13 @@ export const DataBatchContextProvider = connect( | |
const loadMore = () => setLimit((prevLimit) => prevLimit + configuredLimit) | ||
|
||
return ( | ||
<DataBatchContext.Provider value={{ hasMore, loadMore, setSearch }}> | ||
<DataBatchContext.Provider | ||
value={{ combinedLoaderKey, hasMore, loadMore, setSearch }} | ||
> | ||
<ContentTag | ||
tag={tag} | ||
id={id} | ||
key={key} | ||
key={baseKeys.join('-')} | ||
content={content} | ||
attribute={attribute} | ||
dataContext={dataScope.transform(transform)} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,15 @@ import { DataColumnListWidget } from './DataColumnListWidgetClass' | |
import { EditorNote } from '../../Components/EditorNote' | ||
import { Loading } from '../../Components/Loading' | ||
import { DataErrorEditorNote } from '../../Components/DataErrorEditorNote' | ||
import { useContext } from 'react' | ||
import { DataBatchContext } from '../../Components/DataBatchContext' | ||
import { CombinedLoader } from '../DataWidget/CombinedLoader' | ||
|
||
provideComponent( | ||
DataColumnListWidget, | ||
({ widget }) => { | ||
const dataScope = useData() | ||
const { combinedLoaderKey, hasMore } = useContext(DataBatchContext) | ||
let dataError: unknown | ||
|
||
try { | ||
|
@@ -31,17 +35,24 @@ provideComponent( | |
const columnsCount = widget.get('columnsCount') || '2' | ||
|
||
return ( | ||
<div className={`row row-cols-1 row-cols-md-${columnsCount}`}> | ||
{dataItems.map((dataItem) => ( | ||
<ContentTag | ||
content={widget} | ||
attribute="content" | ||
className="col" | ||
dataContext={dataItem} | ||
key={dataItem.id()} | ||
/> | ||
))} | ||
</div> | ||
<> | ||
<div className={`row row-cols-1 row-cols-md-${columnsCount}`}> | ||
{dataItems.map((dataItem) => ( | ||
<ContentTag | ||
content={widget} | ||
attribute="content" | ||
className="col" | ||
dataContext={dataItem} | ||
key={dataItem.id()} | ||
/> | ||
))} | ||
</div> | ||
<CombinedLoader | ||
dataScope={dataScope} | ||
hasMore={hasMore} | ||
key={combinedLoaderKey} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
/> | ||
</> | ||
) | ||
}, | ||
{ loading: Loading }, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { connect, DataScope } from 'scrivito' | ||
import { Loading } from '../../Components/Loading' | ||
|
||
export const CombinedLoader = connect( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
function CombinedLoader({ | ||
dataScope, | ||
hasMore, | ||
}: { | ||
dataScope: DataScope | ||
hasMore: () => boolean | ||
}) { | ||
// use side-effects to trigger loading | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
dataScope.take() | ||
hasMore() | ||
|
||
return null | ||
}, | ||
{ loading: Loading }, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
batchKey
?