Skip to content

Instantly share code, notes, and snippets.

View tricoder42's full-sized avatar
🕴️
I may be slow to respond.

tricoder42

🕴️
I may be slow to respond.
View GitHub Profile
@tricoder42
tricoder42 / lingui_format.json
Created September 1, 2017 18:36
jsLingui message catalog formats
{
"Edit": {
"translation": "Upravit",
"origin": [
[
"src/lingui/projects/scenes/Repository/ui/EditableDescription.js",
81
]
]
},
@tricoder42
tricoder42 / bundle.js
Last active June 23, 2017 16:49
Removing development files from bundle
// load() call is removed, but import remains there
!function(modules) {
function __webpack_require__(moduleId) {
if (installedModules[moduleId]) return installedModules[moduleId].exports;
var module = installedModules[moduleId] = {
i: moduleId,
l: !1,
exports: {}
};
@tricoder42
tricoder42 / 05_auth.js
Created June 13, 2017 08:10
2017/06/13 [Medium] redux-saga factories and decorators
// Just an example of `isAuthenticated` selector
const selector = {
isAuthenticated: state => state.auth.isAuthenticated
}
export const authRequired = (saga) => function* (action) {
const isAuthenticated = yield select(selector.isAuthenticated)
// If user isn't authenticated, redirect him to /login
if (!isAuthenticated) {
@tricoder42
tricoder42 / 03_fetchSaga.full.js
Created June 13, 2017 07:58
2017/06/13 [Medium] redux-saga factories and decorators
export const fetchSaga = (entity, api) => function* ({ payload }) {
try {
const data = yield call(api, payload)
yield put(entity.response(data))
}
catch(error) {
// When there's error.response, it's usually response from
// backend server with status 4xx or 5xx.
// No response mostly means there's a connection error, because
// we don't even get anything from server.
@tricoder42
tricoder42 / 04_network_down.js
Last active June 13, 2017 08:08
2017/06/13 [Medium] redux-saga factories and decorators
export function* checkConnection () {
let interval = 1
// eslint-disable-next-line no-constant-condition
while (true) {
// race == wait for effect which comes first
const { timeout, up } = yield race({
// ping triggered manually
// e.g: 'reconnect now' button
ping: take(action.networkPing),
@tricoder42
tricoder42 / 02_fetchSaga.example.js
Created June 13, 2017 07:50
2017/06/13 [Medium] redux-saga factories and decorators
function ApiGetUser({ id }) {
return fetch(url`/user/${id}`)
}
// Just an example. Action creators are usually
// created using `createAction` from `redux-actions`
const getUser = {
request: (id) => ({
type: 'USER_GET@REQUEST',
payload: { id }
@tricoder42
tricoder42 / 01_fetchSaga.js
Created June 13, 2017 07:39
2017/06/13 [Medium] redux-saga factories and decorators
export const fetchSaga = (entity, api) => function* ({ payload }) {
try {
const data = yield call(api, payload)
yield put(entity.response(data))
}
catch(error) {
yield put(entity.response(error))
}
}
module Y2017.M06.D07.Exercise where
{--
So, here's one of the questions Amazon asks developers to test their under-
standing of data structures.
You have a binary tree of the following structure:
A
/ \
@tricoder42
tricoder42 / 03_local_selector.js
Created June 6, 2017 19:51
2017/06/06 [Medium] Use Selectors in Redux for Great Good
// const reducer = ...
const ID = 'auth'
const local = state => state[ID]
const getUser = state => local(state).user
const isAuthenticated = state => Boolean(getUser(state))
export default {[ID]: reducer}
@tricoder42
tricoder42 / 02_anonymous_user.js
Created June 6, 2017 19:30
2017/06/06 [Medium] Use Selectors in Redux for Great Good
const initialState = {
user: {
name: 'anonymous',
token: null
}
}
const getUser = state => state.user || {}
const isAuthenticated = state => Boolean(getUser(state).token)
const isAnonymous = state => !isAuthenticated(user)