mvp для просмотра и загрузки фильмов
This commit is contained in:
47
src/contexts/ToastProvider.js
Normal file
47
src/contexts/ToastProvider.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import * as React from 'react';
|
||||
import {Toast, ToastContainer} from "react-bootstrap";
|
||||
import {useState} from "react";
|
||||
|
||||
export const ToastContext = React.createContext(undefined);
|
||||
|
||||
export function ToastProvider({children}) {
|
||||
const [toasts, setToasts] = useState([]);
|
||||
|
||||
// Функция для добавления нового тоста
|
||||
const addToast = (message, variant = 'secondary') => {
|
||||
const newToast = {
|
||||
id: Date.now(), // Уникальный идентификатор
|
||||
message,
|
||||
variant,
|
||||
};
|
||||
|
||||
setToasts([newToast, ...toasts]);
|
||||
|
||||
// Удаляем тост через 3 секунды
|
||||
setTimeout(() => {
|
||||
setToasts(toasts.filter(t => t.id !== newToast.id));
|
||||
}, 3000);
|
||||
};
|
||||
|
||||
return (
|
||||
<ToastContext.Provider value={{ addToast }}>
|
||||
{children}
|
||||
<ToastContainer position="bottom-end">
|
||||
{toasts.map(toast => (
|
||||
<Toast
|
||||
key={toast.id}
|
||||
bg={toast.variant}
|
||||
aria-atomic={true}
|
||||
className="rounded me-2 mb-1 me-1 border-0"
|
||||
autohide
|
||||
delay={3000}
|
||||
>
|
||||
<Toast.Body className="text-white">
|
||||
{toast.message}
|
||||
</Toast.Body>
|
||||
</Toast>
|
||||
))}
|
||||
</ToastContainer>
|
||||
</ToastContext.Provider>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user