Skip to content

Instantly share code, notes, and snippets.

View aakashns's full-sized avatar
🎯
Focusing

Aakash N S aakashns

🎯
Focusing
View GitHub Profile
@aakashns
aakashns / ContactForm.jsx
Created December 5, 2017 05:07
Connecting the contactForm reducer with a React component.
const ContactForm = ({ data, editData }) => (
<div>
<h4>Contact Form</h4>
<input
type="text"
value={data.name}
onChange={e => editData({ name: e.target.value })}
/>
<input
type="text"
@aakashns
aakashns / contactForm.js
Last active December 5, 2017 05:04
A simple reducer to manage form state in Redux
const EDIT_CONTACT_FORM = "EDIT_CONTACT_FORM";
const CLEAR_CONTACT_FORM = "CLEAR_CONTACT_FORM";
const contactForm = (state = {}, action) => {
const { type, payload } = action;
switch (type) {
case EDIT_CONTACT_FORM:
return {
...state,
...payload
@aakashns
aakashns / install-opencv-2.4.13.4-in-ubuntu.sh
Last active December 1, 2017 13:04 — forked from arthurbeggs/install_opencv2_ubuntu.sh
Install opencv-2.4.13.4 in Ubuntu
#!/bin/bash
# install dependencies
sudo apt-get update
sudo apt-get install -y build-essential
sudo apt-get install -y cmake
sudo apt-get install -y libgtk2.0-dev
sudo apt-get install -y pkg-config
sudo apt-get install -y python-numpy python-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev
const messagePath = '/dummy';
const messageActionCreator = (value) => ({
type: 'SET_MESSAGE',
payload: value
});
const messageSelector = (state) => state.dummy;
const linkMessage = linkStoreWithPath(messagePath, messageActionCreator, messageSelector)
@aakashns
aakashns / linkStoreWithPath.js
Last active March 23, 2017 15:54
Working instance of linkStateWithPath
const linkStoreWithPath = (path, actionCreator, selector) => {
return (db, store) => {
let previous = selector(store.getState());
let mustWrite = true;
const fromDb = (db, dispatch) => {
const listener = db.ref(path).on("value", snap => {
if (snap.val()) {
mustWrite = false;
dispatch(actionCreator(snap.val()));
componentDidMount() {
const { store } = this.props.store;
this.unlink = linkStoreWithDb(fromDb, fromStore)(
firebase.database(),
store
);
}
componentWillUnmount() {
this.unlink();
@aakashns
aakashns / linkStoreWithDb.js
Last active March 23, 2017 13:47
Smart version of linkStoreWithDb
const linkStoreWithDb = (fromDb, fromStore) => {
return (db, store) => {
const unlinkFromDb = fromDb && fromDb(db, store.dispatch);
const unlinkFromStore = fromStore && store.subscribe(() => fromStore(store.getState, db));
const unlink = () => {
unlinkFromDb && unlinkFromDb();
unlinkFromStore && unlinkFromStore();
}
return unlink;
let mustWrite = true; // Should we write the database?
const fromDb = (db, dispatch) => {
const listener = db.ref('/message').on('value', snap => {
if (snap.val()) {
mustWrite = false; // Don't write the next dispatch
dispatch({ type: 'SET_MESSAGE', payload: data.val()});
mustWrite = true; // Done! Start writing again
}
});
@aakashns
aakashns / linkMessage.js
Created March 23, 2017 08:59
Simple linkMessage example.
const linkMessage = linkStoreWithDb(fromDb, fromStore)
linkMessage(firebase.database, store)
@aakashns
aakashns / linkStoreWithDb.js
Created March 23, 2017 08:54
Simple version of linkStoreWithDb
const linkStoreWithDb = (fromDb, fromStore) => {
return (db, store) => {
fromDb(db, store.dispatch);
store.subscribe(() => fromStore(store.getState(), db)
}
}