First Commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { useEffect, useMemo } from 'react';
|
||||
|
||||
// @third-party
|
||||
import useSWR, { mutate } from 'swr';
|
||||
|
||||
// @project
|
||||
import { usePathname } from '@/utils/navigation';
|
||||
|
||||
const initialState = {
|
||||
activePath: '',
|
||||
data: []
|
||||
};
|
||||
|
||||
export const endpoints = {
|
||||
key: 'api/breadcrumbs',
|
||||
master: 'master'
|
||||
};
|
||||
|
||||
export function useGetBreadcrumbsMaster() {
|
||||
// to fetch initial state based on endpoints
|
||||
|
||||
const { data, isLoading } = useSWR(endpoints.key + endpoints.master, () => initialState, {
|
||||
revalidateIfStale: false,
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: false
|
||||
});
|
||||
|
||||
// reset cache if currentPath doesn't match activePath
|
||||
const currentPath = usePathname();
|
||||
useEffect(() => {
|
||||
if (data && data.activePath !== currentPath) {
|
||||
mutate(endpoints.key + endpoints.master, initialState, false);
|
||||
}
|
||||
}, [currentPath, data]);
|
||||
|
||||
const memoizedValue = useMemo(
|
||||
() => ({
|
||||
breadcrumbsMaster: data,
|
||||
breadcrumbsMasterLoading: isLoading
|
||||
}),
|
||||
[data, isLoading]
|
||||
);
|
||||
|
||||
return memoizedValue;
|
||||
}
|
||||
|
||||
export function handlerBreadcrumbs(activePath, data) {
|
||||
// to update `openedItem` local state based on key
|
||||
|
||||
mutate(
|
||||
endpoints.key + endpoints.master,
|
||||
(currentBreadcrumbsMaster = initialState) => {
|
||||
return { ...currentBreadcrumbsMaster, activePath, data };
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { useMemo } from 'react';
|
||||
|
||||
// @third-party
|
||||
import useSWR, { mutate } from 'swr';
|
||||
|
||||
const initialState = {
|
||||
openedItem: '',
|
||||
isDashboardDrawerOpened: false
|
||||
};
|
||||
|
||||
export const endpoints = {
|
||||
key: 'api/menu',
|
||||
master: 'master'
|
||||
};
|
||||
|
||||
export function useGetMenuMaster() {
|
||||
// to fetch initial state based on endpoints
|
||||
|
||||
const { data, isLoading } = useSWR(endpoints.key + endpoints.master, () => initialState, {
|
||||
revalidateIfStale: false,
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: false
|
||||
});
|
||||
|
||||
const memoizedValue = useMemo(
|
||||
() => ({
|
||||
menuMaster: data,
|
||||
menuMasterLoading: isLoading
|
||||
}),
|
||||
[data, isLoading]
|
||||
);
|
||||
|
||||
return memoizedValue;
|
||||
}
|
||||
|
||||
export function handlerDrawerOpen(isDashboardDrawerOpened) {
|
||||
// to update `isDashboardDrawerOpened` local state based on key
|
||||
|
||||
mutate(
|
||||
endpoints.key + endpoints.master,
|
||||
(currentMenuMaster = initialState) => {
|
||||
return { ...currentMenuMaster, isDashboardDrawerOpened };
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
export function handlerActiveItem(openedItem) {
|
||||
// to update `openedItem` local state based on key
|
||||
|
||||
mutate(
|
||||
endpoints.key + endpoints.master,
|
||||
(currentMenuMaster = initialState) => {
|
||||
return { ...currentMenuMaster, openedItem };
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
import { useMemo } from 'react';
|
||||
|
||||
// @third-party
|
||||
import useSWR, { mutate } from 'swr';
|
||||
|
||||
const endpoints = {
|
||||
key: 'snackbar'
|
||||
};
|
||||
|
||||
const initialState = {
|
||||
action: false,
|
||||
open: false,
|
||||
message: 'Note archived',
|
||||
anchorOrigin: {
|
||||
vertical: 'bottom',
|
||||
horizontal: 'right'
|
||||
},
|
||||
variant: 'default',
|
||||
alert: {
|
||||
color: 'primary',
|
||||
variant: 'filled'
|
||||
},
|
||||
transition: 'Zoom',
|
||||
close: false,
|
||||
actionButton: false,
|
||||
maxStack: 3,
|
||||
dense: false,
|
||||
iconVariant: 'useemojis',
|
||||
hideIconVariant: false
|
||||
};
|
||||
|
||||
export function useGetSnackbar() {
|
||||
const { data } = useSWR(endpoints.key, () => initialState, {
|
||||
revalidateIfStale: false,
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: false
|
||||
});
|
||||
|
||||
const memoizedValue = useMemo(() => ({ snackbar: data }), [data]);
|
||||
|
||||
return memoizedValue;
|
||||
}
|
||||
|
||||
export function openSnackbar(snackbar) {
|
||||
// to update local state based on key
|
||||
|
||||
const { action, open, message, anchorOrigin, variant, alert, transition, close, actionButton } = snackbar;
|
||||
|
||||
mutate(
|
||||
endpoints.key,
|
||||
(currentSnackbar) => {
|
||||
const safeSnackbar = currentSnackbar || initialState;
|
||||
return {
|
||||
...safeSnackbar,
|
||||
action: action || initialState.action,
|
||||
open: open || initialState.open,
|
||||
message: message || initialState.message,
|
||||
anchorOrigin: anchorOrigin || initialState.anchorOrigin,
|
||||
variant: variant || initialState.variant,
|
||||
alert: {
|
||||
color: alert?.color || initialState.alert.color,
|
||||
variant: alert?.variant || initialState.alert.variant
|
||||
},
|
||||
transition: transition || initialState.transition,
|
||||
close: close || initialState.close,
|
||||
actionButton: actionButton || initialState.actionButton
|
||||
};
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
export function closeSnackbar() {
|
||||
// to update local state based on key
|
||||
mutate(
|
||||
endpoints.key,
|
||||
(currentSnackbar) => {
|
||||
const safeSnackbar = currentSnackbar || initialState;
|
||||
return { ...safeSnackbar, open: false };
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
export function handlerIncrease(maxStack) {
|
||||
// to update local state based on key
|
||||
mutate(
|
||||
endpoints.key,
|
||||
(currentSnackbar) => {
|
||||
const safeSnackbar = currentSnackbar || initialState;
|
||||
return { ...safeSnackbar, maxStack };
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
export function handlerDense(dense) {
|
||||
// to update local state based on key
|
||||
mutate(
|
||||
endpoints.key,
|
||||
(currentSnackbar) => {
|
||||
const safeSnackbar = currentSnackbar || initialState;
|
||||
return { ...safeSnackbar, dense };
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
export function handlerIconVariants(iconVariant) {
|
||||
// to update local state based on key
|
||||
mutate(
|
||||
endpoints.key,
|
||||
(currentSnackbar) => {
|
||||
const safeSnackbar = currentSnackbar || initialState;
|
||||
return { ...safeSnackbar, iconVariant, hideIconVariant: iconVariant === 'hide' };
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user