DEV Community

John Eliud Odhiambo
John Eliud Odhiambo

Posted on

Building a Dynamic User Profile Dashboard with GraphQL

Introduction

In today's web development landscape, efficient data fetching is crucial for creating responsive and user-friendly applications. This article explores a project that leverages GraphQL to create a personalized profile dashboard for Zone01 users, demonstrating how modern web technologies can be combined to deliver a seamless user experience.

Project Overview

The GraphQL Profile Dashboard is a web application that connects to Zone01's GraphQL API to fetch and display user data in an interactive and visually appealing manner. The project showcases several key features:

  • Secure JWT Authentication: Users can log in with their Zone01 credentials to access their personalized dashboard
  • Dynamic Data Visualization: Interactive SVG-based charts display user progress and statistics
  • Responsive Design: A clean, modern interface that adapts to different screen sizes
  • Dark/Light Theme Toggle: User preference-based theming with persistent settings

Technical Implementation

Authentication Flow

The application implements a secure authentication flow using JWT (JSON Web Tokens). When users provide their credentials, the application makes a request to Zone01's authentication endpoint, receives a JWT token, and stores it in the browser's localStorage for subsequent API requests.

This approach provides a secure way to maintain user sessions without storing sensitive credentials on the client side. The authentication status is monitored throughout the application, ensuring that protected routes are only accessible to authenticated users.

GraphQL Integration

At the core of this application is its integration with Zone01's GraphQL API. Unlike traditional REST APIs, GraphQL allows the client to specify exactly what data it needs, reducing over-fetching and under-fetching of data.

The application uses a custom GraphQL client that handles:

  • Authentication header inclusion
  • Error handling and token expiration
  • Structured query execution

Here's an example of how the application queries user data:

const USER_PROFILE_QUERY = `query{
  user{
    id
    firstName
    lastName
    login
    email
    campus
    auditRatio
    totalUp
    totalDown
    totalXp: transactions_aggregate(where:{type:{_eq:"xp"},eventId:{_eq:75}}){
      aggregate{
        sum{
          amount
        }
      }
    }
    // Additional fields...
  }
}`;
Enter fullscreen mode Exit fullscreen mode

This query fetches all the necessary user information in a single request, which is then processed and displayed across different sections of the dashboard.

Data Visualization

One of the standout features of this application is its use of custom SVG-based data visualizations. Rather than relying on heavy third-party libraries, the project implements lightweight, custom visualization components:

  • Line Graphs: For tracking XP progress over time
  • Bar Graphs: For comparing project completion statistics
  • Pie Charts: For displaying audit ratios
  • Donut Charts: For showing pass/fail ratios

These visualizations are created using SVG elements, providing a responsive and interactive experience while maintaining a small footprint.

Theme Management

The application implements a theme management system that allows users to toggle between light and dark modes. This preference is stored in localStorage, ensuring that the user's preferred theme persists across sessions.

The theme implementation uses CSS variables and class-based styling, making it easy to maintain and extend:

:root {
  --transition: all 0.5s ease;
  --text-color: #000;
  --dark-text-color: #fff;
  --primary-color: rgb(62, 62, 255);
  --light-bg-color: #fff;
  --dark-bg-color: #121212;
  --box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
  --dark-box-shadow: rgba(255, 255, 255, 0.15) 0px 5px 15px;
}
Enter fullscreen mode Exit fullscreen mode

User Experience

The dashboard is designed with user experience at its core. Upon logging in, users are presented with a clean, organized interface that provides immediate access to their most important information:

  1. Basic Information: User details including name, email, and campus
  2. Audit Statistics: Visualization of audit ratios and activity
  3. XP Statistics: Progress tracking with interactive charts
  4. Project Statistics: Overview of completed and current projects
  5. Skills: Visualization of skill development across different areas

Navigation is streamlined through a sidebar menu that allows users to quickly jump to different sections of their profile.

Challenges and Solutions

Authentication Security

Implementing secure authentication required careful consideration of token storage and transmission. The application uses localStorage for token storage, which provides a balance between persistence and security. All API requests include the JWT in the Authorization header, ensuring secure communication with the backend.

Dynamic Data Loading

Fetching and processing complex nested data from GraphQL required a structured approach to data handling. The application implements a loading state system that provides visual feedback during data fetching operations, enhancing the perceived performance of the application.

SVG Rendering Compatibility

Creating cross-browser compatible SVG visualizations presented challenges, particularly with text rendering and animation. The solution involved implementing fallback options and ensuring that visualizations degrade gracefully in environments with limited support.

Conclusion

This GraphQL Profile Dashboard demonstrates how modern web technologies can be combined to create a powerful, user-friendly application. By leveraging GraphQL for efficient data fetching, implementing custom SVG visualizations, and focusing on user experience, the project delivers a polished and performant solution for displaying user profile data.

The modular architecture and clean separation of concerns make the codebase maintainable and extensible, providing a solid foundation for future enhancements. Whether you're looking to implement GraphQL in your own projects or seeking inspiration for data visualization techniques, this project offers valuable insights and practical examples.

Live Demo

Experience the GraphQL Profile Dashboard in action at https://graphql-uvsx.onrender.com. Note that you'll need Zone01 credentials to log in and explore the full functionality.

Top comments (0)