Skip to content

Commit

Permalink
example: typecheck.
Browse files Browse the repository at this point in the history
  • Loading branch information
thruflo committed Nov 22, 2024
1 parent eb2081f commit 7472cf0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
9 changes: 5 additions & 4 deletions examples/write-patterns/patterns/1-online-writes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react'
import { useShape } from '@electric-sql/react'

import * as client from '../../shared/app/client'
Expand Down Expand Up @@ -27,22 +28,22 @@ export default function OnlineWrites() {
? data.sort((a, b) => a.created_at - b.created_at)
: []

async function handleChange(id, completed) {
async function handleChange(id: string, completed: boolean) {
await client.updateTodo(id, {completed: !completed})
}

async function handleDelete(event, id) {
async function handleDelete(event: React.MouseEvent, id: string) {
event.preventDefault()

await client.deleteTodo(id)
}

async function handleSubmit(event) {
async function handleSubmit(event: React.FormEvent) {
event.preventDefault()

const form = event.target as HTMLFormElement
const formData = new FormData(form)
const title = formData.get('todo')
const title = formData.get('todo') as string

await client.createTodo(title)

Expand Down
16 changes: 11 additions & 5 deletions examples/write-patterns/shared/app/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import { v4 as uuidv4 } from 'uuid'

const API_URL = import.meta.env.API_URL || 'http://localhost:3001'

async function request(url, method, data) {
const options = {
type RequestOptions = {
method: string
headers: HeadersInit
body?: string
}

async function request(url: string, method: string, data?: object) {
const options: RequestOptions = {
method: method,
headers: {
'Content-Type': `application/json`
Expand All @@ -17,7 +23,7 @@ async function request(url, method, data) {
return await fetch(url, options)
}

export async function createTodo(title) {
export async function createTodo(title: string) {
const url = `${API_URL}/todos`
const data = {
id: uuidv4(),
Expand All @@ -27,13 +33,13 @@ export async function createTodo(title) {
return await request(url, 'POST', data)
}

export async function updateTodo(id, data) {
export async function updateTodo(id: string, data: object) {
const url = `${API_URL}/todos/${id}`

return await request(url, 'PUT', data)
}

export async function deleteTodo(id) {
export async function deleteTodo(id: string) {
const url = `${API_URL}/todos/${id}`

return await request(url, 'DELETE')
Expand Down
2 changes: 2 additions & 0 deletions examples/write-patterns/shared/app/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="vite/client" />
/// <reference types="vite/types/importMeta.d.ts" />

0 comments on commit 7472cf0

Please sign in to comment.