Skip to content

Commit

Permalink
Merge pull request #3310 from AtlasOfLivingAustralia/feature/issue3291
Browse files Browse the repository at this point in the history
#3291 start and end date behaviour modification
  • Loading branch information
chrisala authored Aug 28, 2024
2 parents 489e590 + 71283d9 commit 436ae2c
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 44 deletions.
24 changes: 16 additions & 8 deletions grails-app/assets/javascripts/projectExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//= require reef2050Report.js
//= require components.js

var DatePickerModel = function(fromDate, toDate, urlWithoutDates, $location) {
var DatePickerModel = function(fromDate, toDate, isFilterByCompletedProjects, urlWithoutDates, $location) {
var formatString = 'YYYY-MM-DD';
var self = this;
var date = moment('2011-07-01T00:00:00+10:00');
Expand All @@ -18,6 +18,7 @@ var DatePickerModel = function(fromDate, toDate, urlWithoutDates, $location) {
date = rangeEnd;
}
self.selectedRange = ko.observable();
self.isFilterByCompletedProjects = ko.observable(isFilterByCompletedProjects || false);
self.fromDate = ko.observable().extend({simpleDate:false});
if (fromDate) {
self.fromDate(moment(fromDate).format());
Expand All @@ -34,23 +35,23 @@ var DatePickerModel = function(fromDate, toDate, urlWithoutDates, $location) {
$location.href = urlWithoutDates;
};

var validateAndReload = function(newFromDate, newToDate) {
var validateAndReloadAndOption = function(newFromDate, newToDate, newIsFilterByCompletedProjects) {

var formattedFromDate = moment(fromDate).format(fromDate);
var formattedToDate = moment(toDate).format(toDate);
var formattedNewFromDate = moment(newFromDate).format(formatString);
var formattedNewToDate = moment(newToDate).format(formatString);

if (formattedNewFromDate == formattedFromDate && formattedNewToDate == formattedToDate) {
if (formattedNewFromDate == formattedFromDate && formattedNewToDate == formattedToDate && isFilterByCompletedProjects == newIsFilterByCompletedProjects) {
return;
}

if ($('#facet-dates').validationEngine('validate')) {
reloadWithDates(newFromDate, newToDate);
reloadWithDatesAndOption(newFromDate, newToDate, newIsFilterByCompletedProjects);
}
}

var reloadWithDates = function(newFromDate, newToDate) {
var reloadWithDatesAndOption = function(newFromDate, newToDate, isFilterByCompletedProjects) {
var parsedNewFromDate = moment(newFromDate);
var parsedNewToDate = moment(newToDate);
if (newFromDate && parsedNewFromDate.isValid()) {
Expand All @@ -61,23 +62,30 @@ var DatePickerModel = function(fromDate, toDate, urlWithoutDates, $location) {
urlWithoutDates += urlWithoutDates?'&':'?';
urlWithoutDates += 'toDate='+moment(newToDate).format(formatString);
}
if (isFilterByCompletedProjects != undefined) {
urlWithoutDates += urlWithoutDates?'&':'?';
urlWithoutDates += 'isFilterByCompletedProjects='+isFilterByCompletedProjects;
}
$location.href = urlWithoutDates;
}

self.fromDate.subscribe(function(a, b) {
validateAndReload(self.fromDate(), self.toDate());
validateAndReloadAndOption(self.fromDate(), self.toDate(), self.isFilterByCompletedProjects());
});
self.toDate.subscribe(function(toDate) {
validateAndReload(self.fromDate(), self.toDate());
validateAndReloadAndOption(self.fromDate(), self.toDate(), self.isFilterByCompletedProjects());
});

self.selectedRange.subscribe(function(value) {

if (value.from) {
reloadWithDates(value.from, value.to);
reloadWithDatesAndOption(value.from, value.to, self.isFilterByCompletedProjects());
}

});
self.isFilterByCompletedProjects.subscribe(function() {
validateAndReloadAndOption(self.fromDate(), self.toDate(), self.isFilterByCompletedProjects());
});
};

var DownloadViewModel = function(options) {
Expand Down
34 changes: 23 additions & 11 deletions grails-app/services/au/org/ala/merit/SearchService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import grails.core.GrailsApplication
import groovy.json.JsonSlurper
import groovy.util.logging.Slf4j
import org.apache.commons.lang.StringUtils
import org.apache.http.HttpStatus
import org.springframework.beans.factory.annotation.Autowired

import javax.annotation.PostConstruct
import java.math.RoundingMode

/**
* Service for ElasticSearch running on ecodata
*/
Expand Down Expand Up @@ -159,16 +157,30 @@ class SearchService {
}

private void handleDateFilters(params) {
if (params.fromDate || params.toDate) {
List fq = params.getList('fq')
if (!params.fromDate) {
fq += "_query:(plannedStartDate:[* TO ${params.toDate}])"
} else if (!params.toDate) {
fq += "_query:(plannedEndDate:[${params.fromDate} TO *])"
} else {
fq += "_query:(plannedEndDate:[${params.fromDate} TO *] AND plannedStartDate:[* TO ${params.toDate}])"
if ((params.isFilterByCompletedProjects ?: 'false').toBoolean()) {
if (params.fromDate || params.toDate) {
List fq = params.getList('fq')
if (!params.fromDate) {
fq += "_query:(plannedStartDate:[* TO ${params.toDate}) AND plannedEndDate:[* TO ${params.toDate}))"
} else if (!params.toDate) {
fq += "_query:(plannedStartDate:[${params.fromDate} TO *] AND plannedEndDate:[${params.fromDate} TO *])"
} else {
fq += "_query:(plannedStartDate:[${params.fromDate} TO ${params.toDate}] AND plannedEndDate:[${params.fromDate} TO ${params.toDate}])"
}
params.fq = fq
}
} else {
if (params.fromDate || params.toDate) {
List fq = params.getList('fq')
if (!params.fromDate) {
fq += "_query:(plannedStartDate:[* TO ${params.toDate}])"
} else if (!params.toDate) {
fq += "_query:(plannedEndDate:[${params.fromDate} TO *])"
} else {
fq += "_query:(plannedEndDate:[${params.fromDate} TO *] AND plannedStartDate:[* TO ${params.toDate}])"
}
params.fq = fq
}
params.fq = fq
}
}

Expand Down
12 changes: 11 additions & 1 deletion grails-app/views/home/_projectFinder.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
<select style="margin-bottom: 10px" data-bind="options:ranges, optionsText:'display', value:selectedRange"></select>
<div class="input-group" style="margin-bottom: 10px"><label for="fromDate" class="dataClass">From:</label><fc:datePicker targetField="fromDate.date" bs4="bs4" class="dateControl form-control form-control-sm" name="fromDate" data-validation-engine="validate[date]" autocomplete="off"/></div>
<div class="input-group" style="margin-bottom: 10px"><label for="fromDate" class="dataClass">To:</label><fc:datePicker targetField="toDate.date" bs4="bs4" class="dateControl form-control form-control-sm" name="toDate" data-validation-engine="validate[date,future[fromDate]]" autocomplete="off"/></div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="isFilterByCompletedProjectsOption" data-bind="checked: isFilterByCompletedProjects" value="true">
<label class="form-check-label" for="isFilterByCompletedProjectsOption">
Project start and end dates are within the selected range.
</label>
</div>
<div><button data-bind="click:clearDates, enable:fromDate() || toDate()" class="btn btn-sm clearDates"><i class="fa fa-remove"></i> Clear dates</button></div>

</div>
Expand Down Expand Up @@ -472,6 +478,9 @@
<g:if test="${params.toDate}">
params += '&toDate='+'${params.toDate.encodeAsURL()}';
</g:if>
<g:if test="${params.isFilterByCompletedProjects}">
params += '&isFilterByCompletedProjects=' + ${params.isFilterByCompletedProjects.encodeAsJavaScript()};
</g:if>

$.post(url, params).done(function(data1) {
//console.log("getJSON data", data);
Expand Down Expand Up @@ -545,11 +554,12 @@
var urlWithoutDates = '<fc:formatParams params="${params}" requiredParams="query,sort,order,max,fq"/>';
var fromDate = '${params.fromDate?.encodeAsJavaScript()?:''}';
var toDate = '${params.toDate?.encodeAsJavaScript()?:''}';
var isFilterByCompletedProjects = ${params.isFilterByCompletedProjects?.encodeAsJavaScript() ?: false };

var error = "${error?.encodeAsJavaScript()}";

if(!error){
ko.applyBindings(new DatePickerModel(fromDate, toDate, urlWithoutDates, window.location), document.getElementById('facet-dates'));
ko.applyBindings(new DatePickerModel(fromDate, toDate, isFilterByCompletedProjects, urlWithoutDates, window.location), document.getElementById('facet-dates'));
}

function FacetFilterViewModel (params) {
Expand Down
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"devDependencies": {
"@metahub/karma-jasmine-jquery": "^2.0.1",
"chromedriver": "^126.0.4",
"chromedriver": "^128.0.0",
"jasmine-core": "^3.5.0",
"jasmine-jquery": "^2.0.0",
"jquery": "3.6.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package au.org.ala.fieldcapture

import pages.AdminTools
import groovy.util.logging.Slf4j
import pages.Facet
import pages.ProjectExplorer
import spock.lang.Stepwise
import pages.AdminTools

@Slf4j
@Stepwise
class ProjectExplorerSpec extends StubbedCasSpec {

Expand Down Expand Up @@ -60,6 +62,68 @@ class ProjectExplorerSpec extends StubbedCasSpec {

new HashSet(projects.collect{it.name}) == expectedProjects

when: "We filter project by dates"
dateFacet.click()

then:
waitFor 10, {
clearDatesBtn.displayed
}

when:
at ProjectExplorer // reset timer
setFromDate("01/06/2015")

then:
waitFor {hasBeenReloaded()}
waitFor 10, {
clearDatesBtn.displayed
}
waitFor 20, {
projects.size() == 15
}

when: "We filter project by dates"
at ProjectExplorer // reset timer
setToDate("02/07/2018")

then:
waitFor {hasBeenReloaded()}
waitFor 10, {
clearDatesBtn.displayed
}
waitFor 20, {
projects.size() == 14
}

when: "We filter project by dates"
at ProjectExplorer
dateOption.click()

then:
waitFor {hasBeenReloaded()}
waitFor 20, {
clearDatesBtn.displayed
}
waitFor 20, {
projects.size() == 1
}

when: "We clear dates"
at ProjectExplorer
clearDatesBtn.click()

then:
waitFor {hasBeenReloaded()}
waitFor 10, {
clearDatesBtn.displayed
}
waitFor 20, {
projects.size() == 15
facets.size() == 12
chooseMoreFacetTerms.size() == 0
}

when:
Facet projectStatus = findFacetByName("Project Status")
projectStatus.expand()
Expand Down
11 changes: 10 additions & 1 deletion src/integration-test/groovy/pages/ProjectExplorer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pages
import geb.Module
import geb.module.Checkbox
import pages.modules.ViewReef2050PlanReport
import org.openqa.selenium.Keys

class FacetItem extends Module {
static content = {
Expand Down Expand Up @@ -59,7 +60,9 @@ class ProjectExplorer extends ReloadablePage {
chooseMoreFacetTerms(required: false) { $('#facetsContent .moreFacets') }
facetTerms(required: false) { $("#facetsContent .accordion .card-header a") }
facetAccordion(required: false) { $("#facetsContent .accordion") }

dateOption(required: false, wait: true) { $("#isFilterByCompletedProjectsOption", dynamic: true) }
dateFacet(required: false) { $("#projectDates", dynamic: true) }
clearDatesBtn(required: false) { $("#facet-dates .clearDates", dynamic: true) }
inputText{ $("#keywords")}

dashboardContent (required: false) {$("div#dashboard-content")}
Expand Down Expand Up @@ -110,7 +113,13 @@ class ProjectExplorer extends ReloadablePage {
waitFor { dashboardContent.displayed }
}

void setFromDate (String date) {
$("input#fromDate", dynamic: true) << date << Keys.TAB
}

void setToDate (String date) {
$("input#toDate", dynamic: true) << date << Keys.TAB
}
}

class ProjectsList extends Module {
Expand Down
4 changes: 4 additions & 0 deletions src/integration-test/groovy/pages/ReloadablePage.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pages

import geb.Page
import groovy.util.logging.Slf4j

@Slf4j
class ReloadablePage extends Page {

private long atCheckTime = 0l
Expand Down Expand Up @@ -48,5 +50,7 @@ class ReloadablePage extends Page {
if (result) {
saveAtCheckTime()
}

result
}
}
Loading

0 comments on commit 436ae2c

Please sign in to comment.