react-redactor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+
2
+ # Redactor
3
+
4
+ Redactor is a lightweight library for conditional rendering, context management, and navigation in React applications.
5
+
6
+ ## Features
7
+ - Simple badge-based access control using React Context.
8
+ - Conditional rendering with the `Protect` component.
9
+ - Easy navigation with the `useProtection` hook.
10
+
11
+ ## Installation
12
+
13
+ Install the module via npm:
14
+
15
+ ```
16
+ npm install react-redactor
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### 1. Badge Context (`useBadge` and `BadgeProvider`)
22
+
23
+ Wrap your app or part of it with the `BadgeProvider` to set a badge value.
24
+
25
+ ```jsx
26
+ import React from "react";
27
+ import { useBadge, BadgeProvider } from "your-react-badge-module";
28
+
29
+ function App() {
30
+ return (
31
+ <BadgeProvider value={true}>
32
+ <ChildComponent />
33
+ </BadgeProvider>
34
+ );
35
+ }
36
+
37
+ function ChildComponent() {
38
+ const badge = useBadge();
39
+
40
+ return <h1>Badge Value: {badge ? "Authorized" : "Unauthorized"}</h1>;
41
+ }
42
+ ```
43
+
44
+ ### 2. Conditional Rendering with `Protect`
45
+
46
+ Use the `Protect` component to conditionally render components based on the badge value.
47
+
48
+ ```jsx
49
+ import React from "react";
50
+ import { Protect } from "your-react-badge-module";
51
+
52
+ const ProceedComponent = () => <h1>Welcome!</h1>;
53
+ const ReturnComponent = () => <h1>Access Denied!</h1>;
54
+
55
+ function App() {
56
+ const badgeValue = true; // Example badge value
57
+
58
+ return (
59
+ <Protect
60
+ Proceed={ProceedComponent}
61
+ Return={ReturnComponent}
62
+ badgeValue={badgeValue}
63
+ />
64
+ );
65
+ }
66
+ ```
67
+
68
+ ### 3. Navigation with `useProtection`
69
+
70
+ Redirect users based on a validation function.
71
+
72
+ ```jsx
73
+ import React from "react";
74
+ import { useProtection } from "your-react-badge-module";
75
+
76
+ function App() {
77
+ const validationFunc = () => true; // Replace with your logic
78
+
79
+ useProtection(validationFunc, "/dashboard", "/login");
80
+
81
+ return <h1>Redirecting...</h1>;
82
+ }
83
+ ```
84
+
85
+ ## API Reference
86
+
87
+ ### `useBadge`
88
+ - **Description**: Access the current badge value from the context.
89
+ - **Returns**: The current badge value.
90
+
91
+ ### `BadgeProvider`
92
+ - **Props**:
93
+ - `value` (any): The value to set in the badge context.
94
+ - `children` (ReactNode): The components to render within the provider.
95
+
96
+ ### `Protect`
97
+ - **Props**:
98
+ - `Proceed` (React.ComponentType): The component to render if `badgeValue` is `true`.
99
+ - `Return` (React.ComponentType): The component to render if `badgeValue` is `false`.
100
+ - `badgeValue` (boolean): Determines which component to render.
101
+
102
+ ### `useProtection`
103
+ - **Parameters**:
104
+ - `validationFunc` (Function): A function that returns `true` or `false`.
105
+ - `goto_url` (string): The URL to navigate to if validation passes.
106
+ - `returnto_url` (string): The URL to navigate to if validation fails.
107
+ - **Effect**: Redirects the user based on `validationFunc`.
108
+
109
+ ## License
110
+
111
+ This library is licensed under the MIT License. See the LICENSE file for details.
112
+
113
+ ## Contributing
114
+
115
+ Feel free to open issues or pull requests on the [GitHub repository](https://github.com/websitedeb/Redactor).
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { useBadge, BadgeProvider } from "./src/Badge";
2
+ export { Protect } from "./src/Protect";
3
+ export { useProtection } from "./src/useProtection";
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "react-redactor",
3
+ "version": "1.0.0",
4
+ "description": "Redactor is a lightweight library for conditional rendering, context management, and navigation in React applications.",
5
+ "main": "index.jsx",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [
10
+ "redactor",
11
+ "router",
12
+ "route-protector"
13
+ ],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "dependencies": {
17
+ "react": "^18.3.1",
18
+ "react-router-dom": "^7.0.1"
19
+ }
20
+ }
package/src/Badge.js ADDED
@@ -0,0 +1,24 @@
1
+ import React, { createContext, useContext } from "react";
2
+
3
+ // Badge Context
4
+ const BadgeContext = createContext(null);
5
+
6
+ /**
7
+ * `Badge` Hook
8
+ * Allows consumers to access the BadgeContext value.
9
+ * @returns {*} Context value
10
+ */
11
+ export function useBadge() {
12
+ return useContext(BadgeContext);
13
+ }
14
+
15
+ /**
16
+ * `Provider` Component
17
+ * Wraps children with BadgeContext.Provider
18
+ * @param {Object} props
19
+ * @param {*} props.value - The value to pass to the context.
20
+ * @param {React.ReactNode} props.children - Components to render within the provider.
21
+ */
22
+ export function BadgeProvider({ value, children }) {
23
+ return <BadgeContext.Provider value={value}>{children}</BadgeContext.Provider>;
24
+ }
package/src/Protect.js ADDED
@@ -0,0 +1,13 @@
1
+ import React from "react";
2
+
3
+ /**
4
+ * `Protect` Component
5
+ * Conditionally renders one of two components based on a badge value.
6
+ * @param {Object} props
7
+ * @param {React.ComponentType} props.Proceed - Component to render if badgeValue is true.
8
+ * @param {React.ComponentType} props.Return - Component to render if badgeValue is false.
9
+ * @param {boolean} props.badgeValue - Determines which component to render.
10
+ */
11
+ export function Protect({ Proceed, Return, badgeValue }) {
12
+ return badgeValue ? <Proceed /> : <Return />;
13
+ }
@@ -0,0 +1,22 @@
1
+ import { useEffect } from "react";
2
+ import { useNavigate } from "react-router-dom";
3
+
4
+ /**
5
+ * ;)
6
+ * `useProtection` Hook
7
+ * Redirects based on a validation function's result.
8
+ * @param {Function} validationFunc - A function that returns a boolean.
9
+ * @param {string} goto_url - URL to navigate to if the validation passes.
10
+ * @param {string} returnto_url - URL to navigate to if the validation fails.
11
+ */
12
+ export function useProtection(validationFunc, goto_url, returnto_url) {
13
+ const navigate = useNavigate();
14
+
15
+ useEffect(() => {
16
+ if (validationFunc()) {
17
+ navigate(goto_url);
18
+ } else {
19
+ navigate(returnto_url);
20
+ }
21
+ }, [validationFunc, goto_url, returnto_url, navigate]);
22
+ }