Skip to content

Commit

Permalink
Allow provider-specfic default citation styles
Browse files Browse the repository at this point in the history
  • Loading branch information
futa-ikeda committed Nov 14, 2023
1 parent b4964cb commit b938f4d
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/models/provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { attr, hasMany, SyncHasMany, AsyncHasMany } from '@ember-data/model';
import { computed } from '@ember/object';

import CitationStyleModel from './citation-style';
import LicenseModel from './license';
import ModeratorModel from './moderator';
import OsfModel from './osf-model';
Expand Down Expand Up @@ -84,6 +85,9 @@ export default abstract class ProviderModel extends OsfModel {
@hasMany('moderator', { inverse: 'provider' })
moderators!: AsyncHasMany<ModeratorModel> | ModeratorModel[];

@hasMany('citation-style', { inverse: null })
citationStyles!: AsyncHasMany<CitationStyleModel> & CitationStyleModel[];

@computed('permissions')
get currentUserCanReview() {
if (this.permissions) {
Expand Down
5 changes: 4 additions & 1 deletion app/preprints/detail/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@
{{/if}}
<div>
<h4>{{t 'preprints.detail.citations'}}</h4>
<CitationViewer @citable={{this.model.preprint}} />
<CitationViewer
@citable={{this.model.preprint}}
@provider={{this.model.provider}}
/>
</div>
</div>
{{/unless}}
Expand Down
24 changes: 18 additions & 6 deletions lib/osf-components/addon/components/citation-viewer/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { waitFor } from '@ember/test-waiters';
import { all, restartableTask, task, timeout } from 'ember-concurrency';

import { layout } from 'ember-osf-web/decorators/component';
import CitationStyleModel from 'ember-osf-web/models/citation-style';
import CitationStyle from 'ember-osf-web/models/citation-style';
import Node from 'ember-osf-web/models/node';
import Preprint from 'ember-osf-web/models/preprint';
import ProviderModel from 'ember-osf-web/models/provider';
import CurrentUser from 'ember-osf-web/services/current-user';
import fixSpecialChars from 'ember-osf-web/utils/fix-special-char';
import getRelatedHref from 'ember-osf-web/utils/get-related-href';
Expand All @@ -18,14 +20,14 @@ import template from './template';

interface DefaultCitation {
id: string;
displayTitle: string;
title: string;
citation?: string;
}

const defaultCitations: DefaultCitation[] = [
{ id: 'apa', displayTitle: 'APA' },
{ id: 'modern-language-association', displayTitle: 'MLA' },
{ id: 'chicago-author-date', displayTitle: 'Chicago' },
{ id: 'apa', title: 'APA' },
{ id: 'modern-language-association', title: 'MLA' },
{ id: 'chicago-author-date', title: 'Chicago' },
];

function citationUrl(citable: Node | Preprint, citationStyleId: string) {
Expand All @@ -46,6 +48,8 @@ export default class CitationViewer extends Component {
// Required parameter
citable!: Node | Preprint;

// Optional parameter
provider?: ProviderModel;

// Private properties
@service store!: Store;
Expand All @@ -68,13 +72,21 @@ export default class CitationViewer extends Component {
@task({ on: 'init' })
@waitFor
async loadDefaultCitations() {
let citations: CitationStyleModel[] | DefaultCitation[] = [];
if (this.provider) {
citations = (await this.provider.citationStyles).toArray();
}
if (citations.length === 0) {
citations = defaultCitations;
}
const responses: SingleResourceDocument[] = await all(
defaultCitations.map(
citations.map(
c => this.currentUser.authenticatedAJAX({ url: citationUrl(this.citable, c.id) }),
),
);
return responses.map((r, i) => ({
...defaultCitations[i],
...citations[i],
title: citations[i].title,
citation: typeof r.data.attributes!.citation === 'string'
? fixSpecialChars(r.data.attributes!.citation)
: r.data.attributes!.citation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<LoadingIndicator @dark={{true}} />
{{else}}
{{#each this.loadDefaultCitations.last.value as |citation|}}
<h5>{{citation.displayTitle}}</h5>
<p data-analytics-scope='{{citation.displayTitle}} citation'>
<h5>{{citation.title}}</h5>
<p data-analytics-scope='{{citation.title}} citation'>
<CopyableText
data-test-default-citation={{citation.id}}
@text={{citation.citation}}
Expand Down
6 changes: 6 additions & 0 deletions mirage/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ export default function(this: Server) {
relatedModelName: 'preprint',
});

osfNestedResource(this, 'preprint-provider', 'citationStyles', {
only: ['index'],
path: '/providers/preprints/:parentID/citation_styles/',
relatedModelName: 'citation-style',
});

/**
* Preprint Details
*/
Expand Down
1 change: 1 addition & 0 deletions mirage/scenarios/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function(server: Server) {
server.loadFixtures('regions');
server.loadFixtures('preprint-providers');
server.loadFixtures('licenses');
server.loadFixtures('citation-styles');
// server.loadFixtures('registration-providers');

const userTraits = !mirageScenarios.includes('loggedIn') ? []
Expand Down
3 changes: 3 additions & 0 deletions mirage/scenarios/preprints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ function buildrXiv(
currentUser: ModelInstance<User>,
) {
const preprintrxiv = server.schema.preprintProviders.find('preprintrxiv') as ModelInstance<PreprintProvider>;
preprintrxiv.update({
citationStyles: [server.schema.citationStyles.find('another-citation')],
});

const brand = server.create('brand', {
primaryColor: '#286090',
Expand Down
8 changes: 8 additions & 0 deletions mirage/serializers/preprint-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ export default class PreprintProviderSerializer extends ApplicationSerializer<Pr
},
},
},
citationStyles: {
links: {
related: {
href: `${apiUrl}/v2/providers/preprints/${model.id}/citation_styles/`,
meta: {},
},
},
},
// TODO: subscriptions when we move ember-osf-reviews¥
};

Expand Down
1 change: 1 addition & 0 deletions mirage/views/citation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function getCitation(this: HandlerContext, schema: Schema, request: Reque
id: citationStyleID,
type: 'citations',
attributes: {
title: citationStyle.title,
citation: `Pretend citation for "${citable.title}" in the style "${citationStyle.title}"`,
},
},
Expand Down

0 comments on commit b938f4d

Please sign in to comment.