From a4ddc534c76af4fd8895f8b5350e39294e378e52 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Fri, 17 Apr 2020 19:51:41 -0300 Subject: [PATCH] added pagination on import page --- .../client/components/ImportHistoryPage.js | 19 +- .../components/ImportOperationSummary.js | 29 +- .../client/components/PrepareImportPage.js | 265 ++++++++++-------- 3 files changed, 180 insertions(+), 133 deletions(-) diff --git a/app/importer/client/components/ImportHistoryPage.js b/app/importer/client/components/ImportHistoryPage.js index 4788ebe89b3d8..60b5051ac1edd 100644 --- a/app/importer/client/components/ImportHistoryPage.js +++ b/app/importer/client/components/ImportHistoryPage.js @@ -9,6 +9,7 @@ import { useEndpoint } from '../../../../client/contexts/ServerContext'; import { ProgressStep } from '../../lib/ImporterProgressStep'; import ImportOperationSummary from './ImportOperationSummary'; import { useSafely } from '../../../../client/hooks/useSafely'; +import { useMediaQuery } from '../../../ui/client/views/app/components/hooks'; function ImportHistoryPage() { const t = useTranslation(); @@ -76,6 +77,8 @@ function ImportHistoryPage() { } }; + const small = useMediaQuery('(max-width: 768px)'); + return @@ -90,26 +93,26 @@ function ImportHistoryPage() { {t('Import_Type')} {t('Last_Updated')} - {t('Last_Status')} - {t('File')} - {t('Counters')} + {!small && <>{t('Last_Status')} + {t('File')} + {t('Counters')}} - + {!small && {t('Users')} {t('Channels')} {t('Messages')} {t('Total')} - + } {isLoading - ? Array.from({ length: 20 }, (_, i) => ) + ? Array.from({ length: 20 }, (_, i) => ) : <> - {currentOperation?.valid && } + {currentOperation?.valid && } {latestOperations ?.filter(({ _id }) => currentOperation?._id !== _id || !currentOperation?.valid) // Forcing valid=false as the current API only accept preparation/progress over currentOperation - ?.map((operation) => )} + ?.map((operation) => )} } diff --git a/app/importer/client/components/ImportOperationSummary.js b/app/importer/client/components/ImportOperationSummary.js index c1e8463ce12fd..c940aef934c91 100644 --- a/app/importer/client/components/ImportOperationSummary.js +++ b/app/importer/client/components/ImportOperationSummary.js @@ -18,6 +18,7 @@ function ImportOperationSummary({ status, file, user, + small, count: { users = 0, channels = 0, @@ -85,25 +86,27 @@ function ImportOperationSummary({ return {type} {formatDateAndTime(_updatedAt)} - {status && t(status.replace('importer_', 'importer_status_'))} - {fileName} - {users} - {channels} - {messages} - {total} + {!small && <>{status && t(status.replace('importer_', 'importer_status_'))} + {fileName} + {users} + {channels} + {messages} + {total} + } ; } -function ImportOperationSummarySkeleton() { +function ImportOperationSummarySkeleton({ small }) { return - - - - - - + {!small && <> + + + + + + } ; } diff --git a/app/importer/client/components/PrepareImportPage.js b/app/importer/client/components/PrepareImportPage.js index f8c45cd4ea4a5..48e05b7c91e57 100644 --- a/app/importer/client/components/PrepareImportPage.js +++ b/app/importer/client/components/PrepareImportPage.js @@ -9,8 +9,10 @@ import { Table, Tag, Throbber, + Pagination, + Tabs, } from '@rocket.chat/fuselage'; -import React, { useEffect, useState, useMemo } from 'react'; +import React, { useEffect, useState, useMemo, useCallback } from 'react'; import s from 'underscore.string'; import { Page } from '../../../../client/components/basic/Page'; @@ -29,6 +31,7 @@ import { showImporterException } from '../functions/showImporterException'; import { useRoute } from '../../../../client/contexts/RouterContext'; import { useSafely } from '../../../../client/hooks/useSafely'; import { useEndpoint } from '../../../../client/contexts/ServerContext'; +import { useDebounce } from '../../../ui/client/views/app/components/hooks'; const waitFor = (fn, predicate) => new Promise((resolve, reject) => { const callPromise = () => { @@ -45,6 +48,138 @@ const waitFor = (fn, predicate) => new Promise((resolve, reject) => { callPromise(); }); +function PrepareUsers({ usersCount, users, setUsers }) { + const t = useTranslation(); + const [current, setCurrent] = useState(0); + const [itemsPerPage, setItemsPerPage] = useState(25); + const showingResultsLabel = useCallback(({ count, current, itemsPerPage }) => t('Showing results %s - %s of %s', current + 1, Math.min(current + itemsPerPage, count), count), []); + const itemsPerPageLabel = useCallback(() => t('Items_per_page:'), []); + + return <> + + + + + 0} + indeterminate={usersCount > 0 && usersCount !== users.length} + onChange={() => { + setUsers((users) => { + const hasCheckedDeletedUsers = users.some(({ is_deleted, do_import }) => is_deleted && do_import); + const isChecking = usersCount === 0; + + if (isChecking) { + return users.map((user) => ({ ...user, do_import: true })); + } + + if (hasCheckedDeletedUsers) { + return users.map((user) => (user.is_deleted ? { ...user, do_import: false } : user)); + } + + return users.map((user) => ({ ...user, do_import: false })); + }); + }} + /> + + {t('Username')} + {t('Email')} + + + + + {users.slice(current, current + itemsPerPage).map((user) => + + { + const { checked } = event.currentTarget; + setUsers((users) => + users.map((_user) => (_user === user ? { ..._user, do_import: checked } : _user))); + }} + /> + + {user.username} + {user.email} + {user.is_deleted && {t('Deleted')}} + )} + +
+ + ; +} + +function PrepareChannels({ channels, channelsCount, setChannels }) { + const t = useTranslation(); + const [current, setCurrent] = useState(0); + const [itemsPerPage, setItemsPerPage] = useState(25); + const showingResultsLabel = useCallback(({ count, current, itemsPerPage }) => t('Showing results %s - %s of %s', current + 1, Math.min(current + itemsPerPage, count), count), []); + const itemsPerPageLabel = useCallback(() => t('Items_per_page:'), []); + + return channels.length && <> + + + + 0} + indeterminate={channelsCount > 0 && channelsCount !== channels.length} + onChange={() => { + setChannels((channels) => { + const hasCheckedArchivedChannels = channels.some(({ is_archived, do_import }) => is_archived && do_import); + const isChecking = channelsCount === 0; + + if (isChecking) { + return channels.map((channel) => ({ ...channel, do_import: true })); + } + + if (hasCheckedArchivedChannels) { + return channels.map((channel) => (channel.is_archived ? { ...channel, do_import: false } : channel)); + } + + return channels.map((channel) => ({ ...channel, do_import: false })); + }); + }} + /> + + {t('Name')} + + + + + {channels.slice(current, current + itemsPerPage).map((channel) => + + { + const { checked } = event.currentTarget; + setChannels((channels) => + channels.map((_channel) => (_channel === channel ? { ..._channel, do_import: checked } : _channel))); + }} + /> + + {channel.name} + {channel.is_archived && {t('Importer_Archived')}} + )} + +
+ ; +} + function PrepareImportPage() { const t = useTranslation(); const dispatchToastMessage = useToastMessageDispatch(); @@ -53,8 +188,8 @@ function PrepareImportPage() { const [progressRate, setProgressRate] = useSafely(useState(null)); const [status, setStatus] = useSafely(useState(null)); const [messageCount, setMessageCount] = useSafely(useState(0)); - const [users, setUsers] = useSafely(useState([])); - const [channels, setChannels] = useSafely(useState([])); + const [users, setUsers] = useState([]); + const [channels, setChannels] = useState([]); const [isImporting, setImporting] = useSafely(useState(false)); const usersCount = useMemo(() => users.filter(({ do_import }) => do_import).length, [users]); @@ -169,6 +304,10 @@ function PrepareImportPage() { } }; + const [tab, setTab] = useState('users'); + const handleTabClick = useMemo(() => (tab) => () => setTab(tab), []); + + const statusDebounced = useDebounce(status, 100); return @@ -181,8 +320,14 @@ function PrepareImportPage() { - - + + + {statusDebounced && t(statusDebounced.replace('importer_', 'importer_status_'))} + {!isPreparing && + {t('Users')} {usersCount} + {t('Channels')} {channelsCount} + {t('Messages')}{messageCount} + } {isPreparing && <> {progressRate @@ -192,115 +337,11 @@ function PrepareImportPage() { : } } - - {!isPreparing && <> - {status && t(status.replace('importer_', 'importer_status_'))} - - {t('Messages')} {messageCount} - - {t('Users')} {usersCount} - - {users.length && - - - - 0} - indeterminate={usersCount > 0 && usersCount !== users.length} - onChange={() => { - setUsers((users) => { - const hasCheckedDeletedUsers = users.some(({ is_deleted, do_import }) => is_deleted && do_import); - const isChecking = usersCount === 0; - - if (isChecking) { - return users.map((user) => ({ ...user, do_import: true })); - } - - if (hasCheckedDeletedUsers) { - return users.map((user) => (user.is_deleted ? { ...user, do_import: false } : user)); - } - - return users.map((user) => ({ ...user, do_import: false })); - }); - }} - /> - - {t('Username')} - {t('Email')} - - - - - {users.map((user) => - - { - const { checked } = event.currentTarget; - setUsers((users) => - users.map((_user) => (_user === user ? { ..._user, do_import: checked } : _user))); - }} - /> - - {user.username} - {user.email} - {user.is_deleted && {t('Deleted')}} - )} - -
} - - {t('Channels')} {channelsCount} - - {channels.length && - - - - 0} - indeterminate={channelsCount > 0 && channelsCount !== channels.length} - onChange={() => { - setChannels((channels) => { - const hasCheckedArchivedChannels = channels.some(({ is_archived, do_import }) => is_archived && do_import); - const isChecking = channelsCount === 0; - - if (isChecking) { - return channels.map((channel) => ({ ...channel, do_import: true })); - } - - if (hasCheckedArchivedChannels) { - return channels.map((channel) => (channel.is_deleted ? { ...channel, do_import: false } : channel)); - } - - return channels.map((channel) => ({ ...channel, do_import: false })); - }); - }} - /> - - {t('Name')} - - - - - {channels.map((channel) => - - { - const { checked } = event.currentTarget; - setChannels((channels) => - channels.map((_channel) => (_channel === channel ? { ..._channel, do_import: checked } : _channel))); - }} - /> - - {channel.name} - {channel.is_archived && {t('Importer_Archived')}} - )} - -
} - } + {!isPreparing && tab === 'users' && } + {!isPreparing && tab === 'channels' && }
-
+
; }