-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Makes the case library searchable (#161)
- Loading branch information
Showing
33 changed files
with
3,020 additions
and
545 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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class LibrariesController < ApplicationController | ||
before_action :set_library | ||
|
||
def show | ||
head(:not_found) && return unless @library | ||
end | ||
|
||
private | ||
|
||
def set_library | ||
@library = Library.find_by_slug params[:slug] | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
class SearchController < ApplicationController | ||
def index | ||
@cases = Case.all | ||
.merge(libraries_query) | ||
.merge(full_text_query) | ||
.pluck(:slug) | ||
render json: @cases | ||
end | ||
|
||
private | ||
|
||
def libraries_query | ||
return Case.all unless params[:libraries] | ||
|
||
Case.joins(:library) | ||
.where(libraries: { slug: params[:libraries] }) | ||
.ordered | ||
end | ||
|
||
def full_text_query | ||
return Case.all unless params[:q] | ||
|
||
query = params[:q].is_a?(Array) ? params[:q].join(' ') : params[:q] | ||
Case.joins('JOIN cases_search_index_en ON cases_search_index_en.id = cases.id') | ||
.where('cases_search_index_en.document @@ plainto_tsquery(?)', query) | ||
.reorder( | ||
'ts_rank(' \ | ||
'cases_search_index_en.document, ' \ | ||
"plainto_tsquery(#{ActiveRecord::Base.connection.quote(query)})" \ | ||
') DESC' | ||
) | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* @providesModule Home | ||
* @flow | ||
*/ | ||
|
||
import * as React from 'react' | ||
import { values, omit } from 'ramda' | ||
|
||
import Sidebar from 'catalog/Sidebar' | ||
import Features from 'catalog/Features' | ||
import MapView from 'catalog/MapView' | ||
import CaseList from 'catalog/CaseList' | ||
import { Main, CatalogSection, SectionTitle } from 'catalog/shared' | ||
|
||
import type { Case, Enrollment, Reader } from 'redux/state' | ||
import type { Loading } from 'catalog' | ||
|
||
class Home extends React.Component<{ | ||
loading: Loading, | ||
reader: ?Reader, | ||
cases: { [string]: Case }, | ||
enrollments: Enrollment[], | ||
features: string[], | ||
readerIsEditor: boolean, | ||
onDeleteEnrollment: ( | ||
slug: string, | ||
options: { displayBetaWarning?: boolean } | ||
) => any, | ||
}> { | ||
render () { | ||
const { | ||
loading, | ||
reader, | ||
onDeleteEnrollment, | ||
readerIsEditor, | ||
cases, | ||
} = this.props | ||
return [ | ||
<Sidebar | ||
key="sidebar" | ||
loading={loading} | ||
reader={reader} | ||
enrolledCases={this._enrolledCases()} | ||
onDeleteEnrollment={onDeleteEnrollment} | ||
/>, | ||
<Main key="main"> | ||
<Features | ||
readerIsEditor={readerIsEditor} | ||
featuredCases={this._featuredCases()} | ||
/> | ||
<MapView | ||
cases={values(cases).filter(x => !!x.publishedAt)} | ||
title={{ | ||
id: 'catalog.locations', | ||
defaultMessage: 'Site locations', | ||
}} | ||
startingViewport={{ | ||
latitude: 17.770231041567445, | ||
longitude: 16.286555860170893, | ||
zoom: 1.1606345336768273, | ||
}} | ||
/> | ||
<CatalogSection> | ||
<SectionTitle>All cases</SectionTitle> | ||
<CaseList | ||
readerIsEditor={readerIsEditor} | ||
cases={this._allOtherCases()} | ||
/> | ||
</CatalogSection> | ||
</Main>, | ||
] | ||
} | ||
|
||
_enrolledCases = () => | ||
this.props.enrollments | ||
.map(e => this.props.cases[e.caseSlug]) | ||
.filter(x => !!x) | ||
|
||
_featuredCases = () => | ||
this.props.features.map(slug => this.props.cases[slug]).filter(x => !!x) | ||
|
||
_allOtherCases = () => | ||
values( | ||
omit( | ||
this.props.enrollments.map(e => e.caseSlug).concat(this.props.features), | ||
this.props.cases | ||
) | ||
) | ||
.filter(x => !!x.kicker) | ||
.sort((a, b) => a.kicker.localeCompare(b.kicker)) | ||
} | ||
export default Home |
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @providesModule LibraryInfo | ||
* @flow | ||
*/ | ||
|
||
import * as React from 'react' | ||
import styled from 'styled-components' | ||
|
||
import { withRouter } from 'react-router-dom' | ||
import { Orchard } from 'shared/orchard' | ||
|
||
import LibraryLogo from 'overview/LibraryLogo' | ||
import { CatalogSection, SectionTitle } from 'catalog/shared' | ||
|
||
import type { ContextRouter } from 'react-router-dom' | ||
import type { Library } from 'redux/state' | ||
|
||
type Props = ContextRouter & {| slug: string |} | ||
class LibraryInfo extends React.Component<Props, Library> { | ||
componentDidMount () { | ||
this._fetchLibraryInfo() | ||
} | ||
|
||
componentDidUpdate (prevProps: Props) { | ||
if (this.props.slug !== prevProps.slug) this._fetchLibraryInfo() | ||
} | ||
|
||
render () { | ||
if (!this.state) return null | ||
|
||
const { name, description, url } = this.state | ||
return ( | ||
<CatalogSection solid> | ||
<RightFloatLogoContainer> | ||
<LibraryLogo library={this.state} /> | ||
</RightFloatLogoContainer> | ||
<SectionTitle>{name}</SectionTitle> | ||
<Description>{description}</Description> | ||
<LearnMore href={url}>Learn more ›</LearnMore> | ||
</CatalogSection> | ||
) | ||
} | ||
|
||
_fetchLibraryInfo = () => | ||
Orchard.harvest(`libraries/${this.props.slug}`) | ||
.then((library: Library) => this.setState(library)) | ||
.catch(() => this.props.history.replace('/')) | ||
} | ||
export default withRouter(LibraryInfo) | ||
|
||
const RightFloatLogoContainer = styled.div` | ||
position: relative; | ||
float: right; | ||
width: 67px; | ||
height: 116px; | ||
margin: -10px 0 10px 10px; | ||
pointer-events: none; | ||
` | ||
const Description = styled.p` | ||
color: #ebeae4; | ||
margin-bottom: 0.5em; | ||
` | ||
const LearnMore = styled.a` | ||
color: #6acb72; | ||
&:hover { | ||
color: #6acb72; | ||
text-decoration: underline; | ||
} | ||
` |
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.