Skip to content

Commit

Permalink
Remove paramval function
Browse files Browse the repository at this point in the history
  • Loading branch information
jorg-vr committed Apr 4, 2024
1 parent cc610d6 commit 810fa47
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 76 deletions.
2 changes: 0 additions & 2 deletions app/assets/javascripts/components/search/dropdown_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { DodonaElement } from "components/meta/dodona_element";
* @prop {string} type - The type of the filter collection, used to determine the dropdown button text
* @prop {string} param - the searchQuery param to be used for this filter
* @prop {boolean} multi - whether one or more labels can be selected at the same time
* @prop {(l: Label) => string} paramVal - a function that extracts the value that should be used in a searchQuery for a selected label
* @prop {[Label]} labels - all labels that could potentially be selected
*/
@customElement("d-dropdown-filter")
Expand Down Expand Up @@ -97,7 +96,6 @@ export class DropdownFilters extends DodonaElement {
<d-dropdown-filter
.labels=${c.data}
.color=${c.color}
.paramVal=${c.paramVal}
.param=${c.param}
.multi=${c.multi}
.type=${type}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export type FilterCollection = {
data: Label[],
multi: boolean,
color: AccentColor,
paramVal: (l: Label) => string,
param: string
};

Expand All @@ -27,8 +26,6 @@ export class FilterCollectionElement extends DodonaElement {
param: string;
@property({ type: Boolean })
multi: boolean;
@property()
paramVal: (l: Label) => string;
@property({ type: Array })
labels: Array<Label> = [];

Expand All @@ -48,36 +45,32 @@ export class FilterCollectionElement extends DodonaElement {
super.update(changedProperties);
}

private str(label: Label): string {
return this.paramVal(label).toString();
}

private get multiSelected(): string[] {
return searchQueryState.arrayQueryParams.get(this.param) || [];
}

private multiUnSelect(label: Label): void {
searchQueryState.arrayQueryParams.set(this.param, this.multiSelected.filter(s => s !== this.str(label)));
searchQueryState.arrayQueryParams.set(this.param, this.multiSelected.filter(s => s !== label.id));
}

private multiIsSelected(label: Label): boolean {
return this.multiSelected.includes(this.str(label));
return this.multiSelected.includes(label.id);
}

private multiSelect(label: Label): void {
searchQueryState.arrayQueryParams.set(this.param, [...this.multiSelected, this.str(label)]);
searchQueryState.arrayQueryParams.set(this.param, [...this.multiSelected, label.id]);
}

private singleUnSelect(label: Label): void {
searchQueryState.queryParams.set(this.param, undefined);
}

private singleSelect(label: Label): void {
searchQueryState.queryParams.set(this.param, this.str(label));
searchQueryState.queryParams.set(this.param, label.id);
}

private singleIsSelected(label: Label): boolean {
return searchQueryState.queryParams.get(this.param) === this.str(label);
return searchQueryState.queryParams.get(this.param) === label.id;
}

isSelected = this.singleIsSelected;
Expand Down
2 changes: 0 additions & 2 deletions app/assets/javascripts/components/search/filter_tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export class FilterTabs extends watchMixin(FilterCollectionElement) {
multi = false;
@property()
param = "tab";
@property()
paramVal = (label: Label): string => label.id.toString();
@property({ type: Array })
labels: TabInfo[];

Expand Down
5 changes: 2 additions & 3 deletions app/assets/javascripts/components/search/search_field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { html, TemplateResult } from "lit";
import { createDelayer } from "utilities";
import { unsafeHTML } from "lit/directives/unsafe-html.js";
import { ref } from "lit/directives/ref.js";
import { FilterCollectionElement, Label } from "components/search/filter_collection_element";
import { FilterCollection, FilterCollectionElement, Label } from "components/search/filter_collection_element";
import { searchQueryState } from "state/SearchQuery";
import { search } from "search";
import { DodonaElement } from "components/meta/dodona_element";
Expand Down Expand Up @@ -83,7 +83,7 @@ export class SearchField extends DodonaElement {
@property({ type: Boolean })
eager: boolean;
@property( { type: Array })
filterCollections: Record<string, { data: Label[], multi: boolean, paramVal: (l: Label) => string, param: string }>;
filterCollections: Record<string, FilterCollection>;

@property({ state: true })
filter?: string = "";
Expand Down Expand Up @@ -171,7 +171,6 @@ export class SearchField extends DodonaElement {
.labels=${c.data}
.type=${type}
.filter=${this.filter}
.paramVal=${c.paramVal}
.param=${c.param}
.multi=${c.multi}
.index=${i}
Expand Down
1 change: 0 additions & 1 deletion app/assets/javascripts/components/search/search_token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export class SearchTokens extends DodonaElement {
<d-search-token
.labels=${c.data}
.color=${c.color}
.paramVal=${c.paramVal}
.param=${c.param}
.multi=${c.multi}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ import { watchMixin } from "components/meta/watch_mixin";
export class StandaloneDropdownFilter extends watchMixin(FilterCollectionElement) {
@property()
multi = false;
@property()
paramVal = (label: Label): string => label.id.toString();
@property({ type: String })
default;

watch = {
default: () => {
if (this.getSelectedLabels().length === 0) {
this.select(this.labels.find(label => this.paramVal(label) === this.default));
this.select(this.labels.find(label => label.id === this.default));
}
}
};
Expand Down
69 changes: 16 additions & 53 deletions app/views/layouts/_searchbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,159 +33,122 @@
<% if local_assigns.fetch :labels, false %>
filterCollections["labels"] = {
param: "labels",
paramVal: function(l) { return l.name.toString(); },
multi: true,
data: <%= raw render template: 'labels/index', formats: :json %>,
data: <%= raw @labels.map { |l| {id: l.name.to_s, name: l.name.to_s}}.to_json %>,
color: "purple"
};
<% end %>
<% if local_assigns.fetch :programming_languages, false %>
filterCollections["programming_languages"] = {
param: "programming_language",
paramVal: function(p) { return p.name.toString(); },
multi: false,
data: <%= raw render template: 'programming_languages/index', formats: :json %>,
data: <%= raw @programming_languages.map { |l| {id: l.name.to_s, name: l.name.to_s}}.to_json %>,
color: "red",
};
<% end %>
<% if local_assigns.fetch :repositories, false %>
filterCollections["repositories"] = {
param: "repository_id",
paramVal: function(r) { return r.id.toString(); },
multi: false,
data: <%= raw render template: 'repositories/index', formats: :json %>,
data: <%= raw @repositories.map { |l| {id: l.id.to_s, name: l.name.to_s}}.to_json %>,
color: "blue-gray",
};
<% end %>
<% if local_assigns.fetch(:institutions, []).length > 1 %>
filterCollections["institutions"] = {
param: "institution_id",
paramVal: function(i) { return i.id.toString(); },
multi: false,
data: <%= raw local_assigns.fetch(:institutions, []).map{|i| {id: i.id, name: i.name}}.to_json %>,
data: <%= raw local_assigns.fetch(:institutions, []).map{|i| {id: i.id.to_s, name: i.name.to_s}}.to_json %>,
color: "pink",
};
<% end %>
<% if local_assigns.fetch :course_labels, false %>
filterCollections["course_labels"] = {
param: "course_labels",
paramVal: function (cl) {
return cl.name.toString();
},
multi: true,
data: <%= raw local_assigns.fetch(:course_labels, []).map{|cl| {id: cl.id, name: cl.name}}.to_json %>,
data: <%= raw local_assigns.fetch(:course_labels, []).map{|cl| {id: cl.name.to_s, name: cl.name.to_s}}.to_json %>,
color: "orange",
};
<% end %>
<% if local_assigns.fetch :statuses, false %>
filterCollections["statuses"] = {
param: "status",
paramVal: function (s) {
return s.id.toString();
},
multi: false,
data: <%= raw local_assigns.fetch(:statuses, []).map{|s| {id: s, name: Submission.human_enum_name(:status, s)}}.to_json %>,
data: <%= raw local_assigns.fetch(:statuses, []).map{|s| {id: s.to_s, name: Submission.human_enum_name(:status, s)}}.to_json %>,
color: "indigo",
};
<% end %>
<% if local_assigns.fetch :question_states, false %>
filterCollections["question_states"] = {
param: "question_state",
paramVal: function (s) {
return s.id.toString();
},
multi: false,
data: <%= raw local_assigns.fetch(:question_states, []).map{|s| {id: s, name: Question.human_enum_name(:question_state, s)}}.to_json %>,
data: <%= raw local_assigns.fetch(:question_states, []).map{|s| {id: s.to_s, name: Question.human_enum_name(:question_state, s)}}.to_json %>,
color: "indigo",
};
<% end %>
<% if local_assigns.fetch :event_types, false %>
filterCollections["event_types"] = {
param: "type",
paramVal: function (s) {
return s.id.toString();
},
multi: false,
data: <%= raw local_assigns.fetch(:event_types, []).map{|s| {id: s, name: Event.human_enum_name(:event_type, s)}}.to_json %>,
data: <%= raw local_assigns.fetch(:event_types, []).map{|s| {id: s.to_s, name: Event.human_enum_name(:event_type, s)}}.to_json %>,
color: "deep-purple",
};
<% end %>
<% if local_assigns.fetch :activity_types, false %>
filterCollections["activity_types"] = {
param: "type",
paramVal: function (s) {
return s.id.toString();
},
multi: false,
data: <%= raw local_assigns.fetch(:activity_types, []).map{|s| {id: s.name, name: s.model_name.human}}.to_json %>,
data: <%= raw local_assigns.fetch(:activity_types, []).map{|s| {id: s.name.to_s, name: s.model_name.human}}.to_json %>,
color: "deep-purple",
};
<% end %>
<% if local_assigns.fetch :description_languages, false %>
filterCollections["description_languages"] = {
param: "description_languages",
paramVal: function (lang) {
return lang.id.toString();
},
multi: true,
data: <%= raw local_assigns.fetch(:description_languages, []).map{|l| {id: l, name: t("js.#{l}")}}.to_json %>,
data: <%= raw local_assigns.fetch(:description_languages, []).map{|l| {id: l.to_s, name: t("js.#{l}")}}.to_json %>,
color: "orange",
};
<% end %>
<% if local_assigns.fetch :judges, false %>
filterCollections["judges"] = {
param: "judge_id",
paramVal: function(judge) {
return judge.id.toString();
},
multi: false,
data: <%= raw local_assigns.fetch(:judges, []).map{|judge| {id: judge.id, name: "#{judge.name}-judge"}}.to_json %>,
data: <%= raw local_assigns.fetch(:judges, []).map{|judge| {id: judge.id.to_s, name: "#{judge.name}-judge"}}.to_json %>,
color: "red",
}
<% end %>
<% if local_assigns.fetch :courses, false %>
filterCollections["courses"] = {
param: "course_id",
paramVal: function(course) {
return course.id.toString();
},
multi: false,
data: <%= raw local_assigns.fetch(:courses, []).map{|course| {id: course.id, name: "#{course.name} (#{course.formatted_year})"}}.to_json %>,
data: <%= raw local_assigns.fetch(:courses, []).map{|course| {id: course.id.to_s, name: "#{course.name} (#{course.formatted_year})"}}.to_json %>,
color: "orange"
};
<% end %>
<% if local_assigns.fetch :exercises, false %>
filterCollections["exercises"] = {
param: "exercise_id",
paramVal: function(exercise) {
return exercise.id.toString();
},
multi: false,
data: <%= raw local_assigns.fetch(:exercises, []).map{|exercise| {id: exercise.id, name: exercise.name}}.to_json %>,
data: <%= raw local_assigns.fetch(:exercises, []).map{|exercise| {id: exercise.id.to_s, name: exercise.name}}.to_json %>,
color: "blue-gray"
};
<% end %>
<% if local_assigns.fetch :popularity, false %>
filterCollections["popularity"] = {
param: "popularity",
paramVal: function (s) {
return s.id.toString();
},
multi: true,
data: <%= raw local_assigns.fetch(:popularity, []).map{|s| {id: s, name: Activity.human_enum_name(:popularity, s)}}.to_json %>,
data: <%= raw local_assigns.fetch(:popularity, []).map{|s| {id: s.to_s, name: Activity.human_enum_name(:popularity, s)}}.to_json %>,
color: "pink",
};
<% end %>
<% if local_assigns.fetch :draft, false %>
filterCollections["draft"] = {
param: "draft",
paramVal: function (s) {
return s.id.toString();
},
multi: false,
data: [
{id: true, name: "<%= t("activities.index.filters.draft") %>"},
{id: false, name: "<%= t("activities.index.filters.not_draft") %>"}
{id: "true", name: "<%= t("activities.index.filters.draft") %>"},
{id: "false", name: "<%= t("activities.index.filters.not_draft") %>"}
],
color: "indigo",
};
Expand Down

0 comments on commit 810fa47

Please sign in to comment.