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: Filters are reset when the page is reloaded #1820

Merged
merged 10 commits into from
Sep 8, 2022
3 changes: 3 additions & 0 deletions legacy/app/auth/authentication-events.run.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function AuthenticationEvents($rootScope, $location, Authentication, Session, _,
}

function doLogin(redirect, noReload) {
localStorage.removeItem('ush-filterState-2');
TermsOfService.getTosEntry()
.then(function () {
loadSessionData();
Expand Down Expand Up @@ -121,6 +122,8 @@ function AuthenticationEvents($rootScope, $location, Authentication, Session, _,
function doLogout() {
$rootScope.currentUser = null;
$rootScope.loggedin = false;
localStorage.removeItem('ush-filterState');
localStorage.removeItem('ush-filterState-2');
// we don't want to reload until after filters are correctly set with
// the backend default that the user would get when logged out
PostFilters.resetDefaults().then(function () {
Expand Down
10 changes: 8 additions & 2 deletions legacy/app/common/services/post-filters.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = PostFiltersService;

PostFiltersService.$inject = ['_', 'FormEndpoint', 'TagEndpoint', '$q'];
function PostFiltersService(_, FormEndpoint, TagEndpoint, $q) {
PostFiltersService.$inject = ['_', 'FormEndpoint', 'TagEndpoint', '$q', '$rootScope'];
function PostFiltersService(_, FormEndpoint, TagEndpoint, $q, $rootScope) {
// Create initial filter state
var filterState = window.filterState = getDefaults();
var forms = [];
Expand Down Expand Up @@ -138,6 +138,12 @@ function PostFiltersService(_, FormEndpoint, TagEndpoint, $q) {

// Get filterState
function getFilters() {
if (localStorage.getItem('ush-filterState') !== null && $rootScope.currentUser) {
return JSON.parse(localStorage.getItem('ush-filterState'));
}
if (localStorage.getItem('ush-filterState-2') !== null && !$rootScope.currentUser) {
return JSON.parse(localStorage.getItem('ush-filterState-2'));
}
return filterState;
}

Expand Down
4 changes: 4 additions & 0 deletions legacy/app/data/post-view-data.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ function PostViewDataController(
}

function postDoesNotMatchFilters(postObj) {
$scope.filters = PostFilters.getFilters();
var deferred = $q.defer();

if ($scope.hasFilters()) {
Expand Down Expand Up @@ -282,6 +283,7 @@ function PostViewDataController(
}

function newStatusMatchesFilters(postObj) {
$scope.filters = PostFilters.getFilters();
let filters = $scope.hasFilters() ? $scope.filters.status : PostFilters.getDefaults().status;
let matchingStatus = false;

Expand Down Expand Up @@ -332,6 +334,7 @@ function PostViewDataController(
}

function getPosts(query, useOffset, clearPosts, callback) {
$scope.filters = PostFilters.getFilters();
query = query || PostFilters.getQueryParams($scope.filters);

var postQuery = _.extend({}, query, {
Expand Down Expand Up @@ -514,6 +517,7 @@ function PostViewDataController(
}

function getNewPosts() {
$scope.filters = PostFilters.getFilters();
let existingFilters = PostFilters.getQueryParams($scope.filters);
let filterDate = dayjs(existingFilters.date_before).utc();

Expand Down
18 changes: 18 additions & 0 deletions legacy/app/map/post-toolbar/filters/filters-dropdown.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ function FiltersDropdownController($scope, $state, PostFilters, ModalService, $r
return 'app.apply_filters';
};

$scope.removeFiltersFromLocalStorage = function () {
if ($rootScope.currentUser) {
localStorage.removeItem('ush-filterState');
}
if (!$rootScope.currentUser) {
localStorage.removeItem('ush-filterState-2');
}
}

$scope.setFiltersToLocalStorage = function () {
if ($rootScope.currentUser) {
localStorage.setItem('ush-filterState', JSON.stringify($scope.filters));
}
if (!$rootScope.currentUser) {
localStorage.setItem('ush-filterState-2', JSON.stringify($scope.filters));
}
}

$scope.displayStats = function () {
return $state.$current.includes['posts.map'];
};
Expand Down
4 changes: 2 additions & 2 deletions legacy/app/map/post-toolbar/filters/filters-dropdown.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h4 class="dropdown-menu-title" translate="app.filter_by"></h4>
<filter-location center-point-model="filters.center_point" within-km-model="filters.within_km"></filter-location>
<!-- end: filter options -->
<div class="form-field filter-actions">
<button type="button" class="button-beta" ng-click="clearFilters()" translate>global_filter.restore_defaults</button>
<button type="submit" class="button-alpha" translate>{{getButtonText()}}</button>
<button type="button" class="button-beta" ng-click="removeFiltersFromLocalStorage(); clearFilters()" translate>global_filter.restore_defaults</button>
<button type="submit" class="button-alpha" ng-click="setFiltersToLocalStorage()" translate>{{getButtonText()}}</button>
</div>
</div>