Skip to content

Instantly share code, notes, and snippets.

View PavlikPolivka's full-sized avatar

Pavel Polívka PavlikPolivka

View GitHub Profile
const _cacheValues = new Map();
const _cacheResolvedTime = new Map();
const _cachePromises = new Map();
const getDataCached = function (key) {
}
function getData(key){
return new Promise(function(resolve, reject) {
console.log('starting get ' + key)
setTimeout(() => {
console.log('ending get ' + key)
resolve(key);
}, 1000);
})
}
function wordCounter(input) {
const text = input.split(/\s+/)
let wordCount = 0
for (let i = 0; i < text.length; i++) {
if (text[i] !== ' ' && isWord(text[i])) {
wordCount++
}
}
return wordCount
}
const wordsPerMinute = 225
export function readingTime(text) {
return Math.ceil(wordCounter(text) / wordsPerMinute)
}
const paginationHandler = (page) => {
const currentPath = props.router.pathname;
const currentQuery = props.router.query;
currentQuery.page = page.selected + 1;
props.router.push({
pathname: currentPath,
query: currentQuery,
})
<ReactPaginate
previousLabel={'<'}
nextLabel={'>'}
breakLabel={'...'}
breakClassName={'break-me'}
activeClassName={'active'}
containerClassName={'pagination'}
subContainerClassName={'pages pagination'}
initialPage={props.currentPage - 1}
pageCount={props.pageCount}
let content;
if (isLoading) {
content = (
<div className={styles.loadWrapper}>
<Spinner animation="border" role="status">
<span className="visually-hidden">Loading...</span>
</Spinner>
</div>
)
} else {
const [isLoading, setLoading] = useState(false);
const startLoading = () => setLoading(true);
const stopLoading = () => setLoading(false);
useEffect(() => {
Router.events.on('routeChangeStart', startLoading);
Router.events.on('routeChangeComplete', stopLoading);
return () => {
Router.events.off('routeChangeStart', startLoading);
const dev = process.env.NODE_ENV !== 'production';
export const server = dev ? 'http://localhost:3000' : 'https://ppolivka.com';
Blog.getInitialProps = async ({ query }) => {
const page = query.page || 1; //if page empty we request the first page
const response = await fetch(`${server}/api/posts/${page}`)
const posts = await response.json()
return {
totalCount: posts.totalCount,
pageCount: posts.pageCount,
currentPage: posts.currentPage,
perPage: posts.perPage,
posts: posts.posts,