DataTable refactor.

This commit is contained in:
Mike Cao 2023-09-22 00:59:00 -07:00
parent 92ccc64e47
commit 6846355c63
17 changed files with 94 additions and 114 deletions

View File

@ -25,13 +25,13 @@ export function DataTable({
const { page, pageSize, count } = pageInfo || {}; const { page, pageSize, count } = pageInfo || {};
const noResults = Boolean(query && data?.length === 0); const noResults = Boolean(query && data?.length === 0);
const handleChange = () => { const handleChange = value => {
onChange?.({ query, page }); onChange?.({ query: value, page });
}; };
const handleSearch = value => { const handleSearch = value => {
setQuery(value); setQuery(value);
handleChange(); handleChange(value);
}; };
const handlePageChange = page => { const handlePageChange = page => {

View File

@ -7,7 +7,7 @@ import useMessages from 'components/hooks/useMessages';
import useUser from 'components/hooks/useUser'; import useUser from 'components/hooks/useUser';
import { ROLES } from 'lib/constants'; import { ROLES } from 'lib/constants';
import { Button, Icon, Icons, Modal, ModalTrigger, Text, useToasts } from 'react-basics'; import { Button, Icon, Icons, Modal, ModalTrigger, Text, useToasts } from 'react-basics';
import { useState } from 'react'; import { useRef, useState } from 'react';
export function WebsitesList({ export function WebsitesList({
showTeam, showTeam,
@ -20,16 +20,20 @@ export function WebsitesList({
const { user } = useUser(); const { user } = useUser();
const [params, setParams] = useState({}); const [params, setParams] = useState({});
const { get, useQuery } = useApi(); const { get, useQuery } = useApi();
const { data, isLoading, error, refetch } = useQuery( const count = useRef(0);
['websites', includeTeams, onlyTeams], const q = useQuery(
() => ['websites', includeTeams, onlyTeams, params],
get(`/users/${user?.id}/websites`, { () => {
count.current += 1;
return get(`/users/${user?.id}/websites`, {
includeTeams, includeTeams,
onlyTeams, onlyTeams,
...params, ...params,
}), });
},
{ enabled: !!user }, { enabled: !!user },
); );
const { data, refetch, isLoading, error } = q;
const { showToast } = useToasts(); const { showToast } = useToasts();
const handleChange = params => { const handleChange = params => {
@ -60,10 +64,10 @@ export function WebsitesList({
); );
return ( return (
<Page loading={isLoading} error={error}> <Page loading={isLoading && count.current === 0} error={error}>
{showHeader && <PageHeader title={formatMessage(labels.websites)}>{addButton}</PageHeader>} {showHeader && <PageHeader title={formatMessage(labels.websites)}>{addButton}</PageHeader>}
<WebsitesTable <WebsitesTable
data={data} data={data?.data}
showTeam={showTeam} showTeam={showTeam}
showEditButton={showEditButton} showEditButton={showEditButton}
onChange={handleChange} onChange={handleChange}

View File

@ -8,7 +8,6 @@ import DataTable, { DataTableStyles } from 'components/common/DataTable';
export function WebsitesTable({ export function WebsitesTable({
data = [], data = [],
filterValue,
showTeam, showTeam,
showEditButton, showEditButton,
openExternal = false, openExternal = false,
@ -17,12 +16,12 @@ export function WebsitesTable({
const { formatMessage, labels } = useMessages(); const { formatMessage, labels } = useMessages();
const { user } = useUser(); const { user } = useUser();
const showTable = data && (filterValue || data?.data?.length !== 0); const showTable = data.length !== 0;
return ( return (
<DataTable onChange={onChange}> <DataTable onChange={onChange}>
{showTable && ( {showTable && (
<GridTable data={data?.data}> <GridTable data={data}>
<GridColumn name="name" label={formatMessage(labels.name)} /> <GridColumn name="name" label={formatMessage(labels.name)} />
<GridColumn name="domain" label={formatMessage(labels.domain)} /> <GridColumn name="domain" label={formatMessage(labels.domain)} />
{showTeam && ( {showTeam && (

13
src/lib/schema.ts Normal file
View File

@ -0,0 +1,13 @@
import * as yup from 'yup';
export const dateRange = {
startAt: yup.number().integer().required(),
endAt: yup.number().integer().moreThan(yup.ref('startAt')).required(),
};
export const pageInfo = {
query: yup.string(),
page: yup.number().integer().positive(),
pageSize: yup.number().integer().positive().max(200),
orderBy: yup.string(),
};

View File

@ -54,11 +54,11 @@ export interface ReportSearchFilter extends SearchFilter<ReportSearchFilterType>
} }
export interface SearchFilter<T> { export interface SearchFilter<T> {
filter?: string; query?: string;
filterType?: T; page?: number;
pageSize: number; pageSize?: number;
page: number;
orderBy?: string; orderBy?: string;
data?: T;
} }
export interface FilterResult<T> { export interface FilterResult<T> {

View File

@ -1,19 +0,0 @@
import * as yup from 'yup';
export function getDateRangeValidation() {
return {
startAt: yup.number().integer().required(),
endAt: yup.number().integer().moreThan(yup.ref('startAt')).required(),
};
}
// ex: /funnel|insights|retention/i
export function getFilterValidation(matchRegex) {
return {
filter: yup.string(),
filterType: yup.string().matches(matchRegex),
pageSize: yup.number().integer().positive().max(200),
page: yup.number().integer().positive(),
orderBy: yup.string(),
};
}

View File

@ -1,6 +1,6 @@
import { useCors, useValidate } from 'lib/middleware'; import { useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter, TeamSearchFilterType } from 'lib/types'; import { NextApiRequestQueryBody, SearchFilter, TeamSearchFilterType } from 'lib/types';
import { getFilterValidation } from 'lib/yup'; import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { methodNotAllowed } from 'next-basics'; import { methodNotAllowed } from 'next-basics';
import userTeams from 'pages/api/users/[id]/teams'; import userTeams from 'pages/api/users/[id]/teams';
@ -12,7 +12,7 @@ export interface MyTeamsRequestQuery extends SearchFilter<TeamSearchFilterType>
const schema = { const schema = {
GET: yup.object().shape({ GET: yup.object().shape({
...getFilterValidation(/All|Name|Owner/i), ...pageInfo,
}), }),
}; };

View File

@ -1,6 +1,6 @@
import { useAuth, useCors, useValidate } from 'lib/middleware'; import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter, WebsiteSearchFilterType } from 'lib/types'; import { NextApiRequestQueryBody, SearchFilter, WebsiteSearchFilterType } from 'lib/types';
import { getFilterValidation } from 'lib/yup'; import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { methodNotAllowed } from 'next-basics'; import { methodNotAllowed } from 'next-basics';
import userWebsites from 'pages/api/users/[id]/websites'; import userWebsites from 'pages/api/users/[id]/websites';
@ -12,7 +12,7 @@ export interface MyWebsitesRequestQuery extends SearchFilter<WebsiteSearchFilter
const schema = { const schema = {
GET: yup.object().shape({ GET: yup.object().shape({
...getFilterValidation(/All|Name|Domain/i), ...pageInfo,
}), }),
}; };

View File

@ -1,7 +1,7 @@
import { uuid } from 'lib/crypto'; import { uuid } from 'lib/crypto';
import { useAuth, useCors, useValidate } from 'lib/middleware'; import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, ReportSearchFilterType, SearchFilter } from 'lib/types'; import { NextApiRequestQueryBody, ReportSearchFilterType, SearchFilter } from 'lib/types';
import { getFilterValidation } from 'lib/yup'; import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { methodNotAllowed, ok } from 'next-basics'; import { methodNotAllowed, ok } from 'next-basics';
import { createReport, getReportsByUserId } from 'queries'; import { createReport, getReportsByUserId } from 'queries';
@ -21,7 +21,7 @@ export interface ReportRequestBody {
const schema = { const schema = {
GET: yup.object().shape({ GET: yup.object().shape({
...getFilterValidation(/All|Name|Description|Type|Username|Website Name|Website Domain/i), ...pageInfo,
}), }),
POST: yup.object().shape({ POST: yup.object().shape({
websiteId: yup.string().uuid().required(), websiteId: yup.string().uuid().required(),

View File

@ -1,7 +1,8 @@
import * as yup from 'yup';
import { canViewTeam } from 'lib/auth'; import { canViewTeam } from 'lib/auth';
import { useAuth, useValidate } from 'lib/middleware'; import { useAuth, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter, WebsiteSearchFilterType } from 'lib/types'; import { NextApiRequestQueryBody, SearchFilter, WebsiteSearchFilterType } from 'lib/types';
import { getFilterValidation } from 'lib/yup'; import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics'; import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getWebsitesByTeamId } from 'queries'; import { getWebsitesByTeamId } from 'queries';
@ -15,12 +16,10 @@ export interface TeamWebsiteRequestBody {
websiteIds?: string[]; websiteIds?: string[];
} }
import * as yup from 'yup';
const schema = { const schema = {
GET: yup.object().shape({ GET: yup.object().shape({
id: yup.string().uuid().required(), id: yup.string().uuid().required(),
...getFilterValidation(/All|Name|Domain/i), ...pageInfo,
}), }),
POST: yup.object().shape({ POST: yup.object().shape({
id: yup.string().uuid().required(), id: yup.string().uuid().required(),

View File

@ -3,7 +3,7 @@ import { canCreateTeam } from 'lib/auth';
import { uuid } from 'lib/crypto'; import { uuid } from 'lib/crypto';
import { useAuth, useValidate } from 'lib/middleware'; import { useAuth, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter, TeamSearchFilterType } from 'lib/types'; import { NextApiRequestQueryBody, SearchFilter, TeamSearchFilterType } from 'lib/types';
import { getFilterValidation } from 'lib/yup'; import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics'; import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createTeam, getTeamsByUserId } from 'queries'; import { createTeam, getTeamsByUserId } from 'queries';
@ -18,7 +18,7 @@ export interface MyTeamsRequestQuery extends SearchFilter<TeamSearchFilterType>
const schema = { const schema = {
GET: yup.object().shape({ GET: yup.object().shape({
...getFilterValidation(/All|Name|Owner/i), ...pageInfo,
}), }),
POST: yup.object().shape({ POST: yup.object().shape({
name: yup.string().max(50).required(), name: yup.string().max(50).required(),
@ -39,12 +39,11 @@ export default async (
} = req.auth; } = req.auth;
if (req.method === 'GET') { if (req.method === 'GET') {
const { page, filter, pageSize } = req.query; const { page, query } = req.query;
const results = await getTeamsByUserId(userId, { const results = await getTeamsByUserId(userId, {
page, page,
filter, query,
pageSize: +pageSize || undefined,
}); });
return ok(res, results); return ok(res, results);

View File

@ -1,10 +1,11 @@
import * as yup from 'yup';
import { useAuth, useCors, useValidate } from 'lib/middleware'; import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter, TeamSearchFilterType } from 'lib/types'; import { NextApiRequestQueryBody, SearchFilter, TeamSearchFilterType } from 'lib/types';
import { getFilterValidation } from 'lib/yup'; import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics'; import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getTeamsByUserId } from 'queries'; import { getTeamsByUserId } from 'queries';
import * as yup from 'yup';
export interface UserTeamsRequestQuery extends SearchFilter<TeamSearchFilterType> { export interface UserTeamsRequestQuery extends SearchFilter<TeamSearchFilterType> {
id: string; id: string;
} }
@ -18,7 +19,7 @@ export interface UserTeamsRequestBody {
const schema = { const schema = {
GET: yup.object().shape({ GET: yup.object().shape({
id: yup.string().uuid().required(), id: yup.string().uuid().required(),
...getFilterValidation('/All|Name|Owner/i'), ...pageInfo,
}), }),
}; };
@ -40,12 +41,12 @@ export default async (
return unauthorized(res); return unauthorized(res);
} }
const { page, filter, pageSize } = req.query; const { page, query, pageSize } = req.query;
const teams = await getTeamsByUserId(userId, { const teams = await getTeamsByUserId(userId, {
query,
page, page,
filter, pageSize,
pageSize: +pageSize || undefined,
}); });
return ok(res, teams); return ok(res, teams);

View File

@ -1,6 +1,6 @@
import { useAuth, useCors, useValidate } from 'lib/middleware'; import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter, WebsiteSearchFilterType } from 'lib/types'; import { NextApiRequestQueryBody, SearchFilter, WebsiteSearchFilterType } from 'lib/types';
import { getFilterValidation } from 'lib/yup'; import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics'; import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getWebsitesByUserId } from 'queries'; import { getWebsitesByUserId } from 'queries';
@ -17,7 +17,7 @@ const schema = {
id: yup.string().uuid().required(), id: yup.string().uuid().required(),
includeTeams: yup.boolean(), includeTeams: yup.boolean(),
onlyTeams: yup.boolean(), onlyTeams: yup.boolean(),
...getFilterValidation(/All|Name|Domain/i), ...pageInfo,
}), }),
}; };
@ -32,7 +32,7 @@ export default async (
await useValidate(req, res); await useValidate(req, res);
const { user } = req.auth; const { user } = req.auth;
const { id: userId, page, filter, pageSize, includeTeams, onlyTeams } = req.query; const { id: userId, page, pageSize, query, includeTeams, onlyTeams } = req.query;
if (req.method === 'GET') { if (req.method === 'GET') {
if (!user.isAdmin && user.id !== userId) { if (!user.isAdmin && user.id !== userId) {
@ -40,8 +40,8 @@ export default async (
} }
const websites = await getWebsitesByUserId(userId, { const websites = await getWebsitesByUserId(userId, {
query,
page, page,
filter,
pageSize: +pageSize || undefined, pageSize: +pageSize || undefined,
includeTeams, includeTeams,
onlyTeams, onlyTeams,

View File

@ -3,7 +3,7 @@ import { ROLES } from 'lib/constants';
import { uuid } from 'lib/crypto'; import { uuid } from 'lib/crypto';
import { useAuth, useValidate } from 'lib/middleware'; import { useAuth, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, Role, SearchFilter, User, UserSearchFilterType } from 'lib/types'; import { NextApiRequestQueryBody, Role, SearchFilter, User, UserSearchFilterType } from 'lib/types';
import { getFilterValidation } from 'lib/yup'; import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics'; import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createUser, getUserByUsername, getUsers } from 'queries'; import { createUser, getUserByUsername, getUsers } from 'queries';
@ -19,7 +19,7 @@ export interface UsersRequestBody {
import * as yup from 'yup'; import * as yup from 'yup';
const schema = { const schema = {
GET: yup.object().shape({ GET: yup.object().shape({
...getFilterValidation(/All|Username/i), ...pageInfo,
}), }),
POST: yup.object().shape({ POST: yup.object().shape({
username: yup.string().max(255).required(), username: yup.string().max(255).required(),

View File

@ -7,7 +7,7 @@ import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createWebsite } from 'queries'; import { createWebsite } from 'queries';
import userWebsites from 'pages/api/users/[id]/websites'; import userWebsites from 'pages/api/users/[id]/websites';
import * as yup from 'yup'; import * as yup from 'yup';
import { getFilterValidation } from 'lib/yup'; import { pageInfo } from 'lib/schema';
export interface WebsitesRequestQuery extends SearchFilter<WebsiteSearchFilterType> {} export interface WebsitesRequestQuery extends SearchFilter<WebsiteSearchFilterType> {}
@ -19,7 +19,7 @@ export interface WebsitesRequestBody {
const schema = { const schema = {
GET: yup.object().shape({ GET: yup.object().shape({
...getFilterValidation(/All|Name|Domain/i), ...pageInfo,
}), }),
POST: yup.object().shape({ POST: yup.object().shape({
name: yup.string().max(100).required(), name: yup.string().max(100).required(),

View File

@ -1,5 +1,5 @@
import { Prisma, Team } from '@prisma/client'; import { Prisma, Team } from '@prisma/client';
import { ROLES, TEAM_FILTER_TYPES } from 'lib/constants'; import { ROLES } from 'lib/constants';
import { uuid } from 'lib/crypto'; import { uuid } from 'lib/crypto';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { FilterResult, TeamSearchFilter } from 'lib/types'; import { FilterResult, TeamSearchFilter } from 'lib/types';
@ -82,10 +82,10 @@ export async function deleteTeam(
} }
export async function getTeams( export async function getTeams(
TeamSearchFilter: TeamSearchFilter, filters: TeamSearchFilter,
options?: { include?: Prisma.TeamInclude }, options?: { include?: Prisma.TeamInclude },
): Promise<FilterResult<Team[]>> { ): Promise<FilterResult<Team[]>> {
const { userId, filter, filterType = TEAM_FILTER_TYPES.all } = TeamSearchFilter; const { userId, query } = filters;
const mode = prisma.getSearchMode(); const mode = prisma.getSearchMode();
const where: Prisma.TeamWhereInput = { const where: Prisma.TeamWhereInput = {
@ -94,29 +94,24 @@ export async function getTeams(
some: { userId }, some: { userId },
}, },
}), }),
...(filter && { ...(query && {
AND: { AND: {
OR: [ OR: [
{ {
...((filterType === TEAM_FILTER_TYPES.all || filterType === TEAM_FILTER_TYPES.name) && { name: { startsWith: query, ...mode },
name: { startsWith: filter, ...mode },
}),
}, },
{ {
...((filterType === TEAM_FILTER_TYPES.all || teamUser: {
filterType === TEAM_FILTER_TYPES['user:username']) && { some: {
teamUser: { role: ROLES.teamOwner,
some: { user: {
role: ROLES.teamOwner, username: {
user: { startsWith: query,
username: { ...mode,
startsWith: filter,
...mode,
},
}, },
}, },
}, },
}), },
}, },
], ],
}, },
@ -125,7 +120,7 @@ export async function getTeams(
const [pageFilters, getParameters] = prisma.getPageFilters({ const [pageFilters, getParameters] = prisma.getPageFilters({
orderBy: 'name', orderBy: 'name',
...TeamSearchFilter, ...filters,
}); });
const teams = await prisma.client.team.findMany({ const teams = await prisma.client.team.findMany({

View File

@ -1,6 +1,6 @@
import { Prisma, Website } from '@prisma/client'; import { Prisma, Website } from '@prisma/client';
import cache from 'lib/cache'; import cache from 'lib/cache';
import { ROLES, WEBSITE_FILTER_TYPES } from 'lib/constants'; import { ROLES } from 'lib/constants';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { FilterResult, WebsiteSearchFilter } from 'lib/types'; import { FilterResult, WebsiteSearchFilter } from 'lib/types';
@ -19,17 +19,10 @@ export async function getWebsiteByShareId(shareId: string) {
} }
export async function getWebsites( export async function getWebsites(
WebsiteSearchFilter: WebsiteSearchFilter, filters: WebsiteSearchFilter,
options?: { include?: Prisma.WebsiteInclude }, options?: { include?: Prisma.WebsiteInclude },
): Promise<FilterResult<Website[]>> { ): Promise<FilterResult<Website[]>> {
const { const { userId, teamId, includeTeams, onlyTeams, query } = filters;
userId,
teamId,
includeTeams,
onlyTeams,
filter,
filterType = WEBSITE_FILTER_TYPES.all,
} = WebsiteSearchFilter;
const mode = prisma.getSearchMode(); const mode = prisma.getSearchMode();
const where: Prisma.WebsiteWhereInput = { const where: Prisma.WebsiteWhereInput = {
@ -76,27 +69,23 @@ export async function getWebsites(
], ],
}, },
{ {
OR: [ OR: query
{ ? [
...((filterType === WEBSITE_FILTER_TYPES.all || {
filterType === WEBSITE_FILTER_TYPES.name) && { name: { startsWith: query, ...mode },
name: { startsWith: filter, ...mode }, },
}), {
}, domain: { startsWith: query, ...mode },
{ },
...((filterType === WEBSITE_FILTER_TYPES.all || ]
filterType === WEBSITE_FILTER_TYPES.domain) && { : [],
domain: { startsWith: filter, ...mode },
}),
},
],
}, },
], ],
}; };
const [pageFilters, getParameters] = prisma.getPageFilters({ const [pageFilters, getParameters] = prisma.getPageFilters({
orderBy: 'name', orderBy: 'name',
...WebsiteSearchFilter, ...filters,
}); });
const websites = await prisma.client.website.findMany({ const websites = await prisma.client.website.findMany({
@ -115,10 +104,10 @@ export async function getWebsites(
export async function getWebsitesByUserId( export async function getWebsitesByUserId(
userId: string, userId: string,
filter?: WebsiteSearchFilter, filters?: WebsiteSearchFilter,
): Promise<FilterResult<Website[]>> { ): Promise<FilterResult<Website[]>> {
return getWebsites( return getWebsites(
{ userId, ...filter }, { userId, ...filters },
{ {
include: { include: {
teamWebsite: { teamWebsite: {
@ -143,12 +132,12 @@ export async function getWebsitesByUserId(
export async function getWebsitesByTeamId( export async function getWebsitesByTeamId(
teamId: string, teamId: string,
filter?: WebsiteSearchFilter, filters?: WebsiteSearchFilter,
): Promise<FilterResult<Website[]>> { ): Promise<FilterResult<Website[]>> {
return getWebsites( return getWebsites(
{ {
teamId, teamId,
...filter, ...filters,
includeTeams: true, includeTeams: true,
}, },
{ {