Skip to content

Commit

Permalink
add netlify-auth dynamic asset search
Browse files Browse the repository at this point in the history
  • Loading branch information
erquhart committed Sep 8, 2017
1 parent 2b0ce0c commit fde75b9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
"unified": "^6.1.4",
"unist-builder": "^1.0.2",
"unist-util-visit-parents": "^1.1.1",
"url": "^0.11.0",
"uuid": "^2.0.3",
"whatwg-fetch": "^1.0.0"
},
Expand Down
10 changes: 5 additions & 5 deletions src/actions/mediaLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ export function insertMedia(mediaPath) {
return { type: MEDIA_INSERT, payload: { mediaPath } };
}

export function loadMedia(delay = 0) {
export function loadMedia(delay = 0, query) {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
const integration = selectIntegration(state, null, 'assetStore');
if (integration) {
const provider = getIntegrationProvider(state.integrations, backend.getToken, integration);
dispatch(mediaLoading());
return provider.retrieve()
.then(files => dispatch(mediaLoaded(files)))
return provider.retrieve(query)
.then(files => dispatch(mediaLoaded(files, true)))
.catch(error => dispatch(mediaLoadFailed()));
}
dispatch(mediaLoading());
Expand Down Expand Up @@ -128,10 +128,10 @@ export function mediaLoading() {
return { type: MEDIA_LOAD_REQUEST };
}

export function mediaLoaded(files) {
export function mediaLoaded(files, dynamicSearch) {
return {
type: MEDIA_LOAD_SUCCESS,
payload: { files }
payload: { files, dynamicSearch }
};
}

Expand Down
26 changes: 20 additions & 6 deletions src/components/MediaLibrary/MediaLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MediaLibrary extends React.Component {

componentDidMount() {
const { dispatch, closeMediaLibrary } = this.props;
dispatch(loadMedia());
dispatch(loadMedia(0, this.state.query));
}

componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -113,7 +113,7 @@ class MediaLibrary extends React.Component {
const files = [...fileList];
const file = files[0];
return dispatch(persistMedia(file, privateUpload))
.then(() => dispatch(loadMedia()));
.then(() => dispatch(0, loadMedia(this.state.query)));
};

handleInsert = () => {
Expand All @@ -138,7 +138,13 @@ class MediaLibrary extends React.Component {
});
};

handleSearch = event => {
handleSearchKeyDown = (event, dynamicSearch) => {
if (event.key === 'Enter' && dynamicSearch) {
this.props.dispatch(loadMedia(0, this.state.query));
}
};

handleSearchChange = event => {
this.setState({ query: event.target.value });
};

Expand Down Expand Up @@ -191,10 +197,10 @@ class MediaLibrary extends React.Component {
};

render() {
const { isVisible, canInsert, files, forImage, isLoading, isPersisting, isDeleting } = this.props;
const { isVisible, canInsert, files, dynamicSearch, forImage, isLoading, isPersisting, isDeleting } = this.props;
const { query, selectedFile } = this.state;
const filteredFiles = forImage ? this.filterImages(files) : files;
const queriedFiles = query ? this.queryFilter(query, filteredFiles) : filteredFiles;
const queriedFiles = query && !dynamicSearch ? this.queryFilter(query, filteredFiles) : filteredFiles;
const tableData = this.toTableData(queriedFiles);
const hasFiles = files && !!files.length;
const hasImages = filteredFiles && !!filteredFiles.length;
Expand Down Expand Up @@ -227,7 +233,15 @@ class MediaLibrary extends React.Component {
footer={footer}
>
<h1>{forImage ? 'Images' : 'Assets'}</h1>
<input className={styles.searchInput} type="text" value={this.state.query} onChange={this.handleSearch.bind(this)} placeholder="Search..." disabled={!hasFiles || !hasImages} autoFocus/>
<input
className={styles.searchInput}
value={this.state.query}
onChange={this.handleSearchChange}
onKeyDown={event => this.handleSearchKeyDown(event, dynamicSearch)}
placeholder="Search..."
disabled={!hasFiles || !hasImages}
autoFocus
/>
<div style={{ height: '100%', paddingBottom: '130px' }}>
<div style={{ height: '100%', overflowY: 'auto' }} ref={ref => this.tableScrollRef = ref}>
<Table onRowSelect={idx => this.handleRowSelect(tableData[idx])}>
Expand Down
9 changes: 7 additions & 2 deletions src/integrations/providers/assetStore/implementation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { addParams } from '../../../lib/urlHelper';

export default class AssetStore {
constructor(config, getToken) {
this.config = config;
Expand Down Expand Up @@ -66,9 +68,12 @@ export default class AssetStore {
});
}

retrieve() {
retrieve(query) {
const url = query ? addParams(this.getSignedFormURL, { search: query }) : this.getSignedFormURL;
console.log(url);

return this.getToken()
.then(token => this.request(this.getSignedFormURL, {
.then(token => this.request(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Expand Down
8 changes: 8 additions & 0 deletions src/lib/urlHelper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import url from 'url';

function getUrl(url, direct) {
return `${ direct ? '/#' : '' }${ url }`;
}
Expand All @@ -9,3 +11,9 @@ export function getCollectionUrl(collectionName, direct) {
export function getNewEntryUrl(collectionName, direct) {
return getUrl(`/collections/${ collectionName }/entries/new`, direct);
}

export function addParams(urlString, params) {
const parsedUrl = url.parse(urlString, true);
parsedUrl.query = { ...parsedUrl.query, ...params };
return url.format(parsedUrl);
}
5 changes: 3 additions & 2 deletions src/reducers/mediaLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ const mediaLibrary = (state = Map({ isVisible: false, controlMedia: Map() }), ac
return state.set('isLoading', true);
case MEDIA_LOAD_SUCCESS:
return state.withMutations(map => {
map.set('isLoading', false)
map.set('files', action.payload.files)
map.set('isLoading', false);
map.set('files', action.payload.files);
map.set('dynamicSearch', action.payload.dynamicSearch);
});
case MEDIA_LOAD_FAILURE:
return state.set('isLoading', false);
Expand Down

0 comments on commit fde75b9

Please sign in to comment.