Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export interface ConfirmDeleteDraftOptions {
onDelete: () => void;
onReset: () => void;
redirectUrl: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ import { PreprintSteps } from '../../enums';
import { PreprintProviderDetails } from '../../models';
import { PreprintDraftDeletionService } from '../../services/preprint-draft-deletion.service';
import { GetPreprintProviderById, PreprintProvidersSelectors } from '../../store/preprint-providers';
import {
DeletePreprint,
FetchPreprintById,
PreprintStepperSelectors,
ResetPreprintStepperState,
} from '../../store/preprint-stepper';
import { FetchPreprintById, PreprintStepperSelectors, ResetPreprintStepperState } from '../../store/preprint-stepper';

import { CreateNewVersionComponent } from './create-new-version.component';

Expand Down Expand Up @@ -130,16 +125,14 @@ describe('CreateNewVersionComponent', () => {
expect(browserTabMock.updateTabStyles).toHaveBeenCalledWith(mockProvider.faviconUrl, mockProvider.name);
});

it('should reset services, delegate destroy delete, and reset stepper state', () => {
it('should reset services and reset stepper state on destroy', () => {
setup();

component.ngOnDestroy();

expect(headerStyleMock.resetToDefaults).toHaveBeenCalled();
expect(brandServiceMock.resetBranding).toHaveBeenCalled();
expect(browserTabMock.resetToDefaults).toHaveBeenCalled();
expect(draftDeletionMock.deleteOnDestroyIfNeeded).toHaveBeenCalledWith(expect.any(Function));
expect(store.dispatch).toHaveBeenCalledWith(new DeletePreprint());
expect(store.dispatch).toHaveBeenCalledWith(new ResetPreprintStepperState());
});

Expand Down Expand Up @@ -227,7 +220,6 @@ describe('CreateNewVersionComponent', () => {
expect(draftDeletionMock.confirmDeleteDraft).toHaveBeenCalledWith(
expect.objectContaining({
redirectUrl: '/my-preprints',
onDelete: expect.any(Function),
onReset: expect.any(Function),
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ import { createNewVersionStepsConst } from '../../constants';
import { PreprintSteps } from '../../enums';
import { PreprintDraftDeletionService } from '../../services/preprint-draft-deletion.service';
import { GetPreprintProviderById, PreprintProvidersSelectors } from '../../store/preprint-providers';
import {
DeletePreprint,
FetchPreprintById,
PreprintStepperSelectors,
ResetPreprintStepperState,
} from '../../store/preprint-stepper';
import { FetchPreprintById, PreprintStepperSelectors, ResetPreprintStepperState } from '../../store/preprint-stepper';

@Component({
selector: 'osf-create-new-version',
Expand All @@ -65,7 +60,6 @@ export class CreateNewVersionComponent implements OnDestroy, CanDeactivateCompon
getPreprintProviderById: GetPreprintProviderById,
fetchPreprint: FetchPreprintById,
resetState: ResetPreprintStepperState,
deletePreprint: DeletePreprint,
});

readonly preprintProvider = select(PreprintProvidersSelectors.getPreprintProviderDetails(this.providerId()));
Expand Down Expand Up @@ -109,8 +103,6 @@ export class CreateNewVersionComponent implements OnDestroy, CanDeactivateCompon
this.brandService.resetBranding();
this.browserTabHelper.resetToDefaults();

this.draftDeletionService.deleteOnDestroyIfNeeded(() => this.actions.deletePreprint());

this.actions.resetState();
}

Expand Down Expand Up @@ -144,7 +136,6 @@ export class CreateNewVersionComponent implements OnDestroy, CanDeactivateCompon

requestDeletePreprint(): void {
this.draftDeletionService.confirmDeleteDraft({
onDelete: () => this.actions.deletePreprint(),
onReset: () => this.actions.resetState(),
redirectUrl: '/my-preprints',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { PreprintSteps } from '../../enums';
import { PreprintProviderDetails } from '../../models';
import { PreprintDraftDeletionService } from '../../services/preprint-draft-deletion.service';
import { GetPreprintProviderById, PreprintProvidersSelectors } from '../../store/preprint-providers';
import { DeletePreprint, PreprintStepperSelectors, ResetPreprintStepperState } from '../../store/preprint-stepper';
import { PreprintStepperSelectors, ResetPreprintStepperState } from '../../store/preprint-stepper';

import { SubmitPreprintStepperComponent } from './submit-preprint-stepper.component';

Expand Down Expand Up @@ -130,16 +130,14 @@ describe('SubmitPreprintStepperComponent', () => {
expect(browserTabMock.updateTabStyles).toHaveBeenCalledWith(mockProvider.faviconUrl, mockProvider.name);
});

it('should reset services, delegate destroy delete, and reset stepper state', () => {
it('should reset services and reset stepper state on destroy', () => {
setup();

component.ngOnDestroy();

expect(headerStyleMock.resetToDefaults).toHaveBeenCalled();
expect(brandServiceMock.resetBranding).toHaveBeenCalled();
expect(browserTabMock.resetToDefaults).toHaveBeenCalled();
expect(draftDeletionMock.deleteOnDestroyIfNeeded).toHaveBeenCalledWith(expect.any(Function));
expect(store.dispatch).toHaveBeenCalledWith(new DeletePreprint());
expect(store.dispatch).toHaveBeenCalledWith(new ResetPreprintStepperState());
});

Expand Down Expand Up @@ -287,7 +285,6 @@ describe('SubmitPreprintStepperComponent', () => {
expect(draftDeletionMock.confirmDeleteDraft).toHaveBeenCalledWith(
expect.objectContaining({
redirectUrl: '/preprints',
onDelete: expect.any(Function),
onReset: expect.any(Function),
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { submitPreprintSteps } from '../../constants';
import { PreprintSteps } from '../../enums';
import { PreprintDraftDeletionService } from '../../services/preprint-draft-deletion.service';
import { GetPreprintProviderById, PreprintProvidersSelectors } from '../../store/preprint-providers';
import { DeletePreprint, PreprintStepperSelectors, ResetPreprintStepperState } from '../../store/preprint-stepper';
import { PreprintStepperSelectors, ResetPreprintStepperState } from '../../store/preprint-stepper';

@Component({
selector: 'osf-submit-preprint-stepper',
Expand Down Expand Up @@ -74,7 +74,6 @@ export class SubmitPreprintStepperComponent implements OnDestroy, CanDeactivateC
private actions = createDispatchMap({
getPreprintProviderById: GetPreprintProviderById,
resetState: ResetPreprintStepperState,
deletePreprint: DeletePreprint,
});

preprintProvider = select(PreprintProvidersSelectors.getPreprintProviderDetails(this.providerId()));
Expand Down Expand Up @@ -133,14 +132,11 @@ export class SubmitPreprintStepperComponent implements OnDestroy, CanDeactivateC
this.brandService.resetBranding();
this.browserTabHelper.resetToDefaults();

this.draftDeletionService.deleteOnDestroyIfNeeded(() => this.actions.deletePreprint());

this.actions.resetState();
}

requestDeletePreprint(): void {
this.draftDeletionService.confirmDeleteDraft({
onDelete: () => this.actions.deletePreprint(),
onReset: () => this.actions.resetState(),
redirectUrl: '/preprints',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ import { TitleAndAbstractStepComponent } from '../../components/stepper/title-an
import { submitPreprintSteps } from '../../constants';
import { PreprintSteps, ProviderReviewsWorkflow, ReviewsState } from '../../enums';
import { GetPreprintProviderById, PreprintProvidersSelectors } from '../../store/preprint-providers';
import {
DeletePreprint,
FetchPreprintById,
PreprintStepperSelectors,
ResetPreprintStepperState,
} from '../../store/preprint-stepper';
import { FetchPreprintById, PreprintStepperSelectors, ResetPreprintStepperState } from '../../store/preprint-stepper';

@Component({
selector: 'osf-update-preprint-stepper',
Expand Down Expand Up @@ -76,7 +71,6 @@ export class UpdatePreprintStepperComponent implements OnDestroy, CanDeactivateC
getPreprintProviderById: GetPreprintProviderById,
resetState: ResetPreprintStepperState,
fetchPreprint: FetchPreprintById,
deletePreprint: DeletePreprint,
});

readonly preprintProvider = select(PreprintProvidersSelectors.getPreprintProviderDetails(this.providerId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ describe('PreprintDraftDeletionService', () => {
expect(service).toBeTruthy();
});

it('should open confirm delete and run delete, reset, toast, navigate on confirm', () => {
const onDelete = vi.fn();
it('should open confirm delete and run reset, toast, navigate on confirm', () => {
const onReset = vi.fn();

service.confirmDeleteDraft({
onDelete,
onReset,
redirectUrl: '/preprints',
});
Expand All @@ -63,34 +61,22 @@ describe('PreprintDraftDeletionService', () => {
const { onConfirm } = confirmationMock.confirmDelete.mock.calls[0][0];
onConfirm();

expect(onDelete).toHaveBeenCalled();
expect(onReset).toHaveBeenCalled();
expect(toastMock.showSuccess).toHaveBeenCalledWith('preprints.preprintStepper.deleteDraft.success');
expect(routerMock.navigateByUrl).toHaveBeenCalledWith('/preprints');
});

it('should allow canDeactivate and skip deleteOnDestroy after confirmed delete', () => {
const onDelete = vi.fn();
it('should allow canDeactivate after confirmed delete', () => {
const onReset = vi.fn();

service.confirmDeleteDraft({ onDelete, onReset, redirectUrl: '/x' });
service.confirmDeleteDraft({ onReset, redirectUrl: '/x' });
const { onConfirm } = confirmationMock.confirmDelete.mock.calls[0][0];
onConfirm();

expect(service.canDeactivate(false)).toBe(true);

const destroyDelete = vi.fn();
service.deleteOnDestroyIfNeeded(destroyDelete);
expect(destroyDelete).not.toHaveBeenCalled();
});

it('should return canDeactivate true when submitted', () => {
expect(service.canDeactivate(true)).toBe(true);
});

it('should call deleteOnDestroy when not yet deleted', () => {
const destroyDelete = vi.fn();
service.deleteOnDestroyIfNeeded(destroyDelete);
expect(destroyDelete).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@ export class PreprintDraftDeletionService {
return hasBeenSubmitted || this.preprintDeleted;
}

deleteOnDestroyIfNeeded(onDelete: () => void): void {
if (!this.preprintDeleted) {
onDelete();
}
}

confirmDeleteDraft(options: ConfirmDeleteDraftOptions): void {
this.customConfirmationService.confirmDelete({
headerKey: 'preprints.preprintStepper.deleteDraft.header',
messageKey: 'preprints.preprintStepper.deleteDraft.message',
onConfirm: () => {
this.preprintDeleted = true;
options.onDelete();
options.onReset();
this.toastService.showSuccess('preprints.preprintStepper.deleteDraft.success');
this.router.navigateByUrl(options.redirectUrl);
Expand Down
4 changes: 0 additions & 4 deletions src/app/features/preprints/services/preprints.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ export class PreprintsService {
);
}

deletePreprint(id: string) {
return this.jsonApiService.delete(`${this.apiUrl}/preprints/${id}/`);
}

updatePreprint(id: string, payload: Partial<PreprintModel>): Observable<PreprintModel> {
const apiPayload = this.mapPreprintDomainToApiPayload(payload);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ export class ResetPreprintStepperState {
static readonly type = '[Preprint Stepper] Reset State';
}

export class DeletePreprint {
static readonly type = '[Preprint Stepper] Delete Preprint';
}

export class SetPreprintStepperCurrentFolder {
static readonly type = '[Preprint Stepper] Set Preprint Stepper Current Folder';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
CreateNewProject,
CreateNewVersion,
CreatePreprint,
DeletePreprint,
DisconnectProject,
FetchAvailableProjects,
FetchLicenses,
Expand Down Expand Up @@ -479,16 +478,6 @@ export class PreprintStepperState {
return EMPTY;
}

@Action(DeletePreprint)
deletePreprint(ctx: StateContext<PreprintStepperStateModel>) {
const state = ctx.getState();
const createdPreprintId = state.preprint.data?.id;
if (createdPreprintId && !state.hasBeenSubmitted) {
return this.preprintsService.deletePreprint(createdPreprintId);
}
return EMPTY;
}

@Action(SetPreprintStepperCurrentFolder)
setCurrentFolder(ctx: StateContext<PreprintStepperStateModel>, action: SetPreprintStepperCurrentFolder) {
ctx.setState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export type PreprintDraftDeletionServiceMockType = Partial<PreprintDraftDeletion
deleted: boolean;
confirmDeleteDraft: Mock;
canDeactivate: Mock;
deleteOnDestroyIfNeeded: Mock;
};

export const PreprintDraftDeletionServiceMock = {
Expand All @@ -15,11 +14,6 @@ export const PreprintDraftDeletionServiceMock = {
deleted: false,
confirmDeleteDraft: vi.fn(),
canDeactivate: vi.fn((submitted: boolean) => submitted || service.deleted),
deleteOnDestroyIfNeeded: vi.fn((onDelete: () => void) => {
if (!service.deleted) {
onDelete();
}
}),
};
return service;
},
Expand Down
Loading