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

feat: create API for questions search #3996

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
60 changes: 60 additions & 0 deletions packages/cypress/src/integration/questions/search.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
describe('[How To]', () => {
beforeEach(() => {
cy.visit('/questions')
})

describe('[By Everyone]', () => {
it('should clear filters after navigation', () => {
cy.get('[data-cy=questions-search-box]').clear().type(`raincoat`)
cy.url().should('include', 'q=raincoat')
cy.url().should('include', 'sort=MostRelevant')

cy.get('[data-cy=category-select]').click()
cy.get('[id^="react-select-"]').contains('screening').click()
cy.url().should('include', 'category=categoryoix4r6grC1mMA0Xz3K')

cy.get('[data-cy=page-link]').contains('Questions').click()

cy.wait(2000)
cy.get('[data-cy=questions-search-box]')
.invoke('val')
.then((searchText) => expect(searchText).to.equal(''))
cy.get('[data-cy=category-select]').should('have.value', '')
})

it('should remove category filter after back navigation', () => {
cy.get('[data-cy=category-select]').click()
cy.get('[id^="react-select-"]').contains('screening').click()
cy.url().should('include', 'category=categoryoix4r6grC1mMA0Xz3K')
cy.go('back')
cy.get('[data-cy=category-select]').should('have.value', '')
cy.url().should('not.include', 'category=categoryoix4r6grC1mMA0Xz3K')
})

it('should remove search filter after back navigation', () => {
cy.get('[data-cy=questions-search-box]').clear().type(`raincoat`)
cy.url().should('include', 'q=raincoat')

cy.go('back')
cy.wait(2000)

cy.get('[data-cy=questions-search-box]')
.invoke('val')
.then((searchText) => expect(searchText).to.equal(''))
cy.url().should('not.include', 'q=raincoat')
})

it('should show question list items after visit a question', () => {
cy.get('[data-cy=question-list-item]:eq(0)').click()
cy.get('[data-cy=question-title]').should('be.visible')
cy.go('back')
cy.get('[data-cy=question-list-item]').should('be.visible')
})

it('should load more questions', () => {
cy.get('[data-cy=question-list-item]:eq(21)').should('not.exist')
cy.get('[data-cy=load-more]').click()
cy.get('[data-cy=question-list-item]:eq(21)').should('exist')
})
})
})
4 changes: 4 additions & 0 deletions src/pages/Question/QuestionFilterHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export const QuestionFilterHeader = () => {
initCategories()
}, [])

useEffect(() => {
setSearchString(q || '')
}, [q])

const updateFilter = useCallback(
(key: QuestionSearchParams, value: string) => {
const params = new URLSearchParams(searchParams.toString())
Expand Down
21 changes: 10 additions & 11 deletions src/pages/Question/QuestionListing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ import { logger } from 'src/logger'
import { questionService } from 'src/pages/Question/question.service'
import { Flex, Heading } from 'theme-ui'

import { ITEMS_PER_PAGE } from './constants'
import { headings, listing } from './labels'
import { QuestionFilterHeader } from './QuestionFilterHeader'
import { QuestionListItem } from './QuestionListItem'

import type { DocumentData, QueryDocumentSnapshot } from 'firebase/firestore'
import type { IQuestion } from 'oa-shared'
import type { QuestionSortOption } from './QuestionSortOptions'

export const QuestionListing = () => {
const [isFetching, setIsFetching] = useState<boolean>(true)
const [questions, setQuestions] = useState<IQuestion.Item[]>([])
const [total, setTotal] = useState<number>(0)
const [lastVisible, setLastVisible] = useState<
QueryDocumentSnapshot<DocumentData, DocumentData> | undefined
>(undefined)
const [lastVisibleId, setLastVisibleId] = useState<string | undefined>(
undefined,
)
const { userStore } = useCommonStores().stores

const [searchParams, setSearchParams] = useSearchParams()
Expand All @@ -46,9 +44,7 @@ export const QuestionListing = () => {
}
}, [q, category, sort])

const fetchQuestions = async (
skipFrom?: QueryDocumentSnapshot<DocumentData, DocumentData>,
) => {
const fetchQuestions = async (skipFrom?: string | undefined) => {
setIsFetching(true)

try {
Expand All @@ -59,7 +55,6 @@ export const QuestionListing = () => {
category,
sort,
skipFrom,
ITEMS_PER_PAGE,
)

if (result) {
Expand All @@ -70,7 +65,7 @@ export const QuestionListing = () => {
setQuestions(result.items)
}

setLastVisible(result.lastVisible)
setLastVisibleId(result.lastVisibleId)

setTotal(result.total)
}
Expand Down Expand Up @@ -151,7 +146,11 @@ export const QuestionListing = () => {
justifyContent: 'center',
}}
>
<Button type="button" onClick={() => fetchQuestions(lastVisible)}>
<Button
type="button"
onClick={() => fetchQuestions(lastVisibleId)}
data-cy="load-more"
>
{listing.loadMore}
</Button>
</Flex>
Expand Down
95 changes: 0 additions & 95 deletions src/pages/Question/question.service.test.ts

This file was deleted.

Loading
Loading