All files / src/components/features/admin/dev/DevSprintManagement index.tsx

0% Statements 0/304
100% Branches 0/0
0% Functions 0/1
0% Lines 0/304

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
'use client';

/**
 * DevSprintManagement - Admin dev sprint management interface
 */

import React, { useState, useEffect, useCallback } from 'react';
import Link from 'next/link';
import DevSprintList from './DevSprintList';
import { Icon } from '@/components/ui/icons';

interface SprintWithStats {
  id: string;
  name: string;
  goal: string | null;
  status: string;
  startDate: string;
  endDate: string;
  project: {
    id: string;
    name: string;
    key: string;
    color: string;
  };
  _count: {
    tickets: number;
  };
  stats: {
    totalTickets: number;
    completedTickets: number;
    completionPercentage: number;
    totalStoryPoints: number;
    completedStoryPoints: number;
    pointsPercentage: number;
  };
}

interface Project {
  id: string;
  name: string;
  key: string;
  color: string;
}

export default function DevSprintManagement() {
  const [sprints, setSprints] = useState<SprintWithStats[]>([]);
  const [projects, setProjects] = useState<Project[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [statusFilter, setStatusFilter] = useState<string>('');
  const [projectFilter, setProjectFilter] = useState<string>('');

  const fetchSprints = useCallback(async () => {
    setLoading(true);
    setError(null);

    try {
      const params = new URLSearchParams();
      if (statusFilter) params.set('status', statusFilter);
      if (projectFilter) params.set('projectId', projectFilter);

      const response = await fetch(`/api/dev/sprints?${params}`);
      if (!response.ok) {
        throw new Error('Failed to fetch sprints');
      }

      const data = await response.json();
      setSprints(data.data || []);
    } catch (err) {
      setError(err instanceof Error ? err.message : 'An error occurred');
    } finally {
      setLoading(false);
    }
  }, [statusFilter, projectFilter]);

  const fetchProjects = useCallback(async () => {
    try {
      const response = await fetch('/api/dev/projects');
      if (response.ok) {
        const data = await response.json();
        setProjects(data.data || []);
      }
    } catch {
      // Silently fail
    }
  }, []);

  useEffect(() => {
    fetchSprints();
  }, [fetchSprints]);

  useEffect(() => {
    fetchProjects();
  }, [fetchProjects]);

  const handleActivate = async (id: string) => {
    if (!confirm('Are you sure you want to start this sprint?')) {
      return;
    }

    try {
      const response = await fetch(`/api/dev/sprints/${id}`, {
        method: 'PATCH',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ status: 'ACTIVE' })});

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.error || 'Failed to activate sprint');
      }

      fetchSprints();
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to activate sprint');
    }
  };

  const handleComplete = async (id: string) => {
    if (!confirm('Are you sure you want to complete this sprint?')) {
      return;
    }

    try {
      const response = await fetch(`/api/dev/sprints/${id}`, {
        method: 'PATCH',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ status: 'COMPLETED' })});

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.error || 'Failed to complete sprint');
      }

      fetchSprints();
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to complete sprint');
    }
  };

  const handleDelete = async (id: string) => {
    if (!confirm('Are you sure you want to delete this sprint? Tickets will be moved to backlog.')) {
      return;
    }

    try {
      const response = await fetch(`/api/dev/sprints/${id}`, {
        method: 'DELETE'});

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.error || 'Failed to delete sprint');
      }

      fetchSprints();
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to delete sprint');
    }
  };

  const activeSprints = sprints.filter((s) => s.status === 'ACTIVE');
  const planningSprints = sprints.filter((s) => s.status === 'PLANNING');
  const completedSprints = sprints.filter((s) => s.status === 'COMPLETED');

  return (
    <div className="max-w-[1400px] mx-auto px-4 sm:px-7.5 xl:px-0 space-y-6">
      {/* Header */}
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold text-gray-900">Sprints</h1>
          <p className="text-sm text-gray-500 mt-1">
            Manage development sprints and iterations
          </p>
        </div>
        <Link
          href="/admin/dev/sprints/new"
          className="inline-flex items-center px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition-colors"
        >
          <Icon name="plus" size={16} className="mr-2" />
          New Sprint
        </Link>
      </div>

      {/* Filters */}
      <div className="bg-white rounded-lg border border-gray-200 p-4">
        <div className="grid grid-cols-1 sm:grid-cols-3 gap-4">
          <div>
            <label className="block text-sm font-medium text-gray-700 mb-1">
              Project
            </label>
            <select
              value={projectFilter}
              onChange={(e) => setProjectFilter(e.target.value)}
              className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-transparent"
            >
              <option value="">All Projects</option>
              {projects.map((project) => (
                <option key={project.id} value={project.id}>
                  [{project.key}] {project.name}
                </option>
              ))}
            </select>
          </div>
          <div>
            <label className="block text-sm font-medium text-gray-700 mb-1">
              Status
            </label>
            <select
              value={statusFilter}
              onChange={(e) => setStatusFilter(e.target.value)}
              className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-transparent"
            >
              <option value="">All Statuses</option>
              <option value="PLANNING">Planning</option>
              <option value="ACTIVE">Active</option>
              <option value="COMPLETED">Completed</option>
              <option value="CANCELLED">Cancelled</option>
            </select>
          </div>
        </div>
      </div>

      {/* Error */}
      {error && (
        <div className="bg-red-50 text-red-700 px-4 py-3 rounded-lg flex items-center justify-between">
          <span>{error}</span>
          <button
            onClick={() => setError(null)}
            className="text-red-500 hover:text-red-700"
          >
            <Icon name="close" size={20} />
          </button>
        </div>
      )}

      {/* Active Sprints Section */}
      {activeSprints.length > 0 && (
        <div>
          <h2 className="text-lg font-semibold text-gray-900 mb-4 flex items-center gap-2">
            <span className="w-2 h-2 rounded-full bg-green-500" />
            Active Sprints
          </h2>
          <DevSprintList
            sprints={activeSprints}
            loading={false}
            onComplete={handleComplete}
          />
        </div>
      )}

      {/* Planning Sprints Section */}
      {planningSprints.length > 0 && (
        <div>
          <h2 className="text-lg font-semibold text-gray-900 mb-4 flex items-center gap-2">
            <span className="w-2 h-2 rounded-full bg-gray-400" />
            Planning
          </h2>
          <DevSprintList
            sprints={planningSprints}
            loading={false}
            onActivate={handleActivate}
            onDelete={handleDelete}
          />
        </div>
      )}

      {/* Completed Sprints Section */}
      {completedSprints.length > 0 && (
        <div>
          <h2 className="text-lg font-semibold text-gray-900 mb-4 flex items-center gap-2">
            <span className="w-2 h-2 rounded-full bg-blue-500" />
            Completed
          </h2>
          <DevSprintList
            sprints={completedSprints}
            loading={false}
            onDelete={handleDelete}
          />
        </div>
      )}

      {/* Loading State */}
      {loading && (
        <div className="bg-white rounded-lg border border-gray-200 p-8 text-center">
          <div className="animate-spin h-8 w-8 border-4 border-indigo-600 border-t-transparent rounded-full mx-auto" />
          <p className="text-gray-500 mt-4">Loading sprints...</p>
        </div>
      )}

      {/* Empty State */}
      {!loading && sprints.length === 0 && (
        <div className="bg-white rounded-lg border border-gray-200 p-8 text-center">
          <Icon name="calendar" size={48} className="text-gray-400 mx-auto mb-4" />
          <p className="text-gray-500 mb-4">No sprints found</p>
          <Link
            href="/admin/dev/sprints/new"
            className="inline-flex items-center px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition-colors"
          >
            Create First Sprint
          </Link>
        </div>
      )}
    </div>
  );
}