Skip to content

Instantly share code, notes, and snippets.

View selbekk's full-sized avatar
👨
Working dad

Kristofer Giltvedt Selbekk selbekk

👨
Working dad
View GitHub Profile
@selbekk
selbekk / webpack.config.js
Last active August 1, 2018 09:42
webpack and less - webpack.config.js
const path = require('path');
const config = {
// First, let's define an entry point for webpack to start its crawling.
entry: './src/index.js',
// Second, we define where the files webpack produce, are placed
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
@selbekk
selbekk / withApiState.js
Created July 24, 2018 13:19
A simple api state machine implementation
const withApiState = TargetComponent =>
class extends React.Component {
state = {
current: "idle"
};
apiState = {
pending: () => this.setState({ current: "pending" }),
success: () => this.setState({ current: "success" }),
error: () => this.setState({ current: "error" }),
@selbekk
selbekk / TheBestPage.js
Last active July 25, 2018 08:22
A pretty neat boring ol' page!
class SomePage extends React.Component {
async componentDidMount() {
const { apiState } = this.props;
apiState.pending();
try {
const res = await fetch('/api/some-data');
const data = await res.json();
apiState.success();
} catch (e) {
apiState.error();
@selbekk
selbekk / SomeBetterPage.js
Last active July 25, 2018 08:22
A slightly less boring ol' API backed page
class SomePage extends React.Component {
async componentDidMount() {
const { apiState } = this.props;
apiState.next();
try {
const res = await fetch('/api/some-data');
const data = await res.json();
apiState.next(true);
} catch (e) {
apiState.next(false);
@selbekk
selbekk / ApiStateMachinePart2.js
Created July 23, 2018 21:16
API state machine part 2
const withApiState = TargetComponent => class extends React.Component {
state = {
current: 'idle',
};
next = (input) => {
let nextState;
switch (this.state.current) {
case 'idle': nextState = 'pending'; break;
case 'pending': nextState = input ? 'success' : 'error'; break;
default: nextState = 'idle';
@selbekk
selbekk / ApiStateMachinePart1.js
Created July 23, 2018 21:10
API state machine part 1
class ApiStateMachine {
state = {
current: 'idle',
};
next(input) {
let nextState;
switch (this.state.current) {
case 'idle': nextState = 'pending'; break;
case 'pending': nextState = input ? 'success' : 'error'; break;
default: nextState = 'idle';
@selbekk
selbekk / SomePage.js
Last active July 25, 2018 08:12
A boring ol' API backed container component
class SomePage extends React.Component {
state = {
isPending: false,
error: null,
result: null,
};
async componentDidMount() {
this.setState({ isPending: true, error: null, result: null });
try {
const res = await fetch('/api/some-data');
@selbekk
selbekk / api-backed-dropdown.js
Created July 23, 2018 12:16
An API backed dropdown with Flow types
// @flow
import * as React from 'react';
import { TertiaryButton } from '@sb1/ffe-buttons-react';
import { Paragraph } from '@sb1/ffe-core-react';
import Dropdown from '@sb1/ffe-dropdown-react';
import Spinner from '@sb1/ffe-spinner-react';
import withApiState from '../../hocs/with-api-state';
import type { StateMachine } from '../../hocs/with-api-state';
@selbekk
selbekk / withApiState.js
Created July 12, 2018 07:15
An API state machine HOC
// @flow
import * as React from 'react';
const states = {
IDLE: 'idle',
PENDING: 'pending',
ERROR: 'error',
SUCCESS: 'success',
};
@selbekk
selbekk / date_article_7.js
Created June 12, 2018 19:39
Date arithmethic
const date = new Date();
date + date
// "Tue Jun 12 2018 21:38:21 GMT+0200 (CEST)Tue Jun 12 2018 21:38:21 GMT+0200 (CEST)"
date - date
// 0
date * date
// 2.3373282061373058e+24
date / date
// 1
+date