Skip to content

Instantly share code, notes, and snippets.

View f0rest8's full-sized avatar

Mark f0rest8

View GitHub Profile
@f0rest8
f0rest8 / hyper.js
Last active September 5, 2020 02:42
My hyper.js configuration on Windows 10 (providing an almost seamless experience to my previous Command Prompt with git work).
module.exports = {
config: {
// default font size in pixels for all tabs
fontSize: 12,
// font family with optional fallbacks
fontFamily: 'Menlo, "DejaVu Sans Mono", Consolas, "Lucida Console", monospace',
// terminal cursor background color and opacity (hex, rgb, hsl, hsv, hwb or cmyk)
cursorColor: 'rgba(248,28,229,0.8)',
@f0rest8
f0rest8 / new.ex
Last active February 3, 2021 23:09
Person registration with live view implementation with phx_gen_auth
defmodule YourAppWeb.PersonRegistrationLive.New do
use Phoenix.LiveView
alias YourAppWeb.Router.Helpers, as: Routes
alias YourApp.Accounts
alias YourApp.Accounts.Person
def mount(_params, _session, socket) do
changeset = Accounts.change_person_registration(%Person{})
{:ok, assign(socket, changeset: changeset)}
@f0rest8
f0rest8 / new_multi_step_form.ex
Last active February 4, 2021 05:09
Phoenix Live View `new.ex` for a multi-step form tutorial on Medium.
defmodule YourAppWeb.PersonRegistrationLive.New do
use Phoenix.LiveView
alias YourAppWeb.Router.Helpers, as: Routes
alias YourApp.Accounts
alias YourApp.Accounts.Person
def mount(_params, _session, socket) do
changeset = Accounts.change_person_registration(%Person{})
{:ok,
@f0rest8
f0rest8 / new_multi_step_form_template.leex
Last active February 4, 2021 22:07
Phoenix Live View multi-step form template
<%= f = form_for @changeset, "#", [phx_change: :validate, phx_submit: :save] %>
<div class="<%= unless @current_step === 1, do: "hidden" %>">
<!-- Step 1 form fields here -->
</div>
<div class="<%= unless @current_step === 2, do: "hidden" %>">
<!-- Step 2 form fields here -->
</div>
<div class="<%= unless @current_step === 3, do: "hidden" %>">
<!-- Step 3 form fields here -->
@f0rest8
f0rest8 / phone_number.ex
Last active March 15, 2021 03:33
Phone number module for Phoenix Live View multi-step form registration.
defmodule YourApp.Extensions.PhoneNumber do
@moduledoc """
Functions for implementing phone number validations and formatting
using [ex_phone_number](https://hex.pm/packages/ex_phone_number).
"""
@doc """
Parses a given phone number string.
## Example
@f0rest8
f0rest8 / your_app.accounts.person.ex
Last active March 15, 2021 06:48
Phone number validations for Phoenix Live View registration changeset.
defmodule YourApp.Accounts.Person do
...
alias YourApp.Encrypted # optional encryption
alias YourApp.Extensions.PhoneNumber
...
schema "people" do
...
field :phone_number, Encrypted.Binary, redact: true
...
@f0rest8
f0rest8 / new_multi_step_form.ex
Created March 15, 2021 04:09
Phoenix Live View multi-step form updated with phone number step.
defmodule YourAppWeb.PersonRegistrationLive.New do
...
def handle_event("next-step", _value, socket) do
current_step = socket.assigns.current_step
changeset = socket.assigns.changeset
step_invalid =
case current_step do
1 -> Enum.any?(Keyword.keys(changeset.errors), fn k -> k in [:name] end)
@f0rest8
f0rest8 / new_multi_step_form_with_phone_number_template.leex
Last active March 15, 2021 05:51
Phoenix Live View multi-step form template updated for phone number step.
<%= f = form_for @changeset, "#", [phx_change: :validate, phx_submit: :save] %>
<div class="<%= unless @current_step === 1, do: "hidden" %>">
<!-- Step 1 form fields here -->
</div>
<div class="<%= unless @current_step === 2, do: "hidden" %>">
<!-- Step 2 form fields here -->
</div>
<div class="<%= unless @current_step === 3, do: "hidden" %>">
<!-- Step 3 form fields here -->
@f0rest8
f0rest8 / app.js
Last active March 15, 2021 06:14
Phoenix Live View hook example for formatting a phone number.
...
import 'alpinejs' // Optional (we don't actually use alpine in this example)
...
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
...
let Hooks = {}
Hooks.PhoneNumber = {
mounted() {
def render(assigns) do
~H"""
<.form let={f} for={@changeset} url="#" phx_change="validate" phx_submit="save">
<div class={unless @current_step === 1, do: "hidden"}>
<!-- Step 1 form fields here -->
</div>
<div class={unless @current_step === 2, do: "hidden"}>
<!-- Step 2 form fields here -->
</div>