Last active
August 22, 2020 17:01
-
-
Save shiftyp/1a4fa5e28343b2aedee7a93c355b41c1 to your computer and use it in GitHub Desktop.
objects 0.2.0 example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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