|
| 1 | +import { queryOptions, useQuery } from '@tanstack/solid-query' |
| 2 | + |
| 3 | +export interface Geo { |
| 4 | + lat: string |
| 5 | + lng: string |
| 6 | +} |
| 7 | + |
| 8 | +export interface Address { |
| 9 | + street: string |
| 10 | + suite: string |
| 11 | + city: string |
| 12 | + zipcode: string |
| 13 | + geo: Geo |
| 14 | +} |
| 15 | + |
| 16 | +export interface Company { |
| 17 | + name: string |
| 18 | + catchPhrase: string |
| 19 | + bs: string |
| 20 | +} |
| 21 | + |
| 22 | +export interface User { |
| 23 | + id: number |
| 24 | + name: string |
| 25 | + username: string |
| 26 | + email: string |
| 27 | + address: Address |
| 28 | + phone: string |
| 29 | + website: string |
| 30 | + company: Company |
| 31 | +} |
| 32 | + |
| 33 | +export const getUsers = async (): Promise<Array<User>> => { |
| 34 | + const response = await fetch('https://jsonplaceholder.typicode.com/users') |
| 35 | + const users: Array<User> = await response.json() |
| 36 | + return users |
| 37 | +} |
| 38 | + |
| 39 | +export const searchUsers = async (search: string): Promise<Array<User>> => { |
| 40 | + const users = await getUsers() |
| 41 | + const normalizedSearch = search.toLowerCase() |
| 42 | + return users.filter( |
| 43 | + (user) => |
| 44 | + user.name.toLowerCase().includes(normalizedSearch) || |
| 45 | + user.username.toLowerCase().includes(normalizedSearch), |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +export const usersQueryOptions = (search: string) => |
| 50 | + queryOptions({ |
| 51 | + queryKey: ['users', search], |
| 52 | + queryFn: async () => { |
| 53 | + return await searchUsers(search) |
| 54 | + }, |
| 55 | + }) |
| 56 | + |
| 57 | +export interface UsersProps { |
| 58 | + search: string |
| 59 | +} |
| 60 | + |
| 61 | +export const Users = ({ search }: UsersProps) => { |
| 62 | + const users = useQuery(() => usersQueryOptions(search)) |
| 63 | + |
| 64 | + return ( |
| 65 | + <table class="table-auto"> |
| 66 | + <thead> |
| 67 | + <tr class="border text-left"> |
| 68 | + <th class="p-4">Username</th> |
| 69 | + <th class="p-4">Name</th> |
| 70 | + <th class="p-4">Email</th> |
| 71 | + </tr> |
| 72 | + </thead> |
| 73 | + <tbody> |
| 74 | + {users.data?.map((user) => ( |
| 75 | + <tr class="border"> |
| 76 | + <td class="p-4">{user.username}</td> |
| 77 | + <td class="p-4">{user.name}</td> |
| 78 | + <td class="p-4">{user.email}</td> |
| 79 | + </tr> |
| 80 | + ))} |
| 81 | + </tbody> |
| 82 | + </table> |
| 83 | + ) |
| 84 | +} |
0 commit comments