Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add update_selection/1 to dynamically change the selection#69

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
shamanime wants to merge1 commit intomaxmarcon:main
base:main
Choose a base branch
Loading
fromshamanime:shamanime-update_selection

Conversation

shamanime
Copy link
Contributor

Closes#68.

This allows updating the selection by passing a function with 1 arity to:update_selection.

It can be used to add/filter values from the existing selection without passing the whole selection as a value.

Copy link
ContributorAuthor

@shamanimeshamanime left a comment

Choose a reason for hiding this comment

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

Decided to see if I could get something out quickly before the week starts and looks like this is enough to replace what I was using in our app.

Let me know how to improve the API or tell me something obvious that I could have missed.

Thanks!

maxmarcon reacted with rocket emoji
end

defpupdate_selection(nil,_current_selection,_options,_mode,_value_mapper),do:[]
defpset_selection(nil,_current_selection,_options,_mode,_value_mapper),do:[]
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Renamed this function to not cause confusion with the new option, as this function will effectively update the selection as a whole.

Copy link
Owner

Choose a reason for hiding this comment

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

Let us not rename it (see comment above)

Enum.map(new,&normalize_selection_value(&1,options,value_mapper))
|>Enum.reject(&is_nil/1)

Enum.uniq(existing++new)
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

This forces uniqueness. Otherwise appending would allow duplicate values and I couldn't think of a reason to allow it.

Copy link
Owner

Choose a reason for hiding this comment

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

Let's remove this new function, see comment above

@maxmarcon
Copy link
Owner

Thanks@shamanime for your efforts! I will take a look at this soon

Copy link
Owner

@maxmarconmaxmarcon left a comment

Choose a reason for hiding this comment

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

Looks good! I added a couple of suggestions to simplify the implementation plus a comment on the docs

## Dynamically updating the selection
You can also update the selection dynamically by passing an 1 arity function that receives the current selection to `:update_selection`:
Copy link
Owner

@maxmarconmaxmarconJun 10, 2024
edited
Loading

Choose a reason for hiding this comment

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

I would say:

You can dynamically update the selection by using the:update_selection assign.:update_selection must be a 1-arity function that receives the current selection and returns the new one:

In this case, only the values with a label longer than 3 characters will be kept in the selection.
Another example that appends values to the current selection:
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think we need the second example

update(socket,:selection,fn
selection,
%{update_selection:update_fn,options:options,mode:mode,value_mapper:value_mapper}->
update_selection(update_fn,selection,options,mode,value_mapper)
Copy link
Owner

Choose a reason for hiding this comment

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

My suggestion is to replace this line with:

update_selection(update_fn.(selection), selection, options, mode, value_mapper)

Whereupdate_selection is the oldupdate_selection function, we don't rename it and we don't add the newupdate_selection function.

We won't have dupe detection but I think it's overkill. We don't have it anyway when you pass the selection explicitly with:value, why have it in the case of:update_selection? The caller can easily check for dupes and remove them if they want to

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I ran into a problem with this and still need to think of a way to figure it out. Theupdate_fn.(selection) may leave mixed entries (current + new) and it fails when trying to ran the existing ones with thevalue_mapper again. This is why the function I wrote splits it and only "normalize" the new entries.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Sorry that was not clear, my function fixes the problem by splitting it and selectively runningvalue_mapper only on new values. If we are to revert and use the ogupdate_selection we must address thevalue_mapper that may encounter already mapped values.

Copy link
Owner

@maxmarconmaxmarconJun 12, 2024
edited
Loading

Choose a reason for hiding this comment

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

Thanks for clarifying. Ok, but I don't think the problem is actually fixed by your function. Take this example:

current_selection=[%{value:1,label:"one"},%{value:2,label:"two"}]value_mapper=fnn->%{value:n,label:to_string(n)}endupdate_fn=fncurrent_selection->current_selection++[2,3]end

With this arguments, yourupdate_selection function
will fail to spot that 2 is already in the selection, because it's looking for unmapped values (2,3) in the list of mapped values (line 547).

I can't think of an elegant solution for this. Can you?

My impulse would be to say: it's the responsibility of the writer ofupdate_fn to leave the selection in a state that can be mapped using thevalue_mapper they provided.
Same for dupe detection.

Copy link
Owner

Choose a reason for hiding this comment

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

Ok I thought about it and I think another, perhaps better option would to not callvalue_mapper at all when updating the selection programmatically. This makes sense because the caller can map the selection themselves if they want to (i.e. they can provide the selection already in the right format).value_mapper was intended anyway for the case where the user has no way to map the selection (i.e. when it's coming from existing values in the form).

So something like:

update_selection(update_fn.(selection), selection, options, mode, & &1)

end

defpupdate_selection(nil,_current_selection,_options,_mode,_value_mapper),do:[]
defpset_selection(nil,_current_selection,_options,_mode,_value_mapper),do:[]
Copy link
Owner

Choose a reason for hiding this comment

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

Let us not rename it (see comment above)

Enum.map(new,&normalize_selection_value(&1,options,value_mapper))
|>Enum.reject(&is_nil/1)

Enum.uniq(existing++new)
Copy link
Owner

Choose a reason for hiding this comment

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

Let's remove this new function, see comment above

Copy link
Owner

Choose a reason for hiding this comment

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

Love the tests! ❤️

Copy link
Owner

Choose a reason for hiding this comment

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

Love the tests! ❤️

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@maxmarconmaxmarconmaxmarcon requested changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Addupdate_selection/1 to dynamically update (append, filter etc) the selection

2 participants

@shamanime@maxmarcon

[8]ページ先頭

©2009-2025 Movatter.jp