← back to Ken

react-dash/src/setup/steps/FetchWeather.jsx

86 lines

import React, { useState, useEffect } from 'react';
import { post } from '../../api';

export default function FetchWeather({ data, update, next, back }) {
  const [loading, setLoading] = useState(true);
  const [result, setResult] = useState(null);
  const [error, setError] = useState(null);

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

  async function runFetch() {
    setLoading(true);
    setError(null);
    try {
      const r = await post('/weather', { action: 'fetch_all' });
      setResult(r);
      update({ weatherFetched: true });
    } catch {
      setError('Failed to fetch weather data');
    }
    setLoading(false);
  }

  return (
    <div className="space-y-6">
      <div className="text-center">
        <h2 className="text-2xl font-bold mb-2">Weather Data</h2>
        <p className="text-gray-400 text-sm">Pulling NWS forecast data for tracked markets</p>
      </div>

      <div className="card">
        {loading ? (
          <div className="flex flex-col items-center justify-center py-12 gap-4">
            <div className="animate-spin w-10 h-10 border-3 border-blue-500 border-t-transparent rounded-full" />
            <p className="text-sm text-gray-400">Fetching NWS forecast data...</p>
          </div>
        ) : error ? (
          <div className="text-center py-8">
            <div className="w-12 h-12 rounded-full bg-red-500/10 flex items-center justify-center mx-auto mb-3">
              <svg className="w-6 h-6 text-red-400" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z" />
              </svg>
            </div>
            <p className="text-red-400 text-sm mb-3">{error}</p>
            <button onClick={runFetch} className="btn btn-s text-sm">Retry</button>
          </div>
        ) : (
          <div className="space-y-4">
            <div className="flex items-center justify-center gap-3 py-4">
              <div className="w-12 h-12 rounded-full bg-green-500/10 flex items-center justify-center">
                <svg className="w-6 h-6 text-green-400" fill="none" stroke="currentColor" strokeWidth="2" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" />
                </svg>
              </div>
              <div>
                <p className="font-semibold text-green-400">Weather Data Loaded</p>
                <p className="text-xs text-gray-400">NWS forecasts fetched successfully</p>
              </div>
            </div>

            <div className="grid grid-cols-2 gap-4">
              <div className="text-center p-3 bg-gray-800/30 rounded-lg">
                <p className="stat-v text-blue-400">{result?.cities || result?.locations || '—'}</p>
                <p className="text-xs text-gray-500 mt-1">Cities Fetched</p>
              </div>
              <div className="text-center p-3 bg-gray-800/30 rounded-lg">
                <p className="stat-v text-purple-400">{result?.data_points || result?.forecasts || '—'}</p>
                <p className="text-xs text-gray-500 mt-1">Data Points</p>
              </div>
            </div>
          </div>
        )}
      </div>

      <div className="flex items-center justify-between">
        <button onClick={back} className="text-sm text-gray-500 hover:text-gray-300">Back</button>
        <button onClick={next} disabled={loading} className="wizard-btn">
          {loading ? 'Fetching...' : 'Continue'}
        </button>
      </div>
    </div>
  );
}