Skip to content

Commit

Permalink
CR feedback; Refactor secondary metatadata components
Browse files Browse the repository at this point in the history
  • Loading branch information
futa-ikeda committed Oct 19, 2023
1 parent 4a77892 commit fe8e44b
Show file tree
Hide file tree
Showing 11 changed files with 419 additions and 336 deletions.
59 changes: 58 additions & 1 deletion app/models/index-card.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { getOwner } from '@ember/application';
import { inject as service } from '@ember/service';
import { waitFor } from '@ember/test-waiters';
import Model, { AsyncHasMany, attr, hasMany } from '@ember-data/model';
import { dropTask } from 'ember-concurrency';
import IntlService from 'ember-intl/services/intl';

import GetLocalizedPropertyHelper from 'ember-osf-web/helpers/get-localized-property';
import { getOwner } from '@ember/application';
import config from 'ember-osf-web/config/environment';
import OsfModel from 'ember-osf-web/models/osf-model';
import { tracked } from 'tracked-built-ins';
const osfUrl = config.OSF.url;

export interface LanguageText {
'@language': string;
Expand All @@ -23,10 +29,28 @@ export default class IndexCardModel extends Model {

getLocalizedString = new GetLocalizedPropertyHelper(getOwner(this));

@tracked osfModel?: OsfModel;

get resourceId() {
return this.resourceIdentifier[0];
}

get osfModelType() {
const types = this.resourceMetadata.resourceType.map( (item: any) => item['@id']);
if (types.includes('Project') || types.includes('ProjectComponent')) {
return 'node';
} else if (types.includes('Registration') || types.includes('RegistrationComponent')) {
return 'registration';
} else if (types.includes('Preprint')) {
return 'preprint';
} else if (types.includes('Person') || types.includes('Agent')) {
return 'user';
} else if(types.includes('File')) {
return 'file';
}
return null;
}

get label() {
const possibleLabelKeys = ['displayLabel', 'name', 'title'];
for (const key of possibleLabelKeys) {
Expand All @@ -44,6 +68,39 @@ export default class IndexCardModel extends Model {
}
return '';
}

@dropTask
@waitFor
async getOsfModel() {
const identifier = this.resourceIdentifier;
if (identifier && this.osfModelType) {
const guid = this.guidFromIdentifierList(identifier);
const relatedCounts = this.osfModelType === 'user' ? 'nodes,registrations,preprints' : '';
if (guid) {
const osfModel = await this.store.findRecord(this.osfModelType, guid, {
adapterOptions: {
query: {
related_counts: relatedCounts,
},
},
reload: true,
});
this.osfModel = osfModel;
}
}
}

guidFromIdentifierList(ids: string[]) {
for (const iri of ids) {
if (iri && iri.startsWith(osfUrl)) {
const pathSegments = iri.slice(osfUrl.length).split('/').filter(Boolean);
if (pathSegments.length === 1) {
return pathSegments[0]; // one path segment; looks like osf-id
}
}
}
return null;
}
}

declare module 'ember-data/types/registries/model' {
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export default class UserModel extends OsfModel.extend(Validations) {
@hasMany('draft-registration')
draftRegistrations!: AsyncHasMany<DraftRegistrationModel>;

@hasMany('preprints')
@hasMany('preprint')
preprints!: AsyncHasMany<PreprintModel>;

@hasMany('institution', { inverse: 'users' })
Expand Down
61 changes: 15 additions & 46 deletions lib/osf-components/addon/components/search-result-card/component.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { waitFor } from '@ember/test-waiters';
import Store from '@ember-data/store';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import Intl from 'ember-intl/services/intl';
import { dropTask } from 'ember-concurrency';
import { taskFor } from 'ember-concurrency-ts';

import config from 'ember-osf-web/config/environment';
import SearchResultModel from 'ember-osf-web/models/search-result';
import UserModel from 'ember-osf-web/models/user';

const osfUrl = config.OSF.url;

interface Args {
result: SearchResultModel;
}
Expand All @@ -28,54 +22,29 @@ export default class SearchResultCard extends Component<Args> {
@action
toggleSecondaryMetadata() {
this.isOpenSecondaryMetadata = !this.isOpenSecondaryMetadata;
if (this.isUserResultCard && !this.osfUser) {
taskFor(this.getOsfUserModel).perform();
}
}

get cardTypeLabel() {
return this.intl.t(`osf-components.search-result-card.${this.args.result.resourceType}`);
}

get isUserResultCard() {
return this.args.result.resourceType === 'user';
}

get secondaryMetadataComponent() {
const { resourceType } = this.args.result;

return `search-result-card/${resourceType.replace('_component', '')}-secondary-metadata`;
}

@dropTask
@waitFor
async getOsfUserModel() {
const { result } = this.args;
if (result.resourceType === 'user') {
const { identifier } = result.resourceMetadata;
if (identifier) {
const guid = this.guidFromIdentifierList(identifier);
if (guid) {
const user = await this.store.findRecord('user', guid, {
adapterOptions: {
query: {
related_counts: 'nodes,registrations,preprints',
},
},
reload: true,
});
this.osfUser = user;
}
}
}
}

guidFromIdentifierList(ids: Array<{'@value': string}>) {
const osfId = ids.find(id => id['@value'].includes(osfUrl))?.['@value'];
if (osfId) {
// remove osfUrl from id and any leading/trailing slashes
return osfId.replace(osfUrl, '').replace(/^\/|\/$/g, '');
switch (resourceType) {
case 'project':
case 'project_component':
return 'search-result-card/project-secondary-metadata';
case 'registration':
case 'registration_component':
return 'search-result-card/registration-secondary-metadata';
case 'preprint':
return 'search-result-card/preprint-secondary-metadata';
case 'file':
return 'search-result-card/file-secondary-metadata';
case 'user':
return 'search-result-card/user-secondary-metadata';
default:
return null;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
<dl>
{{#if @result.fileTitle}}
<dt>{{t 'osf-components.search-result-card.title'}}</dt>
<dd>{{@result.fileTitle}}</dd>
{{/if}}
{{#if @result.description}}
<dt>{{t 'osf-components.search-result-card.description'}}</dt>
<dd local-class='description'>{{@result.description}}</dd>
{{/if}}
{{#if @result.nodeFunders}}
<dt>{{t 'osf-components.search-result-card.funder'}}</dt>
<dd>
<InlineList
@items={{@result.nodeFunders}}
@total={{@result.nodeFunders.count}}
@truncate={{5}}
as |list|
>
{{#if list.item}}
<a href={{list.item.identifier}} target='_blank' rel='noopener noreferrer'>{{list.item.name}}</a>
{{else if list.remainingCount}}
{{t 'osf-components.search-result-card.remaining_count' count=list.remainingCount}}
{{/if}}
</InlineList>
</dd>
{{/if}}
{{#if @result.resourceNature}}
<dt>{{t 'osf-components.search-result-card.resource_type'}}</dt>
<dd>{{@result.resourceNature}}</dd>
{{/if}}
{{#if @result.nodeLicense}}
<dt>{{t 'osf-components.search-result-card.license'}}</dt>
<dd><a href={{@result.nodeLicense.identifier}} target='_blank' rel='noopener noreferrer'>{{@result.nodeLicense.name}}</a></dd>
{{/if}}
{{#if @result.absoluteUrl}}
<dt>{{t 'osf-components.search-result-card.url'}}</dt>
<dd><a href={{@result.absoluteUrl}} target='_blank' rel='noopener noreferrer'>{{@result.absoluteUrl}}</a></dd>
{{/if}}
{{#if @result.doi}}
<dt>{{t 'osf-components.search-result-card.doi'}}</dt>
<dd>
<InlineList
@items={{@result.doi}}
@total={{@result.doi.count}}
@truncate={{5}}
as |list|
>
<a href={{list.item}} target='_blank' rel='noopener noreferrer'>{{list.item}}</a>
</InlineList>
</dd>
{{/if}}
</dl>
<CpPanel class={{@wrapperClass}} id={{@wrapperId}} @open={{@isOpen}} as |panel|>
<panel.body>
<hr>
<dl>
{{#if @result.fileTitle}}
<dt>{{t 'osf-components.search-result-card.title'}}</dt>
<dd>{{@result.fileTitle}}</dd>
{{/if}}
{{#if @result.description}}
<dt>{{t 'osf-components.search-result-card.description'}}</dt>
<dd local-class='description'>{{@result.description}}</dd>
{{/if}}
{{#if @result.nodeFunders}}
<dt>{{t 'osf-components.search-result-card.funder'}}</dt>
<dd>
<InlineList
@items={{@result.nodeFunders}}
@total={{@result.nodeFunders.count}}
@truncate={{5}}
as |list|
>
{{#if list.item}}
<a href={{list.item.identifier}} target='_blank' rel='noopener noreferrer'>{{list.item.name}}</a>
{{else if list.remainingCount}}
{{t 'osf-components.search-result-card.remaining_count' count=list.remainingCount}}
{{/if}}
</InlineList>
</dd>
{{/if}}
{{#if @result.resourceNature}}
<dt>{{t 'osf-components.search-result-card.resource_type'}}</dt>
<dd>{{@result.resourceNature}}</dd>
{{/if}}
{{#if @result.nodeLicense}}
<dt>{{t 'osf-components.search-result-card.license'}}</dt>
<dd><a href={{@result.nodeLicense.identifier}} target='_blank' rel='noopener noreferrer'>{{@result.nodeLicense.name}}</a></dd>
{{/if}}
{{#if @result.absoluteUrl}}
<dt>{{t 'osf-components.search-result-card.url'}}</dt>
<dd><a href={{@result.absoluteUrl}} target='_blank' rel='noopener noreferrer'>{{@result.absoluteUrl}}</a></dd>
{{/if}}
{{#if @result.doi}}
<dt>{{t 'osf-components.search-result-card.doi'}}</dt>
<dd>
<InlineList
@items={{@result.doi}}
@total={{@result.doi.count}}
@truncate={{5}}
as |list|
>
<a href={{list.item}} target='_blank' rel='noopener noreferrer'>{{list.item}}</a>
</InlineList>
</dd>
{{/if}}
</dl>
</panel.body>
</CpPanel>
Loading

0 comments on commit fe8e44b

Please sign in to comment.