@blocklet/list 0.8.15 → 0.8.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/base.js +21 -24
- package/lib/components/{aside.js → aside/aside.js} +11 -12
- package/lib/components/aside/category-link-list.js +78 -0
- package/lib/components/aside/index.js +13 -0
- package/lib/components/category-select.js +55 -0
- package/lib/components/{button.js → custom-select/button.js} +1 -1
- package/lib/components/{custom-select.js → custom-select/custom-select.js} +8 -6
- package/lib/components/custom-select/index.js +13 -0
- package/lib/components/filter-author.js +2 -3
- package/lib/components/{empty.js → list/empty.js} +6 -8
- package/lib/components/list/index.js +13 -0
- package/lib/components/{list.js → list/list.js} +12 -12
- package/lib/components/search.js +20 -18
- package/lib/contexts/filter.js +268 -0
- package/lib/index.js +6 -16
- package/lib/libs/prop-types.js +34 -0
- package/lib/{tools → libs}/utils.js +6 -6
- package/package.json +60 -64
- package/src/base.js +17 -18
- package/src/components/{aside.js → aside/aside.js} +7 -8
- package/src/components/aside/category-link-list.js +53 -0
- package/src/components/aside/index.js +3 -0
- package/src/components/category-select.js +43 -0
- package/src/components/{button.js → custom-select/button.js} +0 -0
- package/src/components/{custom-select.js → custom-select/custom-select.js} +5 -4
- package/src/components/custom-select/index.js +3 -0
- package/src/components/filter-author.js +2 -3
- package/src/components/{empty.js → list/empty.js} +7 -8
- package/src/components/list/index.js +3 -0
- package/src/components/{list.js → list/list.js} +10 -10
- package/src/components/search.js +17 -13
- package/src/contexts/filter.js +197 -0
- package/src/index.js +6 -16
- package/src/libs/prop-types.js +26 -0
- package/src/{tools → libs}/utils.js +6 -6
- package/lib/components/categories.js +0 -143
- package/lib/contexts/store.js +0 -336
- package/lib/hooks/page-state.js +0 -69
- package/src/components/categories.js +0 -129
- package/src/contexts/store.js +0 -266
- package/src/hooks/page-state.js +0 -53
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { MenuItem } from '@mui/material';
|
|
3
|
+
|
|
4
|
+
import { useFilterContext } from '../contexts/filter';
|
|
5
|
+
import CustomSelect from './custom-select';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 小屏幕下的分类选择器
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
const CategorySelect = () => {
|
|
12
|
+
const { categoryList, selectedCategory, handleCategory, t, locale } = useFilterContext();
|
|
13
|
+
|
|
14
|
+
const itemRender = (item) => {
|
|
15
|
+
return (
|
|
16
|
+
<MenuItem
|
|
17
|
+
className={['my-select__option', selectedCategory?.includes(item.name) ? 'my-select__option--active' : ''].join(
|
|
18
|
+
' '
|
|
19
|
+
)}
|
|
20
|
+
key={item._id}
|
|
21
|
+
value={item.name}
|
|
22
|
+
onClick={() => handleCategory(item.name)}>
|
|
23
|
+
{item.locales[locale]}
|
|
24
|
+
</MenuItem>
|
|
25
|
+
);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<CustomSelect
|
|
30
|
+
value={selectedCategory || 'all'}
|
|
31
|
+
options={categoryList}
|
|
32
|
+
title={categoryList.find((i) => i.name === selectedCategory)?.locales[locale] || t('category.all')}
|
|
33
|
+
itemRender={itemRender}
|
|
34
|
+
prepend={
|
|
35
|
+
<MenuItem value="all" onClick={() => handleCategory('all')}>
|
|
36
|
+
{t('category.all')}
|
|
37
|
+
</MenuItem>
|
|
38
|
+
}
|
|
39
|
+
/>
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export default CategorySelect;
|
|
File without changes
|
|
@@ -4,7 +4,8 @@ import useTheme from '@mui/styles/useTheme';
|
|
|
4
4
|
import styled from 'styled-components';
|
|
5
5
|
import PropTypes from 'prop-types';
|
|
6
6
|
import { ClickAwayListener, Grow, MenuItem, MenuList, Paper, Popper, SvgIcon, useMediaQuery } from '@mui/material';
|
|
7
|
-
import
|
|
7
|
+
import CheckIcon from '@mui/icons-material/Check';
|
|
8
|
+
import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown';
|
|
8
9
|
import cloneDeep from 'lodash/cloneDeep';
|
|
9
10
|
import isEmpty from 'lodash/isEmpty';
|
|
10
11
|
import isEqual from 'lodash/isEqual';
|
|
@@ -81,9 +82,9 @@ const CustomSelect = ({
|
|
|
81
82
|
<div className="my-select__icon">{icon}</div>
|
|
82
83
|
{title}
|
|
83
84
|
{multiple && currentValue.length > 1 && ` (${currentValue.length})`}
|
|
84
|
-
<SvgIcon className="my-select__arrowdown" component={
|
|
85
|
+
<SvgIcon className="my-select__arrowdown" component={KeyboardArrowDownIcon} fontSize="small" />
|
|
85
86
|
</StyledButton>
|
|
86
|
-
<Popper open={open} anchorEl={anchorRef.current} transition style={{ zIndex: '
|
|
87
|
+
<Popper open={open} anchorEl={anchorRef.current} transition style={{ zIndex: '9999' }}>
|
|
87
88
|
{({ TransitionProps, placement }) => (
|
|
88
89
|
<Grow
|
|
89
90
|
{...TransitionProps}
|
|
@@ -106,7 +107,7 @@ const CustomSelect = ({
|
|
|
106
107
|
].join(' ')}>
|
|
107
108
|
{multiple && (
|
|
108
109
|
<SvgIcon
|
|
109
|
-
component={
|
|
110
|
+
component={CheckIcon}
|
|
110
111
|
className={[
|
|
111
112
|
'my-select__option__icon',
|
|
112
113
|
containsValue(option.value) ? 'my-select__option__icon--active' : '',
|
|
@@ -4,7 +4,7 @@ import styled from 'styled-components';
|
|
|
4
4
|
import { Chip } from '@mui/material';
|
|
5
5
|
import FaceIcon from '@mui/icons-material/Face';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { useFilterContext } from '../contexts/filter';
|
|
8
8
|
|
|
9
9
|
const StyleDiv = styled.div`
|
|
10
10
|
.MuiChip-root {
|
|
@@ -14,8 +14,7 @@ const StyleDiv = styled.div`
|
|
|
14
14
|
}
|
|
15
15
|
`;
|
|
16
16
|
const FilterAuthor = ({ user, deleteUserTag, ...containerProps }) => {
|
|
17
|
-
const
|
|
18
|
-
const { t } = searchStore;
|
|
17
|
+
const { t } = useFilterContext();
|
|
19
18
|
if (!user) return null;
|
|
20
19
|
return (
|
|
21
20
|
<StyleDiv {...containerProps}>
|
|
@@ -3,11 +3,10 @@ import PropTypes from 'prop-types';
|
|
|
3
3
|
import Box from '@mui/material/Box';
|
|
4
4
|
import Typography from '@mui/material/Typography';
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { useFilterContext } from '../../contexts/filter';
|
|
7
7
|
|
|
8
8
|
const NoResults = () => {
|
|
9
|
-
const
|
|
10
|
-
const { t } = searchStore;
|
|
9
|
+
const { t } = useFilterContext();
|
|
11
10
|
return (
|
|
12
11
|
<Typography style={{ textAlign: 'center' }} variant="subtitle2">
|
|
13
12
|
{t('blocklet.noResults')}
|
|
@@ -15,8 +14,8 @@ const NoResults = () => {
|
|
|
15
14
|
);
|
|
16
15
|
};
|
|
17
16
|
const NoResultsTips = ({ filterTip, keywordTip }) => {
|
|
18
|
-
const
|
|
19
|
-
|
|
17
|
+
const { t, locale } = useFilterContext();
|
|
18
|
+
|
|
20
19
|
const getSplit = () => {
|
|
21
20
|
if (locale === 'zh') return '、';
|
|
22
21
|
return ' , ';
|
|
@@ -39,11 +38,11 @@ NoResultsTips.defaultProps = {
|
|
|
39
38
|
filterTip: false,
|
|
40
39
|
keywordTip: false,
|
|
41
40
|
};
|
|
42
|
-
const EmptyTitle = ({ primaryStart, primaryEnd,
|
|
41
|
+
const EmptyTitle = ({ primaryStart, primaryEnd, filter }) => {
|
|
43
42
|
return (
|
|
44
43
|
<Typography variant="subtitle2">
|
|
45
44
|
<span>{primaryStart}</span>
|
|
46
|
-
<span className="primary"> {
|
|
45
|
+
<span className="primary"> {filter} </span>
|
|
47
46
|
<span>{primaryEnd} </span>
|
|
48
47
|
</Typography>
|
|
49
48
|
);
|
|
@@ -51,7 +50,7 @@ const EmptyTitle = ({ primaryStart, primaryEnd, search }) => {
|
|
|
51
50
|
EmptyTitle.propTypes = {
|
|
52
51
|
primaryStart: PropTypes.string.isRequired,
|
|
53
52
|
primaryEnd: PropTypes.string.isRequired,
|
|
54
|
-
|
|
53
|
+
filter: PropTypes.string.isRequired,
|
|
55
54
|
};
|
|
56
55
|
|
|
57
56
|
export { NoResults, EmptyTitle, NoResultsTips };
|
|
@@ -8,14 +8,14 @@ import Grid from '@mui/material/Grid';
|
|
|
8
8
|
import CircularProgress from '@mui/material/CircularProgress';
|
|
9
9
|
|
|
10
10
|
import { NoResults, EmptyTitle, NoResultsTips } from './empty';
|
|
11
|
-
import {
|
|
12
|
-
import { formatError } from '
|
|
11
|
+
import { useFilterContext } from '../../contexts/filter';
|
|
12
|
+
import { formatError } from '../../libs/utils';
|
|
13
13
|
|
|
14
14
|
export default function BlockletList({ blocklets, ...rest }) {
|
|
15
|
-
const { blockletRender, errors, loadings, selectedCategory, blockletList, getCategoryLocale,
|
|
16
|
-
|
|
15
|
+
const { blockletRender, errors, loadings, selectedCategory, blockletList, getCategoryLocale, filters, t } =
|
|
16
|
+
useFilterContext();
|
|
17
17
|
|
|
18
|
-
const showFilterTip = !!selectedCategory || !!
|
|
18
|
+
const showFilterTip = !!selectedCategory || !!filters.price;
|
|
19
19
|
|
|
20
20
|
if (errors.fetchBlockletsError) {
|
|
21
21
|
return (
|
|
@@ -31,25 +31,25 @@ export default function BlockletList({ blocklets, ...rest }) {
|
|
|
31
31
|
</Box>
|
|
32
32
|
);
|
|
33
33
|
}
|
|
34
|
-
if (
|
|
34
|
+
if (filters.keyword && showFilterTip && blockletList.length === 0) {
|
|
35
35
|
return (
|
|
36
36
|
<CustomEmpty>
|
|
37
37
|
<EmptyTitle
|
|
38
38
|
primaryStart={t('blocklet.noBlockletPart1')}
|
|
39
39
|
primaryEnd={t('blocklet.noBlockletPart2')}
|
|
40
|
-
|
|
40
|
+
filter={filters.keyword}
|
|
41
41
|
/>
|
|
42
42
|
<NoResultsTips keywordTip filterTip />
|
|
43
43
|
</CustomEmpty>
|
|
44
44
|
);
|
|
45
45
|
}
|
|
46
|
-
if (
|
|
46
|
+
if (filters.keyword && blockletList.length === 0) {
|
|
47
47
|
return (
|
|
48
48
|
<CustomEmpty>
|
|
49
49
|
<EmptyTitle
|
|
50
50
|
primaryStart={t('blocklet.noBlockletPart1')}
|
|
51
51
|
primaryEnd={t('blocklet.noBlockletPart2')}
|
|
52
|
-
|
|
52
|
+
filter={filters.keyword}
|
|
53
53
|
/>
|
|
54
54
|
<NoResultsTips keywordTip />
|
|
55
55
|
</CustomEmpty>
|
|
@@ -63,7 +63,7 @@ export default function BlockletList({ blocklets, ...rest }) {
|
|
|
63
63
|
<EmptyTitle
|
|
64
64
|
primaryStart={t('blocklet.noCategoryResults1')}
|
|
65
65
|
primaryEnd={t('blocklet.noCategoryResults2')}
|
|
66
|
-
|
|
66
|
+
filter={categoryLocale}
|
|
67
67
|
/>
|
|
68
68
|
) : (
|
|
69
69
|
<NoResults />
|
package/src/components/search.js
CHANGED
|
@@ -1,27 +1,31 @@
|
|
|
1
|
-
import React, { useState } from 'react';
|
|
1
|
+
import React, { useEffect, useState } from 'react';
|
|
2
2
|
import PropTypes from 'prop-types';
|
|
3
|
-
import
|
|
3
|
+
import SearchIcon from '@mui/icons-material/Search';
|
|
4
|
+
import CloseIcon from '@mui/icons-material/Close';
|
|
4
5
|
import { OutlinedInput, InputAdornment } from '@mui/material';
|
|
5
6
|
import { useDebounceFn } from 'ahooks';
|
|
6
7
|
import styled from 'styled-components';
|
|
7
8
|
|
|
8
|
-
import {
|
|
9
|
+
import { useFilterContext } from '../contexts/filter';
|
|
9
10
|
|
|
10
11
|
const Search = ({ placeholder, ...rest }) => {
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const [searchStr, setSearchStr] = useState(queryParams.get('search') || '');
|
|
12
|
+
const { filters, handleKeyword } = useFilterContext();
|
|
13
|
+
const [searchStr, setSearchStr] = useState(filters.keyword || '');
|
|
14
14
|
|
|
15
|
-
const
|
|
15
|
+
const debouncedSearch = useDebounceFn(handleKeyword, { wait: 300 });
|
|
16
16
|
const handleChange = (event) => {
|
|
17
17
|
const { value } = event.target;
|
|
18
18
|
setSearchStr(value);
|
|
19
|
-
|
|
19
|
+
debouncedSearch.run(value);
|
|
20
20
|
};
|
|
21
21
|
const handleClose = () => {
|
|
22
22
|
setSearchStr('');
|
|
23
|
-
|
|
23
|
+
handleKeyword();
|
|
24
24
|
};
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
setSearchStr(filters.keyword || '');
|
|
27
|
+
}, [filters.keyword]);
|
|
28
|
+
|
|
25
29
|
return (
|
|
26
30
|
<StyledSearch
|
|
27
31
|
inputProps={{
|
|
@@ -29,7 +33,7 @@ const Search = ({ placeholder, ...rest }) => {
|
|
|
29
33
|
}}
|
|
30
34
|
startAdornment={
|
|
31
35
|
<InputAdornment position="start">
|
|
32
|
-
<
|
|
36
|
+
<StyledSearchIcon />
|
|
33
37
|
</InputAdornment>
|
|
34
38
|
}
|
|
35
39
|
onChange={handleChange}
|
|
@@ -40,7 +44,7 @@ const Search = ({ placeholder, ...rest }) => {
|
|
|
40
44
|
endAdornment={
|
|
41
45
|
searchStr && (
|
|
42
46
|
<InputAdornment position="end">
|
|
43
|
-
<
|
|
47
|
+
<StyledCloseIcon data-cy="search-delete" onClick={handleClose} />
|
|
44
48
|
</InputAdornment>
|
|
45
49
|
)
|
|
46
50
|
}
|
|
@@ -80,7 +84,7 @@ const StyledSearch = styled(OutlinedInput)`
|
|
|
80
84
|
}
|
|
81
85
|
`;
|
|
82
86
|
|
|
83
|
-
const
|
|
87
|
+
const StyledSearchIcon = styled(SearchIcon)`
|
|
84
88
|
color: ${(props) => props.theme.palette.grey[500]};
|
|
85
89
|
font-size: 28px;
|
|
86
90
|
@media (max-width: ${(props) => props.theme.breakpoints.values.md}px) {
|
|
@@ -88,7 +92,7 @@ const StyledMagnify = styled(Magnify)`
|
|
|
88
92
|
}
|
|
89
93
|
`;
|
|
90
94
|
|
|
91
|
-
const
|
|
95
|
+
const StyledCloseIcon = styled(CloseIcon)`
|
|
92
96
|
color: ${(props) => props.theme.palette.grey[500]};
|
|
93
97
|
font-size: 16px;
|
|
94
98
|
cursor: pointer;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import React, { useContext, createContext, useMemo, useEffect } from 'react';
|
|
2
|
+
import PropTypes from 'prop-types';
|
|
3
|
+
import { useRequest } from 'ahooks';
|
|
4
|
+
import orderBy from 'lodash/orderBy';
|
|
5
|
+
import axios from 'axios';
|
|
6
|
+
// import joinUrl from 'url-join';
|
|
7
|
+
|
|
8
|
+
import { getCategories, filterBlockletByPrice, replaceTranslate } from '../libs/utils';
|
|
9
|
+
import translations from '../assets/locale';
|
|
10
|
+
import { propTypes, defaultProps } from '../libs/prop-types';
|
|
11
|
+
|
|
12
|
+
const Filter = createContext({});
|
|
13
|
+
const { Provider, Consumer } = Filter;
|
|
14
|
+
|
|
15
|
+
function FilterProvider({
|
|
16
|
+
onSelectBlocklet,
|
|
17
|
+
selectedBlocklets,
|
|
18
|
+
filters,
|
|
19
|
+
children,
|
|
20
|
+
baseUrl,
|
|
21
|
+
endpoint,
|
|
22
|
+
locale,
|
|
23
|
+
blockletRender,
|
|
24
|
+
onFilterChange,
|
|
25
|
+
}) {
|
|
26
|
+
const storeApi = axios.create({
|
|
27
|
+
baseURL: endpoint,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const {
|
|
31
|
+
data: allBlocklets,
|
|
32
|
+
error: fetchBlockletsError,
|
|
33
|
+
loading: fetchBlockletsLoading,
|
|
34
|
+
run: fetchBlocklets,
|
|
35
|
+
} = useRequest(
|
|
36
|
+
async () => {
|
|
37
|
+
const { data: list } = await storeApi.get('/api/blocklets.json');
|
|
38
|
+
return list;
|
|
39
|
+
},
|
|
40
|
+
{ initialData: [], manual: true }
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const {
|
|
44
|
+
data: allCategories,
|
|
45
|
+
error: fetchCategoriesError,
|
|
46
|
+
loading: fetchCategoriesLoading,
|
|
47
|
+
run: fetchCategories,
|
|
48
|
+
} = useRequest(
|
|
49
|
+
async () => {
|
|
50
|
+
const { data: list } = await storeApi.get('/api/blocklets/categories');
|
|
51
|
+
return list;
|
|
52
|
+
},
|
|
53
|
+
{ initialData: [], manual: true }
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const selectedCategory = filters.category;
|
|
57
|
+
|
|
58
|
+
const hasDeveloperFilter = !!filters.developer;
|
|
59
|
+
const categoryState = useMemo(() => {
|
|
60
|
+
return !hasDeveloperFilter ? { data: allCategories } : getCategories(allBlocklets, filters.developer);
|
|
61
|
+
}, [hasDeveloperFilter, allCategories]);
|
|
62
|
+
|
|
63
|
+
const blockletList = useMemo(() => {
|
|
64
|
+
const sortByName = (x) => x?.title?.toLocaleLowerCase() || x?.name?.toLocaleLowerCase(); // 按名称排序
|
|
65
|
+
const sortByPopularity = (x) => x.stats.downloads; // 按下载量排序
|
|
66
|
+
const sortByPublish = (x) => x.lastPublishedAt; // 按发布时间
|
|
67
|
+
const sortMap = {
|
|
68
|
+
nameAsc: sortByName,
|
|
69
|
+
nameDesc: sortByName,
|
|
70
|
+
popularity: sortByPopularity,
|
|
71
|
+
publishAt: sortByPublish,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
let result = allBlocklets || [];
|
|
75
|
+
// 按照付费/免费筛选
|
|
76
|
+
result = filterBlockletByPrice(result, filters.price);
|
|
77
|
+
// 按照分类筛选
|
|
78
|
+
result = result.filter((item) => (selectedCategory ? item?.category?.name === selectedCategory : true));
|
|
79
|
+
// 按照作者筛选
|
|
80
|
+
result = result.filter((item) => (filters?.developer ? item.owner.did === filters.developer : true));
|
|
81
|
+
const lowerSearch = filters?.keyword?.toLocaleLowerCase() || '';
|
|
82
|
+
// 按照搜索筛选
|
|
83
|
+
result = result.filter((item) => {
|
|
84
|
+
return (
|
|
85
|
+
(item?.title || item?.name)?.toLocaleLowerCase().includes(lowerSearch) ||
|
|
86
|
+
item.description?.toLocaleLowerCase().includes(lowerSearch) ||
|
|
87
|
+
item?.version?.toLocaleLowerCase().includes(lowerSearch)
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
// 排序
|
|
91
|
+
return orderBy(result, [sortMap[filters.sortBy]], [filters.sortDirection]);
|
|
92
|
+
}, [allBlocklets, filters]);
|
|
93
|
+
|
|
94
|
+
const categoryList = useMemo(() => {
|
|
95
|
+
const list = categoryState.data || [];
|
|
96
|
+
// 分类按照名称排序
|
|
97
|
+
return orderBy(list, [(i) => i.name], ['asc']);
|
|
98
|
+
}, [categoryState.data]);
|
|
99
|
+
|
|
100
|
+
const translate = (key, data) => {
|
|
101
|
+
if (!translations[locale] || !translations[locale][key]) {
|
|
102
|
+
console.warn(`Warning: no ${key} translation of ${locale}`);
|
|
103
|
+
return key;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return replaceTranslate(translations[locale][key], data);
|
|
107
|
+
};
|
|
108
|
+
const filterStore = {
|
|
109
|
+
errors: { fetchBlockletsError, fetchCategoriesError },
|
|
110
|
+
loadings: { fetchBlockletsLoading, fetchCategoriesLoading },
|
|
111
|
+
endpoint,
|
|
112
|
+
blockletList,
|
|
113
|
+
t: translate,
|
|
114
|
+
filters: { sortBy: 'popularity', sortDirection: 'desc', ...filters },
|
|
115
|
+
selectedCategory,
|
|
116
|
+
categoryList,
|
|
117
|
+
baseUrl,
|
|
118
|
+
blockletRender,
|
|
119
|
+
locale,
|
|
120
|
+
selectedBlocklets,
|
|
121
|
+
onSelectBlocklet,
|
|
122
|
+
handleSort: (sort) => {
|
|
123
|
+
const changeData = {
|
|
124
|
+
...filters,
|
|
125
|
+
sortBy: sort,
|
|
126
|
+
sortDirection: sort === 'nameAsc' ? 'asc' : 'desc',
|
|
127
|
+
};
|
|
128
|
+
onFilterChange(changeData);
|
|
129
|
+
},
|
|
130
|
+
handleKeyword: (keyWord) => {
|
|
131
|
+
const changeData = { ...filters, keyword: keyWord || undefined };
|
|
132
|
+
onFilterChange(changeData);
|
|
133
|
+
},
|
|
134
|
+
handlePrice: (price) => {
|
|
135
|
+
const changeData = {
|
|
136
|
+
...filters,
|
|
137
|
+
price: price === filters.price ? undefined : price,
|
|
138
|
+
};
|
|
139
|
+
onFilterChange(changeData);
|
|
140
|
+
},
|
|
141
|
+
handleCategory: (category) => {
|
|
142
|
+
if (category === 'all') {
|
|
143
|
+
const changeData = { ...filters, category: undefined };
|
|
144
|
+
onFilterChange(changeData);
|
|
145
|
+
} else {
|
|
146
|
+
const changeData = { ...filters, category };
|
|
147
|
+
onFilterChange(changeData);
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
handleDeveloper: (developer) => {
|
|
151
|
+
const changeData = { ...filters, developer: developer || undefined };
|
|
152
|
+
onFilterChange(changeData);
|
|
153
|
+
},
|
|
154
|
+
getCategoryLocale: (category) => {
|
|
155
|
+
if (!category) return null;
|
|
156
|
+
let result = null;
|
|
157
|
+
const find = categoryState.data.find((item) => item.name === category);
|
|
158
|
+
if (find) {
|
|
159
|
+
result = find.locales[locale];
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
},
|
|
163
|
+
get developerName() {
|
|
164
|
+
return allBlocklets.find((i) => i.owner.did === filters.developer)?.owner?.name || '';
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
useEffect(() => {
|
|
169
|
+
if (!hasDeveloperFilter) {
|
|
170
|
+
fetchCategories();
|
|
171
|
+
}
|
|
172
|
+
}, [hasDeveloperFilter]);
|
|
173
|
+
|
|
174
|
+
useEffect(() => {
|
|
175
|
+
fetchBlocklets();
|
|
176
|
+
fetchCategories();
|
|
177
|
+
}, [endpoint]);
|
|
178
|
+
|
|
179
|
+
return <Provider value={filterStore}>{children}</Provider>;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
FilterProvider.propTypes = {
|
|
183
|
+
...propTypes,
|
|
184
|
+
children: PropTypes.any.isRequired,
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
FilterProvider.defaultProps = defaultProps;
|
|
188
|
+
|
|
189
|
+
function useFilterContext() {
|
|
190
|
+
const filterStore = useContext(Filter);
|
|
191
|
+
if (!filterStore) {
|
|
192
|
+
return {};
|
|
193
|
+
}
|
|
194
|
+
return filterStore;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export { FilterProvider, Consumer as FilterConsumer, useFilterContext };
|
package/src/index.js
CHANGED
|
@@ -1,27 +1,17 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import PropTypes from 'prop-types';
|
|
3
2
|
|
|
4
3
|
import SelectBase from './base';
|
|
5
|
-
import {
|
|
4
|
+
import { FilterProvider } from './contexts/filter';
|
|
5
|
+
import { propTypes, defaultProps } from './libs/prop-types';
|
|
6
6
|
|
|
7
7
|
export default function BlockletList(props) {
|
|
8
8
|
return (
|
|
9
|
-
<
|
|
9
|
+
<FilterProvider {...props}>
|
|
10
10
|
<SelectBase />
|
|
11
|
-
</
|
|
11
|
+
</FilterProvider>
|
|
12
12
|
);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
BlockletList.propTypes =
|
|
16
|
-
baseUrl: PropTypes.string,
|
|
17
|
-
endpoint: PropTypes.string.isRequired,
|
|
18
|
-
// 组件的类型: page 单独作为页面使用 持久化数据将存储在 url 和 localstorage,select 作为选择器组件使用 数据在存储在内存中
|
|
19
|
-
type: PropTypes.oneOf(['select', 'page']).isRequired,
|
|
20
|
-
locale: PropTypes.oneOf(['zh', 'en']),
|
|
21
|
-
blockletRender: PropTypes.func.isRequired,
|
|
22
|
-
};
|
|
15
|
+
BlockletList.propTypes = propTypes;
|
|
23
16
|
|
|
24
|
-
BlockletList.defaultProps =
|
|
25
|
-
baseUrl: null,
|
|
26
|
-
locale: 'zh',
|
|
27
|
-
};
|
|
17
|
+
BlockletList.defaultProps = defaultProps;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import PropTypes from 'prop-types';
|
|
2
|
+
|
|
3
|
+
const propTypes = {
|
|
4
|
+
filters: PropTypes.shape({
|
|
5
|
+
keyword: PropTypes.string,
|
|
6
|
+
sortBy: PropTypes.string,
|
|
7
|
+
sortDirection: PropTypes.string,
|
|
8
|
+
price: PropTypes.string,
|
|
9
|
+
category: PropTypes.string,
|
|
10
|
+
developer: PropTypes.string,
|
|
11
|
+
}),
|
|
12
|
+
endpoint: PropTypes.string.isRequired,
|
|
13
|
+
blockletRender: PropTypes.func.isRequired,
|
|
14
|
+
onFilterChange: PropTypes.func,
|
|
15
|
+
baseUrl: PropTypes.string,
|
|
16
|
+
locale: PropTypes.oneOf(['zh', 'en']),
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const defaultProps = {
|
|
20
|
+
baseUrl: null,
|
|
21
|
+
locale: 'zh',
|
|
22
|
+
filters: {},
|
|
23
|
+
onFilterChange: () => {},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { propTypes, defaultProps };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import joinURL from 'url-join';
|
|
2
|
-
import cloneDeep from 'lodash
|
|
2
|
+
import cloneDeep from 'lodash/cloneDeep';
|
|
3
3
|
|
|
4
4
|
const isFreeBlocklet = (meta) => {
|
|
5
5
|
if (!meta.payment) {
|
|
@@ -87,13 +87,13 @@ const replaceTranslate = (template, data) =>
|
|
|
87
87
|
template.replace(/{(\w*)}/g, (m, key) => (data.hasOwnProperty(key) ? data[key] : ''));
|
|
88
88
|
|
|
89
89
|
const removeUndefined = (obj) => {
|
|
90
|
-
const
|
|
91
|
-
Object.keys(
|
|
92
|
-
if (
|
|
93
|
-
delete
|
|
90
|
+
const clone = cloneDeep(obj);
|
|
91
|
+
Object.keys(clone).forEach((key) => {
|
|
92
|
+
if (clone[key] === undefined) {
|
|
93
|
+
delete clone[key];
|
|
94
94
|
}
|
|
95
95
|
});
|
|
96
|
-
return
|
|
96
|
+
return clone;
|
|
97
97
|
};
|
|
98
98
|
|
|
99
99
|
const urlStringify = (obj) => {
|