Skip to content

Commit

Permalink
Merge pull request #1611 from emanueldima/fix-ui-private-record-file-…
Browse files Browse the repository at this point in the history
…stats

Fix ui for private record (interaction with file stats)
  • Loading branch information
nharraud authored Dec 15, 2017
2 parents 4b46346 + 64bd80b commit 557c74f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 65 deletions.
5 changes: 3 additions & 2 deletions webui/src/components/record.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ const Record = React.createClass({

const isLatestVersion = !record.has('versions') || recordID == record.getIn(['versions', 0, 'id']);
function onNewVersion (e) {
e.preventDefault();
serverCache.createRecordVersion(record, newRecordID => browser.gotoEditRecord(newRecordID));
}

Expand Down Expand Up @@ -433,8 +434,8 @@ const Record = React.createClass({
: false
}
{ isRecordOwner(record) && isLatestVersion ?
<Link onClick={onNewVersion} className="btn btn-warning" style={{margin: '0 0.5em'}}>
Create New Version</Link>
<a href='#' onClick={onNewVersion} className="btn btn-warning" style={{margin: '0 0.5em'}}>
Create New Version</a>
: false
}
</div>
Expand Down
5 changes: 3 additions & 2 deletions webui/src/components/versions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ const DraftVersions = React.createClass({
return (
<div style={style}>
You are now creating a new version of
<Link onClick={e=>browser.gotoRecord(versions[0].id)} href="#"> this published record</Link>.
<a href="#" onClick={e => { e.preventDefault(); browser.gotoRecord(versions[0].id)} }
> this published record</a>.
</div>
);
}
Expand Down Expand Up @@ -86,7 +87,7 @@ const PublishedVersions = React.createClass({
{ beQuiet ? "" : "This record has newer versions. " }
</div>

<div style={{display: 'inline-block', verticalAlign: 'middle', marginBottom: '1px', padding: '0', width: '17em'}}>
<div style={{display: 'inline-block', verticalAlign: 'middle', marginBottom: '1px', padding: '0px', width: '17em'}}>
<DropdownList data={versions} defaultValue={thisVersion}
valueComponent={VerItemRenderer} itemComponent={VerItemRenderer}
onChange={handleVersionChange}/>
Expand Down
105 changes: 44 additions & 61 deletions webui/src/data/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,55 @@ class ServerCache {
});
}

function fetchFileStats(store, recordID, bucketID) {
var data = {
"fileDownloads": {
"stat": "bucket-file-download-total",
"params": {
"bucket_id": bucketID,
}
}
};
ajaxPost({
url: apiUrls.statistics(),
params: data,
successFn: response => {
const fileDownloads = response && response.fileDownloads;
if (!fileDownloads) {
return;
}
fileDownloads.buckets.forEach(kv => {
const index = store.getIn(['recordCache', recordID, 'files']).findIndex(f => f.get('key') == kv.key);
if (index >= 0) {
store.setIn(['recordCache', recordID, 'files', index, 'downloads'], kv.value);
} else {
console.error('cannot find file with key: ', kv.key);
}
});
},
});
}

this.getters.record = new Pool(recordID =>
new Getter(apiUrls.record(recordID), null,
(data) => {
if (data.files) { data.files = data.files.map(this.fixFile); }
this.store.setIn(['recordCache', recordID], fromJS(data));
if (data.files && data.files[0]) {
fetchFileStats(this.store, recordID, data.files[0].bucket);
} else if (!data.files && data.links.files) {
// private or embargoed record, needs manual file fetching
ajaxGet({
url: data.links.files,
successFn: (filedata) => {
const files = filedata.contents.map(this.fixFile);
this.store.setIn(['recordCache', recordID, 'files'], fromJS(files));
// do not fetch file statistiscs for private files
// (these files are missing the 'bucket' field)
},
errorFn: (xhr) => this.store.setIn(['recordCache', recordID, 'files'], new Error(xhr)),
});
}
retrieveVersions(this.store, data.links, ['recordCache', recordID, 'versions']);
},
(xhr) => this.store.setIn(['recordCache', recordID], new Error(xhr)) ));
Expand Down Expand Up @@ -556,70 +600,9 @@ class ServerCache {

getRecord(id) {
this.getters.record.get(id).fetch();
this.fetchRecordFiles(id);
return this.store.getIn(['recordCache', id]);
}

fetchRecordFiles(id) {
const record = this.store.getIn(['recordCache', id]);
if (!record || !record.get) {
return null;
}
const files = record.get('files');
if (files) {
// code path for published records
const file0 = files.get(0);
if (file0 && !file0.get('downloads')) {
this.fetchFileStats(id, file0.get('bucket'));
}
return files;
}
const url = record.getIn(['links', 'files']);
if (url) {
// code path for drafts
ajaxGet({
url: url,
successFn: (data) => {
const files = fromJS(data.contents.map(this.fixFile));
this.store.setIn(['recordCache', id, 'files'], files);
if (files && files.get(0)) {
this.fetchFileStats(id, files.get(0).get('bucket'));
}
},
errorFn: (xhr) => this.store.setIn(['recordCache', id, 'files'], new Error(xhr)),
});
}
}

fetchFileStats(recordID, bucketID) {
var data = {
"fileDownloads": {
"stat": "bucket-file-download-total",
"params": {
"bucket_id": bucketID,
}
}
};
ajaxPost({
url: apiUrls.statistics(),
params: data,
successFn: response => {
const fileDownloads = response && response.fileDownloads;
if (!fileDownloads) {
return;
}
fileDownloads.buckets.forEach(kv => {
const index = this.store.getIn(['recordCache', recordID, 'files']).findIndex(f => f.get('key') == kv.key);
if (index >= 0) {
this.store.setIn(['recordCache', recordID, 'files', index, 'downloads'], kv.value);
} else {
console.error('cannot find file with key: ', kv.key);
}
});
},
});
}

getDraft(id) {
this.getters.draft.get(id).fetch();
return this.store.getIn(['draftCache', id]);
Expand Down

0 comments on commit 557c74f

Please sign in to comment.