-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(headless): added defineResultList SSR controller (#3099)
- Loading branch information
1 parent
804af40
commit 9fce780
Showing
8 changed files
with
137 additions
and
57 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
packages/headless/src/controllers/ssr/result-list/headless-ssr-result-list.ts
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,23 @@ | ||
import {SearchEngine} from '../../../app/search-engine/search-engine'; | ||
import {ControllerDefinitionWithoutProps} from '../../../app/ssr-engine/types/common'; | ||
import { | ||
ResultList, | ||
ResultListProps, | ||
buildResultList, | ||
} from '../../result-list/headless-result-list'; | ||
|
||
export type { | ||
ResultListOptions, | ||
ResultListProps, | ||
ResultListState, | ||
ResultList, | ||
} from '../../result-list/headless-result-list'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const defineResultList = ( | ||
props?: ResultListProps | ||
): ControllerDefinitionWithoutProps<SearchEngine, ResultList> => ({ | ||
build: (engine) => buildResultList(engine, props), | ||
}); |
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
17 changes: 17 additions & 0 deletions
17
packages/samples/headless-ssr/cypress/e2e/ssr-e2e-utils.ts
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,17 @@ | ||
export const ConsoleAliases = { | ||
error: '@consoleError', | ||
warn: '@consoleWarn', | ||
log: '@consoleLog', | ||
}; | ||
|
||
export function spyOnConsole() { | ||
cy.window().then((win) => { | ||
cy.spy(win.console, 'error').as(ConsoleAliases.error.substring(1)); | ||
cy.spy(win.console, 'warn').as(ConsoleAliases.warn.substring(1)); | ||
cy.spy(win.console, 'log').as(ConsoleAliases.log.substring(1)); | ||
}); | ||
} | ||
|
||
export function waitForHydration() { | ||
cy.get('#hydrated-indicator').should('be.checked'); | ||
} |
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
35 changes: 35 additions & 0 deletions
35
packages/samples/headless-ssr/src/components/hydration-metadata.tsx
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 @@ | ||
import {FunctionComponent} from 'react'; | ||
import {SearchCSRState, SearchSSRState} from '../common/engine'; | ||
|
||
export interface HydrationMetadataProps { | ||
ssrState: SearchSSRState; | ||
csrResult?: SearchCSRState; | ||
} | ||
|
||
export const HydrationMetadata: FunctionComponent<HydrationMetadataProps> = ({ | ||
ssrState, | ||
csrResult, | ||
}) => ( | ||
<> | ||
<div> | ||
Hydrated:{' '} | ||
<input | ||
id="hydrated-indicator" | ||
type="checkbox" | ||
readOnly | ||
checked={!!csrResult} | ||
/> | ||
</div> | ||
<span id="hydrated-msg"> | ||
Rendered page with{' '} | ||
{(csrResult ?? ssrState).controllers.resultList.state.results.length}{' '} | ||
results | ||
</span> | ||
<div> | ||
Rendered on{' '} | ||
<span id="timestamp" suppressHydrationWarning> | ||
{new Date().toISOString()} | ||
</span> | ||
</div> | ||
</> | ||
); |
48 changes: 26 additions & 22 deletions
48
packages/samples/headless-ssr/src/components/result-list.tsx
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,26 +1,30 @@ | ||
'use client'; | ||
import { | ||
ResultList as ResultListController, | ||
ResultListState, | ||
} from '@coveo/headless'; | ||
import {useEffect, useState, FunctionComponent} from 'react'; | ||
|
||
import {Result} from '@coveo/headless'; | ||
interface ResultListProps { | ||
initialState: ResultListState; | ||
controller?: ResultListController; | ||
} | ||
|
||
export const ResultList: FunctionComponent<ResultListProps> = (props) => { | ||
const {initialState, controller} = props; | ||
const [state, setState] = useState(initialState); | ||
|
||
useEffect( | ||
() => controller?.subscribe(() => setState({...controller.state})), | ||
[controller] | ||
); | ||
|
||
export default function ResultList({results}: {results: Result[]}) { | ||
return ( | ||
<div> | ||
<span id="hydrated-msg"> | ||
Hydrated engine with {results?.length} results | ||
</span> | ||
<ul> | ||
{results?.map((result) => ( | ||
<li key={result.uniqueId}> | ||
<a href={result.clickUri}>{result.title}</a> | ||
</li> | ||
))} | ||
</ul> | ||
<div> | ||
Rendered on{' '} | ||
<span id="timestamp" suppressHydrationWarning> | ||
{new Date().toISOString()} | ||
</span> | ||
</div> | ||
</div> | ||
<ul> | ||
{state.results.map((result) => ( | ||
<li key={result.uniqueId}> | ||
<h3>{result.title}</h3> | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
} | ||
}; |
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