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
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;
}
111 changes: 111 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,111 @@
import { MockProvider } from 'ng-mocks';

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

import { WINDOW } from '@core/provider/window.provider';
import { CurrentResource } from '@osf/shared/models/current-resource.model';
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: CurrentResource = {
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 locationMock: { href: string };
let historyMock: { back: ReturnType<typeof vi.fn> };

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

function setup(override: Partial<CurrentResource> = {}) {
locationMock = { href: '' };
historyMock = { back: vi.fn() };

TestBed.configureTestingModule({
imports: [RedirectPageComponent],
providers: [
provideOSFCore(),
MockProvider(WINDOW, {
location: locationMock,
history: historyMock,
} as unknown as Window),
provideMockStore({
signals: [
{
selector: CurrentResourceSelectors.getCurrentResource,
value: { ...mockResource, ...override },
},
],
}),
],
});

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

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

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

it('should fall back to empty redirectDestination when redirect URL is missing', () => {
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(mockResource.redirectUrl);
expect(fixture.nativeElement.querySelector('button')).toBeTruthy();
});

it('should go back in history when go back is clicked', () => {
setup();
const button = fixture.nativeElement.querySelector('button') as HTMLButtonElement;
button.click();
expect(historyMock.back).toHaveBeenCalled();
});

it('should not redirect when redirectDestination is empty', () => {
vi.useFakeTimers();
setup({ redirectUrl: undefined });
vi.advanceTimersByTime(component.redirectDelay);
expect(locationMock.href).toBe('');
});

it('should automatically redirect after redirectDelay', () => {
vi.useFakeTimers();
setup();
expect(locationMock.href).toBe('');
vi.advanceTimersByTime(component.redirectDelay);
expect(locationMock.href).toBe(mockResource.redirectUrl);
});

it('should not redirect if component is destroyed before redirectDelay', () => {
vi.useFakeTimers();
setup();
fixture.destroy();
vi.advanceTimersByTime(component.redirectDelay);
expect(locationMock.href).toBe('');
});
});
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