tahamajs/IE / Projects /front /src /components /dashboard /widgets /WritingGoalWidget.tsx
tahamajs's picture
download
raw
6.89 kB
// src/components/dashboard/widgets/WritingGoalWidget.tsx
import { useState, useEffect, useCallback, memo } from "react";
import { Target, TrendingUp, Calendar, Edit2, X } from "lucide-react";
import { toast } from "react-toastify";
import api from "@/lib/api";
interface WritingGoal {
dailyGoal: number;
currentWords: number;
streak: number;
monthlyGoal: number;
}
export const WritingGoalWidget = memo(function WritingGoalWidget() {
const [goal, setGoal] = useState<WritingGoal>({
dailyGoal: 500,
currentWords: 0,
streak: 0,
monthlyGoal: 15000,
});
const [loading, setLoading] = useState(true);
const [showModal, setShowModal] = useState(false);
const [tempGoal, setTempGoal] = useState({ dailyGoal: 500, monthlyGoal: 15000 });
const fetchGoal = useCallback(async () => {
try {
const res = await api.get("/user/writing-goal");
if (res.data) {
setGoal({
dailyGoal: res.data.dailyGoal || 500,
currentWords: res.data.currentWords || 0,
streak: res.data.streak || 0,
monthlyGoal: res.data.monthlyGoal || 15000,
});
}
} catch (err) {
console.error("Failed to fetch writing goal", err);
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
void fetchGoal();
}, [fetchGoal]);
const updateProgress = useCallback(async () => {
const newWords = goal.currentWords + 100;
setGoal((prev) => ({ ...prev, currentWords: newWords }));
try {
await api.put("/user/writing-goal/progress", { words: newWords });
toast.success("Progress updated (+100 words)");
} catch (err) {
console.error("Failed to update progress", err);
toast.error("Failed to save progress update to server");
}
}, [goal]);
const saveGoal = useCallback(async () => {
try {
await api.put("/user/writing-goal", tempGoal);
setGoal((prev) => ({ ...prev, dailyGoal: tempGoal.dailyGoal, monthlyGoal: tempGoal.monthlyGoal }));
toast.success("Goal updated");
setShowModal(false);
} catch {
toast.error("Failed to save goal");
}
}, [tempGoal]);
const progress = goal.dailyGoal > 0 ? Math.min(100, (goal.currentWords / goal.dailyGoal) * 100) : 0;
const monthlyProgress = goal.monthlyGoal > 0 ? Math.min(100, (goal.currentWords * 30) / goal.monthlyGoal) : 0;
if (loading) return <div className="animate-pulse h-32 bg-white/5 border border-white/5 rounded-2xl" />;
return (
<div className="space-y-4 bg-white/5 dark:bg-black/20 backdrop-blur-md border border-white/10 dark:border-white/5 rounded-2xl p-4">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<Target className="w-4 h-4 text-primary" />
<h3 className="text-sm font-semibold text-gray-200">Daily Writing Goal</h3>
</div>
<button
onClick={() => {
setTempGoal({ dailyGoal: goal.dailyGoal, monthlyGoal: goal.monthlyGoal });
setShowModal(true);
}}
className="text-gray-400 hover:text-primary transition"
>
<Edit2 className="w-3.5 h-3.5" />
</button>
</div>
<div className="space-y-1.5">
<div className="flex justify-between text-xs mb-1 text-gray-300">
<span>Today: {goal.currentWords} / {goal.dailyGoal} words</span>
<span className="font-semibold text-primary">{Math.round(progress)}%</span>
</div>
<div className="w-full bg-white/5 dark:bg-black/40 rounded-full h-1.5 overflow-hidden">
<div
className="bg-gradient-to-r from-primary to-secondary h-1.5 rounded-full transition-all duration-500"
style={{ width: `${progress}%` }}
/>
</div>
</div>
<div className="flex items-center gap-4 text-xs text-gray-400">
<span className="flex items-center gap-1">
<TrendingUp className="w-3.5 h-3.5 text-green-400" />
{goal.streak} day streak
</span>
<span className="flex items-center gap-1">
<Calendar className="w-3.5 h-3.5 text-amber-400" />
{Math.round(monthlyProgress)}% of monthly
</span>
</div>
<button
onClick={updateProgress}
className="w-full py-2 bg-gradient-to-r from-primary to-secondary text-white font-medium text-xs rounded-lg hover:brightness-110 shadow-lg shadow-primary/20 transition-all duration-300"
>
Update Progress (+100 words)
</button>
{/* Modal */}
{showModal && (
<div
className="fixed inset-0 bg-black/60 backdrop-blur-xs flex items-center justify-center z-50 p-4"
onClick={() => setShowModal(false)}
>
<div
className="bg-slate-900 border border-white/10 rounded-2xl max-w-sm w-full p-5 shadow-2xl animate-in fade-in zoom-in-95 duration-200"
onClick={(e) => e.stopPropagation()}
>
<div className="flex justify-between items-center mb-4">
<h3 className="text-sm font-bold text-gray-200">Edit Writing Goals</h3>
<button
onClick={() => setShowModal(false)}
className="text-gray-400 hover:text-gray-200"
>
<X className="w-4 h-4" />
</button>
</div>
<div className="space-y-4">
<div>
<label className="block text-xs text-gray-400 mb-1">Daily goal (words)</label>
<input
type="number"
value={tempGoal.dailyGoal}
onChange={(e) => setTempGoal({ ...tempGoal, dailyGoal: parseInt(e.target.value) || 0 })}
className="w-full px-3 py-2 bg-white/5 border border-white/10 rounded-lg text-xs text-gray-200 focus:outline-none focus:border-white/20 transition-all"
/>
</div>
<div>
<label className="block text-xs text-gray-400 mb-1">Monthly goal (words)</label>
<input
type="number"
value={tempGoal.monthlyGoal}
onChange={(e) => setTempGoal({ ...tempGoal, monthlyGoal: parseInt(e.target.value) || 0 })}
className="w-full px-3 py-2 bg-white/5 border border-white/10 rounded-lg text-xs text-gray-200 focus:outline-none focus:border-white/20 transition-all"
/>
</div>
<button
onClick={saveGoal}
className="w-full py-2 bg-gradient-to-r from-primary to-secondary text-white font-medium text-xs rounded-lg hover:brightness-110 shadow-lg shadow-primary/20 transition-all duration-300"
>
Save Goals
</button>
</div>
</div>
</div>
)}
</div>
);
});

Xet Storage Details

Size:
6.89 kB
·
Xet hash:
f1264d7c8b49eca4472053435503f96969416bd842dbbbf6c3127b1ac6998405

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.