File size: 3,618 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { Card } from '@automattic/components';
import Search, { useFuzzySearch } from '@automattic/search';
import styled from '@emotion/styled';
import { useI18n } from '@wordpress/react-i18n';
import { localize } from 'i18n-calypso';
import { find } from 'lodash';
import PropTypes from 'prop-types';
import { Component, Fragment } from 'react';
import { connect } from 'react-redux';
import NoSitesMessage from 'calypso/components/empty-content/no-sites-message';
import InfiniteList from 'calypso/components/infinite-list';
import getUndeletedSites from 'calypso/state/selectors/get-undeleted-sites';
import { isRequestingSites } from 'calypso/state/sites/selectors';
import Blog from './blog';
import Placeholder from './placeholder';

import './style.scss';

const createPlaceholder = () => <Placeholder />;
const noop = () => {};
const getItemRef = ( { ID } ) => `blog-${ ID }`;

const BlogSearch = styled( Search )( {
	boxShadow: '0 0 0 1px var(--color-border-subtle)',
} );

const SITES_SEARCH_INDEX_KEYS = [ 'name', 'domain' ];

const FilteredInfiniteList = ( props ) => {
	const { __ } = useI18n();
	const filteredItems = useFuzzySearch( {
		data: props.items,
		keys: SITES_SEARCH_INDEX_KEYS,
		query: props.searchTerm ?? '',
	} );
	return (
		<Fragment>
			{ filteredItems.length >= 1 && (
				<InfiniteList
					items={ filteredItems }
					lastPage
					fetchNextPage={ noop }
					fetchingNextPage={ false }
					guessedItemHeight={ 69 }
					getItemRef={ getItemRef }
					renderItem={ props.renderItem }
					renderLoadingPlaceholders={ createPlaceholder }
				/>
			) }
			{ filteredItems.length === 0 && <Card compact>{ __( 'No sites match your search.' ) }</Card> }
		</Fragment>
	);
};

class BlogsSettings extends Component {
	static propTypes = {
		sites: PropTypes.array.isRequired,
		requestingSites: PropTypes.bool.isRequired,
		settings: PropTypes.array,
		hasUnsavedChanges: PropTypes.bool.isRequired,
		onToggle: PropTypes.func.isRequired,
		onSave: PropTypes.func.isRequired,
		onSaveToAll: PropTypes.func.isRequired,
	};

	state = { searchTerm: '' };

	render() {
		const { sites, requestingSites } = this.props;

		if ( ! sites || ! this.props.settings ) {
			return <Placeholder />;
		}

		if ( sites.length === 0 && ! requestingSites ) {
			return <NoSitesMessage />;
		}

		const renderBlog = ( site, index, disableToggle = false ) => {
			const onSave = () => this.props.onSave( site.ID );
			const onSaveToAll = () => this.props.onSaveToAll( site.ID );
			const blogSettings = find( this.props.settings, { blog_id: site.ID } );

			return (
				<Blog
					key={ `blog-${ site.ID }` }
					siteId={ site.ID }
					disableToggle={ disableToggle }
					hasUnsavedChanges={ this.props.hasUnsavedChanges }
					settings={ blogSettings }
					onToggle={ this.props.onToggle }
					onSave={ onSave }
					onSaveToAll={ onSaveToAll }
				/>
			);
		};

		if ( sites.length === 1 ) {
			return renderBlog( sites[ 0 ], null, true );
		}

		return (
			<Fragment>
				{ sites.length >= 10 && (
					<BlogSearch
						onSearch={ ( searchTerm ) => {
							this.setState( { searchTerm } );
						} }
						placeholder={ this.props.translate( 'Search by name or domain…' ) }
						disableAutocorrect
						defaultValue=""
					/>
				) }
				<FilteredInfiniteList
					searchTerm={ this.state.searchTerm }
					items={ sites }
					renderItem={ renderBlog }
				/>
			</Fragment>
		);
	}
}

const mapStateToProps = ( state ) => ( {
	sites: getUndeletedSites( state ),
	requestingSites: isRequestingSites( state ),
} );

export default connect( mapStateToProps )( localize( BlogsSettings ) );