diff --git a/app/assets/javascripts/search.ts b/app/assets/javascripts/search.ts index 848e034d30..64bc83cb54 100644 --- a/app/assets/javascripts/search.ts +++ b/app/assets/javascripts/search.ts @@ -54,6 +54,7 @@ export class SearchQuery { appliedIndex = 0; arrayQueryParams: QueryParameters = new QueryParameters(); queryParams: QueryParameters = new QueryParameters(); + localStorageKey?: string; setRefreshElement(refreshElement: string): void { this.refreshElement = refreshElement; @@ -87,13 +88,13 @@ export class SearchQuery { } // initialise present parameters - for (const key of url.searchParams.keys()) { - if (key.endsWith("[]")) { - this.arrayQueryParams.updateParam(key.substring(0, key.length-2), url.searchParams.getAll(key)); - } else { - this.queryParams.updateParam(key, url.searchParams.get(key)); - } - } + this.initialiseParams(url.searchParams); + } + + setLocalStorageKey(localStorageKey: string): void { + this.localStorageKey = localStorageKey; + // apply parameters from local storage + this.useLocalStorage(); } initPagination(): void { @@ -194,8 +195,71 @@ export class SearchQuery { eval(data); } document.getElementById("progress-filter").style.visibility = "hidden"; + + // if there is local storage key => update the value to reuse later + if (this.localStorageKey) { + const urlObj = new URL(url); + localStorage.setItem(this.localStorageKey, urlObj.searchParams.toString()); + } }); } + + /** + * fetch params from localStorage using the localStorageKey if present and apply them to the current url + */ + useLocalStorage() : void { + if (this.localStorageKey) { + const searchParamsStringFromStorage = localStorage.getItem(this.localStorageKey); + if (searchParamsStringFromStorage) { + const searchParamsFromStorage = new URLSearchParams(searchParamsStringFromStorage); + // don't overwrite currently set params with params from the localStorage + searchParamsFromStorage.forEach((_value: string, key:string) => { + if (this.queryParams.params.get(key) !== undefined || + (this.isArrayQueryParamsKey(key) && + this.arrayQueryParams.params.get(this.extractArrayQueryParamsKey(key)) !== undefined)) { + searchParamsFromStorage.delete(key); + } + }); + + this.initialiseParams(searchParamsFromStorage); + } + } + } + + /** + * @param {URLSearchParams} searchParams the obj whose params we want to use + * + * apply the param values from the URLSearchParams obj to the current queryParams and arrayQueryParams + */ + initialiseParams(searchParams: URLSearchParams) : void { + for (const key of searchParams.keys()) { + if (this.isArrayQueryParamsKey(key)) { + this.arrayQueryParams.updateParam(this.extractArrayQueryParamsKey(key), searchParams.getAll(key)); + } else { + this.queryParams.updateParam(key, searchParams.get(key)); + } + } + } + + /** + * + * @param {string} key the key value stored in the url + * @private + * @return {boolean} true if the key ends with [] otherwise false + */ + private isArrayQueryParamsKey(key: string): boolean { + return key.endsWith("[]"); + } + + /** + * + * @param {string} key the key value stored in the url + * @private + * @return {string} the key without the [] at the end + */ + private extractArrayQueryParamsKey(key: string): string { + return key.substring(0, key.length-2); + } } dodona.searchQuery = dodona.searchQuery || new SearchQuery(); diff --git a/app/models/provider/g_suite.rb b/app/models/provider/g_suite.rb index a9c76d3164..ad035290a3 100644 --- a/app/models/provider/g_suite.rb +++ b/app/models/provider/g_suite.rb @@ -51,6 +51,6 @@ def readable_name # We want to display gmail for private accounts return 'Gmail' if institution.nil? - super.readable_name + super end end diff --git a/app/views/layouts/_searchbar.html.erb b/app/views/layouts/_searchbar.html.erb index 165c5f8091..5484ae173d 100644 --- a/app/views/layouts/_searchbar.html.erb +++ b/app/views/layouts/_searchbar.html.erb @@ -41,7 +41,7 @@ multi: false, data: <%= raw render template: 'programming_languages/index', formats: :json %>, color: function () { - return "teal"; + return "red"; }, }; <% end %> @@ -171,8 +171,13 @@ } }; <% end %> + dodona.searchQuery.setRefreshElement("<%= local_assigns.fetch :refresh_element, "" %>"); + + // load the given localStorageKey if present. This key is used to retrieve the values stored in localStorage + const localStorageKey = "<%= local_assigns.fetch :local_storage_key, "" %>" dodona.searchQuery.setBaseUrl("<%= local_assigns.fetch :baseUrl, "" %>"); + dodona.searchQuery.setLocalStorageKey(localStorageKey); const searchFields = Array.from(document.getElementsByTagName("d-search-field")); searchFields.forEach( searchField => { diff --git a/app/views/series/edit.html.erb b/app/views/series/edit.html.erb index 4926e82f5f..4759828449 100644 --- a/app/views/series/edit.html.erb +++ b/app/views/series/edit.html.erb @@ -84,7 +84,7 @@

<%= t ".add_activities" %>

- <%= render partial: 'layouts/searchbar', locals: {baseUrl: available_activities_series_path(@series), eager: true, labels: @labels, programming_languages: @programming_languages, repositories: @repositories, activity_types: [ContentPage, Exercise], description_languages: ["en", "nl"]} %> + <%= render partial: 'layouts/searchbar', locals: {baseUrl: available_activities_series_path(@series), eager: true, labels: @labels, programming_languages: @programming_languages, repositories: @repositories, activity_types: [ContentPage, Exercise], description_languages: ["en", "nl"], local_storage_key: 'seriesActivitySearch'} %>
diff --git a/config/initializers/00_version.rb b/config/initializers/00_version.rb index 39a4c4ad8e..31c5ebe2f7 100644 --- a/config/initializers/00_version.rb +++ b/config/initializers/00_version.rb @@ -3,7 +3,7 @@ class Application module Version MAJOR = 6 MINOR = 0 - PATCH = 2 + PATCH = 3 STRING = [MAJOR, MINOR, PATCH].compact.join('.') end diff --git a/test/models/provider_test.rb b/test/models/provider_test.rb index 17bcfa2b67..b9fa7fd791 100644 --- a/test/models/provider_test.rb +++ b/test/models/provider_test.rb @@ -69,6 +69,11 @@ class ProviderTest < ActiveSupport::TestCase assert_equal DEFAULT_NAMES, provider.extract_institution_name(OmniAuth::AuthHash.new({ info: {} })) end + test 'gsuite readable_name gives Google Workspace as name when account is NOT private' do + provider = build :gsuite_provider, institution: @institution + assert_equal Provider::GSuite.readable_name, provider.readable_name + end + test 'smartschool extracts name of institution' do provider = Provider::Smartschool # This hash is extracted from the one we receive when logging in.