import React, { useLayoutEffect, useMemo, useState } from 'react'; import { FormattedMessage } from 'react-intl'; import Link from 'components/common/Link'; import DateFilter from 'components/common/DateFilter'; import Page from 'components/layout/Page'; import EmptyPlaceholder from 'components/common/EmptyPlaceholder'; import useFetch from 'hooks/useFetch'; import useDateRange from 'hooks/useDateRange'; import useShareToken from 'hooks/useShareToken'; import Arrow from 'assets/arrow-right.svg'; import { get } from 'lib/web'; import { TOKEN_HEADER } from 'lib/constants'; import { useRouter } from 'next/router'; import { useTable, usePagination } from 'react-table'; import find from 'lodash.find'; export default function WebsiteList({ userId }) { const [stats, setStats] = useState([]); const fetchedData = useFetch('/api/websites', { params: { user_id: userId } }); const { basePath } = useRouter(); const shareToken = useShareToken(); const [dateRange, setDateRange] = useDateRange(); const { startDate, endDate, value } = dateRange; const websitesIds = useMemo(() => { if (!fetchedData.data) return []; return fetchedData.data.map(site => site.website_id); }, [fetchedData.data]); const tableData = useMemo(() => { if (!fetchedData.data || !stats.length) return []; const _data = []; fetchedData.data.forEach(i => { const stat = find(stats, { id: i.website_id }) || {}; _data.push({ ...i, ...stat.data }); }); return _data; }, [fetchedData.data, stats.length]); const getStats = async () => { const websitesData = []; for (let id of websitesIds) { const url = `/api/website/${id}/stats`; const _data = await get( `${basePath}${url}`, { start_at: +startDate, end_at: +endDate, url, }, { [TOKEN_HEADER]: shareToken?.token, }, ); websitesData.push({ data: _data.data, id }); } return Promise.all(websitesData).then(res => { setStats(res); }); }; useLayoutEffect(() => { getStats(); }, [fetchedData.data, stats.length]); const tableColumns = useMemo( () => [ { Header: 'Name', accessor: 'name', }, { Header: 'Domain', accessor: 'domain', }, { Header: 'Views', accessor: 'pageviews', }, { Header: 'Visitors', accessor: 'uniques', }, { Header: 'Bounce rate', accessor: 'bounces', }, { Header: 'Details', accessor: 'details', Cell: ({ website_id }) => ( } size="small" iconRight > Details ), }, ], [], ); const { getTableProps, getTableBodyProps, headerGroups, page, prepareRow, canPreviousPage, canNextPage, pageOptions, pageCount, gotoPage, nextPage, previousPage, setPageSize, state: { pageIndex, pageSize }, } = useTable( { columns: tableColumns, data: tableData, initialState: { pageIndex: 0, pageSize: 10 }, }, usePagination, ); return ( {headerGroups.map(headerGroup => ( {headerGroup.headers.map(column => ( ))} ))} {page.map(row => { prepareRow(row); return ( {row.cells.map(cell => { return ( ); })} ); })}
{column.render('Header')}
{cell.render('Cell', { ...row.original })}
{' '} {' '} {' '} {' '} Page{' '} {pageIndex + 1} of {pageOptions.length} {' '} | Go to page:{' '} { const page = e.target.value ? Number(e.target.value) - 1 : 0; gotoPage(page); }} style={{ width: '100px' }} /> {' '}
{/* */} {/* {data.map(({ website_id, name, domain }) => (
))} */} {fetchedData.data?.length === 0 && ( } > } iconRight> )} ); }