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 search results when navigating back in history after page load #1310

Merged
merged 1 commit into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions explorer/client/src/__tests__/reducers/search.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import reducer, { IState } from '../../reducers'
import { SearchAction } from '../../reducers/search'
import { Action } from '../../reducers/search'

describe('reducers/search', () => {
it('returns an initial state', () => {
const action = {} as SearchAction
const action = {} as Action
const state = reducer({}, action) as IState

expect(state.search).toEqual({
query: undefined
})
})

it('can update the search query', () => {
const action = {
type: 'UPDATE_SEARCH_QUERY',
query: 'something'
} as SearchAction
it('can parse the search query from a query param', () => {
const location = { toString: () => 'http://localhost/?search=find-me' }
const action = { location } as Action
const state = reducer({}, action) as IState

expect(state.search).toEqual({
query: 'something'
query: 'find-me'
})
})
})
5 changes: 3 additions & 2 deletions explorer/client/src/containers/JobRuns/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import List from '../../components/JobRuns/List'
import { getJobRuns } from '../../actions/jobRuns'
import { IState } from '../../reducers'
import { ChangePageEvent } from '../../components/Table'

const EMPTY_MSG =
"We couldn't find any results for your search query. Try again with the job id, run id, requester, requester id or transaction hash"
Expand Down Expand Up @@ -42,14 +43,14 @@ interface IProps extends WithStyles<typeof styles> {

const Index = withStyles(styles)((props: IProps) => {
const [currentPage, setCurrentPage] = useState(0)
const onChangePage = (event: any, page: number) => {
const onChangePage = (_event: ChangePageEvent, page: number) => {
setCurrentPage(page)
props.getJobRuns(props.query, page + 1, props.rowsPerPage)
}

useEffect(() => {
props.getJobRuns(props.query, currentPage + 1, props.rowsPerPage)
}, [])
}, [props.query])

return (
<div className={props.classes.container}>
Expand Down
21 changes: 5 additions & 16 deletions explorer/client/src/reducers/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,19 @@ export interface IState {

export type Query = string | undefined

export type SearchAction =
| { type: 'UPDATE_SEARCH_QUERY'; query?: string }
| { type: '@@redux/INIT' }
| { type: '@@INIT' }
export type Action = { type: string; location?: Location }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a little gross as it matches all actions. I couldn't think of a better way to inject location for testing. Open to suggestions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just happy we've reached a point where we can all agree string type that matches all actions is gross.


const initialState = { query: undefined }

const initQuery = (): Query => {
const searchParams = new URL(document.location.toString()).searchParams
const parseQuery = (location: Location = document.location): Query => {
const searchParams = new URL(location.toString()).searchParams
const search = searchParams.get('search')

if (search) {
return search
}
}

export default (state: IState = initialState, action: SearchAction) => {
switch (action.type) {
case '@@redux/INIT':
case '@@INIT':
return Object.assign({}, state, { query: initQuery() })
case 'UPDATE_SEARCH_QUERY':
return Object.assign({}, state, { query: action.query })
default:
return state
}
export default (state: IState = initialState, action: Action) => {
return Object.assign({}, state, { query: parseQuery(action.location) })
}