All files / Context/WebAppsUX WebAppsUX.js

100% Statements 83/83
100% Branches 46/46
100% Functions 18/18
100% Lines 81/81

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221                  20x   20x   20x 222x 220x 220x 220x       220x       220x 220x   220x 220x   220x 90x 90x 90x   90x             220x 90x     89x 89x 89x         1x 1x         220x 90x   88x               88x 88x         2x 1x             1x         220x   2x 2x 2x       220x           220x 201x     220x   3x 3x 2x 2x   3x 3x       220x   1x 1x 1x       220x 2x 1x   1x       220x   3x 3x 2x 2x   3x 3x       220x   1x   1x 1x 1x   1x 1x       220x 2x 1x   1x       220x             220x               220x               220x 2x 2x     218x 90x     128x                                      
import React, { createContext, useCallback, useEffect, useRef, useState } from 'react';
import { throttle } from 'lodash';
 
import { client as APIClient } from '../../API';
import { getDeviceConfig } from '../../Helpers/getDeviceConfig';
import { isWithinBreakpoint } from '../../Helpers/isWithinBreakpoint';
import { ToastProvider } from '../../Toasts';
import Loader from '../../Components/Loader';
 
export const WebAppsUXContext = createContext({});
 
let APIController = new AbortController();
 
export const WebAppsUX = props => {
    const [coreError, setCoreError] = useState(null);
    const [breakpoint, setBreakpoint] = useState(() => getDeviceConfig(window.innerWidth));
    const [darkMode, setDarkMode] = useState();
    const [drawer, setDrawer] = useState({
        display_mode: 'side',
        opened: true,
    });
    const [flyout, setFlyout] = useState({
        display_mode: 'side',
        opened: false,
    });
    const [navigation, setNavigation] = useState({});
    const [theme, setTheme] = useState();
 
    const isMountedRef = useRef(true);
    const isMounted = useCallback(() => isMountedRef.current, []);
 
    useEffect(async () => {
        window.addEventListener('resize', calcInnerWidth);
        await loadUI();
        await loadNavigation();
 
        return /* istanbul ignore next */ () => {
            void (isMountedRef.current = false);
            APIController.abort();
            window.removeEventListener('resize', calcInnerWidth);
        }
    }, []);
 
    const loadUI = async () => {
        await APIClient('/api/setting', { key: JSON.stringify(['core.ui.theme', 'core.ui.dark_mode']) }, { signal: APIController.signal })
            .then(json => {
                /* istanbul ignore else */
                if (isMounted()) {
                    setTheme(json.data['core.ui.theme']);
                    setDarkMode(json.data['core.ui.dark_mode']);
                }
            })
            .catch(error => {
                /* istanbul ignore else */
                if (!error.status?.isAbort && isMounted()) {
                    setCoreError(error.data.message);
                }
            });
    };
 
    const loadNavigation = async () => {
        await APIClient('/api/navigation', undefined, { signal: APIController.signal })
            .then(json => {
                let navigation = {
                    menu: json.data.navigation,
                    routes: json.data.routes,
                    color_mode: json.data.sidebar.color_mode,
                    display_mode: isBreakpoint('lg') ? 'side' : 'overlay',
                    opened: isBreakpoint('lg') ? true : false,
                }
                /* istanbul ignore else */
                if (isMounted()) {
                    setNavigation({ ...navigation });
                }
            })
            .catch(error => {
                /* istanbul ignore else */
                if (!error.status?.isAbort && isMounted()) {
                    let navigation = {
                        color_mode: 'dark',
                        menu: {
                            error: true,
                            message: error.data?.message
                        }
                    }
                    setNavigation({ ...navigation });
                }
            });
    };
 
    const toggleNavigation = () => {
        /* istanbul ignore else */
        if (isMounted()) {
            navigation.opened = !navigation.opened
            setNavigation({ ...navigation });
        }
    }
 
    const calcInnerWidth = throttle(/* istanbul ignore next */() => {
        if (isMounted()) {
            setBreakpoint(getDeviceConfig(window.innerWidth));
        }
    }, 200);
 
    const isBreakpoint = bp => {
        return isWithinBreakpoint(breakpoint, bp);
    };
 
    const openDrawer = () => {
        /* istanbul ignore else */
        if (!drawer.opened && isMounted()) {
            if (isBreakpoint('lg') && flyout.active) {
                flyout.opened = false;
                setFlyout({ ...flyout });
            }
            drawer.opened = true;
            setDrawer({ ...drawer });
        }
    };
 
    const closeDrawer = () => {
        /* istanbul ignore else */
        if (drawer.opened && isMounted()) {
            drawer.opened = false;
            setDrawer({ ...drawer });
        }
    };
 
    const toggleDrawer = () => {
        if (drawer.opened) {
            closeDrawer();
        } else {
            openDrawer();
        }
    };
 
    const openFlyout = () => {
        /* istanbul ignore else */
        if (!flyout.opened && isMounted()) {
            if (isBreakpoint('lg') && drawer.active) {
                drawer.opened = false;
                setDrawer({ ...drawer });
            }
            flyout.opened = true;
            setFlyout({ ...flyout });
        }
    };
 
    const closeFlyout = () => {
        /* istanbul ignore else */
        if (flyout.opened && isMounted()) {
            /* istanbul ignore else */
            if (isBreakpoint('lg') && drawer.active) {
                drawer.opened = true;
                setDrawer({ ...drawer });
            }
            flyout.opened = false;
            setFlyout({ ...flyout });
        }
    };
 
    const toggleFlyout = () => {
        if (flyout.opened) {
            closeFlyout();
        } else {
            openFlyout();
        }
    };
 
    const useNavigation = {
        navigation,
        loadNavigation,
        setNavigation,
        toggleNavigation,
    }
 
    const useDrawer = {
        drawer,
        setDrawer,
        toggleDrawer,
        closeDrawer,
        openDrawer,
    };
 
    const useFlyouts = {
        flyout,
        setFlyout,
        toggleFlyout,
        closeFlyout,
        openFlyout,
    };
 
    if (coreError) {
        APIController.abort();
        throw Error(coreError);
    }
 
    if (!navigation.menu && !theme) {
        return <Loader />;
    }
 
    return (
        <WebAppsUXContext.Provider
            value={{
                breakpoint,
                darkMode,
                isBreakpoint,
                setDarkMode,
                setTheme,
                theme,
                useDrawer,
                useFlyouts,
                useNavigation,
            }}
        >
            <ToastProvider theme={theme} autoDismiss="true" autoDismissTimeout="3000">
                {props.children}
            </ToastProvider>
        </WebAppsUXContext.Provider>
    )
}