Skip to content

Commit

Permalink
render sort by in the header
Browse files Browse the repository at this point in the history
  • Loading branch information
mnsrulz committed Sep 26, 2024
1 parent 2f1b6e8 commit 5095ab2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@mui/x-date-pickers": "^7.3.2",
"@prisma/client": "^5.12.1",
"@uidotdev/usehooks": "^2.4.1",
"collect.js": "^4.36.1",
"dayjs": "^1.11.10",
"he": "^1.2.0",
"human-format": "^1.2.0",
Expand Down
35 changes: 28 additions & 7 deletions src/components/Watchlist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as React from 'react';
import DeleteIcon from '@mui/icons-material/Delete';
import InfoIcon from '@mui/icons-material/Info';
import { Button, Dialog, DialogContent, DialogTitle } from '@mui/material';
import { Button, Dialog, DialogContent, DialogTitle, FormControl, InputLabel, MenuItem, Select } from '@mui/material';
import { DataGrid, GridActionsCellItem, GridColDef, useGridApiRef } from '@mui/x-data-grid';
import { useEffect, useState } from 'react';
import { StockTickerSymbolView, StockTickerView } from './StockTicker';
Expand All @@ -13,7 +13,7 @@ import { SearchTickerItem } from '@/lib/types';
import { TickerSearch } from './TickerSearch';
import { TradingViewWidgetDialog } from './TradingViewWidgetDialog';
import { subscribeStockPriceBatchRequest } from '@/lib/socket';

import collect from 'collect.js';

interface IWatchlistProps {
tickers: SearchTickerItem[]
Expand All @@ -26,13 +26,14 @@ export const Watchlist = (props: IWatchlistProps) => {
const { tickers, removFromWatchlist, loading, addToWatchlist } = props;
const apiRef = useGridApiRef();
const [currentStock, setCurrentStock] = useState<SearchTickerItem | null>(null);
const [sortMode, setSortMode] = useState('symbol');

useEffect(() => {
const interval = setInterval(()=>{
useEffect(() => {
const interval = setInterval(() => {
subscribeStockPriceBatchRequest(tickers);
}, 1000); //every one second just ping the server to resubscribe

return ()=> clearInterval(interval);
return () => clearInterval(interval);
}, [tickers]);

const columns: GridColDef<SearchTickerItem>[] = [
Expand All @@ -53,6 +54,22 @@ export const Watchlist = (props: IWatchlistProps) => {
resizable: false,
field: 'price', headerName: '', headerAlign: 'right', align: 'right', flex: 0.5, renderCell: (p) => {
return <StockTickerView item={p.row}></StockTickerView>
}, renderHeader: () => {
return <>Sort <FormControl variant="standard" sx={{ m: 1, minWidth: 120 }} size='small'>
{/* <InputLabel id="sort-by-label">Sort by</InputLabel> */}

<Select
labelId="sort-by-label"
value={sortMode}
onChange={(e) => setSortMode(e.target.value)}
label="Sort by"
size='small'
autoWidth
>
<MenuItem value="symbol">Ticker</MenuItem>
<MenuItem value="name">Name</MenuItem>
</Select>
</FormControl></>
}
},
{
Expand Down Expand Up @@ -107,9 +124,12 @@ export const Watchlist = (props: IWatchlistProps) => {

const handleCloseAddTrade = () => { setOpenAddTrade(false); };
const handleAddToWatchlist = (item: SearchTickerItem) => { addToWatchlist(item); setOpenAddToWatchlist(false); }

return <div>
{/* <Typography variant='body2'>Watchlist</Typography> */}
<DataGrid rows={tickers}


<DataGrid rows={collect(tickers).sortBy(sortMode).all()}
columns={columns}
//sx={{ '& .MuiDataGrid-columnSeparator': { display: 'none' } }}
sx={{ display: 'grid', '& .MuiDataGrid-columnSeparator': { display: 'none' } }}
Expand Down Expand Up @@ -146,4 +166,5 @@ export const Watchlist = (props: IWatchlistProps) => {

{openTradingViewDialog && currentStock?.symbol && <TradingViewWidgetDialog symbol={currentStock.symbol} onClose={() => { setOpenTradingViewDialog(false) }} />}
</div>
}
}

0 comments on commit 5095ab2

Please sign in to comment.