2020-03-09 16:49:12 +01:00
|
|
|
import React from 'react';
|
2020-03-31 11:25:26 +02:00
|
|
|
import { isEmpty } from 'lodash';
|
2020-03-09 16:49:12 +01:00
|
|
|
import { PageFooter } from 'strapi-helper-plugin';
|
2020-03-24 00:11:16 +01:00
|
|
|
import { generatePageFromStart, generateStartFromPage } from '../../utils';
|
2020-03-09 16:49:12 +01:00
|
|
|
import Filters from '../Filters';
|
|
|
|
import Flex from '../Flex';
|
2020-03-23 16:24:26 +01:00
|
|
|
import List from '../List';
|
|
|
|
import ListEmpty from '../ListEmpty';
|
|
|
|
import Padded from '../Padded';
|
2020-03-09 16:49:12 +01:00
|
|
|
import SelectAll from '../SelectAll';
|
2020-03-23 16:24:26 +01:00
|
|
|
import SortPicker from '../SortPicker';
|
|
|
|
import useModalContext from '../../hooks/useModalContext';
|
2020-03-09 16:49:12 +01:00
|
|
|
import Wrapper from './Wrapper';
|
2020-03-25 17:55:06 +01:00
|
|
|
import CardControl from '../CardControl';
|
2020-03-09 16:49:12 +01:00
|
|
|
|
|
|
|
const BrowseAssets = () => {
|
|
|
|
const {
|
|
|
|
count,
|
2020-03-23 16:24:26 +01:00
|
|
|
files,
|
|
|
|
goTo,
|
|
|
|
handleAllFilesSelection,
|
|
|
|
handleFileSelection,
|
2020-03-25 17:55:06 +01:00
|
|
|
handleGoToEditFile,
|
2020-03-09 16:49:12 +01:00
|
|
|
multiple,
|
2020-03-23 16:24:26 +01:00
|
|
|
params,
|
|
|
|
removeFilter,
|
2020-03-09 16:49:12 +01:00
|
|
|
selectedFiles,
|
2020-03-23 16:24:26 +01:00
|
|
|
setParam,
|
2020-03-09 16:49:12 +01:00
|
|
|
} = useModalContext();
|
|
|
|
|
|
|
|
const handleChangeParams = ({ target: { name, value } }) => {
|
|
|
|
setParam({ name, value });
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleChangeListParams = ({ target: { name, value } }) => {
|
|
|
|
if (name.includes('_page')) {
|
|
|
|
handleChangeParams({
|
|
|
|
target: { name: '_start', value: generateStartFromPage(value, params._limit) },
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
handleChangeParams({ target: { name: '_limit', value } });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDeleteFilter = index => {
|
|
|
|
removeFilter(index);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleGoToUpload = () => {
|
|
|
|
goTo('browse');
|
|
|
|
};
|
|
|
|
|
|
|
|
const limit = parseInt(params._limit, 10) || 10;
|
|
|
|
const start = parseInt(params._start, 10) || 0;
|
2020-04-06 11:11:40 +02:00
|
|
|
const canSelectFile = multiple === true || (selectedFiles.length < 1 && !multiple);
|
2020-03-09 16:49:12 +01:00
|
|
|
|
2020-03-25 17:55:06 +01:00
|
|
|
const handleListCardClick = id => {
|
2020-04-07 15:52:48 +02:00
|
|
|
if (!canSelectFile && id !== selectedFiles[0].id) {
|
2020-04-06 11:11:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-25 17:55:06 +01:00
|
|
|
handleFileSelection({
|
2020-03-30 14:07:24 +02:00
|
|
|
target: {
|
|
|
|
name: id,
|
|
|
|
},
|
2020-03-25 17:55:06 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-03-09 16:49:12 +01:00
|
|
|
const paginationParams = {
|
|
|
|
_limit: parseInt(params._limit, 10) || 10,
|
|
|
|
_page: generatePageFromStart(start, limit),
|
|
|
|
};
|
|
|
|
|
2020-03-24 10:31:40 +01:00
|
|
|
const hasSomeCheckboxSelected = files.some(file =>
|
|
|
|
selectedFiles.find(selectedFile => file.id === selectedFile.id)
|
|
|
|
);
|
|
|
|
const areAllCheckboxesSelected =
|
|
|
|
files.every(file => selectedFiles.find(selectedFile => file.id === selectedFile.id)) &&
|
|
|
|
hasSomeCheckboxSelected;
|
2020-03-09 16:49:12 +01:00
|
|
|
|
2020-03-31 11:25:26 +02:00
|
|
|
const hasFilters = !isEmpty(params.filters.filter(filter => !filter.isDisabled));
|
|
|
|
const hasSearch = !isEmpty(params._q);
|
|
|
|
const areResultsEmptyWithSearchOrFilters = isEmpty(files) && (hasSearch || hasFilters);
|
|
|
|
|
2020-03-09 16:49:12 +01:00
|
|
|
return (
|
2020-03-30 17:18:10 +02:00
|
|
|
<Wrapper>
|
2020-03-31 19:56:16 +02:00
|
|
|
<Padded top>
|
2020-03-09 16:49:12 +01:00
|
|
|
<Flex flexWrap="wrap">
|
|
|
|
{multiple && (
|
|
|
|
<Padded right size="sm">
|
|
|
|
<SelectAll
|
|
|
|
checked={areAllCheckboxesSelected}
|
2020-03-23 16:24:26 +01:00
|
|
|
onChange={handleAllFilesSelection}
|
2020-03-09 16:49:12 +01:00
|
|
|
someChecked={hasSomeCheckboxSelected && !areAllCheckboxesSelected}
|
|
|
|
/>
|
|
|
|
</Padded>
|
|
|
|
)}
|
2020-03-25 17:55:06 +01:00
|
|
|
<SortPicker onChange={handleChangeParams} value={params._sort} />
|
2020-03-09 16:49:12 +01:00
|
|
|
<Padded left size="sm" />
|
|
|
|
<Filters
|
2020-03-23 16:24:26 +01:00
|
|
|
filters={params.filters}
|
2020-03-09 16:49:12 +01:00
|
|
|
onChange={handleChangeParams}
|
|
|
|
onClick={handleDeleteFilter}
|
|
|
|
/>
|
|
|
|
</Flex>
|
|
|
|
</Padded>
|
|
|
|
{!files || files.length === 0 ? (
|
2020-03-31 11:25:26 +02:00
|
|
|
<ListEmpty
|
|
|
|
numberOfRows={2}
|
|
|
|
onClick={handleGoToUpload}
|
|
|
|
hasSearchApplied={areResultsEmptyWithSearchOrFilters}
|
|
|
|
/>
|
2020-03-09 16:49:12 +01:00
|
|
|
) : (
|
2020-03-31 20:01:54 +02:00
|
|
|
<>
|
2020-03-09 16:49:12 +01:00
|
|
|
<List
|
|
|
|
canSelect={canSelectFile}
|
2020-03-23 16:24:26 +01:00
|
|
|
data={files}
|
2020-03-09 16:49:12 +01:00
|
|
|
onChange={handleFileSelection}
|
|
|
|
selectedItems={selectedFiles}
|
2020-04-06 11:11:40 +02:00
|
|
|
onCardClick={handleListCardClick}
|
2020-04-02 10:39:48 +02:00
|
|
|
smallCards
|
2020-03-25 17:55:06 +01:00
|
|
|
renderCardControl={id => (
|
|
|
|
<CardControl
|
|
|
|
small
|
2020-03-31 16:30:43 +02:00
|
|
|
title="edit"
|
2020-03-25 17:55:06 +01:00
|
|
|
color="#9EA7B8"
|
|
|
|
type="pencil"
|
|
|
|
onClick={() => handleGoToEditFile(id)}
|
|
|
|
/>
|
|
|
|
)}
|
2020-03-09 16:49:12 +01:00
|
|
|
/>
|
|
|
|
<Padded left right>
|
|
|
|
<PageFooter
|
|
|
|
context={{ emitEvent: () => {} }}
|
|
|
|
count={count}
|
|
|
|
onChangeParams={handleChangeListParams}
|
|
|
|
params={paginationParams}
|
|
|
|
/>
|
|
|
|
</Padded>
|
2020-03-31 20:01:54 +02:00
|
|
|
</>
|
2020-03-09 16:49:12 +01:00
|
|
|
)}
|
|
|
|
</Wrapper>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default BrowseAssets;
|