Skip to content

Commit

Permalink
use api for search ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
mnsrulz committed Apr 13, 2024
1 parent 7fca43e commit e6b0bd0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/components/ticker-search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface ITickerProps {
}

export const TickerSearch = (props: ITickerProps) => {
const [options, setOptions] = useState<any>([]);
const [options, setOptions] = useState<SearchTickerItem[]>([]);
const onInputChange = (ev: any, value: any, reason: any) => {
if (value) {
getData(value);
Expand All @@ -23,7 +23,7 @@ export const TickerSearch = (props: ITickerProps) => {

const getData = async (searchTerm: string) => {
const result = await searchTicker(searchTerm);
setOptions(result.items);
setOptions(result);
};

return <Autocomplete filterOptions={(x) => x}
Expand All @@ -47,6 +47,6 @@ export const TickerSearch = (props: ITickerProps) => {
)}
options={options}
getOptionLabel={(option: SearchTickerItem) => `${option.symbol} - ${option.name}`}
fullWidth
fullWidth
/>
}
36 changes: 27 additions & 9 deletions src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,33 @@ export type SearchTickerResult = { items: SearchTickerItem[] };
export type SearchTickerItem = { symbol: string, name: string }
export type AddTickerToMyListResult = { success: boolean }
export const searchTicker = async (searchTerm: string) => {
return new Promise<SearchTickerResult>((res, rej) => {
socket.emit('stock-list-request', {
const { items } = await ky('/api/symbols/search', {
searchParams: {
q: searchTerm
});

socket.once(`stock-list-response`, (args: SearchTickerResult) => {
res(args);
});
});
}
}).json<{
items: {
quotes: {
symbol: string,
longname: string,
isYahooFinance: boolean
}[]
}
}>();

return items.quotes.filter(f => f.isYahooFinance).map(r => ({
symbol: r.symbol,
name: r.longname
}));
// return new Promise<SearchTickerResult>((res, rej) => {
// socket.emit('stock-list-request', {
// q: searchTerm
// });

// socket.once(`stock-list-response`, (args: SearchTickerResult) => {
// res(args);
// });
// });
}

export const AddTickerToMyList = (item: SearchTickerItem) => {
Expand Down Expand Up @@ -103,7 +121,7 @@ export const useOptionTracker = (symbol: string) => {
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
setIsLoading(true);
ky(`/api/symbols/${symbol}/options/analyze`).json<OptionsData>().then(r => setOd(r)).finally(()=> setIsLoading(false));
ky(`/api/symbols/${symbol}/options/analyze`).json<OptionsData>().then(r => setOd(r)).finally(() => setIsLoading(false));
// socket.emit('options-subscribe-request', item);
// socket.on(`options-subscribe-response`, setOd);
// return () => {
Expand Down

0 comments on commit e6b0bd0

Please sign in to comment.