Skip to content

Instantly share code, notes, and snippets.

View ftes's full-sized avatar

Fredrik Teschke ftes

View GitHub Profile
@ftes
ftes / _seating_plan.html.haml
Created November 20, 2019 21:26
Drag and drop in CSS grid with rails 6, stimulus and rails-ujs
-# https://johnbeatty.co/2018/03/09/stimulus-js-tutorial-how-do-i-drag-and-drop-items-in-a-list/
.grid--draggable{ 'data-controller': 'seating-plan',
'data-seating-plan-endpoint': endpoint,
'data-action': 'dragstart->seating-plan#onDragStart dragover->seating-plan#onDragOver dragenter->seating-plan#onDragEnter drop->seating-plan#onDrop dragend->seating-plan#onDragEnd' }
- seating_plan.each do |seat|
- if seat[:is_empty]
.grid__item.grid__item--empty{ 'data-row': seat[:row],
'data-col': seat[:col],
style: "grid-row: #{seat[:row]}; grid-column: #{seat[:col]};",
class: ('grid__item--border' if seat[:is_border]) }
@ftes
ftes / exercises.md
Created March 14, 2023 06:39
Software Developer Bootcamp
@ftes
ftes / dijkstra.livemd
Created December 12, 2022 19:42
Elixir Dijkstra's Shortest Path Algorithm

dijkstra

Mix.install(
  [
    {:kino, "~> 0.7.0"},
    {:vega_lite, "~> 0.1.6"},
    {:kino_vega_lite, "~> 0.1.7"}
@ftes
ftes / FormWebComponent.jsx
Created September 5, 2022 15:29
Phoenix LiveView Form + HeadlessUI
export default class ComboboxFormWebcomponent extends HTMLElement {
connectedCallback() {
// ...
const inputId = this.getAttribute("input-id")
this.inputEl = document.getElementById(inputId)
this.inputEl.addEventListener("change", this.onValueChange)
this.render()
}
disconnectedCallback() {
@ftes
ftes / PushEventHook.js
Created September 5, 2022 07:00
Phoenix LiveView + HeadlessUI
const PushEventHook = {
mounted() {
const target = this.el.attributes["phx-target"]?.value
this.el.__pushEvent = (event, value, onReply = () => {}) =>
target
? this.pushEventTo(target, event, value, onReply)
: this.pushEvent(event, value, onReply)
}
}
@ftes
ftes / _error_messages.html.haml
Last active April 27, 2022 20:42
Rails bulma form builder with errors
-# layouts/_error_messages.html.haml
- if f.object.errors.any?
.notification.is-danger Please review the problems below:
- if f.object.errors[:base].present?
.notification.is-danger= f.object.errors[:base].join(', ')
@ftes
ftes / projection.ex
Created November 5, 2021 09:32
Reset all commanded read model projections via macro and module attribute
defmodule Mixins.Projection do
defmacro __using__(params) do
table = Keyword.get(params, :table) || raise "Missing param :table"
Module.register_attribute(__CALLER__.module, :table, persist: true)
Module.put_attribute(__CALLER__.module, :table, table)
quote do
use Ecto.Schema
end
end
@ftes
ftes / page_live.ex
Last active October 4, 2021 14:18
Drag'n'drop on the PETAL(S) stack
defmodule PetalsDragNDropWeb.PageLive do
# ...
@impl true
def render(assigns) do
~F"""
...
<GridWithDragAndDrop
dragged="letter_dragged"
@ftes
ftes / grid_with_drag_and_drop.html
Last active October 4, 2021 14:17
Drag and drop on the PETAL(S) stack
<div
x-data="{dragging: false}"
:hook="GridWithDragAndDrop"
>
{#for %{x: x, y: y} = item <- @data}
<div
id={item[:id]}
draggable="true"
...
>
@ftes
ftes / grid_with_drag_and_drop.ex
Created October 3, 2021 19:51
Drag 'n drop on the PETAL(S) stack
if @dragged.target == :live_view do
"hook.pushEvent('#{@dragged.name}', {from, to});"
else
"hook.pushEventTo('#{@dragged.target}', '#{@dragged.name}', {from, to});"
end