сохранение текущего прогресса в упражнении в locslstorage
This commit is contained in:
parent
64d2b21cc0
commit
00e6cf0222
@ -50,6 +50,35 @@ export const Exercise = () => {
|
|||||||
const [totalSets, setTotalSets] = useState(3)
|
const [totalSets, setTotalSets] = useState(3)
|
||||||
const [isRotating, setIsRotating] = useState(false)
|
const [isRotating, setIsRotating] = useState(false)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//Можно реализовать полностью на клиенте.
|
||||||
|
// Ограничение — прогресс сохраняется только на устройстве пользователя.
|
||||||
|
// Если нужно синхронизировать между устройствами или сохранять историю, потребуется бэкенд.
|
||||||
|
//сохраняем прогресс
|
||||||
|
const handlePause = () => {
|
||||||
|
const currentProgress = {
|
||||||
|
exerciseId: exercise?.id,
|
||||||
|
position: currentTime,
|
||||||
|
set: currentSet,
|
||||||
|
};
|
||||||
|
localStorage.setItem('exerciseProgress', JSON.stringify(currentProgress));
|
||||||
|
console.log('прогресс:', currentProgress)
|
||||||
|
// или отправить на сервер
|
||||||
|
};
|
||||||
|
|
||||||
|
// В начале упражнения
|
||||||
|
useEffect(() => {
|
||||||
|
const savedProgress = localStorage.getItem('exerciseProgress');
|
||||||
|
if (savedProgress) {
|
||||||
|
const progress = JSON.parse(savedProgress);
|
||||||
|
if (progress.exerciseId === exercise?.id) {
|
||||||
|
setCurrentTime(progress.position);
|
||||||
|
setCurrentSet(progress.set);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [exercise]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log("Course ID:", courseId)
|
console.log("Course ID:", courseId)
|
||||||
console.log("Exercise ID:", exerciseId)
|
console.log("Exercise ID:", exerciseId)
|
||||||
@ -338,7 +367,13 @@ export const Exercise = () => {
|
|||||||
</div>
|
</div>
|
||||||
<div className="flex space-x-3">
|
<div className="flex space-x-3">
|
||||||
<button
|
<button
|
||||||
onClick={() => setIsPlaying(!isPlaying)}
|
onClick={() => {
|
||||||
|
setIsPlaying(!isPlaying)
|
||||||
|
handlePause()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
className={`flex-1 font-bold py-3 px-4 rounded-xl transition-all duration-300 transform hover:scale-105 flex items-center justify-center space-x-2 cursor-pointer ${isPlaying
|
className={`flex-1 font-bold py-3 px-4 rounded-xl transition-all duration-300 transform hover:scale-105 flex items-center justify-center space-x-2 cursor-pointer ${isPlaying
|
||||||
? "bg-gray-400 text-white shadow-lg"
|
? "bg-gray-400 text-white shadow-lg"
|
||||||
: "bg-[#2BACBE] hover:bg-[#2099A8] text-white shadow-lg"
|
: "bg-[#2BACBE] hover:bg-[#2099A8] text-white shadow-lg"
|
||||||
@ -359,6 +394,7 @@ export const Exercise = () => {
|
|||||||
setCurrentTime(0)
|
setCurrentTime(0)
|
||||||
setIsPlaying(false)
|
setIsPlaying(false)
|
||||||
handleClick()
|
handleClick()
|
||||||
|
|
||||||
}}
|
}}
|
||||||
className="cursor-pointer px-6 py-3 bg-white text-gray-800 font-bold rounded-xl transition-all duration-300 hover:scale-105 hover:shadow-lg border border-gray-200 flex items-center justify-center"
|
className="cursor-pointer px-6 py-3 bg-white text-gray-800 font-bold rounded-xl transition-all duration-300 hover:scale-105 hover:shadow-lg border border-gray-200 flex items-center justify-center"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user