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

Fix issues with shape drawing on initialize #860

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export const useDrawingAndDisplayModels = ({
const { listenTo, stopListening } = useBackbone()
useListenTo(
(wreqr as any).vent,
'search:linedisplay search:polydisplay search:bboxdisplay search:circledisplay search:keyworddisplay',
'search:linedisplay search:polydisplay search:bboxdisplay search:circledisplay search:keyworddisplay search:areadisplay',
(model: any) => {
setModels((currentModels) => {
let newModels = currentModels
Expand Down Expand Up @@ -228,7 +228,7 @@ export const useDrawingAndDisplayModels = ({
React.useEffect(() => {
;(wreqr as any).vent.trigger('search:requestlocationmodels')
}, [])
const updateFilterModels = () => {
const updateFilterModels = React.useCallback(() => {
for (const model of filterModels) {
stopListening(model)
}
Expand Down Expand Up @@ -270,10 +270,10 @@ export const useDrawingAndDisplayModels = ({
(m) => !locationIds.has(m.get('locationId'))
)
setFilterModels(dedupedModels)
}
}, [filterTree, models])
React.useEffect(() => {
updateFilterModels()
}, [filterTree, models])
}, [updateFilterModels])
useListenTo(selectionInterface, 'change:currentQuery', updateFilterModels)
useListenTo(
TypedUserInstance.getPreferences(),
Expand Down Expand Up @@ -315,14 +315,6 @@ export const useDrawingAndDisplayModels = ({
setDrawingModels([])
}
}, [isDrawing])
React.useEffect(() => {
const timeoutId = window.setTimeout(() => {
updateFilterModels()
}, 1000)
return () => {
window.clearTimeout(timeoutId)
}
}, [])
const callback = React.useMemo(() => {
return () => {
if (map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ export default Backbone.AssociatedModel.extend({
isTransient: true,
},
],
// override constructor slightly to ensure options are available on the self ref immediately
constructor(_attributes: any, options: any) {
// override constructor slightly to ensure options / attributes are available on the self ref immediately
constructor(attributes: any, options: any) {
if (
!options ||
!options.transformDefaults ||
Expand All @@ -143,6 +143,7 @@ export default Backbone.AssociatedModel.extend({
'Options for transformDefaults, transformFilterTree, transformSorts, and transformCount must be provided'
)
}
this._constructorAttributes = attributes || {}
this.options = options
return Backbone.AssociatedModel.apply(this, arguments)
},
Expand All @@ -168,13 +169,27 @@ export default Backbone.AssociatedModel.extend({
}
return json
},
/**
* Do not put filterTree in this, otherwise things that only set cql and are meant to rebuild the filter tree won't work (see initialize)
*/
defaults() {
const filterTree = this._constructorAttributes?.filterTree
let constructedFilterTree: FilterBuilderClass
let constructedCql = this._constructorAttributes?.cql || "anyText ILIKE '*'"
if (filterTree && typeof filterTree === 'string') {
constructedFilterTree = new FilterBuilderClass(JSON.parse(filterTree))
} else if (!filterTree || filterTree.id === undefined) {
// when we make drastic changes to filter tree it will be necessary to fall back to cql and reconstruct a filter tree that's compatible
constructedFilterTree = cql.read(constructedCql)
console.warn('migrating a filter tree to the latest structure')
// allow downstream projects to handle how they want to inform users of migrations
;(wreqr as any).vent.trigger('filterTree:migration', {
search: this,
})
} else {
constructedFilterTree = new FilterBuilderClass(filterTree)
}
return this.options.transformDefaults({
originalDefaults: {
cql: "anyText ILIKE '*'",
cql: constructedCql,
filterTree: constructedFilterTree,
associatedFormModel: undefined,
excludeUnnecessaryAttributes: true,
count: StartupDataStore.Configuration.getResultCount(),
Expand All @@ -189,7 +204,7 @@ export default Backbone.AssociatedModel.extend({
// initialize this here so we can avoid creating spurious references to LazyQueryResults objects
result: new QueryResponse({
lazyResults: new LazyQueryResults({
filterTree: this.get('filterTree'),
filterTree: constructedFilterTree,
sorts: [],
sources: [],
transformSorts: ({ originalSorts }) => {
Expand Down Expand Up @@ -252,21 +267,6 @@ export default Backbone.AssociatedModel.extend({
initialize(attributes: any) {
// @ts-expect-error ts-migrate(2769) FIXME: No overload matches this call.
_.bindAll.apply(_, [this].concat(_.functions(this))) // underscore bindAll does not take array arg
const filterTree = this.get('filterTree')
if (filterTree && typeof filterTree === 'string') {
this.set('filterTree', new FilterBuilderClass(JSON.parse(filterTree)))
}
// when we make drastic changes to filter tree it will be necessary to fall back to cql and reconstruct a filter tree that's compatible
if (!filterTree || filterTree.id === undefined) {
this.set('filterTree', cql.read(this.get('cql'))) // reconstruct
console.warn('migrating a filter tree to the latest structure')
// allow downstream projects to handle how they want to inform users of migrations
;(wreqr as any).vent.trigger('filterTree:migration', {
search: this,
})
} else {
this.set('filterTree', new FilterBuilderClass(filterTree)) // instantiate the class if everything is a-okay
}
this._handleDeprecatedFederation(attributes)
this.listenTo(
this,
Expand Down
Loading