front-end god mode with reagent and figwheel

68
Front-enD God-modE w / ReagenT AnD FigwheeL DaviD Y. KaY

Upload: david-kay

Post on 11-Apr-2017

398 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Front-end God Mode with Reagent and Figwheel

Front-enD God-modE w / ReagenT AnD FigwheeL

DaviD Y. KaY

Page 2: Front-end God Mode with Reagent and Figwheel

State of Front-end Dev

Page 3: Front-end God Mode with Reagent and Figwheel
Page 4: Front-end God Mode with Reagent and Figwheel

State of the Art• Write Code

• Reload Browser

• Lose Application State

• Manipulate Application State

• Test/verify behavior

• Repeat

Page 5: Front-end God Mode with Reagent and Figwheel
Page 6: Front-end God Mode with Reagent and Figwheel
Page 7: Front-end God Mode with Reagent and Figwheel
Page 8: Front-end God Mode with Reagent and Figwheel
Page 9: Front-end God Mode with Reagent and Figwheel
Page 10: Front-end God Mode with Reagent and Figwheel

Goal for this Talk

• How to develop code interactively

• Minimal incidental complexity

• Be the coolest kid on the block

Page 11: Front-end God Mode with Reagent and Figwheel

Demo

• Show what Pharmasoft / Chat App Looks Like

• Show a one-line change

• Show the recursive nature of components

Page 12: Front-end God Mode with Reagent and Figwheel

The Armory

Clojurescript Figwheel Reagent

Page 13: Front-end God Mode with Reagent and Figwheel

Why Clojurescript?

Page 14: Front-end God Mode with Reagent and Figwheel

Clojurescript

• More sane than JS• Functional• S-expressions• Code is data

Page 15: Front-end God Mode with Reagent and Figwheel

(defn square [x] (* x x))

function square(x){ return x*x; }

Page 16: Front-end God Mode with Reagent and Figwheel

Why Figwheel?

Page 17: Front-end God Mode with Reagent and Figwheel

Figwheel3 Steps:

• Edit

• Save

• Watch in Awe

Page 18: Front-end God Mode with Reagent and Figwheel

Bret Victor - Inventing on Principle

Page 19: Front-end God Mode with Reagent and Figwheel

Figwheel

• Pro:

• Code changes occur instantly

• App state is maintained

• Con:

• Have to write code so that it’s reloadable

Page 20: Front-end God Mode with Reagent and Figwheel
Page 21: Front-end God Mode with Reagent and Figwheel

Code Changes

Figwheel Server (Lein figwheel)

CLJS App

Figwheel Client

Page 22: Front-end God Mode with Reagent and Figwheel

Why Reagent?

Page 23: Front-end God Mode with Reagent and Figwheel

Reagent

• a minimal, sleek React.js

• SIMPLE

• Dataflow!

• Likened more to Elm than React.js

Page 24: Front-end God Mode with Reagent and Figwheel

Atoms(defonce app-state (r/atom {:chat-input "" :messages [] :username "Anon"}))

(swap! app-state assoc :username "David")

(reset! app-state new-state)

Page 25: Front-end God Mode with Reagent and Figwheel

Adjust with Feedback

• Show some mis-steps, correct them

Page 26: Front-end God Mode with Reagent and Figwheel

Review

• Clojurescript

• Figwheel

• Reagent

Page 27: Front-end God Mode with Reagent and Figwheel
Page 28: Front-end God Mode with Reagent and Figwheel

Come for the workshop!

Page 29: Front-end God Mode with Reagent and Figwheel
Page 30: Front-end God Mode with Reagent and Figwheel
Page 31: Front-end God Mode with Reagent and Figwheel

MailinG LisT

www.davidykay.com/i-love-clojurescript

Page 32: Front-end God Mode with Reagent and Figwheel

ReagenT AnD FigwheeL WorkshoP

Page 33: Front-end God Mode with Reagent and Figwheel

Installing the Tools

brew install leiningen

Page 34: Front-end God Mode with Reagent and Figwheel

Running the Code

Terminal 1:

lein ring server

Terminal 2:

lein figwheel

Page 35: Front-end God Mode with Reagent and Figwheel

Part 0: Prologue

Page 36: Front-end God Mode with Reagent and Figwheel

Set the Goal

• By the end of this workshop, you should be able to create a chat client using CLJS, Foundation, Reagent, and Figwheel

• Here’s what it looks like…

Page 37: Front-end God Mode with Reagent and Figwheel

Demo

• Show what Chat App Looks Like

Page 38: Front-end God Mode with Reagent and Figwheel
Page 39: Front-end God Mode with Reagent and Figwheel

Roadmap• Clojurescript

• Data structures

• Functions

• Figwheel

• Reagent

• Atoms

• Components

• Putting it all together - Chat App

Page 40: Front-end God Mode with Reagent and Figwheel

Part 1: Intro

Page 41: Front-end God Mode with Reagent and Figwheel

Figwheel

• Pro:

• Code changes occur instantly

• App state is maintained

• Con:

• Have to write code so that it’s reloadable

Page 42: Front-end God Mode with Reagent and Figwheel

Hello World

lein new figwheel chat

cd chat

lein figwheel

view localhost:3449

Page 43: Front-end God Mode with Reagent and Figwheel

Clojurescript

Page 44: Front-end God Mode with Reagent and Figwheel

(defn square [x] (* x x))

Page 45: Front-end God Mode with Reagent and Figwheel

Data Structures

List - ( )

Vector - [ ]

Map - { }

Set - #{ }

Page 46: Front-end God Mode with Reagent and Figwheel

(def app-db {:greeting "Hello Van FP!" :email “[email protected]" :password "" :screen :login})

Page 47: Front-end God Mode with Reagent and Figwheel

(if (= id (:current-condition @app-state)) "current" "")

Page 48: Front-end God Mode with Reagent and Figwheel

(fn [n] (clojure.string/join

" " "tabs-title"

(if (= n (:active-tab @app-state)) "is-active" nil)]))

Page 49: Front-end God Mode with Reagent and Figwheel

Part 2: Basics

Page 50: Front-end God Mode with Reagent and Figwheel

Reagent

Page 51: Front-end God Mode with Reagent and Figwheel

Hiccup Syntax

[:div.top-bar-right [:ul.menu [:li [:a {:href "#"} "History"]] [:li [:a {:href "#"} "Log Out"]]]]

Page 52: Front-end God Mode with Reagent and Figwheel

Button Handler

[:button {:class "button" :on-click

#(do (refresh-messages) (.preventDefault %))} "Refresh"]

Page 53: Front-end God Mode with Reagent and Figwheel

Button Handler

[:button {:class "button" :on-click

(fn [ev] (refresh-messages) (.preventDefault ev))} "Refresh"]

Page 54: Front-end God Mode with Reagent and Figwheel

Button Handler

(defn handle-click [ev] (refresh-messages) (.preventDefault ev))

[:button {:class "button" :on-click handle-click} "Refresh"]

Page 55: Front-end God Mode with Reagent and Figwheel

Atoms(defonce app-state (r/atom {:chat-input "" :messages [] :username "Anon"}))

(swap! app-state assoc :username "David")

(reset! app-state new-state)

Page 56: Front-end God Mode with Reagent and Figwheel

Atoms(swap! app-state assoc :username “David")

(swap! app-state (fn [old] (assoc old :username “david”))

(reset! app-state new-state)

Page 57: Front-end God Mode with Reagent and Figwheel

Text Input

[:input {:placeholder "Neo", :type "text" :value (:username @app-state) :on-change (fn [ev] (swap! app-state assoc :username (-> ev .-target .-value)))}]

Page 58: Front-end God Mode with Reagent and Figwheel

Text Input

(-> ev .-target .-value)

(:value (:target ev))

(.-value (.-target ev))

Page 59: Front-end God Mode with Reagent and Figwheel

Part 3: Intermediate

Page 60: Front-end God Mode with Reagent and Figwheel

HTTP Calls

(GET "/" {:handler (fn [response] (println response)) :error-handler error-handler})

(defn error-handler [{:keys [status status-text]}] (println "error code: " status " " status-text))

Page 61: Front-end God Mode with Reagent and Figwheel

Reagent Component

(defn component [medication] [:div [:h2 "Pre-Interview Notes"] [:p (:general-notes @app-state)] [:form ...]])

Page 62: Front-end God Mode with Reagent and Figwheel

Review• Figwheel

• Reloadable code

• Clojurescript

• Reagent

• Ratoms

• Components

Page 63: Front-end God Mode with Reagent and Figwheel

Resources

• Figwheel Talk at Clojure/West 2015

• Reagent Docs - holmsand.github.io/reagent/

Page 64: Front-end God Mode with Reagent and Figwheel

MailinG LisT

www.davidykay.com/i-love-clojurescript

Page 65: Front-end God Mode with Reagent and Figwheel

ExtrA ImageS

Page 66: Front-end God Mode with Reagent and Figwheel
Page 67: Front-end God Mode with Reagent and Figwheel
Page 68: Front-end God Mode with Reagent and Figwheel