Skip to content

Commit

Permalink
Update preprint-doi component to handle versions
Browse files Browse the repository at this point in the history
  • Loading branch information
futa-ikeda committed Dec 4, 2024
1 parent 500e4bd commit 738dd01
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 23 deletions.
82 changes: 82 additions & 0 deletions app/preprints/-components/preprint-doi/component-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { click } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import { setupMirage } from 'ember-cli-mirage/test-support';
import { ModelInstance } from 'ember-cli-mirage';

import PreprintProviderModel from 'ember-osf-web/models/preprint-provider';
import PreprintModel from 'ember-osf-web/models/preprint';

module('Integration | Component | preprint-doi', function(hooks) {
setupRenderingTest(hooks);
setupMirage(hooks);

test('it renders', async function(assert) {
this.store = this.owner.lookup('service:store');
server.loadFixtures('preprint-providers');
const mirageProvider = server.schema.preprintProviders.find('osf') as ModelInstance<PreprintProviderModel>;
const miragePreprint = server.create('preprint', {
id: 'doied',
provider: mirageProvider,
}, 'withVersions');
// Version 1 has a DOI and has a preprintDoiCreated date
const version1 = server.schema.preprints.find('doied_v1') as ModelInstance<PreprintModel>;
version1.update({ preprintDoiCreated: new Date('2020-02-02') });
// Version 2 has a DOI but no preprintDoiCreated date
const version2 = server.schema.preprints.find('doied_v2') as ModelInstance<PreprintModel>;
version2.update({ preprintDoiCreated: null });
// Version 3 is pending moderator approval and is not published, therefore has no DOI
const version3 = server.schema.preprints.find('doied_v3') as ModelInstance<PreprintModel>;
version3.update({
preprintDoiCreated: null,
isPublished: false,
isPreprintDoi: false, // Mirage flag used to determine if a DOI should be created
});

const preprint = await this.store.findRecord('preprint', miragePreprint.id);
const versions = await preprint.queryHasMany('versions');

const provider = await this.store.findRecord('preprint-provider', mirageProvider.id);
this.set('versions', versions);
this.set('provider', provider);

await render(hbs`
<Preprints::-Components::PreprintDoi
@versions={{this.versions}}
@provider={{this.provider}}
/>
`);

// check headings exist
assert.dom('[data-test-preprint-doi-heading]').exists('Preprint DOI heading exists');
assert.dom('[data-test-preprint-doi-heading]').hasText('Preprint DOI', 'Preprint DOI heading has correct text');

// check dropdown exists
assert.dom('[data-test-version-select-dropdown]').exists('Version select dropdown exists');
assert.dom('[data-test-version-select-dropdown]')
.hasText('Version 3', 'Dropdown has latest version selected by default');

// check version3 has no DOI
assert.dom('[data-test-no-doi-text]').exists('No DOI text exists');
assert.dom('[data-test-no-doi-text]').hasText('DOI created after moderator approval', 'No DOI text is correct');

// check version2 has DOI, but no preprintDoiCreated date
await click('[data-test-version-select-dropdown]');
await click('[data-test-preprint-version="2"]');
assert.dom('[data-test-no-doi-text]').doesNotExist('No DOI text does not exist');
assert.dom('[data-test-unlinked-doi-url]').exists('Preprint DOI URL exists');
assert.dom('[data-test-unlinked-doi-description]').exists('Preprint DOI description exists');
assert.dom('[data-test-unlinked-doi-description]')
// eslint-disable-next-line max-len
.hasText('DOIs are minted by a third party, and may take up to 24 hours to be registered.', 'Description is correct');

// check version1 has DOI and preprintDoiCreated date
await click('[data-test-version-select-dropdown]');
await click('[data-test-preprint-version="1"]');
assert.dom('[data-test-unlinked-doi-url]').doesNotExist('Unlinked preprint DOI URL does not exist');
assert.dom('[data-test-unlinked-doi-description]').doesNotExist('Unlinked description does not exist');
assert.dom('[data-test-linked-doi-url]').exists('Preprint DOI URL exists');
});
});
12 changes: 10 additions & 2 deletions app/preprints/-components/preprint-doi/component.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { action } from '@ember/object';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import PreprintModel from 'ember-osf-web/models/preprint';
import PreprintProviderModel from 'ember-osf-web/models/preprint-provider';

interface InputArgs {
preprint: PreprintModel;
versions: PreprintModel[];
provider: PreprintProviderModel;
}

export default class PreprintAbstract extends Component<InputArgs> {
provider = this.args.provider;
documentType = this.provider.documentType.singularCapitalized;

documentType = this.provider.documentType.singular;
@tracked selectedVersion = this.args.versions[0];

@action
selectVersion(version: PreprintModel) {
this.selectedVersion = version;
}
}
61 changes: 43 additions & 18 deletions app/preprints/-components/preprint-doi/template.hbs
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
<div>
<h4>{{t 'preprints.detail.preprint_doi' documentType=this.documentType}}</h4>
{{#if @preprint.preprintDoiUrl}}
{{#if @preprint.preprintDoiCreated}}
<OsfLink
data-test-article-doi
data-analytics-name='preprint doi'
@href={{@preprint.preprintDoiUrl}}
<h4 data-test-preprint-doi-heading>
{{t 'preprints.detail.preprint_doi' documentType=this.documentType}}
</h4>
{{#if @versions}}
<PowerSelect
data-test-version-select-dropdown
@options={{@versions}}
@selected={{this.selectedVersion}}
@onChange={{this.selectVersion}}
as |version|
>
<span
data-test-preprint-version={{version.preprintVersion}}
>
{{@preprint.preprintDoiUrl}}
</OsfLink>
{{else}}
<p> {{@preprint.preprintDoiUrl}} </p>
<p> {{t 'preprints.detail.preprint_pending_doi_minted'}} </p>
{{/if}}
{{t 'preprints.detail.version_doi_title' number=version.preprintVersion}}
</span>
</PowerSelect>
{{else}}
{{#if (not @preprint.public)}}
{{t 'preprints.detail.preprint_pending_doi' documentType=this.documentType}}
{{else if (and this.provider.reviewsWorkflow (not this.preprint.isPublished))}}
{{t 'preprints.detail.preprint_pending_doi_moderation'}}
<p>{{t 'preprints.detail.no_versions'}}</p>
{{/if}}
{{#if this.selectedVersion}}
{{#if this.selectedVersion.preprintDoiUrl}}
{{#if this.selectedVersion.preprintDoiCreated}}
<OsfLink
data-test-linked-doi-url
data-analytics-name='preprint doi'
@href={{this.selectedVersion.preprintDoiUrl}}
>
{{this.selectedVersion.preprintDoiUrl}}
</OsfLink>
{{else}}
<p data-test-unlinked-doi-url> {{this.selectedVersion.preprintDoiUrl}} </p>
<p data-test-unlinked-doi-description> {{t 'preprints.detail.preprint_pending_doi_minted'}} </p>
{{/if}}
{{else}}
<p data-test-no-doi-text>
{{#if (not this.selectedVersion.public)}}
{{t 'preprints.detail.preprint_pending_doi' documentType=this.documentType}}
{{else if (and this.provider.reviewsWorkflow (not this.preprint.isPublished))}}
{{t 'preprints.detail.preprint_pending_doi_moderation'}}
{{else}}
{{t 'preprints.detail.no_doi'}}
{{/if}}
</p>
{{/if}}
{{/if}}
</div>
</div>
5 changes: 4 additions & 1 deletion app/preprints/detail/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@
</OsfLink>
</div>
{{/if}}
<Preprints::-Components::PreprintDoi @preprint={{this.model.preprint}} @provider={{this.model.provider}} />
<Preprints::-Components::PreprintDoi
@versions={{this.model.versions}}
@provider={{this.model.provider}}
/>
{{#if this.model.preprint.articleDoiUrl}}
<div>
<h4>{{t 'preprints.detail.article_doi'}}</h4>
Expand Down
4 changes: 3 additions & 1 deletion mirage/factories/preprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ export default Factory.extend<PreprintMirageModel & PreprintTraits>({
isLatestVersion: version === 3,
});
});
preprint.provider.update({ preprints: versionedPreprints });
if (preprint.provider) {
preprint.provider.update({ preprints: versionedPreprints });
}
},
}),

Expand Down
3 changes: 2 additions & 1 deletion mirage/views/preprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export function getPreprintVersions(this: HandlerContext, schema: Schema) {
const baseId = preprintId.split('_v')[0]; // assumes preprint id is of the form <baseId>_v<versionNumber>
const preprints = schema.preprints.all().models
.filter((preprint: ModelInstance<PreprintModel>) => preprint.id !== baseId && preprint.id.includes(baseId));
const versions = preprints.sortBy('versionNumber').reverse();
return process(schema, this.request, this,
preprints.map((preprint: ModelInstance) => this.serialize(preprint).data));
versions.map((version: ModelInstance) => this.serialize(version).data));
}
2 changes: 2 additions & 0 deletions translations/en-us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1431,8 +1431,10 @@ preprints:
original_publication_date: 'Original Publication Date'
orphan_preprint: 'The user has removed this file.'
preprint_doi: '{documentType} DOI'
version_doi_title: 'Version {number}'
preprint_pending_doi: 'DOI created after {documentType} is made public'
preprint_pending_doi_moderation: 'DOI created after moderator approval'
no_doi: 'No DOI'
preprint_pending_doi_minted: 'DOIs are minted by a third party, and may take up to 24 hours to be registered.'
private_preprint_warning: 'This {documentType} is private. Contact {supportEmail} if this is in error.'
project_button:
Expand Down

0 comments on commit 738dd01

Please sign in to comment.