-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Components: Display site icon by performing media lookup of icon ID
- Loading branch information
Showing
10 changed files
with
407 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* Returns a media object by site ID, media ID, or null if not known | ||
* | ||
* @param {Object} state Global state tree | ||
* @param {Number} siteId Site ID | ||
* @param {Number} mediaId Media ID | ||
* @return {?Object} Media object, if known | ||
*/ | ||
export default function getMediaItem( state, siteId, mediaId ) { | ||
return get( state.media.items, [ siteId, mediaId ], null ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import safeImageUrl from 'lib/safe-image-url'; | ||
import { getMediaItem } from './'; | ||
|
||
/** | ||
* Returns the URL for a media item, or null if not known | ||
* | ||
* @param {Object} state Global state tree | ||
* @param {Number} siteId Site ID | ||
* @param {Number} mediaId Media ID | ||
* @return {?String} Media URL, if known | ||
*/ | ||
export default function getMediaUrl( state, siteId, mediaId ) { | ||
const media = getMediaItem( state, siteId, mediaId ); | ||
if ( ! media ) { | ||
return null; | ||
} | ||
|
||
return safeImageUrl( media.URL ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getRawSite } from 'state/sites/selectors'; | ||
import { getSiteSettings } from 'state/site-settings/selectors'; | ||
|
||
/** | ||
* Returns a ID to the media associated with a site's current site icon, or | ||
* null if not known or an icon is not assigned. | ||
* | ||
* @param {Object} state Global state tree | ||
* @param {Number} siteId Site ID | ||
* @return {?Number} Media ID of site icon, if known and exists | ||
*/ | ||
export default function getSiteIconId( state, siteId ) { | ||
// Treat site object as preferred source of truth of media ID | ||
const site = getRawSite( state, siteId ); | ||
if ( site ) { | ||
return get( site, 'icon.media_id', null ); | ||
} | ||
|
||
// Fall back to site settings in case we know settings prior to having | ||
// received the site itself | ||
const settings = getSiteSettings( state, siteId ); | ||
if ( settings ) { | ||
return settings.site_icon; | ||
} | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getRawSite } from 'state/sites/selectors'; | ||
import { getSiteIconId, getMediaUrl } from './'; | ||
|
||
/** | ||
* Returns a URL to the site's current site icon, or null if no icon exists or | ||
* if site is not known | ||
* | ||
* @param {Object} state Global state tree | ||
* @param {Number} siteId Site ID | ||
* @return {?String} URL of site icon, if known and exists | ||
*/ | ||
export default function getSiteIconUrl( state, siteId ) { | ||
const iconId = getSiteIconId( state, siteId ); | ||
const url = getMediaUrl( state, siteId, iconId ); | ||
if ( url ) { | ||
return url; | ||
} | ||
|
||
// If cannot find media by ID, use icon.img property if available, | ||
// otherwise assume icon is not set | ||
return get( getRawSite( state, siteId ), 'icon.img', null ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getMediaItem } from '../'; | ||
|
||
describe( 'getMediaItem()', () => { | ||
it( 'should return null if the site is not in state', () => { | ||
const item = getMediaItem( { | ||
media: { | ||
items: {} | ||
} | ||
}, 2916284, 42 ); | ||
|
||
expect( item ).to.be.null; | ||
} ); | ||
|
||
it( 'should return null if the media for the site is not in state', () => { | ||
const item = getMediaItem( { | ||
media: { | ||
items: { | ||
2916284: {} | ||
} | ||
} | ||
}, 2916284, 42 ); | ||
|
||
expect( item ).to.be.null; | ||
} ); | ||
|
||
it( 'should return the media item', () => { | ||
const item = getMediaItem( { | ||
media: { | ||
items: { | ||
2916284: { | ||
42: { ID: 42, title: 'flowers' } | ||
} | ||
} | ||
} | ||
}, 2916284, 42 ); | ||
|
||
expect( item ).to.eql( { ID: 42, title: 'flowers' } ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getMediaUrl } from '../'; | ||
|
||
describe( 'getMediaUrl()', () => { | ||
it( 'should return null if the item is not in state', () => { | ||
const url = getMediaUrl( { | ||
media: { | ||
items: { | ||
2916284: {} | ||
} | ||
} | ||
}, 2916284, 42 ); | ||
|
||
expect( url ).to.be.null; | ||
} ); | ||
|
||
it( 'should return null if the media item URL is invalid', () => { | ||
const url = getMediaUrl( { | ||
media: { | ||
items: { | ||
2916284: { | ||
42: { ID: 42, title: 'flowers' } | ||
} | ||
} | ||
} | ||
}, 2916284, 42 ); | ||
|
||
expect( url ).to.be.null; | ||
} ); | ||
|
||
it( 'should return a safe variation of the media URL', () => { | ||
const url = getMediaUrl( { | ||
media: { | ||
items: { | ||
2916284: { | ||
42: { | ||
ID: 42, | ||
title: 'flowers', | ||
URL: 'https://example.files.wordpress.com/2014/06/flower.gif' | ||
} | ||
} | ||
} | ||
} | ||
}, 2916284, 42 ); | ||
|
||
expect( url ).to.equal( 'https://example.files.wordpress.com/2014/06/flower.gif' ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getSiteIconId } from '../'; | ||
|
||
describe( 'getSiteIconId()', () => { | ||
it( 'should return null if neither the site nor settings are known', () => { | ||
const id = getSiteIconId( { | ||
sites: { | ||
items: {} | ||
}, | ||
siteSettings: { | ||
items: {} | ||
} | ||
}, 2916284 ); | ||
|
||
expect( id ).to.be.null; | ||
} ); | ||
|
||
it( 'should prefer site state', () => { | ||
const id = getSiteIconId( { | ||
sites: { | ||
items: { | ||
2916284: { | ||
ID: 2916284, | ||
name: 'WordPress.com Example Blog', | ||
icon: { | ||
media_id: 42 | ||
} | ||
} | ||
} | ||
}, | ||
siteSettings: { | ||
items: { | ||
2916284: { | ||
site_icon: 36 | ||
} | ||
} | ||
} | ||
}, 2916284 ); | ||
|
||
expect( id ).to.equal( 42 ); | ||
} ); | ||
|
||
it( 'should prefer site state, even if unset', () => { | ||
const id = getSiteIconId( { | ||
sites: { | ||
items: { | ||
2916284: { | ||
ID: 2916284, | ||
name: 'WordPress.com Example Blog' | ||
} | ||
} | ||
}, | ||
siteSettings: { | ||
items: { | ||
2916284: { | ||
site_icon: 42 | ||
} | ||
} | ||
} | ||
}, 2916284 ); | ||
|
||
expect( id ).to.be.null; | ||
} ); | ||
|
||
it( 'should fall back to settings state', () => { | ||
const id = getSiteIconId( { | ||
sites: { | ||
items: {} | ||
}, | ||
siteSettings: { | ||
items: { | ||
2916284: { | ||
site_icon: 42 | ||
} | ||
} | ||
} | ||
}, 2916284 ); | ||
|
||
expect( id ).to.equal( 42 ); | ||
} ); | ||
} ); |
Oops, something went wrong.