Skip to content
Open
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
26 changes: 26 additions & 0 deletions src/app/core/components/redirect-page/redirect-page.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<section
class="container flex flex-column flex-1 my-7 mx-3 p-3 align-items-center md:p-4 md:flex-row md:justify-content-between"
>
<img
class="redirect-image w-8 md:w-6"
ngSrc="assets/images/laptop-books.png"
width="500"
height="346"
Comment thread
futa-ikeda marked this conversation as resolved.
Resolved
priority
[alt]="'redirectPage.altText' | translate"
/>

<div class="flex flex-column align-items-center m-4 md:align-items-start">
<h2>{{ 'redirectPage.header' | translate }}</h2>
<p class="mt-4">{{ 'redirectPage.message' | translate }}</p>

<div class="flex flex-column mt-4 gap-4 md:flex-row md:justify-content-start">
<a class="p-button font-bold no-underline" [href]="redirectDestination()">
{{ 'common.buttons.continue' | translate }}
</a>
<p-button severity="info" (onClick)="goBack()">
{{ 'common.buttons.goBack' | translate }}
</p-button>
</div>
</div>
</section>
19 changes: 19 additions & 0 deletions src/app/core/components/redirect-page/redirect-page.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@use "styles/mixins" as mix;

:host {
@include mix.flex-center;
flex: 1;
background: var(--gradient-3);
}

.container {
background: var(--white);
border-radius: 0.75rem;
box-shadow: 0 2px 4px var(--grey-outline);
color: var(--dark-blue-1);
}

.redirect-image {
height: auto;
object-fit: contain;
}
105 changes: 105 additions & 0 deletions src/app/core/components/redirect-page/redirect-page.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { MockProvider } from 'ng-mocks';

import { ComponentFixture, TestBed } from '@angular/core/testing';

import { WINDOW } from '@core/provider/window.provider';
import { CurrentResourceSelectors } from '@osf/shared/stores/current-resource/current-resource.selectors';

import { provideOSFCore } from '@testing/osf.testing.provider';
import { provideMockStore } from '@testing/providers/store-provider.mock';

import { RedirectPageComponent } from './redirect-page.component';

const mockResource = {
id: 'mock-resource-id',
type: 'mock-resource-type',
redirectUrl: 'https://example.com',
permissions: [],
redirectAutomatically: false,
};

describe('RedirectPageComponent', () => {
let component: RedirectPageComponent;
let fixture: ComponentFixture<RedirectPageComponent>;
let windowMock: any;

afterEach(() => {
vi.useRealTimers();
});

const setup = (override?: Partial<typeof mockResource>) => {
const resource = { ...mockResource, ...override };
const historyMock = { back: vi.fn() };
windowMock = { history: historyMock, location: { href: '', set: vi.fn() } };
TestBed.configureTestingModule({
imports: [RedirectPageComponent],
providers: [
provideOSFCore(),
MockProvider(WINDOW, windowMock),
provideMockStore({
signals: [{ selector: CurrentResourceSelectors.getCurrentResource, value: resource }],
}),
],
});

fixture = TestBed.createComponent(RedirectPageComponent);
component = fixture.componentInstance;
fixture.detectChanges();
};

it('should create', () => {
setup();
expect(component).toBeTruthy();
});

it('should have redirectDestination when resource has redirect URL', () => {
setup();
expect(component.redirectDestination()).toBe(mockResource.redirectUrl);
});

it('should not have redirectDestination when resource has no redirect URL', () => {
setup({ redirectUrl: undefined });
expect(component.redirectDestination()).toBe('');
});

it('should render continue link and go back button', () => {
setup();
const continueLink = fixture.nativeElement.querySelector('a') as HTMLAnchorElement;
expect(continueLink.getAttribute('href')).toBe(component.redirectDestination());
const backButton = fixture.nativeElement.querySelector('button') as HTMLButtonElement;
expect(backButton).toBeTruthy();
});

it('should call goBack when go back button is clicked', () => {
setup();
const goBackSpy = vi.spyOn(windowMock.history, 'back');
const button = fixture.nativeElement.querySelector('button') as HTMLButtonElement;
button.click();
expect(goBackSpy).toHaveBeenCalled();
});

it('should not redirect when redirectDestination is empty', () => {
vi.useFakeTimers();
setup({ redirectUrl: undefined });
const redirectSpy = vi.spyOn(windowMock.location, 'href', 'set');
vi.advanceTimersByTime(component.redirectDelay);
expect(redirectSpy).not.toHaveBeenCalled();
});

it('should automatically redirect after redirectDelay amount of time', () => {
vi.useFakeTimers();
setup();
const redirectSpy = vi.spyOn(windowMock.location, 'href', 'set');
vi.advanceTimersByTime(component.redirectDelay);
expect(redirectSpy).toHaveBeenCalledWith(component.redirectDestination());
});

it('should not redirect if component is destroyed before redirectDelay', () => {
vi.useFakeTimers();
setup();
const redirectSpy = vi.spyOn(windowMock.location, 'href', 'set');
fixture.destroy();
vi.advanceTimersByTime(component.redirectDelay);
expect(redirectSpy).not.toHaveBeenCalled();
});
});
47 changes: 47 additions & 0 deletions src/app/core/components/redirect-page/redirect-page.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { select } from '@ngxs/store';

import { TranslatePipe } from '@ngx-translate/core';

import { Button } from 'primeng/button';

import { timer } from 'rxjs';

import { NgOptimizedImage } from '@angular/common';
import { afterNextRender, ChangeDetectionStrategy, Component, computed, DestroyRef, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';

import { WINDOW } from '@core/provider/window.provider';
import { CurrentResourceSelectors } from '@osf/shared/stores/current-resource/current-resource.selectors';

@Component({
selector: 'osf-redirect-page',
imports: [Button, NgOptimizedImage, TranslatePipe],
templateUrl: './redirect-page.component.html',
styleUrl: './redirect-page.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class RedirectPageComponent {
private readonly currentResource = select(CurrentResourceSelectors.getCurrentResource);
private readonly window = inject(WINDOW);
private readonly destroyRef = inject(DestroyRef);
readonly redirectDelay = 5000;
readonly redirectDestination = computed(() => this.currentResource()?.redirectUrl ?? '');

constructor() {
afterNextRender(() => {
const redirectUrl = this.redirectDestination();
if (!redirectUrl) {
return;
}
timer(this.redirectDelay)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => {
this.window.location.href = this.redirectDestination();
});
});
}

goBack() {
this.window.history.back();
}
}
Comment thread
futa-ikeda marked this conversation as resolved.
Resolved
6 changes: 6 additions & 0 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@
"editAndResubmit": "Edit And Resubmit",
"embed": "Embed",
"filter": "Filter",
"goBack": "Go back",
"hide": "Hide",
"leaveThisView": "Leave this view",
"loadMore": "Load more",
Expand Down Expand Up @@ -2166,6 +2167,11 @@
"view": "View"
}
},
"redirectPage": {
"header": "You're leaving OSF.",
"message": "You’re about to leave the OSF. The site you’re about to visit has its own terms and privacy policy. You will be automatically redirected in a moment.",
"altText": "You're leaving OSF"
},
"registries": {
"addRegistration": "Add a Registration",
"browse": "Browse Registrations",
Expand Down
Binary file added src/assets/images/laptop-books.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading