Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iris 3255 select all #313

Merged
merged 5 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions src/hooks/useSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useCallback, useEffect, useState } from 'react';
import { omit, isEmpty } from 'lodash';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { omit, isEmpty, map } from 'lodash';

export const useSelection = (currentFolderId, setCount) => {
export const useSelection = (currentFolderId, setCount, count, items = []) => {
const [selected, setSelected] = useState({});
const [isSelecting, setIsSelecting] = useState(false);
const [folderId, setFolderId] = useState(currentFolderId);

const [isSelectModeOn, setIsSelectModeOn] = useState(false);
useEffect(() => {
setIsSelecting(!isEmpty(selected));
setIsSelectModeOn(!isEmpty(selected));
if (currentFolderId !== folderId) {
setSelected({});
setFolderId(currentFolderId);
setIsSelecting(false);
setIsSelectModeOn(false);
setCount(0);
}
}, [currentFolderId, folderId, selected, setCount]);

const isAllSelected = useMemo(() => count === items.length, [count, items.length]);
const selectItem = useCallback(
(id) => {
if (selected[id]) {
Expand All @@ -35,9 +35,35 @@ export const useSelection = (currentFolderId, setCount) => {
);

const deselectAll = useCallback(() => {
setIsSelectModeOn(false);
setSelected({});
setCount(0);
}, [setCount]);

const selectAll = useCallback(() => {
map(items, (conv) => {
if (!selected[conv.id]) {
selectItem(conv.id);
}
});
}, [items, selectItem, selected]);

const selectAllModeOff = useCallback(() => {
setSelected({});
setCount(0);
setTimeout(() => {
setIsSelectModeOn(true);
});
}, [setCount]);

return { selected, isSelecting, toggle: selectItem, deselectAll };
return {
selected,
toggle: selectItem,
deselectAll,
isSelectModeOn,
setIsSelectModeOn,
selectAll,
isAllSelected,
selectAllModeOff
};
};
3 changes: 0 additions & 3 deletions src/store/tests/messages-slice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import React from 'react';
import fetchMock from 'jest-fetch-mock';
import { getMsg } from '../actions';
import { selectMessage, selectMessages } from '../messages-slice';
import { generateStore } from '../../tests/generators/store';
import { getSetupServer } from '../../carbonio-ui-commons/test/jest-setup';

describe('Messages Slice', () => {
describe('GetMsg', () => {
Expand Down
65 changes: 57 additions & 8 deletions src/ui-actions/select-panel-action-message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import React, { FC, ReactElement, useMemo } from 'react';
import { Container, Dropdown, IconButton } from '@zextras/carbonio-design-system';
import { FOLDERS, useTags } from '@zextras/carbonio-shell-ui';
import React, { FC, ReactElement, useContext, useMemo } from 'react';
import {
Button,
Container,
Dropdown,
IconButton,
SnackbarManagerContext
} from '@zextras/carbonio-design-system';
import { FOLDERS, t, useTags } from '@zextras/carbonio-shell-ui';
import { map, every, filter, some } from 'lodash';
import { useDispatch } from 'react-redux';

Expand All @@ -25,14 +31,21 @@ type SelectMessagesPanelActionsPropType = {
folderId: string;
selectedIds: Array<string>;
deselectAll: () => void;
selectAll: () => void;
isAllSelected: boolean;
selectAllModeOff: () => void;
};
const SelectMessagesPanelActions: FC<SelectMessagesPanelActionsPropType> = ({
messages,
folderId,
selectedIds,
deselectAll
deselectAll,
selectAll,
isAllSelected,
selectAllModeOff
}) => {
const dispatch = useDispatch();
const createSnackbar = useContext(SnackbarManagerContext);
const ids = useMemo(() => Object.keys(selectedIds ?? []), [selectedIds]);
const selectedConversation = filter(messages, (convo) => ids.indexOf(convo.id ?? '') !== -1);
const tags = useTags();
Expand Down Expand Up @@ -197,8 +210,16 @@ const SelectMessagesPanelActions: FC<SelectMessagesPanelActionsPropType> = ({
onClick={deselectAll}
data-testid="action-button-deselect-all"
/>
{/* Uncomment this line to show the `Select all` Button */}
{/* <Button type='ghost' label='Select all' color='primary' /> */}
<Button
type="ghost"
label={
isAllSelected
? t('label.deselect_all', 'DESELECT all')
: t('label.select_all', 'SELECT all')
}
color="primary"
onClick={isAllSelected ? selectAllModeOff : selectAll}
/>
</Container>
<Container background="gray5" orientation="horizontal" mainAlignment="flex-end">
{map(
Expand All @@ -210,7 +231,21 @@ const SelectMessagesPanelActions: FC<SelectMessagesPanelActionsPropType> = ({
iconColor="primary"
onClick={(ev): void => {
if (ev) ev.preventDefault();
action.click && action.click();
if (ids.length === 0) {
createSnackbar({
key: `edit`,
replace: true,
type: 'info',
label: t(
'label.need_to_select_atleast_one_item',
'You need to select at least one item to perform the action'
),
autoHideTimeout: 3000,
hideButton: true
});
} else {
action.click && action.click();
}
}}
size="large"
/>
Expand Down Expand Up @@ -239,7 +274,21 @@ const SelectMessagesPanelActions: FC<SelectMessagesPanelActionsPropType> = ({
label: action.label,
click: (ev: Event): void => {
if (ev) ev.preventDefault();
action.click && action.click();
if (ids.length === 0) {
createSnackbar({
key: `edit`,
replace: true,
type: 'info',
label: t(
'label.need_to_select_atleast_one_item',
'You need to select at least one item to perform the action'
),
autoHideTimeout: 3000,
hideButton: true
});
} else {
action.click && action.click();
}
},
customComponent: action.customComponent,
items: action.items
Expand Down
75 changes: 63 additions & 12 deletions src/ui-actions/select-panel-action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Container, Dropdown, IconButton } from '@zextras/carbonio-design-system';
import { FOLDERS, useTags, useUserAccount } from '@zextras/carbonio-shell-ui';
import {
Button,
Container,
Dropdown,
IconButton,
SnackbarManagerContext
} from '@zextras/carbonio-design-system';
import { FOLDERS, t, useTags, useUserAccount } from '@zextras/carbonio-shell-ui';
import { every, filter, map, some } from 'lodash';
import React, { FC, useMemo } from 'react';
import React, { FC, useContext, useMemo } from 'react';
import { useDispatch } from 'react-redux';
import { Conversation } from '../types';

Expand All @@ -25,15 +31,22 @@ type SelectPanelActionsPropType = {
folderId: string;
selectedIds: Array<string>;
deselectAll: () => void;
selectAll: () => void;
isAllSelected: boolean;
selectAllModeOff: () => void;
};
const SelectPanelActions: FC<SelectPanelActionsPropType> = ({
conversation,
folderId,
selectedIds,
deselectAll
deselectAll,
selectAll,
isAllSelected,
selectAllModeOff
}) => {
const dispatch = useDispatch();
const account = useUserAccount();
const createSnackbar = useContext(SnackbarManagerContext);
const ids = useMemo(() => Object.keys(selectedIds ?? []), [selectedIds]);
const selectedConversation = filter(conversation, (convo) => ids.indexOf(convo.id) !== -1);
const tags = useTags();
Expand Down Expand Up @@ -258,8 +271,16 @@ const SelectPanelActions: FC<SelectPanelActionsPropType> = ({
onClick={deselectAll}
data-testid="action-button-deselect-all"
/>
{/* Uncomment this line to show the `Select all` Button */}
{/* <Button type='ghost' label='Select all' color='primary' /> */}
<Button
type="ghost"
label={
isAllSelected
? t('label.deselect_all', 'DESELECT all')
: t('label.select_all', 'SELECT all')
}
color="primary"
onClick={isAllSelected ? selectAllModeOff : selectAll}
/>
</Container>
<Container background="gray5" orientation="horizontal" mainAlignment="flex-end">
{map(filter(primaryActions), (action) => {
Expand All @@ -274,9 +295,24 @@ const SelectPanelActions: FC<SelectPanelActionsPropType> = ({
iconColor="primary"
onClick={(ev): void => {
if (ev) ev.preventDefault();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
action.click(ev);

if (ids.length === 0) {
createSnackbar({
key: `edit`,
replace: true,
type: 'info',
label: t(
'label.need_to_select_atleast_one_item',
'You need to select at least one item to perform the action'
),
autoHideTimeout: 3000,
hideButton: true
});
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
action.click(ev);
}
}}
size="large"
/>
Expand Down Expand Up @@ -308,9 +344,24 @@ const SelectPanelActions: FC<SelectPanelActionsPropType> = ({
label: action.label,
click: (ev): void => {
if (ev) ev.preventDefault();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
action.click();

if (ids.length === 0) {
createSnackbar({
key: `edit`,
replace: true,
type: 'info',
label: t(
'label.need_to_select_atleast_one_item',
'You need to select at least one item to perform the action'
),
autoHideTimeout: 3000,
hideButton: true
});
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
action.click();
}
},
customComponent: action.customComponent,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
52 changes: 42 additions & 10 deletions src/views/app/folder-panel/breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,36 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Container, Divider, Row, Text, Padding } from '@zextras/carbonio-design-system';
import {
Container,
Divider,
Row,
Text,
Padding,
IconCheckbox
} from '@zextras/carbonio-design-system';
import { FOLDERS, t } from '@zextras/carbonio-shell-ui';
import React, { FC, useMemo } from 'react';
import styled from 'styled-components';

const SelectIconCheckbox = styled(IconCheckbox)`
svg {
color: ${(props): string => props.theme.palette.primary.regular};
}
`;

export const Breadcrumbs: FC<{
folderPath: string;
itemsCount: number;
folderId: string | number;
}> = ({ folderPath, itemsCount, folderId }) => {
isSelectModeOn: boolean;
setIsSelectModeOn: (ev: boolean) => void;
}> = ({ folderPath, itemsCount, folderId, isSelectModeOn, setIsSelectModeOn }) => {
const folderTitle = useMemo(
() =>
folderId === FOLDERS.SPAM ? t('label.spam', 'Spam') : folderPath?.split('/')?.join(' / '),
folderId === FOLDERS.SPAM
? `/ ${t('label.spam', 'Spam')}`
: folderPath?.split('/')?.join(' / '),
[folderId, folderPath]
);
return (
Expand All @@ -27,16 +45,30 @@ export const Breadcrumbs: FC<{
<Row
height="100%"
width="fill"
padding={{ all: 'small' }}
padding={{ all: 'extrasmall' }}
mainAlignment="space-between"
takeAvailableSpace
>
<Row
mainAlignment="flex-start"
takeAvailableSpace
padding={{ all: 'small', right: 'medium' }}
>
<Text size="medium" data-testid="BreadcrumbPath">
<Row mainAlignment="flex-start" takeAvailableSpace padding={{ right: 'medium' }}>
<SelectIconCheckbox
borderRadius="regular"
icon="CheckmarkSquare"
defaultChecked={isSelectModeOn}
size="regular"
onChange={(): null => null}
onClick={(): void => {
setIsSelectModeOn(!isSelectModeOn);
}}
/>
<Text
size="medium"
color="secondary"
style={{ marginLeft: '0.5rem' }}
data-testid="BreadcrumbPath"
>
{t('label.path', 'Path')}
</Text>
<Text size="medium" style={{ marginLeft: '0.25rem' }} data-testid="BreadcrumbFolderPath">
{folderTitle}
</Text>
</Row>
Expand Down
Loading