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:
parent
dee84cc1a9
commit
297bd473a6
|
|
@ -9,6 +9,7 @@ import {ThemeComponent} from "./theme/theme.component";
|
|||
import {CookieService} from 'ngx-cookie-service';
|
||||
import {MapComponent} from './map/map.component';
|
||||
import {RulesComponent} from './rules/rules.component';
|
||||
import {CopyIpComponent} from './copy-ip/copy-ip.component';
|
||||
|
||||
const routes: Routes = [
|
||||
{path: '', component: HomeComponent},
|
||||
|
|
@ -29,6 +30,7 @@ const routes: Routes = [
|
|||
BrowserModule,
|
||||
RouterModule.forRoot(routes),
|
||||
NgOptimizedImage,
|
||||
CopyIpComponent,
|
||||
],
|
||||
providers: [CookieService],
|
||||
bootstrap: [AppComponent]
|
||||
|
|
|
|||
3
frontend/src/app/copy-ip/copy-ip.component.html
Normal file
3
frontend/src/app/copy-ip/copy-ip.component.html
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
<button type="button" style="margin-top: 50px;" class="button-outer" (click)="copyIpToClipboard()">
|
||||
<span class="button-inner">{{ displayText }}</span>
|
||||
</button>
|
||||
0
frontend/src/app/copy-ip/copy-ip.component.scss
Normal file
0
frontend/src/app/copy-ip/copy-ip.component.scss
Normal file
23
frontend/src/app/copy-ip/copy-ip.component.spec.ts
Normal file
23
frontend/src/app/copy-ip/copy-ip.component.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
16
frontend/src/app/copy-ip/copy-ip.component.ts
Normal file
16
frontend/src/app/copy-ip/copy-ip.component.ts
Normal 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!';
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -17,10 +17,7 @@
|
|||
<!-- TODO load player count from old api or backend?-->
|
||||
<h2 style="color: white;"><span class="player-count">Loading...</span></h2>
|
||||
<h2 style="color: white;">Server IP: play.alttd.com</h2>
|
||||
<!-- TODO make copy be angular not js-->
|
||||
<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>
|
||||
<app-copy-ip></app-copy-ip>
|
||||
</section>
|
||||
<section class="darkmodeSection">
|
||||
<div class="container">
|
||||
|
|
@ -140,9 +137,7 @@
|
|||
<img ngSrc="/public/img/logos/log.png" alt="Alternative Altitude Server Logo" height="129"
|
||||
width="120">
|
||||
<h2 style="margin: 20px 0; font-size: 1.8em;">play.alttd.com</h2>
|
||||
<button type="button" class="button-outer" (click)="copyToClipboard('play.alttd.com')">
|
||||
<span class="button-inner" id="copybutton2">Copy IP</span>
|
||||
</button>
|
||||
<app-copy-ip></app-copy-ip>
|
||||
</div>
|
||||
</section>
|
||||
<a (click)="this.scrollService.scrollToTop()" class="scroll-up-button, active">
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ export class HomeComponent implements OnInit {
|
|||
this.randomizeSlides()
|
||||
}
|
||||
|
||||
|
||||
private randomizeSlides(): void {
|
||||
const array = [...this.slides];
|
||||
|
||||
|
|
@ -55,10 +54,6 @@ export class HomeComponent implements OnInit {
|
|||
this.slideIndex = (this.slideIndex + 1) % this.slides.length;
|
||||
}
|
||||
|
||||
public copyToClipboard(text: string): void {
|
||||
navigator.clipboard.writeText(text);
|
||||
}
|
||||
|
||||
public getSlideIndices(): number[] {
|
||||
return Array.from({length: this.slides.length}, (_, i) => i);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user