-
Notifications
You must be signed in to change notification settings - Fork 71
feat: Add some modifications to add direct link to filters #616
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
base: master
Are you sure you want to change the base?
Conversation
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.
Hi @CJs0800 ! thanks for preparing this PR.
it is a good start 👍
I didn't test the feature yet: I made a first pass on your code, suggesting some simplifications and asking a few questions.
I'll do another pass soonish
src/app/learnocaml_index_main.ml
Outdated
let encode str = | ||
Re.Pcre.substitute ~rex:(Re.Pcre.regexp ",") ~subst:(fun _ -> "-c") ( | ||
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "&") ~subst:(fun _ -> "-a") ( | ||
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "=") ~subst:(fun _ -> "-e") ( | ||
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-") ~subst:(fun _ -> "--") str))) | ||
let decode str = | ||
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "--") ~subst:(fun _ -> "-") ( | ||
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-e") ~subst:(fun _ -> "=") ( | ||
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-a") ~subst:(fun _ -> "&") ( | ||
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-c") ~subst:(fun _ -> ",") str))) |
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.
This code is correct, but it's possible to make it shorter/simpler, by applying these ideas:
- locally open the Re.Pcre module :
let open Re.Pcre in
and replaceRe.Pcre.substitute
withsubstitute
,Re.Pcre.regexp
withregexp
; - reduce the need of parentheses by using the
@@
operator → FYI,f @@ g @@ h etc = f (g (h etc))
Hence:
let encode str = | |
Re.Pcre.substitute ~rex:(Re.Pcre.regexp ",") ~subst:(fun _ -> "-c") ( | |
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "&") ~subst:(fun _ -> "-a") ( | |
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "=") ~subst:(fun _ -> "-e") ( | |
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-") ~subst:(fun _ -> "--") str))) | |
let decode str = | |
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "--") ~subst:(fun _ -> "-") ( | |
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-e") ~subst:(fun _ -> "=") ( | |
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-a") ~subst:(fun _ -> "&") ( | |
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-c") ~subst:(fun _ -> ",") str))) | |
let encode str = | |
let open Re.Pcre in | |
substitute ~rex:(regexp ",") ~subst:(fun _ -> "-c") | |
@@ substitute ~rex:(regexp "&") ~subst:(fun _ -> "-a") | |
@@ substitute ~rex:(regexp "=") ~subst:(fun _ -> "-e") | |
@@ substitute ~rex:(regexp "-") ~subst:(fun _ -> "--") | |
@@ str |
And likewise for decode
!
src/app/learnocaml_index_main.ml
Outdated
| By_skill -> "skill" | ||
| By_difficulty -> "difficulty" | ||
in | ||
update_fragment "sort" sort_value;set_exercise_sort sort;true); |
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.
Nitpick: add spaces here
update_fragment "sort" sort_value;set_exercise_sort sort;true); | |
update_fragment "sort" sort_value; set_exercise_sort sort; true); |
src/app/learnocaml_index_main.ml
Outdated
let (exercise_sort_signal: exercise_ordering React.signal), set_exercise_sort = | ||
React.S.create By_category | ||
|
||
let (expand_state: string list React.signal), set_expand_state = |
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.
Just for uniformity, you could add the _signal
suffix here as well:
let (expand_state: string list React.signal), set_expand_state = | |
let (expand_state_signal: string list React.signal), set_expand_state = |
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-a") ~subst:(fun _ -> "&") ( | ||
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-c") ~subst:(fun _ -> ",") str))) | ||
|
||
let rec update_expand ?value fragment = |
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.
It appears in your PR, there is no additional (* comments *)
while IMO it'd be a good idea to:
-
Add a small global comment, for example before
encode
, that briefly explains the feature added in the PR, its design, and the need to add the encode/decode mutual bijections… -
Add individual comments of one line or so just before each new function, to explain their usefulness (except if their semantics is trivial, e.g., no need to document the
join
local function):- if the new function at stake is public (it has a
val …
in the.mli
): document the function in the .mli only is OK - if the new function at stake is private (only in the
.ml
): document the function in the .ml
- if the new function at stake is public (it has a
src/app/learnocaml_index_main.ml
Outdated
let update_fragment key value = | ||
let fragment = Js_utils.parse_fragment () in | ||
let filtered_fragment = | ||
if (key = "expand") then |
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.
Nitpick:
if (key = "expand") then | |
if key = "expand" then |
src/app/learnocaml_index_main.ml
Outdated
|
||
let update_sort value fragment = | ||
let filtered_fragment = List.remove_assoc "sort" (update_expand fragment) in | ||
filtered_fragment@[("sort",value)] |
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.
Nitpick:
filtered_fragment@[("sort",value)] | |
filtered_fragment @ [("sort", value)] |
src/app/learnocaml_index_main.ml
Outdated
] ] | ||
in | ||
let update_expand_class id ids = | ||
if (List.mem id ids) then |
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.
Nitpick:
if (List.mem id ids) then | |
if List.mem id ids then |
src/app/learnocaml_index_main.ml
Outdated
| [] -> [] | ||
| [_] -> | ||
let expand_ids = React.S.value expand_state in | ||
if (expand_ids = []) then |
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.
Nitpick:
if (expand_ids = []) then | |
if expand_ids = [] then |
src/app/learnocaml_index_main.ml
Outdated
update_expand_class id expand_ids | ||
| _ -> | ||
let expand_ids = React.S.value expand_state in | ||
if (expand_ids = []) then |
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.
Nitpick:
if (expand_ids = []) then | |
if expand_ids = [] then |
ignore (Manip.toggleClass title "collapsed"); | ||
false); |
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.
This two lines were untouched by your PR except the indentation (spaces),
it might be better to restore the previous indentation so that the diff is not ambiguous (i.e., suggesting that the lines changed, while they didn't)
Add current files of my error situation
feature
Description
This PR implements URL synchronization for the exercise index page:
This enables sharing direct links to specific filtered or expanded states.
Known issue:
Group expansions from URL fragments (#expand=...) are not applied correctly.