1
0
Fork 0
freesewing/packages/react/context/LoadingStatus/index.mjs

123 lines
3.3 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect, createContext } from 'react'
import { Spinner } from '@freesewing/react/components/Spinner'
import { OkIcon, WarningIcon } from '@freesewing/react/components/Icon'
2023-08-19 18:01:22 +02:00
/*
* The actual context
*/
2023-09-04 08:40:05 +02:00
export const LoadingStatusContext = createContext([false])
2023-08-19 18:01:22 +02:00
/*
* Timeout in seconds before the loading status dissapears
*/
const timeout = 2
/*
* The React component displaying the loading status
*/
2023-08-19 18:01:22 +02:00
const LoadingStatus = ({ loadingStatus }) => {
const [fade, setFade] = useState('tw:opacity-100')
2023-08-19 18:01:22 +02:00
const [timer, setTimer] = useState(false)
useEffect(() => {
if (loadingStatus[2]) {
if (timer) clearTimeout(timer)
setTimer(
2023-09-29 16:01:27 +02:00
window.setTimeout(
() => {
setFade('opacity-0')
},
timeout * 1000 - 350
)
2023-08-19 18:01:22 +02:00
)
}
}, [loadingStatus[2]])
if (!loadingStatus[0]) return null
let color = 'secondary'
let icon = <Spinner />
if (loadingStatus[2]) {
color = loadingStatus[3] ? 'success' : 'error'
icon = loadingStatus[3] ? (
<OkIcon stroke={4} className="tw:w-8 tw:h-8" />
2023-08-19 18:01:22 +02:00
) : (
<WarningIcon className="tw:w-8 tw:h-8" stroke={2} />
2023-08-19 18:01:22 +02:00
)
}
return (
<div
className="tw:fixed tw:bottom-14 tw:md:top-28 tw:left-0 tw:w-full tw:z-50 tw:md:px-4 tw:md:mx-auto"
style={{ zIndex: 500 }}
>
<div
className={`tw:w-full tw:md:max-w-2xl tw:m-auto tw:bg-${color} tw:flex tw:flex-row tw:items-center tw:gap-4 tw:p-4 tw:px-4 ${fade}
tw:transition-opacity tw:delay-[${timeout * 1000 - 400}ms] tw:duration-300
tw:md:rounded-lg tw:shadow tw:text-secondary-content tw:text-lg tw:lg:text-xl tw:font-medium tw:md:bg-${color}/90`}
>
<span className="tw:shrink-0">{icon}</span>
{loadingStatus[1]}
</div>
</div>
2023-08-19 18:01:22 +02:00
)
}
/*
* An animated loading state
*/
2023-08-24 19:01:48 +02:00
const LoadingProgress = ({ val = 0, max = 1, msg }) => (
<div className="tw:flex tw:flex-col tw:gap-2 tw:w-full tw:grow-0">
2023-08-21 16:14:58 +02:00
{msg}
<progress className="tw:progress tw:progress-white" value={val} max={max}></progress>
2023-08-21 16:14:58 +02:00
</div>
)
/*
* The Context provider
*/
2023-09-04 08:40:05 +02:00
export const LoadingStatusContextProvider = ({ children }) => {
2023-08-19 18:01:22 +02:00
/*
* LoadingStatus should hold an array with 1 to 4 elements:
* 0 => Show loading status or not (true or false)
* 1 => Message to show
* 2 => Set this to true to make the loadingStatus dissapear after 2 seconds
* 3 => Set this to true to show success, false to show error (only when 2 is true)
*/
const [timer, setTimer] = useState(false)
2023-09-04 08:40:05 +02:00
const [__loadingStatus, __setLoadingStatus] = useState({
status: [false],
setLoadingStatus,
loading: false,
LoadingStatus: () => <LoadingStatus loadingStatus={[false]} />,
LoadingProgress,
})
2023-08-19 18:01:22 +02:00
useEffect(() => {
2023-09-04 08:40:05 +02:00
if (__loadingStatus.status[2]) {
2023-08-19 18:01:22 +02:00
if (timer) clearTimeout(timer)
setTimer(
window.setTimeout(() => {
setLoadingStatus([false])
}, timeout * 1000)
)
}
2023-09-04 08:40:05 +02:00
}, [__loadingStatus.status[2]])
2023-09-04 08:40:05 +02:00
function setLoadingStatus(newStatus) {
__setLoadingStatus({
...__loadingStatus,
status: newStatus,
loading: newStatus[0] || false,
LoadingStatus: () => <LoadingStatus loadingStatus={newStatus} />,
})
}
2023-09-04 08:40:05 +02:00
return (
<LoadingStatusContext.Provider value={__loadingStatus}>
{children}
</LoadingStatusContext.Provider>
)
}