Skip to content

Commit

Permalink
WIP new version re-work
Browse files Browse the repository at this point in the history
  • Loading branch information
futa-ikeda committed Dec 20, 2024
1 parent 550042d commit 1b83dfa
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export default class PreprintStateMachine extends Component<StateMachineArgs>{

provider = this.args.provider;
@tracked preprint: PreprintModel;
@tracked tempVersion?: PreprintModel;
displayAuthorAssertions = false;
@tracked statusFlowIndex = 1;
@tracked isEditFlow = false;
Expand All @@ -73,9 +72,6 @@ export default class PreprintStateMachine extends Component<StateMachineArgs>{
super(owner, args);

if (this.args.newVersion) {
// Create ephemeral preprint to prevent the original preprint from being overwritten
// Also stores primary file for new version
this.tempVersion = this.store.createRecord('preprint');
this.preprint = this.args.preprint;
return;
}
Expand Down Expand Up @@ -237,43 +233,22 @@ export default class PreprintStateMachine extends Component<StateMachineArgs>{

if (this.isNewVersionFlow) {
try {
const url = this.preprint.links.preprint_versions as string;
if (url && this.tempVersion) {
const savedVersionData = await this.currentUser.authenticatedAJAX({
url,
type: 'POST',
headers: {
'Content-Type': 'application/json',
},
data: JSON.stringify({
data: {
type: 'preprints',
attributes: {
primary_file: (await this.tempVersion.primaryFile)?.get('id'),
},
},
}),
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,
});
this.store.pushPayload('preprint', savedVersionData);
const storedPreprintRecord = this.store.peekRecord('preprint', savedVersionData.data.id);
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: storedPreprintRecord,
});
await reviewAction.save();
} else {
storedPreprintRecord.isPublished = true;
await storedPreprintRecord.save();
}
this.tempVersion.destroyRecord();
await this.preprint.reload(); // Refresh the original preprint as this is no longer latest version
this.toast.success(toastMessage);
this.router.transitionTo('preprints.detail', this.provider.id, storedPreprintRecord.id);
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',
Expand Down Expand Up @@ -771,7 +746,7 @@ export default class PreprintStateMachine extends Component<StateMachineArgs>{
@task
@waitFor
public async addProjectFile(file: FileModel): Promise<void>{
const target = (this.isNewVersionFlow ? this.tempVersion : this.preprint) as PreprintModel;
const target = this.preprint;
await file.copy(target, '/', 'osfstorage', {
conflict: 'replace',
});
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.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
8 changes: 4 additions & 4 deletions app/preprints/detail/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +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'
@route='preprints.new-version'
@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
26 changes: 14 additions & 12 deletions app/preprints/new-version/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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';
Expand Down Expand Up @@ -35,19 +36,20 @@ export default class PreprintNewVersionRoute extends Route {

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

if (!preprint.canCreateNewVersion) {
let message = this.intl.t('prperints.submit.new-version.redirect.latest-published',
{ preprintWord: provider.documentType.singular });
if (!preprint.currentUserIsAdmin) {
message = this.intl.t('prperints.submit.new-version.redirect.permission',
{ preprintWord: provider.documentType.singular });
}
// 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('prperints.submit.new-version.redirect.title');
this.toast.info(message, title);
this.router.transitionTo('preprints.detail', args.provider_id, args.guid);
return null;
}
// 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,
Expand Down
8 changes: 5 additions & 3 deletions mirage/factories/preprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,23 +246,25 @@ export default Factory.extend<PreprintMirageModel & PreprintTraits>({
afterCreate(preprint, server) {
const baseId = preprint.id;
const versionedPreprints = [1, 2, 3].map((version: number) => {
server.create('preprint', {
const isLatestVersion = version === 3;
return server.create('preprint', {
title: preprint.title,
description: preprint.description,
provider: preprint.provider,
id: `${baseId}_v${version}`,
reviewsState: preprint.reviewsState,
preprintVersion: version,
isLatestVersion: version === 3,
isLatestVersion,
});
});
preprint.update({
// A bit of a workaround since the API will return the latest version when getting baseId
preprintVersion: 3,
isLatestVersion: true,
});

if (preprint.provider) {
preprint.provider.update({ preprints: versionedPreprints });
preprint.provider.preprints.models.pushObjects(versionedPreprints);
}
},
}),
Expand Down
Loading

0 comments on commit 1b83dfa

Please sign in to comment.