Refactor IP copy functionality into a reusable component

Extracted the "Copy IP" button to a new `CopyIpComponent` to improve reusability and modularity. Updated `HomeComponent` to use this new component and removed redundant methods for copying text to the clipboard. Added tests and styles for the new component.
This commit is contained in:
Teriuihi 2025-04-05 22:07:53 +02:00
parent dee84cc1a9
commit 297bd473a6
7 changed files with 46 additions and 12 deletions

View File

@ -9,6 +9,7 @@ import {ThemeComponent} from "./theme/theme.component";
import {CookieService} from 'ngx-cookie-service'; import {CookieService} from 'ngx-cookie-service';
import {MapComponent} from './map/map.component'; import {MapComponent} from './map/map.component';
import {RulesComponent} from './rules/rules.component'; import {RulesComponent} from './rules/rules.component';
import {CopyIpComponent} from './copy-ip/copy-ip.component';
const routes: Routes = [ const routes: Routes = [
{path: '', component: HomeComponent}, {path: '', component: HomeComponent},
@ -29,6 +30,7 @@ const routes: Routes = [
BrowserModule, BrowserModule,
RouterModule.forRoot(routes), RouterModule.forRoot(routes),
NgOptimizedImage, NgOptimizedImage,
CopyIpComponent,
], ],
providers: [CookieService], providers: [CookieService],
bootstrap: [AppComponent] bootstrap: [AppComponent]

View File

@ -0,0 +1,3 @@
<button type="button" style="margin-top: 50px;" class="button-outer" (click)="copyIpToClipboard()">
<span class="button-inner">{{ displayText }}</span>
</button>

View File

@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { CopyIpComponent } from './copy-ip.component';
describe('CopyIpComponent', () => {
let component: CopyIpComponent;
let fixture: ComponentFixture<CopyIpComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [CopyIpComponent]
})
.compileComponents();
fixture = TestBed.createComponent(CopyIpComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,16 @@
import {Component} from '@angular/core';
@Component({
selector: 'app-copy-ip',
templateUrl: './copy-ip.component.html',
styleUrl: './copy-ip.component.scss'
})
export class CopyIpComponent {
public displayText: string = 'Copy IP'
public copyIpToClipboard(): void {
navigator.clipboard.writeText('play.alttd.com');
this.displayText = 'Copied!';
}
}

View File

@ -17,10 +17,7 @@
<!-- TODO load player count from old api or backend?--> <!-- TODO load player count from old api or backend?-->
<h2 style="color: white;"><span class="player-count">Loading...</span></h2> <h2 style="color: white;"><span class="player-count">Loading...</span></h2>
<h2 style="color: white;">Server IP: play.alttd.com</h2> <h2 style="color: white;">Server IP: play.alttd.com</h2>
<!-- TODO make copy be angular not js--> <app-copy-ip></app-copy-ip>
<button type="button" style="margin-top: 50px;" class="button-outer" (click)="copyToClipboard('play.alttd.com')">
<span class="button-inner" id="copybutton">Copy IP</span>
</button>
</section> </section>
<section class="darkmodeSection"> <section class="darkmodeSection">
<div class="container"> <div class="container">
@ -140,9 +137,7 @@
<img ngSrc="/public/img/logos/log.png" alt="Alternative Altitude Server Logo" height="129" <img ngSrc="/public/img/logos/log.png" alt="Alternative Altitude Server Logo" height="129"
width="120"> width="120">
<h2 style="margin: 20px 0; font-size: 1.8em;">play.alttd.com</h2> <h2 style="margin: 20px 0; font-size: 1.8em;">play.alttd.com</h2>
<button type="button" class="button-outer" (click)="copyToClipboard('play.alttd.com')"> <app-copy-ip></app-copy-ip>
<span class="button-inner" id="copybutton2">Copy IP</span>
</button>
</div> </div>
</section> </section>
<a (click)="this.scrollService.scrollToTop()" class="scroll-up-button, active"> <a (click)="this.scrollService.scrollToTop()" class="scroll-up-button, active">

View File

@ -25,7 +25,6 @@ export class HomeComponent implements OnInit {
this.randomizeSlides() this.randomizeSlides()
} }
private randomizeSlides(): void { private randomizeSlides(): void {
const array = [...this.slides]; const array = [...this.slides];
@ -55,10 +54,6 @@ export class HomeComponent implements OnInit {
this.slideIndex = (this.slideIndex + 1) % this.slides.length; this.slideIndex = (this.slideIndex + 1) % this.slides.length;
} }
public copyToClipboard(text: string): void {
navigator.clipboard.writeText(text);
}
public getSlideIndices(): number[] { public getSlideIndices(): number[] {
return Array.from({length: this.slides.length}, (_, i) => i); return Array.from({length: this.slides.length}, (_, i) => i);
} }