|
| 1 | +import { Component, createSignal, For, Show } from "solid-js"; |
| 2 | +import { createDbStore, supabaseAdapter, DbRow, DbStoreError } from "../src/index.js"; |
| 3 | +import { createClient, SupabaseClient } from "@supabase/supabase-js"; |
| 4 | +import { reconcile } from "solid-js/store"; |
| 5 | + |
| 6 | +const TodoList = (props: { client: SupabaseClient }) => { |
| 7 | + const [error, setError] = createSignal<DbStoreError<DbRow>>(); |
| 8 | + const [todos, setTodos] = createDbStore({ |
| 9 | + adapter: supabaseAdapter({ client: props.client, table: "todos" }), |
| 10 | + onError: setError, |
| 11 | + }); |
| 12 | + const [edit, setEdit] = createSignal<DbRow>(); |
| 13 | + const done = (task: DbRow) => setTodos(reconcile(todos.filter(todo => todo !== task))); |
| 14 | + const add = (task: string) => setTodos(todos.length, { task }); |
| 15 | + return ( |
| 16 | + <> |
| 17 | + <Show when={error()} keyed> |
| 18 | + {(err) => <p class="text-red-600">{`Error: ${err.message} Cause: ${err.cause} Action: ${err.action} Data: ${JSON.stringify(err.data)}`} ${err.server ? 'server' : 'client'} <button onClick={() => setError()}>ok</button></p>} |
| 19 | + </Show> |
| 20 | + <ul> |
| 21 | + <For |
| 22 | + each={Array.from(todos).toSorted((a, b) => Number(a.id) - Number(b.id))} |
| 23 | + fallback={<li>No to-dos yet</li>} |
| 24 | + > |
| 25 | + {item => ( |
| 26 | + <li class="flex flex-row items-center" data-id={item.id}> |
| 27 | + <Show when={item === edit()} fallback={<span onClick={() => setEdit(item)}>{item.task}</span>}> |
| 28 | + <input name="update" value={item.task} /><button onClick={() => { |
| 29 | + const updated = (document.querySelector('[name="update"]') as HTMLInputElement).value; |
| 30 | + const index = todos.indexOf(item); |
| 31 | + if (updated && index > -1) setTodos(index, 'task', updated); |
| 32 | + setEdit(undefined); |
| 33 | + }}>update</button><button onClick={() => setEdit(undefined)}>cancel</button> |
| 34 | + </Show>{" "} |
| 35 | + <span role="button" class="p-2" onClick={() => done(item)} title="Done"> |
| 36 | + x |
| 37 | + </span> |
| 38 | + </li> |
| 39 | + )} |
| 40 | + </For> |
| 41 | + <li class="flex flex-row items-center"> |
| 42 | + <label> |
| 43 | + new task: <input name="task" /> |
| 44 | + </label>{" "} |
| 45 | + <button |
| 46 | + onClick={() => { |
| 47 | + const task = (document.querySelector('[name="task"]') as HTMLInputElement).value; |
| 48 | + if (task) { |
| 49 | + add(task); |
| 50 | + (document.querySelector('[name="task"]') as HTMLInputElement).value = ""; |
| 51 | + } |
| 52 | + }} |
| 53 | + > |
| 54 | + do it! |
| 55 | + </button> |
| 56 | + </li> |
| 57 | + </ul> |
| 58 | + </> |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +const App: Component = () => { |
| 63 | + const [client, setClient] = createSignal<SupabaseClient<any, "public", any>>(); |
| 64 | + const connect = () => { |
| 65 | + const url = (document.querySelector('[type="url"]') as HTMLInputElement | null)?.value; |
| 66 | + const key = (document.querySelector('[type="password"]') as HTMLInputElement | null)?.value; |
| 67 | + url && key && setClient(createClient(url, key)); |
| 68 | + }; |
| 69 | + |
| 70 | + return ( |
| 71 | + <div class="box-border flex min-h-screen w-full flex-col items-center justify-center space-y-4 bg-gray-800 p-24 text-white"> |
| 72 | + <div class="wrapper-v"> |
| 73 | + <h4>db-store-backed to-do list</h4> |
| 74 | + <Show |
| 75 | + when={client()} |
| 76 | + keyed |
| 77 | + fallback={ |
| 78 | + <> |
| 79 | + <details> |
| 80 | + <summary>To configure your own database,</summary> |
| 81 | + <ul class="list-disc"> |
| 82 | + <li> |
| 83 | + Register with <a href="https://supabase.com">Supabase</a>. |
| 84 | + </li> |
| 85 | + <li> |
| 86 | + Create a new database and note down the url and the key (that usually go into an |
| 87 | + environment) |
| 88 | + </li> |
| 89 | + <li>Within the database, create a table and configure it to be public, promote changes in realtime and has no row protection: |
| 90 | + |
| 91 | + <pre><code>{ |
| 92 | +`-- Create table |
| 93 | +create table todos ( |
| 94 | + id serial primary key, |
| 95 | + task text |
| 96 | +); |
| 97 | +-- Turn off row-level security |
| 98 | +alter table "todos" |
| 99 | +disable row level security; |
| 100 | +-- Allow anonymous access |
| 101 | +create policy "Allow anonymous access" |
| 102 | +on todos |
| 103 | +for select |
| 104 | +to anon |
| 105 | +using (true);` |
| 106 | + }</code></pre> |
| 107 | + </li> |
| 108 | + <li>Fill in the url and key in the fields below and press "connect".</li> |
| 109 | + </ul> |
| 110 | + </details> |
| 111 | + <p> |
| 112 | + <label> |
| 113 | + DB |
| 114 | + <select> |
| 115 | + <option value="supabase">SupaBase</option> |
| 116 | + </select> |
| 117 | + </label> |
| 118 | + </p> |
| 119 | + <p> |
| 120 | + <label> |
| 121 | + Client URL <input type="url" /> |
| 122 | + </label> |
| 123 | + </p> |
| 124 | + <p> |
| 125 | + <label> |
| 126 | + Client Key <input type="password" /> |
| 127 | + </label> |
| 128 | + </p> |
| 129 | + <button class="btn" onClick={connect}> |
| 130 | + Connect |
| 131 | + </button> |
| 132 | + </> |
| 133 | + } |
| 134 | + > |
| 135 | + {(client: SupabaseClient) => <TodoList client={client} />} |
| 136 | + </Show> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + ); |
| 140 | +}; |
| 141 | + |
| 142 | +export default App; |
0 commit comments