Skip to content

Instantly share code, notes, and snippets.

@productioncoder
productioncoder / index.js
Last active October 13, 2023 18:45
Session-based authentication in express.js
const express = require('express');
const session = require('express-session');
const redis = require('redis');
const connectRedis = require('connect-redis');
const app = express();
// if you run behind a proxy (e.g. nginx)
// app.set('trust proxy', 1);
@productioncoder
productioncoder / Comments.js
Last active February 5, 2019 07:34
Youtube in React: wiring up AddComment, CommentsHeader and Comment component
import React from 'react';
import {CommentsHeader} from "./CommentsHeader/CommentsHeader";
import {AddComment} from "./AddComment/AddComment";
import {Comment} from "./Comment/Comment";
export class Comments extends React.Component {
render() {
return (
<div>
<CommentsHeader amountComments={this.props.amountComments}/>
@productioncoder
productioncoder / App.js
Created January 29, 2019 18:36
Youtube in React: show side bar and header in app component
import React, {Component} from 'react';
import HeaderNav from './containers/HeaderNav/HeaderNav';
import {SideBar} from './containers/SideBar/SideBar';
class App extends Component {
render() {
return (
<React.Fragment>
<HeaderNav/>
<SideBar/>
@productioncoder
productioncoder / Watch.js
Created December 18, 2018 10:58
Youtube in React: wiring Watch component up so it can dispatch actions
/* ... */
import {bindActionCreators} from 'redux';
import * as watchActions from '../../store/actions/watch';
import {withRouter} from 'react-router-dom';
import {connect} from 'react-redux';
import {getYoutubeLibraryLoaded} from '../../store/reducers/api';
function mapStateToProps(state) {
return {
youtubeLibraryLoaded: getYoutubeLibraryLoaded(state),
@productioncoder
productioncoder / HomeContent.js
Created December 17, 2018 16:21
Youtube in React: rendering video grids dynamically in HomeContent component
/*....*/
import {getVideoCategoryIds} from '../../store/reducers/videos';
class Home extends React.Component {
render() {
const trendingVideos = this.getTrendingVideos();
const categoryGrids = this.getVideoGridsForCategories();
return (
<div className='home-content'>
@productioncoder
productioncoder / HomeContent.js
Created December 17, 2018 16:18
Youtube in React: update mapStateToProps in HomeContent to pull the videos per category in
/*...*/
import {getMostPopularVideos, getVideosByCategory} from '../../../store/reducers/videos';
/* ... */
function mapStateToProps(state) {
return {
videosByCategory: getVideosByCategory(state),
mostPopularVideos: getMostPopularVideos(state),
};
@productioncoder
productioncoder / HomeContent.js
Created December 17, 2018 16:16
Youtube in React: dynamically creating video grids in Home Content
/*...*/
import {getMostPopularVideos, getVideosByCategory} from '../../../store/reducers/videos';
class HomeContent extends React.Component {
/* ... */
getVideoGridsForCategories() {
const categoryTitles = Object.keys(this.props.videosByCategory || {});
return categoryTitles.map((categoryTitle,index) => {
const videos = this.props.videosByCategory[categoryTitle];
@productioncoder
productioncoder / Home.js
Created December 17, 2018 16:04
Youtube in React: wiring Home component up with video categories
/*....*/
import {getVideoCategoryIds} from '../../store/reducers/videos';
/* ... */
function mapStateToProps(state) {
return {
youtubeLibraryLoaded: getYoutubeLibraryLoaded(state),
videoCategories: getVideoCategoryIds(state),
};
@productioncoder
productioncoder / index.js
Created December 16, 2018 15:20
Youtube in React: generic fetching function based on redux-saga
import {all, call, put} from 'redux-saga/effects';
/*
... rest unchange for now
*/
/*
* entity must have a success, request and failure method
* request is a function that returns a promise when called
* */
@productioncoder
productioncoder / Rating.js
Created December 15, 2018 14:40
Youtube in React: Rating component skeleton
import React from 'react';
import './Rating.scss';
import {Icon, Progress} from "semantic-ui-react";
export function Rating() {
let progress = null;
if (props.likeCount && props.dislikeCount) {
const percent = 100 * (props.likeCount / (props.likeCount + props.dislikeCount));
progress = <Progress className='progress' percent={percent} size='tiny'/>;
}