File size: 5,245 Bytes
70ed4fd
f074415
70ed4fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f074415
 
 
 
70ed4fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f074415
 
70ed4fd
f074415
 
 
 
70ed4fd
 
 
 
 
 
f074415
 
70ed4fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f074415
70ed4fd
 
 
 
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
import React, { useState, useEffect } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import axios from 'axios';
import toast from 'react-hot-toast';
import ExampleCard from '../components/ExampleCard';
import { Shuffle, RefreshCw, Database } from 'lucide-react';

const Random = () => {
  const [currentExample, setCurrentExample] = useState(null);
  const [loading, setLoading] = useState(true);
  const [refreshing, setRefreshing] = useState(false);
  const [stats, setStats] = useState({ totalExamples: 0 });

  const fetchRandomExample = async (showLoadingState = true) => {
    if (showLoadingState) {
      setRefreshing(true);
    }
    
    try {
      const response = await axios.get('/api/examples/random');
      setCurrentExample(response.data.data);
    } catch (error) {
      toast.error('Failed to load random example');
      console.error('Error fetching random example:', error);
    } finally {
      setLoading(false);
      setRefreshing(false);
    }
  };

  const fetchStats = async () => {
    try {
      const response = await axios.get('/api/stats');
      setStats(response.data.data);
    } catch (error) {
      console.error('Error fetching stats:', error);
    }
  };

  useEffect(() => {
    fetchRandomExample();
    fetchStats();
  }, []);

  const handleNewExample = () => {
    fetchRandomExample(true);
  };

  if (loading) {
    return (
      <div className="loading">
        <div className="spinner"></div>
      </div>
    );
  }

  return (
    <motion.div
      initial={{ opacity: 0, y: 20 }}
      animate={{ opacity: 1, y: 0 }}
      transition={{ duration: 0.6 }}
    >
      <div className="card" style={{ 
        marginBottom: '2rem', 
        textAlign: 'center',
        maxWidth: '800px',
        margin: '0 auto 2rem auto'
      }}>
        <h1 style={{ 
          marginBottom: '1rem', 
          display: 'flex', 
          alignItems: 'center', 
          justifyContent: 'center',
          gap: '0.5rem' 
        }}>
          <Shuffle size={24} />
          Random Discovery
        </h1>
        <p style={{ color: '#666', marginBottom: '1.5rem' }}>
          Discover transcreation examples through serendipitous exploration. 
          Each click reveals a new example to inspire and educate.
        </p>

        <div style={{ 
          display: 'flex', 
          gap: '1rem', 
          justifyContent: 'center', 
          flexWrap: 'wrap',
          marginBottom: '1rem'
        }}>
          <button 
            className="btn"
            onClick={handleNewExample}
            disabled={refreshing}
            style={{ 
              display: 'inline-flex',
              alignItems: 'center',
              gap: '0.5rem'
            }}
          >
            {refreshing ? (
              <>
                <RefreshCw size={20} className="spin" />
                Loading...
              </>
            ) : (
              <>
                <Shuffle size={20} />
                {currentExample ? 'Get Another Example' : 'Try Again'}
              </>
            )}
          </button>
        </div>

        <div style={{ 
          display: 'flex', 
          alignItems: 'center', 
          justifyContent: 'center',
          gap: '1rem',
          fontSize: '0.9rem',
          color: '#666'
        }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: '0.25rem' }}>
            <Database size={14} />
            <span>{stats.totalExamples} examples available</span>
          </div>
        </div>
      </div>

      {currentExample ? (
        <AnimatePresence mode="wait">
          <motion.div
            key={currentExample.id}
            initial={{ opacity: 0, scale: 0.95 }}
            animate={{ opacity: 1, scale: 1 }}
            exit={{ opacity: 0, scale: 0.95 }}
            transition={{ duration: 0.3 }}
            style={{ 
              maxWidth: '800px', 
              margin: '0 auto'
            }}
          >
            <ExampleCard example={currentExample} />
          </motion.div>
        </AnimatePresence>
      ) : (
        <div className="card" style={{ textAlign: 'center', padding: '3rem' }}>
          <h3 style={{ marginBottom: '1rem' }}>No examples available</h3>
          <p style={{ color: '#666', marginBottom: '1.5rem' }}>
            Please try again later.
          </p>
          <button 
            className="btn"
            onClick={handleNewExample}
            disabled={refreshing}
            style={{ 
              display: 'inline-flex',
              alignItems: 'center',
              gap: '0.5rem'
            }}
          >
            {refreshing ? (
              <>
                <RefreshCw size={20} className="spin" />
                Loading...
              </>
            ) : (
              <>
                <Shuffle size={20} />
                Try Again
              </>
            )}
          </button>
        </div>
      )}

      <style jsx>{`
        .spin {
          animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
          from { transform: rotate(0deg); }
          to { transform: rotate(360deg); }
        }
      `}</style>
    </motion.div>
  );
};

export default Random;