Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Dropdown} from "semantic-ui-react";
  2. import React, {useContext, useEffect, useState} from "react";
  3. import {AuthContext} from "../../services/auth";
  4.  
  5.  
  6. export default function UserSearch(props) {
  7.     const [options, setOptions] = useState(
  8.     );
  9.         let userContext = useContext(AuthContext);
  10.  
  11.     return (
  12.  
  13.         <Dropdown
  14.             icon={null}
  15.             placeholder="Enter username"
  16.             search
  17.             selection
  18.             onChange={(e, data) => {
  19.                 props.history.push('/');
  20.                 setTimeout(() => {
  21.                     props.history.push(`/${data.value}`)
  22.                 }, 100);
  23.  
  24.             }}
  25.             onSearchChange={async (e) => {
  26.                 const jwt = userContext.getJwt();
  27.                 let results = await fetch(`/api/v1/users/search?search=${e.target.value}`, {
  28.                     headers: {
  29.  
  30.  
  31.                         Authorization: `Bearer ${jwt}`,
  32.  
  33.                     }
  34.                 });
  35.                 let searched_users = await results.json();
  36.                 let opts = Object.values(searched_users).map((user, index) => {
  37.                     return (
  38.                         {
  39.                             key: index, value: user.username, text: user.username, image: {
  40.                                 avatar: true,
  41.                                 src: user.ava_url,
  42.                             }
  43.                         }
  44.                     )
  45.                 });
  46.                 console.log('options: ', opts);
  47.                 setOptions(Object.values(opts));
  48.  
  49.             }}
  50.                 options={options}
  51.                 />
  52.                 )
  53.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement