DEV Community

Anjali Gurjar
Anjali Gurjar

Posted on

For me

Aap E-commerce Website ke Header + Navigation Flow aur Authentication-based UI Changes chahte hain, jisme:

  • βœ… Header me konsa button kab show hoga
  • βœ… User login hone ke baad Profile/Register ka visibility control
  • βœ… Buy, Like, Comment buttons ka logic
  • βœ… Live Data (Real-time Updates) kaise implement hoga

Chaliye sab kuch step-by-step cover karte hain! πŸš€


πŸ›  1. Header Navigation (Header.component.html)

Header me dynamic buttons hone chahiye jo user ke role aur authentication ke basis pe show hon.

πŸ“Œ Header Components & Navigation

<nav>
  <a routerLink="/">🏠 Home</a>
  <a routerLink="/products">πŸ› Shop</a>

  <!-- User Not Logged In -->
  <ng-container *ngIf="!isLoggedIn">
    <a routerLink="/login">πŸ”‘ Login</a>
    <a routerLink="/signup">πŸ“ Register</a>
  </ng-container>

  <!-- User Logged In -->
  <ng-container *ngIf="isLoggedIn">
    <a routerLink="/profile">πŸ‘€ Profile</a>
    <a routerLink="/cart">πŸ›’ Cart ({{ cartCount }})</a>
    <a (click)="logout()">πŸšͺ Logout</a>
  </ng-container>

  <!-- Admin Panel Link (Only for Admins) -->
  <ng-container *ngIf="isAdmin">
    <a routerLink="/admin">βš™οΈ Admin Panel</a>
  </ng-container>
</nav>
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ 2. Header Logic (Header.component.ts)

Yeh logic user login hone ke baad dynamic buttons ko control karega.

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { CartService } from '../services/cart.service';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit {
  isLoggedIn = false;
  isAdmin = false;
  cartCount = 0;

  constructor(private authService: AuthService, private cartService: CartService) {}

  ngOnInit() {
    // Check if user is logged in
    this.authService.isAuthenticated().subscribe(status => {
      this.isLoggedIn = status;
      this.isAdmin = this.authService.getUserRole() === 'admin';
    });

    // Cart Count Update (Real-time)
    this.cartService.cartCount$.subscribe(count => {
      this.cartCount = count;
    });
  }

  logout() {
    this.authService.logout();
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸš€ 3. Register/Login ke baad UI Change

πŸ”Ή Jab user register ya login karega, toh:

  • Header me Profile, Cart, Logout buttons show honge.
  • Login/Register button hide ho jayega.
  • Buy Now, Like, Comment buttons enable ho jayenge.

πŸ› 4. Buy Now Button (Product Detail Page)

  • Login ke baad enable hoga.
  • Click karne pe checkout page pe le jayega.
  • Agar user logged in nahi hai, toh login page pe redirect karega.

βœ… Button UI (product-detail.component.html)

<button *ngIf="isLoggedIn" (click)="buyNow()">πŸ›’ Buy Now</button>
<button *ngIf="!isLoggedIn" routerLink="/login">πŸ”‘ Login to Buy</button>
Enter fullscreen mode Exit fullscreen mode

βœ… Button Logic (product-detail.component.ts)

buyNow() {
  if (!this.isLoggedIn) {
    this.router.navigate(['/login']);
  } else {
    this.router.navigate(['/checkout']);
  }
}
Enter fullscreen mode Exit fullscreen mode

❀️ 5. Like & Comment Feature

πŸ”Ή Like Button:

  • Only logged-in users can like.
  • Clicking toggles like/unlike.

πŸ”Ή Comment Box:

  • Only logged-in users can comment.
  • Live updates (real-time comments).

βœ… Like Button UI (product-detail.component.html)

<button *ngIf="isLoggedIn" (click)="toggleLike()">
  ❀️ {{ liked ? 'Unlike' : 'Like' }}
</button>
<button *ngIf="!isLoggedIn" routerLink="/login">πŸ”‘ Login to Like</button>
Enter fullscreen mode Exit fullscreen mode

βœ… Like Button Logic (product-detail.component.ts)

toggleLike() {
  this.liked = !this.liked;
  this.productService.updateLike(this.productId, this.liked).subscribe();
}
Enter fullscreen mode Exit fullscreen mode

βœ… Comment Section UI (product-detail.component.html)

<div *ngIf="isLoggedIn">
  <textarea [(ngModel)]="commentText" placeholder="Write a comment..."></textarea>
  <button (click)="postComment()">πŸ’¬ Comment</button>
</div>

<div *ngFor="let comment of comments">
  <p>{{ comment.text }} - <strong>{{ comment.user }}</strong></p>
</div>
Enter fullscreen mode Exit fullscreen mode

βœ… Comment Logic (product-detail.component.ts)

postComment() {
  if (this.commentText.trim() !== '') {
    this.productService.postComment(this.productId, this.commentText).subscribe(newComment => {
      this.comments.push(newComment);  // Live Update
      this.commentText = '';
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”„ 6. Live Data (Real-Time Updates)

  • WebSocket ya Firebase Realtime Database ka use karke updates live dikhaye ja sakte hain.
  • Agar WebSocket use karna hai, toh har naye like/comment pe page auto-update hoga.

βœ… WebSocket Implementation (Real-Time Comments)

import { WebSocketService } from '../services/websocket.service';

ngOnInit() {
  this.webSocketService.listen('newComment').subscribe(comment => {
    this.comments.push(comment); // Live update
  });
}

postComment() {
  this.webSocketService.emit('postComment', { text: this.commentText, user: this.authService.getUser() });
}
Enter fullscreen mode Exit fullscreen mode

🎯 Final Summary

Feature Condition Action
Header UI Logged in Show Profile, Cart, Logout
Header UI Not logged in Show Login, Register
Buy Now Logged in Goes to Checkout
Buy Now Not logged in Redirects to Login
Like Button Logged in Toggle Like
Like Button Not logged in Redirects to Login
Comments Logged in Post Comment
Comments Not logged in Redirects to Login
Live Data WebSocket Auto-update Comments

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs