Skip to content

Commit

Permalink
[ENG-6460] [ENG-6640] Add new preprint version workflow (#2427)
Browse files Browse the repository at this point in the history
-   Ticket: [ENG-6460] [ENG-6640]
-   Feature flag: n/a

## Purpose
- Add a new workflow to create a new preprint version

## Summary of Changes
- Add a new `preprint.new-version` route
- Add a new `newVersion` argument to the submission-flow component
  - Invoke `submission-flow` component in the new-version route
  • Loading branch information
futa-ikeda authored Dec 26, 2024
1 parent 5515ddc commit 701f265
Show file tree
Hide file tree
Showing 21 changed files with 399 additions and 78 deletions.
2 changes: 1 addition & 1 deletion app/preprints/-components/submit/file/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default class PreprintFile extends Component<FileArgs>{
}

@action
public async addNewfile(): Promise<void> {
public addNewfile(): void {
this.file = null;
this.isFileAttached = false;
this.isFileUploadDisplayed = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { Permission } from 'ember-osf-web/models/osf-model';
import { ReviewsState } from 'ember-osf-web/models/provider';
import { taskFor } from 'ember-concurrency-ts';
import InstitutionModel from 'ember-osf-web/models/institution';
import CurrentUserService from 'ember-osf-web/services/current-user';
import { ReviewActionTrigger } from 'ember-osf-web/models/review-action';

export enum PreprintStatusTypeEnum {
titleAndAbstract = 'titleAndAbstract',
Expand All @@ -34,6 +36,7 @@ interface StateMachineArgs {
preprint: PreprintModel;
setPageDirty: () => void;
resetPageDirty: () => void;
newVersion?: boolean;
}

/**
Expand All @@ -44,6 +47,8 @@ export default class PreprintStateMachine extends Component<StateMachineArgs>{
@service router!: RouterService;
@service intl!: Intl;
@service toast!: Toast;
@service currentUser!: CurrentUserService;

titleAndAbstractValidation = false;
fileValidation = false;
metadataValidation = false;
Expand All @@ -60,11 +65,16 @@ export default class PreprintStateMachine extends Component<StateMachineArgs>{
@tracked statusFlowIndex = 1;
@tracked isEditFlow = false;
@tracked displayFileUploadStep = true;
@tracked isNewVersionFlow = this.args.newVersion;
affiliatedInstitutions = [] as InstitutionModel[];

constructor(owner: unknown, args: StateMachineArgs) {
super(owner, args);

if (this.args.newVersion) {
this.preprint = this.args.preprint;
return;
}
if (this.args.preprint) {
this.preprint = this.args.preprint;
this.setValidationForEditFlow();
Expand Down Expand Up @@ -221,11 +231,41 @@ export default class PreprintStateMachine extends Component<StateMachineArgs>{
public async onSubmit(): Promise<void> {
this.args.resetPageDirty();

if (this.isNewVersionFlow) {
try {
await this.preprint.save();
let toastMessage = this.intl.t('preprints.submit.new-version.success');

if (this.provider.reviewsWorkflow) {
toastMessage = this.intl.t('preprints.submit.new-version.success-review');
const reviewAction = this.store.createRecord('review-action', {
actionTrigger: ReviewActionTrigger.Submit,
target: this.preprint,
});
await reviewAction.save();
} else {
this.preprint.isPublished = true;
await this.preprint.save();
}
this.toast.success(toastMessage);
this.router.transitionTo('preprints.detail', this.provider.id, this.preprint.id);
} catch (e) {
const errorTitle = this.intl.t('preprints.submit.new-version.error.title');
let errorMessage = this.intl.t('preprints.submit.new-version.error.description',
{ preprintWord: this.provider.documentType.singular });
if (e.errors[0].status === 409) { // Conflict
errorMessage = this.intl.t('preprints.submit.new-version.error.conflict');
}
this.toast.error(errorMessage, errorTitle);
}
return;
}

if (this.preprint.reviewsState === ReviewsState.ACCEPTED) {
await this.preprint.save();
} else if (this.provider.reviewsWorkflow) {
const reviewAction = this.store.createRecord('review-action', {
actionTrigger: 'submit',
actionTrigger: ReviewActionTrigger.Submit,
target: this.preprint,
});
await reviewAction.save();
Expand All @@ -244,6 +284,12 @@ export default class PreprintStateMachine extends Component<StateMachineArgs>{
@task
@waitFor
public async onNext(): Promise<void> {
if (this.isNewVersionFlow) {
// no need to save original or new version on new version flow
this.statusFlowIndex++;
return;
}

if (this.isEditFlow) {
this.args.resetPageDirty();
} else {
Expand Down Expand Up @@ -510,6 +556,16 @@ export default class PreprintStateMachine extends Component<StateMachineArgs>{
}

private getTypeIndex(type: string): number {
if (this.isNewVersionFlow) {
if (type === PreprintStatusTypeEnum.file) {
return 1;
} else if (type === PreprintStatusTypeEnum.review) {
return 2;
} else {
return 0;
}
}

if (this.displayFileUploadStep) {
if (type === PreprintStatusTypeEnum.titleAndAbstract) {
return 1;
Expand Down Expand Up @@ -603,6 +659,16 @@ export default class PreprintStateMachine extends Component<StateMachineArgs>{
* @returns boolean
*/
public shouldDisplayStatusType(type: string): boolean{
if (this.isNewVersionFlow) {
if (type === PreprintStatusTypeEnum.file) {
return true;
} else if (type === PreprintStatusTypeEnum.review) {
return true;
} else {
return false;
}
}

if (type === PreprintStatusTypeEnum.file) {
return this.displayFileUploadStep;
} else if (type === PreprintStatusTypeEnum.authorAssertions) {
Expand Down Expand Up @@ -680,13 +746,14 @@ export default class PreprintStateMachine extends Component<StateMachineArgs>{
@task
@waitFor
public async addProjectFile(file: FileModel): Promise<void>{
await file.copy(this.preprint, '/', 'osfstorage', {
const target = this.preprint;
await file.copy(target, '/', 'osfstorage', {
conflict: 'replace',
});
const theFiles = await this.preprint.files;
const theFiles = await target.files;
const rootFolder = await theFiles.firstObject!.rootFolder;
const primaryFile = await rootFolder!.files;
this.preprint.set('primaryFile', primaryFile.lastObject);
target.set('primaryFile', primaryFile.lastObject);
}

@action
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
&.long {
height: 175px;
}

&.short {
height: 40px;
}
}

&.mobile {
Expand All @@ -32,6 +36,10 @@
&.long {
height: fit-content;
}

&.short {
height: fit-content;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div local-class='status-flow-container {{if (is-mobile) 'mobile'}}'>
<div local-class='line {{if @manager.displayAuthorAssertions 'long'}}'/>
<div local-class='line {{if @manager.displayAuthorAssertions 'long' (if @manager.isNewVersionFlow 'short')}}'/>
<Preprints::-Components::Submit::PreprintStateMachine::StatusFlow::StatusFlowDisplay
@manager={{@manager}}
@type={{@manager.getTitleAndAbstractType}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
isPreviousButtonDisabled=this.isPreviousButtonDisabled
isEditFlow=this.isEditFlow
displayFileUploadStep=this.displayFileUploadStep
isNewVersionFlow=this.isNewVersionFlow

isDeleteButtonDisplayed=this.isDeleteButtonDisplayed
isWithdrawalButtonDisplayed=this.isWithdrawalButtonDisplayed

Expand Down
7 changes: 4 additions & 3 deletions app/preprints/-components/submit/submission-flow/template.hbs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{{page-title (t @header documentType=this.provider.documentType)}}

<Preprints::-Components::Submit::PreprintStateMachine
<Preprints::-Components::Submit::PreprintStateMachine
@provider={{@provider}}
@preprint={{@preprint}}
@setPageDirty={{@setPageDirty}}
@resetPageDirty={{@resetPageDirty}}
@newVersion={{@newVersion}}
as |manager|>
<OsfLayout @backgroundClass={{local-class 'submit-page-container'}} as |layout|>
<layout.heading local-class='header-container'
{{with-branding @brand}}
{{with-branding @brand}}
>
<div local-class='header {{if (is-mobile) 'mobile'}}'>
{{t @header
{{t @header
documentType = @provider.documentType.singularCapitalized
}}
</div>
Expand Down
25 changes: 25 additions & 0 deletions app/preprints/detail/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { Permission } from 'ember-osf-web/models/osf-model';
import { ReviewsState, PreprintProviderReviewsWorkFlow } from 'ember-osf-web/models/provider';
import { tracked } from '@glimmer/tracking';
import Media from 'ember-responsive';
import Toast from 'ember-toastr/services/toast';
import { task } from 'ember-concurrency';
import { waitFor } from '@ember/test-waiters';


/**
Expand Down Expand Up @@ -44,6 +47,7 @@ export default class PrePrintsDetailController extends Controller {
@service features!: Features;
@service intl!: Intl;
@service media!: Media;
@service toast!: Toast;

@tracked fullScreenMFR = false;
@tracked plauditIsReady = false;
Expand Down Expand Up @@ -140,6 +144,27 @@ export default class PrePrintsDetailController extends Controller {
this.send('click', category, label, url);
}

@task
@waitFor
async createNewVersion() {
try {
const url = this.model.preprint.links.preprint_versions as string;
const newVersion = await this.currentUser.authenticatedAJAX({
url,
type: 'POST',
});
this.transitionToRoute('preprints.new-version', this.model.provider.id, newVersion.data.id);
} catch (e) {
const errorTitle = this.intl.t('preprints.submit.new-version.error.title');
let errorMessage = this.intl.t('preprints.submit.new-version.error.description',
{ preprintWord: this.model.provider.documentType.singular });
if (e.errors[0].status === 409) { // Conflict
errorMessage = this.intl.t('preprints.submit.new-version.error.conflict');
}
this.toast.error(errorMessage, errorTitle);
}
}

get isMobile() {
return this.media.isMobile;
}
Expand Down
2 changes: 1 addition & 1 deletion app/preprints/detail/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default class PreprintsDetail extends Route {

} catch (error) {
captureException(error);
this.router.transitionTo('not-found', 'preprints');
this.router.transitionTo('not-found', this.router.currentURL.slice(1));
return null;
}
}
Expand Down
9 changes: 4 additions & 5 deletions app/preprints/detail/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@
{{/if}}
{{/unless}}
{{#if this.model.preprint.canCreateNewVersion}}
<OsfLink
<Button
data-test-create-new-version-button
data-analytics-name='Create new version'
local-class='btn btn-primary'
{{!-- TODO: Update when new version route is implemented --}}
@route='preprints.edit'
@models={{array this.model.provider.id this.model.preprint.id}}
@layout='fake-link'
{{on 'click' (perform this.createNewVersion)}}
>
{{t 'preprints.detail.create_new_version'}}
</OsfLink>
</Button>
{{/if}}
</div>
</div>
Expand Down
9 changes: 9 additions & 0 deletions app/preprints/new-version/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Controller from '@ember/controller';
import { action } from '@ember/object';

export default class PreprintNewVersionController extends Controller {
@action
noop() {
return;
}
}
78 changes: 78 additions & 0 deletions app/preprints/new-version/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import Store from '@ember-data/store';
import Route from '@ember/routing/route';
import RouterService from '@ember/routing/router-service';
import { inject as service } from '@ember/service';
import Toast from 'ember-toastr/services/toast';
import Intl from 'ember-intl/services/intl';

import Theme from 'ember-osf-web/services/theme';
import MetaTags, { HeadTagDef } from 'ember-osf-web/services/meta-tags';
import PreprintModel from 'ember-osf-web/models/preprint';

export default class PreprintNewVersionRoute extends Route {
@service store!: Store;
@service theme!: Theme;
@service router!: RouterService;
@service metaTags!: MetaTags;
@service toast!: Toast;
@service intl!: Intl;


headTags?: HeadTagDef[];

buildRouteInfoMetadata() {
return {
osfMetrics: {
providerId: this.theme.id,
},
};
}

async model(args: any) {
try {
const provider = await this.store.findRecord('preprint-provider', args.provider_id);
this.theme.providerType = 'preprint';
this.theme.id = args.provider_id;

const preprint: PreprintModel = await this.store.findRecord('preprint', args.guid);

// TODO: Re-evaluate if re-route logic is necessary
// if (!preprint.canCreateNewVersion) {
// let message = this.intl.t('preprints.submit.new-version.redirect.latest-published',
// { preprintWord: provider.documentType.singular });
// if (!preprint.currentUserIsAdmin) {
// message = this.intl.t('preprints.submit.new-version.redirect.permission',
// { preprintWord: provider.documentType.singular });
// }

// const title = this.intl.t('preprints.submit.new-version.redirect.title');
// this.toast.info(message, title);
// this.router.transitionTo('preprints.detail', args.provider_id, args.guid);
// return null;
// }

return {
provider,
preprint,
brand: provider.brand.content,
};
} catch (e) {
this.router.transitionTo('not-found', `preprints/${args.provider_id}`);
return null;
}
}

afterModel(model: any) {
const {provider} = model;
if (provider && provider.assets && provider.assets.favicon) {
const headTags = [{
type: 'link',
attrs: {
rel: 'icon',
href: provider.assets.favicon,
},
}];
this.set('headTags', headTags);
}
}
}
9 changes: 9 additions & 0 deletions app/preprints/new-version/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Preprints::-Components::submit::submission-flow
@provider={{this.model.provider}}
@brand={{this.model.brand}}
@newVersion={{true}}
@preprint={{this.model.preprint}}
@header='preprints.submit.title-new-version'
@setPageDirty={{this.noop}}
@resetPageDirty={{this.noop}}
/>
Loading

0 comments on commit 701f265

Please sign in to comment.