|
1 |
| -"use client"; |
2 | 1 | import React, { useState, useEffect } from 'react';
|
3 |
| -import { useDelayedClickListener } from "../../hooks"; |
4 | 2 | import { useQuickForm } from "../../state/QuickFormContext";
|
5 | 3 | import { quickformtokens } from '../../style/quickFormTokensDefinition';
|
6 | 4 |
|
7 | 5 | type ErrorPopupProps = {
|
8 | 6 | readonly message: string;
|
9 | 7 | };
|
10 | 8 |
|
11 |
| -export const ErrorPopup: React.FC<ErrorPopupProps> = ({ message }: ErrorPopupProps) => { |
| 9 | +export const ErrorPopup: React.FC<ErrorPopupProps> = ({ message }) => { |
12 | 10 | const [isVisible, setIsVisible] = useState(false);
|
13 |
| - const { dispatch, state } = useQuickForm(); |
| 11 | + const [opacity, setOpacity] = useState(0); |
| 12 | + const { dispatch } = useQuickForm(); |
14 | 13 |
|
15 |
| - /** |
16 |
| - * DISCUSS - What cases is there for resetting error and can it be handled in reducer all alone?. |
17 |
| - * When an error is shown, upon next answer it can be cleared. |
18 |
| - * Possible a dissmis button - but i dont think it should automatically just remove when clicked. |
19 |
| - */ |
20 |
| - |
21 |
| - |
22 |
| - //const resetErrorMessage = () => { |
23 |
| - // if (state.errorMsg !== "") { |
24 |
| - // dispatch({ type: "SET_ERROR_MSG", msg: "" }) |
25 |
| - // } |
26 |
| - //} |
27 |
| - |
28 |
| - // useDelayedClickListener(resetErrorMessage); |
| 14 | + const resetErrorPopup = () => { |
| 15 | + dispatch({ type: 'SET_ERROR_MSG', msg: "" }); |
| 16 | + setIsVisible(false); |
| 17 | + setOpacity(0); |
| 18 | + } |
29 | 19 |
|
30 | 20 | useEffect(() => {
|
| 21 | + let timer: NodeJS.Timeout; |
31 | 22 | if (message) {
|
32 | 23 | setIsVisible(true);
|
33 |
| - setTimeout(() => setIsVisible(false), 350); |
| 24 | + setOpacity(0); |
| 25 | + setTimeout(() => setOpacity(1), 10); |
| 26 | + |
| 27 | + timer = setTimeout(() => { |
| 28 | + resetErrorPopup(); |
| 29 | + }, 3000); |
| 30 | + } else { |
| 31 | + resetErrorPopup(); |
34 | 32 | }
|
| 33 | + |
| 34 | + return () => { |
| 35 | + clearTimeout(timer); |
| 36 | + setOpacity(0); |
| 37 | + }; |
35 | 38 | }, [message]);
|
36 | 39 |
|
37 |
| - if (message === "") { |
38 |
| - return <></>; |
39 |
| - } |
| 40 | + if (!isVisible) return null; |
40 | 41 |
|
41 |
| - const errorStyle: React.CSSProperties = { |
42 |
| - alignItems: 'flex-end', |
43 |
| - animation: isVisible ? 'slide-up 0.35s linear 1 forwards' : '', |
44 |
| - backgroundColor: quickformtokens.error, |
45 |
| - borderRadius: '3px', |
46 |
| - color: quickformtokens.onError, |
47 |
| - display: 'flex', |
48 |
| - fontSize: '1.5rem', |
49 |
| - marginTop: '15px', |
50 |
| - padding: '8px 12px', |
51 |
| - width: 'max-content', |
52 |
| - }; |
53 |
| - |
54 |
| - const mobileErrorStyle: React.CSSProperties = { |
55 |
| - ...errorStyle, |
56 |
| - fontSize: '1.75rem', |
57 |
| - marginTop: '22px', |
| 42 | + const backdropStyle: React.CSSProperties = { |
| 43 | + position: 'fixed', |
| 44 | + top: 0, |
| 45 | + left: 0, |
58 | 46 | width: '100%',
|
| 47 | + height: '100%', |
| 48 | + backgroundColor: 'rgba(0, 0, 0, 0.5)', |
| 49 | + display: 'flex', |
| 50 | + justifyContent: 'center', |
| 51 | + alignItems: 'center', |
| 52 | + zIndex: 1000, |
59 | 53 | };
|
60 | 54 |
|
61 |
| - const imgStyle: React.CSSProperties = { |
62 |
| - marginRight: '4px', |
| 55 | + const toastStyle: React.CSSProperties = { |
| 56 | + padding: '20px', |
| 57 | + borderRadius: '10px', |
| 58 | + backgroundColor: quickformtokens.error, |
| 59 | + color: quickformtokens.onError, |
| 60 | + fontSize: '16px', |
| 61 | + boxShadow: '0 4px 6px rgba(0, 0, 0, 0.1)', |
| 62 | + opacity: opacity, |
| 63 | + transition: 'opacity 0.5s ease-in', |
63 | 64 | };
|
64 | 65 |
|
65 | 66 | return (
|
66 |
| - <div style={window.innerWidth <= 599 ? mobileErrorStyle : errorStyle}> |
67 |
| - {/* If there's an image you want to include inside the error message */} |
68 |
| - {/* <img src="path_to_your_image" alt="Error" style={imgStyle} /> */} |
69 |
| - {message} |
| 67 | + <div style={backdropStyle} onClick={resetErrorPopup}> |
| 68 | + <div style={toastStyle} onClick={(e) => e.stopPropagation()}> |
| 69 | + {message} |
| 70 | + </div> |
70 | 71 | </div>
|
71 | 72 | );
|
72 | 73 | };
|
0 commit comments