-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from uktrade/ORPD-100-consume-drf-endpoint-in-…
…react feature/ORPD-100 - Consume Django Rest Framework endpoint in React
- Loading branch information
Showing
10 changed files
with
191 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,3 +82,8 @@ | |
text-decoration: underline; | ||
} | ||
} | ||
|
||
.orp-marked-text { | ||
font-weight: bold; | ||
background-color: inherit; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,95 @@ | ||
import { SkeletonResults } from "./SkeletonResults" | ||
|
||
function Results({ results, isLoading, searchQuery }) { | ||
// console.log("Results", results) | ||
|
||
if (isLoading) { | ||
return <SkeletonResults /> | ||
} | ||
|
||
const highlight = (text) => { | ||
const searchWords = searchQuery.join("|") | ||
const regex = new RegExp(searchWords, "gi") | ||
const highlightedText = text.replace(regex, (match) => `<mark>${match}</mark>`) | ||
const highlightedText = text.replace(regex, (match) => `<mark class="orp-marked-text">${match}</mark>`) | ||
return { __html: highlightedText } | ||
} | ||
|
||
const truncateDescription = (description, matchIndex) => { | ||
if (matchIndex === -1 || matchIndex <= 200) { | ||
return description.length > 200 ? description.substring(0, 200) + "..." : description | ||
} else { | ||
const start = 0 // Always start from the beginning | ||
const end = Math.min(description.length, matchIndex + 150) // Include some context after the match | ||
return description.substring(start, end) + "..." | ||
} | ||
} | ||
|
||
// This needs to be in a utils file | ||
const formatDate = (dateString) => { | ||
const date = new Date(dateString) | ||
return date.toLocaleDateString("en-GB", { | ||
day: "numeric", | ||
month: "long", | ||
year: "numeric", | ||
}) | ||
} | ||
|
||
const renderRegulatoryTopics = (topics, searchQuery) => { | ||
return topics.map((topic, index) => { | ||
const highlightedTopic = topic.toLowerCase().includes(searchQuery[0].toLowerCase()) ? ( | ||
<span dangerouslySetInnerHTML={highlight(topic)} /> | ||
) : ( | ||
topic | ||
) | ||
|
||
return ( | ||
<li key={index} className="govuk-body-s orp-secondary-text-colour"> | ||
{highlightedTopic} | ||
</li> | ||
) | ||
}) | ||
} | ||
|
||
return results ? ( | ||
<div className="govuk-summary-list orp-search-results"> | ||
{results.slice(0, 10).map((result) => { | ||
const { id, type, title, description, publisher, date_modified, regulatory_topics } = result.c | ||
{results.map((result) => { | ||
const { id, type, title, description, publisher, date_modified, regulatory_topics } = result | ||
|
||
// Check if the search term appears within the first 200 characters | ||
const searchWords = searchQuery.join("|") | ||
const regex = new RegExp(searchWords, "gi") | ||
const matchIndex = description.search(regex) | ||
|
||
let concatDescription | ||
if (matchIndex === -1 || matchIndex <= 200) { | ||
concatDescription = description.length > 200 ? description.substring(0, 200) + "..." : description | ||
} else { | ||
// Adjust the substring to include the highlighted text without cutting off the start | ||
const start = 0 // Always start from the beginning | ||
const end = Math.min(description.length, matchIndex + 150) // Include some context after the match | ||
concatDescription = description.substring(start, end) + "..." | ||
} | ||
|
||
const highlightedTitle = title ? <span dangerouslySetInnerHTML={highlight(title)} /> : "" | ||
const truncatedDescription = truncateDescription(description, matchIndex) | ||
// const highlightedTitle = title ? <span dangerouslySetInnerHTML={highlight(title)} /> : "" | ||
const highlightedDescription = description ? ( | ||
<span dangerouslySetInnerHTML={highlight(concatDescription)} /> | ||
<span dangerouslySetInnerHTML={highlight(truncatedDescription)} /> | ||
) : ( | ||
"" | ||
) | ||
|
||
const date = new Date(date_modified) | ||
const govukDate = date.toLocaleDateString("en-GB", { | ||
day: "numeric", | ||
month: "long", | ||
year: "numeric", | ||
}) | ||
|
||
const regulatory_topics_array = regulatory_topics.split("\n") | ||
const govukDate = formatDate(date_modified) | ||
|
||
return ( | ||
<div className="govuk-summary-list__row--no-border" key={id}> | ||
<span className="govuk-caption-m">{type}</span> | ||
<h2 className="govuk-heading-m"> | ||
<a href={`/document/${id}`} className="govuk-link"> | ||
{highlightedTitle} | ||
{title} | ||
</a> | ||
</h2> | ||
<p className="govuk-body">{highlightedDescription}</p> | ||
<p className="govuk-body-s orp-secondary-text-colour govuk-!-margin-bottom-2">Published by: {publisher}</p> | ||
<p className="govuk-body-s orp-secondary-text-colour">Last updated: {govukDate}</p> | ||
<ul className="govuk-list orp-topics-list"> | ||
{regulatory_topics_array.map((regulatory_topic, index) => ( | ||
<li key={index} className="govuk-body-s orp-secondary-text-colour"> | ||
{regulatory_topic} | ||
</li> | ||
))} | ||
</ul> | ||
{regulatory_topics && regulatory_topics.length > 0 ? ( | ||
<ul className="govuk-list orp-topics-list">{renderRegulatoryTopics(regulatory_topics, searchQuery)}</ul> | ||
) : null} | ||
<hr className="govuk-section-break govuk-section-break--l govuk-section-break--visible" /> | ||
</div> | ||
) | ||
})} | ||
</div> | ||
) : ( | ||
<p>No results found</p> | ||
) | ||
) : null | ||
} | ||
|
||
export { Results } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.