add privileges store

This commit is contained in:
nflnkr 2023-08-08 15:59:54 +03:00
parent cadc903159
commit 27d853ac8e
4 changed files with 38 additions and 6 deletions

@ -14,7 +14,7 @@
"dependencies": {
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"@frontend/kitui": "^1.0.17",
"@frontend/kitui": "^1.0.19",
"@mui/icons-material": "^5.10.14",
"@mui/material": "^5.10.14",
"@popperjs/core": "^2.11.8",

@ -24,13 +24,14 @@ import Layout from "./components/Layout";
import { clearUserData, setUser, setUserAccount, useUserStore } from "./stores/user";
import TariffConstructor from "./pages/TariffConstructor/TariffConstructor";
import { useUser } from "./utils/hooks/useUser";
import { clearAuthToken, getMessageFromFetchError } from "@frontend/kitui";
import { clearAuthToken, getMessageFromFetchError, usePrivilegeFetcher } from "@frontend/kitui";
import { useUserAccount } from "./utils/hooks/useUserAccount";
import { setCustomTariffs } from "@root/stores/customTariffs";
import { useCustomTariffs } from "@root/utils/hooks/useCustomTariffs";
import { useDiscounts } from "./utils/hooks/useDiscounts";
import { setDiscounts } from "./stores/discounts";
import { pdfjs } from "react-pdf";
import { setPrivileges } from "./stores/privileges";
pdfjs.GlobalWorkerOptions.workerSrc = new URL("pdfjs-dist/build/pdf.worker.min.js", import.meta.url).toString();
@ -85,6 +86,13 @@ const App = () => {
}
});
usePrivilegeFetcher({
onSuccess: setPrivileges,
onError: error => {
console.log("usePrivilegeFetcher error :>> ", error);
}
});
if (location.state?.redirectTo) return <Navigate to={location.state.redirectTo} replace state={{ backgroundLocation: location }} />;
return (

24
src/stores/privileges.ts Normal file

@ -0,0 +1,24 @@
import { PrivilegeWithAmount } from "@frontend/kitui";
import { create } from "zustand";
import { devtools } from "zustand/middleware";
interface PrivilegeStore {
privileges: PrivilegeWithAmount[];
}
const initialState: PrivilegeStore = {
privileges: [],
};
const usePrivilegeStore = create<PrivilegeStore>()(
devtools(
(get, set) => initialState,
{
name: "Privileges",
enabled: process.env.NODE_ENV === "development",
}
)
);
export const setPrivileges = (privileges: PrivilegeStore["privileges"]) => usePrivilegeStore.setState({ privileges });

@ -1532,10 +1532,10 @@
minimatch "^3.1.2"
strip-json-comments "^3.1.1"
"@frontend/kitui@^1.0.17":
version "1.0.17"
resolved "https://penahub.gitlab.yandexcloud.net/api/v4/projects/21/packages/npm/@frontend/kitui/-/@frontend/kitui-1.0.17.tgz#a5bddaaa18b168be0e1814d5cfbd86e4030d15af"
integrity sha1-pb3aqhixaL4OGBTVz72G5AMNFa8=
"@frontend/kitui@^1.0.19":
version "1.0.19"
resolved "https://penahub.gitlab.yandexcloud.net/api/v4/projects/21/packages/npm/@frontend/kitui/-/@frontend/kitui-1.0.19.tgz#0a1c3e68e1fa595188b8a6a327b1ea709cf6624c"
integrity sha1-Chw+aOH6WVGIuKajJ7HqcJz2Ykw=
dependencies:
immer "^10.0.2"
reconnecting-eventsource "^1.6.2"