CloudLibrary
1 year ago
Settings
1 year ago
StarterSites
3 years ago
App.js
2 years ago
Card.js
3 years ago
CategoriesTabs.js
3 years ago
CategorySelector.js
2 years ago
CustomTooltip.js
3 years ago
DocNotice.js
2 years ago
EditorSelector.js
2 years ago
EditorTabs.js
4 years ago
Header.js
2 years ago
Icon.js
2 years ago
ImportModal.js
1 year ago
ImportModalError.js
2 years ago
ImportModalMock.js
4 years ago
ImportModalNote.js
4 years ago
ImportStepper.js
1 year ago
InstallModal.js
1 year ago
License.js
2 years ago
LicensePanelContext.js
3 years ago
Loading.js
4 years ago
Main.js
1 year ago
Migration.js
4 years ago
NewTCNotice.js
1 year ago
Notification.js
4 years ago
OnboardingContent.js
4 years ago
PreviewFrame.js
3 years ago
Search.js
2 years ago
StarterSiteCard.js
3 years ago
OnboardingContent.js
258 lines
| 1 | import { withSelect, withDispatch } from '@wordpress/data'; |
| 2 | import { compose } from '@wordpress/compose'; |
| 3 | import { useState } from '@wordpress/element'; |
| 4 | import { __ } from '@wordpress/i18n'; |
| 5 | import { Button } from '@wordpress/components'; |
| 6 | |
| 7 | import VizSensor from 'react-visibility-sensor'; |
| 8 | import Fuse from 'fuse.js/dist/fuse.min'; |
| 9 | |
| 10 | import StarterSiteCard from './StarterSiteCard'; |
| 11 | import PreviewFrame from './PreviewFrame'; |
| 12 | import { TAGS } from '../utils/common'; |
| 13 | import Filters from './StarterSites/Filters'; |
| 14 | |
| 15 | const OnboardingContent = ( { |
| 16 | getSites, |
| 17 | category, |
| 18 | resetCategory, |
| 19 | editor, |
| 20 | previewOpen, |
| 21 | currentSiteData, |
| 22 | isOnboarding, |
| 23 | cancelOnboarding, |
| 24 | setSearchQuery, |
| 25 | searchQuery, |
| 26 | } ) => { |
| 27 | const { sites = {} } = getSites; |
| 28 | |
| 29 | const getAllSites = () => { |
| 30 | const finalData = {}; |
| 31 | const builders = getBuilders(); |
| 32 | |
| 33 | builders.forEach( ( builder ) => { |
| 34 | const sitesData = sites && sites[ builder ] ? sites[ builder ] : {}; |
| 35 | finalData[ builder ] = [ ...Object.values( sitesData ) ]; |
| 36 | } ); |
| 37 | |
| 38 | return finalData; |
| 39 | }; |
| 40 | |
| 41 | const filterByCategory = ( items, cat ) => { |
| 42 | if ( 'free' === cat ) { |
| 43 | return items.filter( ( item ) => ! item.upsell ); |
| 44 | } |
| 45 | |
| 46 | if ( 'all' !== cat ) { |
| 47 | return items.filter( ( item ) => item.keywords.includes( cat ) ); |
| 48 | } |
| 49 | |
| 50 | return items; |
| 51 | }; |
| 52 | |
| 53 | const filterBySearch = ( items ) => { |
| 54 | if ( ! searchQuery ) { |
| 55 | return items; |
| 56 | } |
| 57 | |
| 58 | const fuse = new Fuse( items, { |
| 59 | includeScore: true, |
| 60 | keys: [ 'title', 'slug', 'keywords' ], |
| 61 | } ); |
| 62 | return fuse.search( searchQuery ).map( ( item ) => item.item ); |
| 63 | }; |
| 64 | |
| 65 | const getSitesForBuilder = ( builder ) => { |
| 66 | const allSites = getAllSites(); |
| 67 | return allSites[ builder ]; |
| 68 | }; |
| 69 | |
| 70 | const getBuilders = () => Object.keys( sites ); |
| 71 | |
| 72 | const getFilteredSites = () => { |
| 73 | const allSites = getAllSites(); |
| 74 | let builderSites = allSites[ editor ]; |
| 75 | builderSites = filterBySearch( builderSites ); |
| 76 | builderSites = filterByCategory( builderSites, category ); |
| 77 | |
| 78 | return builderSites; |
| 79 | }; |
| 80 | |
| 81 | const Sites = () => { |
| 82 | const [ maxShown, setMaxShown ] = useState( 9 ); |
| 83 | const allData = getFilteredSites(); |
| 84 | return ( |
| 85 | <div className="ob-sites is-grid"> |
| 86 | { allData.slice( 0, maxShown ).map( ( site, index ) => { |
| 87 | return <StarterSiteCard key={ index } data={ site } />; |
| 88 | } ) } |
| 89 | |
| 90 | <VizSensor |
| 91 | onChange={ ( isVisible ) => { |
| 92 | if ( ! isVisible ) { |
| 93 | return false; |
| 94 | } |
| 95 | setMaxShown( maxShown + 9 ); |
| 96 | } } |
| 97 | > |
| 98 | <span |
| 99 | style={ { |
| 100 | height: 10, |
| 101 | width: 10, |
| 102 | display: 'block', |
| 103 | } } |
| 104 | /> |
| 105 | </VizSensor> |
| 106 | </div> |
| 107 | ); |
| 108 | }; |
| 109 | |
| 110 | const getSiteNav = ( prev = false ) => { |
| 111 | if ( null === currentSiteData ) { |
| 112 | return null; |
| 113 | } |
| 114 | const allSites = getAllSites()[ editor ]; |
| 115 | const position = allSites.indexOf( currentSiteData ); |
| 116 | |
| 117 | if ( -1 === position ) { |
| 118 | return null; |
| 119 | } |
| 120 | |
| 121 | if ( 1 === allSites.length ) { |
| 122 | return null; |
| 123 | } |
| 124 | |
| 125 | if ( prev && 0 === position ) { |
| 126 | return allSites[ allSites.length - 1 ]; |
| 127 | } |
| 128 | |
| 129 | if ( ! prev && position === allSites.length - 1 ) { |
| 130 | return allSites[ 0 ]; |
| 131 | } |
| 132 | |
| 133 | return allSites[ prev ? position - 1 : position + 1 ]; |
| 134 | }; |
| 135 | |
| 136 | if ( 1 > sites.length ) { |
| 137 | return ( |
| 138 | <> |
| 139 | <p> |
| 140 | { __( |
| 141 | 'Starter sites could not be loaded. Please refresh and try again.', |
| 142 | 'templates-patterns-collection' |
| 143 | ) } |
| 144 | { isOnboarding && ( |
| 145 | <Button |
| 146 | style={ { |
| 147 | display: 'block', |
| 148 | margin: '20px auto', |
| 149 | } } |
| 150 | isPrimary |
| 151 | onClick={ cancelOnboarding } |
| 152 | > |
| 153 | { __( 'Close', 'templates-patterns-collection' ) } |
| 154 | </Button> |
| 155 | ) } |
| 156 | </p> |
| 157 | </> |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | return ( |
| 162 | <> |
| 163 | <Filters |
| 164 | getSitesForBuilder={ getSitesForBuilder } |
| 165 | filterBySearch={ filterBySearch } |
| 166 | filterByCategory={ filterByCategory } |
| 167 | /> |
| 168 | { 0 === getFilteredSites().length && ( |
| 169 | <div className="no-results"> |
| 170 | <p> |
| 171 | { __( |
| 172 | 'No results found.', |
| 173 | 'templates-patterns-collection' |
| 174 | ) } |
| 175 | |
| 176 | { __( |
| 177 | 'You can try a different search or use one of the categories below.', |
| 178 | 'templates-patterns-collection' |
| 179 | ) } |
| 180 | </p> |
| 181 | <div className="tags"> |
| 182 | { TAGS.map( ( tag, index ) => { |
| 183 | return ( |
| 184 | <Button |
| 185 | key={ index } |
| 186 | isPrimary |
| 187 | className="tag" |
| 188 | onClick={ ( e ) => { |
| 189 | e.preventDefault(); |
| 190 | setSearchQuery( tag ); |
| 191 | resetCategory(); |
| 192 | } } |
| 193 | > |
| 194 | { tag } |
| 195 | </Button> |
| 196 | ); |
| 197 | } ) } |
| 198 | </div> |
| 199 | </div> |
| 200 | ) } |
| 201 | <Sites /> |
| 202 | { previewOpen && currentSiteData && ( |
| 203 | <PreviewFrame |
| 204 | next={ getSiteNav() } |
| 205 | prev={ getSiteNav( true ) } |
| 206 | /> |
| 207 | ) } |
| 208 | </> |
| 209 | ); |
| 210 | }; |
| 211 | |
| 212 | export default compose( |
| 213 | withDispatch( ( dispatch ) => { |
| 214 | const { |
| 215 | setOnboardingState, |
| 216 | setCurrentCategory, |
| 217 | setCurrentTab, |
| 218 | setSearchQuery, |
| 219 | } = dispatch( 'neve-onboarding' ); |
| 220 | return { |
| 221 | cancelOnboarding: () => { |
| 222 | setOnboardingState( false ); |
| 223 | }, |
| 224 | resetCategory: () => { |
| 225 | setCurrentCategory( 'all' ); |
| 226 | }, |
| 227 | setCurrentTab, |
| 228 | setSearchQuery, |
| 229 | }; |
| 230 | } ), |
| 231 | withSelect( ( select ) => { |
| 232 | const { |
| 233 | getCurrentEditor, |
| 234 | getCurrentCategory, |
| 235 | getPreviewStatus, |
| 236 | getCurrentSite, |
| 237 | getImportModalStatus, |
| 238 | getOnboardingStatus, |
| 239 | getSites, |
| 240 | getInstallModalStatus, |
| 241 | getCurrentTab, |
| 242 | getSearchQuery, |
| 243 | } = select( 'neve-onboarding' ); |
| 244 | return { |
| 245 | editor: getCurrentEditor(), |
| 246 | category: getCurrentCategory(), |
| 247 | previewOpen: getPreviewStatus(), |
| 248 | currentSiteData: getCurrentSite(), |
| 249 | importModal: getImportModalStatus(), |
| 250 | installModal: getInstallModalStatus(), |
| 251 | isOnboarding: getOnboardingStatus(), |
| 252 | getSites: getSites(), |
| 253 | currentTab: getCurrentTab(), |
| 254 | searchQuery: getSearchQuery(), |
| 255 | }; |
| 256 | } ) |
| 257 | )( OnboardingContent ); |
| 258 |