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

Add init support for using next 14 with app router #2295

Draft
wants to merge 1 commit into
base: next
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion packages/playground
Submodule playground updated 1 files
+298 −277 yarn.lock
1 change: 1 addition & 0 deletions packages/web/examples/ssr-app-router/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.next
32 changes: 32 additions & 0 deletions packages/web/examples/ssr-app-router/app/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable react/prop-types */
import React from 'react';
import App from 'next/app';
import { getServerState } from '@appbaseio/reactivesearch';

import '../styles/movies.css';
import '../styles/index.css';
import Main from './page';

class MyApp extends App {
static async getInitialProps({ Component, ctx }) {
let pageProps = {};

if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx);
}

// Perform your server-side data fetching or initialization here
let initialState = {};
initialState = await getServerState(Main, ctx.resolvedUrl);

return { pageProps, initialState };
}

render() {
const { Component, pageProps, initialState } = this.props;

return <Component {...pageProps} initialState={initialState} />;
}
}

export default MyApp;
41 changes: 41 additions & 0 deletions packages/web/examples/ssr-app-router/app/_document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import Document, { Head, Html, Main, NextScript } from 'next/document';
import { renderToString } from 'react-dom/server';

export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx);
const { renderPage } = ctx;
// for emotion-js
const page = renderPage();
const styles = renderToString(page.html);
return { ...initialProps, ...page, ...styles };
}

constructor(props) {
// for emotion-js
super(props);
const { __NEXT_DATA__, ids } = props;
if (ids) {
__NEXT_DATA__.ids = ids;
}
}

render() {
return (
<Html lang="en">
<Head>
<link rel="stylesheet" href="/_next/static/style.css" />
<meta charSet="utf-8" />
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
{/* for emotion-js */}
<style dangerouslySetInnerHTML={{ __html: this.props.css }} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
116 changes: 116 additions & 0 deletions packages/web/examples/ssr-app-router/app/datepicker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import React from 'react';
import {
ReactiveBase,
DatePicker,
SelectedFilters,
ReactiveList,
ResultCard,
getServerState,
} from '@appbaseio/reactivesearch';
import PropTypes from 'prop-types';

import moment from 'moment';

import Layout from '../components/Layout';

const dateQuery = (value, props) => {
let query = null;
if (value) {
query = [
{
range: {
[props.dataField]: {
gte: moment(value).valueOf(),
},
},
},
];
}
return query ? { query: { bool: { must: query } } } : null;
};

const settings = {
app: 'airbnb-dev',
url: 'https://a03a1cb71321:75b6603d-9456-4a5a-af6b-a487b309eb61@appbase-demo-ansible-abxiydt-arc.searchbase.io',
enableAppbase: true,
};

// eslint-disable-next-line no-return-assign
const Main = props => (
<Layout title="SSR | DatePicker">
<ReactiveBase
{...settings}
{...(props.contextCollector ? { contextCollector: props.contextCollector } : {})}
initialState={props.initialState}
>
<div className="row">
<div className="col">
<DatePicker
componentId="DateSensor"
dataField="available_from"
customQuery={dateQuery}
initialMonth={new Date('2017-05-05')}
placeholder="Available From - YYYY-MM-DD"
URLParams
defaultValue="2017-05-05"
/>
</div>

<div className="col">
<SelectedFilters />
<ReactiveList
componentId="SearchResult"
dataField="name"
className="result-list-container"
from={0}
size={40}
// eslint-disable-next-line react/jsx-no-bind, func-names
render={function ({ data }) {
return (
<ReactiveList.ResultCardsWrapper>
{data.map(item => (
<ResultCard href={item.listing_url} key={item._id}>
<ResultCard.Image src={item.picture_url} />
<ResultCard.Title>{item.name}</ResultCard.Title>
<ResultCard.Description>
<div>
<div>${item.price}</div>
<span
style={{
backgroundImage: `url(${item.picture_url})`,
}}
/>
<p>
{item.room_type} · {item.accommodates}{' '}
guests
</p>
</div>
</ResultCard.Description>
</ResultCard>
))}
</ReactiveList.ResultCardsWrapper>
);
}}
react={{ and: ['DateSensor'] }}
pagination
URLParams
/>
</div>
</div>
</ReactiveBase>
</Layout>
);
export async function getServerSideProps(context) {
const initialState = await getServerState(Main, context.resolvedUrl);
return {
props: { initialState },
// will be passed to the page component as props
};
}

Main.propTypes = {
// eslint-disable-next-line
initialState: PropTypes.object,
contextCollector: PropTypes.func,
};
export default Main;
119 changes: 119 additions & 0 deletions packages/web/examples/ssr-app-router/app/daterange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import React from 'react';
import {
ReactiveBase,
SelectedFilters,
ReactiveList,
ResultCard,
DateRange,
getServerState,
} from '@appbaseio/reactivesearch';
import PropTypes from 'prop-types';

import moment from 'moment';

import Layout from '../components/Layout';

const dateQuery = (value, props) => {
let query = null;
if (value) {
query = [
{
range: {
[props.dataField]: {
lte: moment(value.end).valueOf(),
gte: moment(value.start).valueOf(),
},
},
},
];
}
return query ? { query: { bool: { must: query } } } : null;
};

const settings = {
app: 'airbnb-dev',
url: 'https://a03a1cb71321:75b6603d-9456-4a5a-af6b-a487b309eb61@appbase-demo-ansible-abxiydt-arc.searchbase.io',
enableAppbase: true,
};

// eslint-disable-next-line no-return-assign
const Main = props => (
<Layout title="SSR | DateRange">
<ReactiveBase
{...settings}
{...(props.contextCollector ? { contextCollector: props.contextCollector } : {})}
initialState={props.initialState}
>
<div className="row">
<div className="col">
<DateRange
componentId="DateSensor"
dataField="available_from"
initialMonth={new Date('2021-05-05')}
customQuery={dateQuery}
defaultValue={{
start: new Date('2022-04-07'),
end: new Date('2022-05-26'),
}}
URLParams
/>
</div>

<div className="col">
<SelectedFilters />
<ReactiveList
componentId="SearchResult"
dataField="name"
className="result-list-container"
from={0}
size={40}
// eslint-disable-next-line react/jsx-no-bind, func-names
render={function ({ data }) {
return (
<ReactiveList.ResultCardsWrapper>
{data.map(item => (
<ResultCard href={item.listing_url} key={item._id}>
<ResultCard.Image src={item.picture_url} />
<ResultCard.Title>{item.name}</ResultCard.Title>
<ResultCard.Description>
<div>
<div>${item.price}</div>
<span
style={{
backgroundImage: `url(${item.picture_url})`,
}}
/>
<p>
{item.room_type} · {item.accommodates}{' '}
guests
</p>
</div>
</ResultCard.Description>
</ResultCard>
))}
</ReactiveList.ResultCardsWrapper>
);
}}
react={{ and: ['DateSensor'] }}
pagination
URLParams
/>
</div>
</div>
</ReactiveBase>
</Layout>
);
export async function getServerSideProps(context) {
const initialState = await getServerState(Main, context.resolvedUrl);
return {
props: { initialState },
// will be passed to the page component as props
};
}

Main.propTypes = {
// eslint-disable-next-line
initialState: PropTypes.object,
contextCollector: PropTypes.func,
};
export default Main;
74 changes: 74 additions & 0 deletions packages/web/examples/ssr-app-router/app/dynamicrangeslider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React from 'react';
import {
ReactiveBase,
SelectedFilters,
ReactiveList,
DynamicRangeSlider,
getServerState,
} from '@appbaseio/reactivesearch';
import PropTypes from 'prop-types';

import Layout from '../components/Layout';
import BookCard from '../components/BookCard';

const settings = {
app: 'good-books-ds',
url: 'https://a03a1cb71321:75b6603d-9456-4a5a-af6b-a487b309eb61@appbase-demo-ansible-abxiydt-arc.searchbase.io',
enableAppbase: true,
};

const dynamicRangeSliderProps = {
componentId: 'BookSensor',
dataField: 'ratings_count',
defaultValue: () => ({
start: 4000,
end: 8000,
}),
URLParams: true,
};

const reactiveListProps = {
componentId: 'SearchResult',
dataField: 'original_title',
className: 'result-list-container',
from: 0,
size: 5,
renderItem: data => <BookCard key={data._id} data={data} />,
react: {
and: ['BookSensor'],
},
};

const Main = props => (
<Layout title="SSR | DynamicRangeSlider">
<ReactiveBase
{...settings}
{...(props.contextCollector ? { contextCollector: props.contextCollector } : {})}
initialState={props.initialState}
>
<div className="row">
<div className="col">
<DynamicRangeSlider {...dynamicRangeSliderProps} />
</div>

<div className="col">
<SelectedFilters />
<ReactiveList {...reactiveListProps} />
</div>
</div>
</ReactiveBase>
</Layout>
);
export async function getServerSideProps(context) {
const initialState = await getServerState(Main, context.resolvedUrl);
return {
props: { initialState },
// will be passed to the page component as props
};
}
Main.propTypes = {
// eslint-disable-next-line
initialState: PropTypes.object,
contextCollector: PropTypes.func,
};
export default Main;
Loading