Skip to content

refactor: extract dashboard API calls into custom React hooks #1053

@Priyanshu-byte-coder

Description

@Priyanshu-byte-coder

Refactor Request

Describe the problem
Dashboard widget components directly call fetch() inside useEffect hooks, leading to duplicated loading/error state logic, inconsistent error handling, and difficulty in testing.

Describe the solution you'd like
Extract API calls into reusable custom hooks:

  • useMetrics() — fetches /api/metrics
  • useStreak() — fetches /api/streak
  • useNotifications() — fetches /api/notifications
  • useUserSettings() — fetches /api/user/settings

Hook structure

function useMetrics() {
  const [data, setData] = useState<Metrics | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<Error | null>(null);
  // fetch logic...
  return { data, loading, error, refetch };
}

Implementation Details

  • Create src/hooks/ directory
  • One file per hook
  • Each hook handles loading, error, and data states consistently
  • Hooks support manual refetch
  • Update consuming components to use hooks

Acceptance Criteria

  • All 4 hooks created in src/hooks/
  • All consuming components updated to use hooks
  • No duplicated fetch logic remaining in components
  • Each hook has at least 3 unit tests
  • TypeScript types properly defined for all hook return values

Metadata

Metadata

Assignees

Labels

gssoc26GSSoC 2026 contributiongssoc:assignedGSSoC: Issue assigned to a contributorlevel:intermediateGSSoC: Intermediate difficulty (35 pts)type:refactorGSSoC type bonus: refactor (+10 pts)

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions