Skip to content

Instantly share code, notes, and snippets.

@shiftyp
Last active August 22, 2020 17:01
Show Gist options
  • Select an option

  • Save shiftyp/1a4fa5e28343b2aedee7a93c355b41c1 to your computer and use it in GitHub Desktop.

Select an option

Save shiftyp/1a4fa5e28343b2aedee7a93c355b41c1 to your computer and use it in GitHub Desktop.
objects 0.2.0 example
import React from 'react'
import { useCreate, useWatch } from '@objects/hooks'
import { map, filter, dedupe } from '@objects/operators'
interface Search {
title: string
}
interface Data {
results: string[]
}
export const MovieSearch = () => {
const [search] = useCreate(() => {
title: '',
} as Search)
const movies = useWatch(search).title(
through(
map((title: string) => title.trim())
filter((title: string) => title.length),
dedupe(),
map((title: string) =>
fetch(`/api/movies?title=${title}`)
.then(resp => resp.json()) as Promise<Data>
)
map((data: Data) => data.results)
),
[]
)
return (
<>
<input
placeholder="Search for movies!"
value={search.title}
onChange={(e) => {
search.title = e.target.value
}}
/>
<ul>
{
movies.length ?
movies.map(movie => <li>{movie}</li>) :
<li>No Results</li>
}
</ul>
</>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment