Skip to content

Instantly share code, notes, and snippets.

View francisngo's full-sized avatar

Francis Ngo francisngo

View GitHub Profile
@francisngo
francisngo / fetch.js
Last active December 28, 2017 20:53
Display Random Users and Submit a Post using Vanilla JS, Fetch API & Promises
const ul = document.getElementById('users');
const url = 'https://jsonplaceholder.typicode.com';
// Create the type of element you pass in the parameters
function createNode(element) {
return document.createElement(element);
}
// Append the second parameter(element) to the first one
function append(parent, el) {
@francisngo
francisngo / react.js
Last active December 29, 2017 02:39
A stateful React component to add todo
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
todos: [],
todo: '',
}
@francisngo
francisngo / react.test.js
Last active January 2, 2018 19:58
Used Jest to unit-test a React component
import App from './App';
import React from 'react';
import { configure, shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
describe('App', () => {
it('should have the `th` "Todos"', () => {
const wrapper = shallow(
@francisngo
francisngo / sql.sql
Created December 29, 2017 02:22
A SQL select statement with multiple joins
/*
Sample Data
-----------
movie
------------------------------------------------
id title yr director budget gross
actor
-----------
id name
@francisngo
francisngo / user.js
Created December 29, 2017 03:40
User model to create and get users.
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
module.exports = mongoose.model('User', new Schema({
name: String,
password: String,
admin: Boolean
}));
@francisngo
francisngo / config.js
Created December 29, 2017 03:43
Store variables and configuration for sample
module.exports = {
'secret': 'thisissupposedtobetopsecret',
'database': 'mongodb://localhost:27017/test'
};
@francisngo
francisngo / server.js
Created December 29, 2017 03:58
Authenticate a Node.js API with JSON Web Tokens
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const morgan = require('morgan');
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const config = require('./config');
const User = require('./app/models/user');
@francisngo
francisngo / nosql.js
Last active December 29, 2017 07:33
Create MongoDB queries to insert a document into a collection and to find a document in a collection
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/test';
mongoClient.connect(url, function(err, db) {
if (err) {
throw err;
console.log('ERROR - Unable to connect to database.')
}
console.log('INFO - Database connected.');
@francisngo
francisngo / index.js
Last active December 30, 2017 00:49
Collection for Redux todo example - This file handles creating the redux store and passing it to the App container
import React from 'react';
import { render } from 'react-dom';
import { createStore } from 'redux';
import App from './App';
import { reducer } from './reducer';
const store = createStore(reducer);
render(<App store={store}/>, document.getElementById('root'));
@francisngo
francisngo / actions.js
Last active December 30, 2017 00:48
Collection for Redux todo example - This file defines the action types and actionCreators helper functions to create actions.
export const types = {
ADDTODO: 'ADD_TODO',
REMOVETODO: 'REMOVE_TODO'
};
export const actionCreators = {
addTodo: item => {
return {
type: types.ADDTODO,
payload: item