From 4f6decc71937d0078eae7bc38d36aaf4ed3b2b99 Mon Sep 17 00:00:00 2001 From: Marcus Aspin Date: Thu, 21 Sep 2023 14:19:50 +0100 Subject: [PATCH] PI-1449 Improve searching on input + added examples page (#30) --- assets/scss/local.scss | 7 +++ assets/scss/new-tech.scss | 11 ++-- packages/probation-search-frontend/README.md | 8 ++- .../components/probationSearch/results.njk | 18 ++++++ .../components/probationSearch/template.njk | 63 +++++++++---------- .../routes/search.ts | 6 +- server/routes/index.ts | 11 +++- server/views/pages/examples.njk | 26 ++++++++ server/views/pages/index.njk | 2 +- server/views/pages/newTech/index.njk | 36 +++++------ 10 files changed, 123 insertions(+), 65 deletions(-) create mode 100644 packages/probation-search-frontend/components/probationSearch/results.njk create mode 100644 server/views/pages/examples.njk diff --git a/assets/scss/local.scss b/assets/scss/local.scss index 35cba2b3..99664b8f 100644 --- a/assets/scss/local.scss +++ b/assets/scss/local.scss @@ -1,3 +1,10 @@ .govuk-main-wrapper { min-height: 600px; } + +.example-output { + background-color: govuk-colour('light-grey'); + border: 1px solid $govuk-border-colour; + margin-bottom: govuk-spacing(4); + padding: govuk-spacing(4); +} \ No newline at end of file diff --git a/assets/scss/new-tech.scss b/assets/scss/new-tech.scss index 6e1a8d86..285f9067 100644 --- a/assets/scss/new-tech.scss +++ b/assets/scss/new-tech.scss @@ -7,7 +7,7 @@ $govuk-page-width: $moj-page-width; @import 'govuk/all'; @import 'moj/all'; -#search-form { +.probation-search__form { background-color: $govuk-brand-colour; padding-top: govuk-spacing(5); .govuk-form-group { @@ -15,7 +15,7 @@ $govuk-page-width: $moj-page-width; } } -#search-input-container { +.probation-search__input-container { @include govuk-width-container; @include govuk-clearfix; margin-bottom: govuk-spacing(4); @@ -24,18 +24,17 @@ $govuk-page-width: $moj-page-width; } } -#search-result-summary, #search-pagination, #search-results { +.probation-search__results-container { @include govuk-width-container; } -#search-results { - @include govuk-width-container; +.probation-search__has-results { & ~ .govuk-width-container > .govuk-main-wrapper { display: none; // hide welcome content when results are rendered } } -#search-suggestions { +.probation-search__suggestions { color: govuk-colour("white"); float: left; margin-top: govuk-spacing(2) diff --git a/packages/probation-search-frontend/README.md b/packages/probation-search-frontend/README.md index c76ed5ae..5ae73871 100644 --- a/packages/probation-search-frontend/README.md +++ b/packages/probation-search-frontend/README.md @@ -4,7 +4,7 @@ A Nunjucks component to search for probation cases. Easily build probation case search functionality into your HMPPS service, while delivering a consistent search experience to probation practitioners. -Try it out in the dev environment: https://probation-search-dev.hmpps.service.justice.gov.uk +Try it out in the dev environment: https://probation-search-dev.hmpps.service.justice.gov.uk/examples ## Get started @@ -21,8 +21,11 @@ Register the macro by adding the `'node_modules/@ministryofjustice/probation-sea ```nunjuck {% from "probationSearch/macro.njk" import probationSearch %} -{{ probationSearch({ id: "search", name: "search", results: probationSearchResults }) }} +{{ probationSearch({ id: "search", results: probationSearchResults }) }} ``` +Example: +* [nunjucksSetup.ts](https://github.com/ministryofjustice/hmpps-sentence-plan-ui/blob/4cf961428e4c4e69565367bf7af17bac8c8da674/server/utils/nunjucksSetup.ts#L34) +* [search.njk](https://github.com/ministryofjustice/hmpps-sentence-plan-ui/blob/4cf961428e4c4e69565367bf7af17bac8c8da674/server/views/pages/search.njk) ### 3. Configure the Express routes @@ -39,6 +42,7 @@ probationSearchRoutes({ oauthClient: service.hmppsAuthClient, // a reference to your HMPPS Auth client }) ``` +Example: [routes/index.ts](https://github.com/ministryofjustice/hmpps-sentence-plan-ui/blob/4cf961428e4c4e69565367bf7af17bac8c8da674/server/routes/index.ts#L16) That's it! Start your service and visit http://localhost:3000/search to try it out. diff --git a/packages/probation-search-frontend/components/probationSearch/results.njk b/packages/probation-search-frontend/components/probationSearch/results.njk new file mode 100644 index 00000000..dc3ad5cb --- /dev/null +++ b/packages/probation-search-frontend/components/probationSearch/results.njk @@ -0,0 +1,18 @@ +{% from "govuk/components/pagination/macro.njk" import govukPagination %} +{% from "govuk/components/table/macro.njk" import govukTable %} + +{% if params.results.response.totalElements > 0 %} +

Showing {{ params.results.page.from }} to {{ params.results.page.to }} of {{ params.results.page.totalResults }} results.

+{% endif %} + +{% if params.results.results is string %} + {{ params.results.results | safe }} +{% elif params.results.response.totalElements > 0 %} + {{ govukTable({ firstCellIsHeader: true, head: params.results.results.head, rows: params.results.results.rows }) }} +{% elif params.results.query != null %} +

There are no results for your search. Try refining your query above.

+{% endif %} + +{% if params.results.page.items | length > 1 %} + {{ govukPagination({ previous: { href: params.results.page.prev }, next: { href: params.results.page.next }, items: params.results.page.items }) }} +{% endif %} \ No newline at end of file diff --git a/packages/probation-search-frontend/components/probationSearch/template.njk b/packages/probation-search-frontend/components/probationSearch/template.njk index 5e175ba4..851b2a36 100644 --- a/packages/probation-search-frontend/components/probationSearch/template.njk +++ b/packages/probation-search-frontend/components/probationSearch/template.njk @@ -1,12 +1,10 @@ {% from "govuk/components/input/macro.njk" import govukInput %} {% from "govuk/components/button/macro.njk" import govukButton %} -{% from "govuk/components/pagination/macro.njk" import govukPagination %} -{% from "govuk/components/table/macro.njk" import govukTable %} -
+ -
+
{{ govukInput({ attributes: params.attributes, autocomplete: params.autocomplete, @@ -21,21 +19,21 @@ classes: "govuk-label--l", isPageHeading: true }, - name: params.name, + name: "probation-search-input", type: params.type if params.type else "search", value: params.results.query }) }} - {% if params.results.suggestions | length %} -

+

+ {% if params.results.suggestions | length %} Did you mean {%- set comma = joiner() %} {%- for suggestion in params.results.suggestions %}{{ comma() }} {{ suggestion }} {%- endfor %}? -

- {% endif %} + {% endif %} +

{% if params.postHint %} {{ params.postHint.html | safe if params.postHint.html else '

' + params.postHint.text + '

' }} @@ -44,10 +42,24 @@ {% if params.searchOnInput %} {% else %} @@ -56,24 +68,7 @@
-{% if params.results.response.totalElements > 0 %} -
-

Showing {{ params.results.page.from }} to {{ params.results.page.to }} of {{ params.results.page.totalResults }} results.

-
-{% endif %} - -{% if params.results.results is string %} - {{ params.results.results | safe }} -{% elif params.results.response.totalElements > 0 %} -
- {{ govukTable({ firstCellIsHeader: true, head: params.results.results.head, rows: params.results.results.rows }) }} -
-{% elif params.results.query != null %} -

There are no results for your search. Try refining your query above.

-{% endif %} - -{% if params.results.page.items | length > 1 %} -
- {{ govukPagination({ previous: { href: params.results.page.prev }, next: { href: params.results.page.next }, items: params.results.page.items }) }} -
-{% endif %} \ No newline at end of file +
+{% include "./results.njk" %} +
\ No newline at end of file diff --git a/packages/probation-search-frontend/routes/search.ts b/packages/probation-search-frontend/routes/search.ts index a7bd59ac..b4e0ed56 100644 --- a/packages/probation-search-frontend/routes/search.ts +++ b/packages/probation-search-frontend/routes/search.ts @@ -74,8 +74,8 @@ export default function probationSearchRoutes({ const client = new ProbationSearchClient(oauthClient, environment === 'local' ? localData : environment) router.post(path, (req, res) => { - const { search } = req.body - if (!allowEmptyQuery && (search == null || search.length === 0)) { + const query = req.body['probation-search-input'] + if (!allowEmptyQuery && (query == null || query.length === 0)) { res.render(template, { probationSearchResults: { errorMessage: { text: 'Please enter a search term' }, @@ -83,7 +83,7 @@ export default function probationSearchRoutes({ }, }) } else { - res.redirect(`${path}?q=${req.body.search}`) + res.redirect(`${path}?q=${query}`) } }) diff --git a/server/routes/index.ts b/server/routes/index.ts index fb4383e3..0c9f5964 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -3,8 +3,8 @@ import { type RequestHandler, Router } from 'express' import probationSearchRoutes from '@ministryofjustice/probation-search-frontend/routes/search' import nunjucks from 'nunjucks' import { - ProbationSearchResponse, ProbationSearchRequest, + ProbationSearchResponse, } from '@ministryofjustice/probation-search-frontend/data/probationSearchClient' import asyncMiddleware from '../middleware/asyncMiddleware' import config from '../config' @@ -29,6 +29,15 @@ export default function routes(service: Services): Router { oauthClient: service.hmppsAuthClient, }) + // Examples page + probationSearchRoutes({ + router, + path: '/examples', + template: 'pages/examples', + environment: config.environment, + oauthClient: service.hmppsAuthClient, + }) + // Delius search screen (fka new tech) probationSearchRoutes({ router, diff --git a/server/views/pages/examples.njk b/server/views/pages/examples.njk new file mode 100644 index 00000000..23d901ac --- /dev/null +++ b/server/views/pages/examples.njk @@ -0,0 +1,26 @@ +{% extends "../partials/layout.njk" %} +{% from "probationSearch/macro.njk" import probationSearch %} + +{% set pageTitle = applicationName + " - Home" %} +{% set mainClasses = "app-container govuk-body" %} + +{% block content %} +

Examples

+

A collection of examples showing how the probation-search-frontend UI component can be configured.

+ +
+
    +
  • +

    Basic example

    +
    {% raw %}{{ probationSearch({ id: "basic", results: probationSearchResults }) }}{% endraw %}
    +
    {{ probationSearch({ id: "basic", results: probationSearchResults }) }}
    +
  • + +
  • +

    Search as you type

    +
    {% raw %}{{ probationSearch({ id: "on-input", searchOnInput: true, results: probationSearchResults }) }}{% endraw %}
    +
    {{ probationSearch({ id: "on-input", searchOnInput: true, results: probationSearchResults }) }}
    +
  • +
+
+{% endblock %} \ No newline at end of file diff --git a/server/views/pages/index.njk b/server/views/pages/index.njk index 09190078..9133d863 100644 --- a/server/views/pages/index.njk +++ b/server/views/pages/index.njk @@ -7,7 +7,7 @@ {% block content %}
- {{ probationSearch({ id: "search", name: "search", results: probationSearchResults }) }} + {{ probationSearch({ id: "search", results: probationSearchResults }) }}
{% endblock %} \ No newline at end of file diff --git a/server/views/pages/newTech/index.njk b/server/views/pages/newTech/index.njk index 56364f52..faf5e7a8 100644 --- a/server/views/pages/newTech/index.njk +++ b/server/views/pages/newTech/index.njk @@ -17,7 +17,6 @@ {{ probationSearch({ id: "search", - name: "search", classes: "app-search-input govuk-body-l govuk-!-padding-3 govuk-!-margin-bottom-2", label: { text: "Search for people on probation", @@ -61,23 +60,24 @@ {% endblock %}