redux-astroglide 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,255 @@
1
+ # Redux-Astroglide
2
+
3
+ #### Squeezing a huge package into a tiny space
4
+  
5
+
6
+ ## Installation
7
+
8
+ Follow the installation instructions for your app from [@reduxjs/toolkit](https://github.com/reduxjs/redux-toolkit#installation)
9
+
10
+ Now install Astroglide:
11
+
12
+ ```bash
13
+ # NPM
14
+ npm install redux-astroglide
15
+
16
+ # Yarn
17
+ yarn add redux-astroglide
18
+
19
+ # PNPM
20
+ pnpm add redux-astroglide
21
+
22
+ ```
23
+  
24
+
25
+
26
+ ## Setup
27
+
28
+ Astroglide will create your store for you using a call to RTK's configureStore function. This step allows Astroglide to manage all aspects of reducers including code bundle injection without intervention.
29
+
30
+ ```jsx
31
+ // app/store.js
32
+ import configure from "redux-astroglide";
33
+
34
+ const { store, createSlice } = configure({
35
+ // ... (configureStore options
36
+ });
37
+
38
+ ```
39
+  
40
+
41
+ [ Learn more about RTK's configureStore function ](https://redux-toolkit.js.org/usage/usage-with-typescript#configurestore)
42
+
43
+
44
+ Now just create a slice anywhere in your application and in addition to the actions created by Redux Toolkit you'll get some memoized selectors and hooks from Astroglide:
45
+
46
+ ```jsx
47
+ import { createSlice } from "../../app/store";
48
+
49
+ const slice = createSlice(
50
+ "LoginForm", // reducer namespace
51
+ { // initial state
52
+ username: "",
53
+ password: "",
54
+ }
55
+ );
56
+
57
+ export const { setPassword, setUsername } = slice.actions;
58
+ export const { selectUsername, selectPassword } = slice.selectors;
59
+ export const { useUsername, usePassword } = slice.hooks;
60
+ ```
61
+  
62
+
63
+
64
+ You can also create the slice using [the same API specified by RTK](https://redux-toolkit.js.org/usage/usage-with-typescript#createslice).
65
+
66
+ ```jsx
67
+ const slice = createSlice({
68
+ name: "Login",
69
+ initialState: {
70
+ username: "",
71
+ password: "",
72
+ },
73
+ reducers: {
74
+ // custom reducers are the most likely reason to use this syntax
75
+ },
76
+ // other RTK functionality can go here
77
+ })
78
+ ```
79
+  
80
+
81
+
82
+ ## Usage
83
+
84
+ These hooks can be used in a React component with the same API as React's setState:
85
+
86
+ ```jsx
87
+ export const UsernameField = (props) => {
88
+ const [username, setUsername] = useUsername();
89
+
90
+ return <input
91
+ name="username"
92
+ type="text"
93
+ value={username}
94
+ onChange={e=> setUsername(e.target.value)} // this triggers a redux action
95
+ />
96
+ }
97
+ ```
98
+ &nbsp;
99
+
100
+ Astroglide's createSlice exposes global domain selectors and setters, if you need something like that:
101
+
102
+ ```jsx
103
+ const slice = createSlice(
104
+ // ...
105
+ );
106
+
107
+ export { useSlice } = slice.hooks;
108
+ export { selectSlice } = slice.selectors;
109
+ export { setSlice } = slice.actions; // will not conflict with existing `slice` prop actions
110
+ ```
111
+ &nbsp;
112
+
113
+
114
+ The setter actions can be passed a function to receive a copy of the latest state value, just like with React's setState:
115
+
116
+ ```jsx
117
+ <input
118
+ //...
119
+ onChange={e => setUsername(currentUsername =>
120
+ isValid(e.target.value) ? e.target.value : currentUsername
121
+ )}
122
+ />
123
+ ```
124
+ &nbsp;
125
+
126
+
127
+ The hooks can also be used outside of a React component (like in a thunk or saga) by destructuring the `select` and `update` props. This allows your reducer file to export as few variables as possible:
128
+
129
+ ```jsx
130
+ // thunk.js
131
+ import { createAsyncThunk } from "@reduxjs/toolkit";
132
+ import { useUsername, usePassword } from "./slice";
133
+
134
+ const loginThunk = createAsyncThunk("login", async (
135
+ args,
136
+ { dispatch, getState }
137
+ )=> {
138
+ const username = useUsername.select(getState());
139
+ const password = usePassword.select(getState());
140
+ // logic ...
141
+ dispatch(useUsername.update("newUsername"));
142
+ // ...
143
+
144
+ // OR
145
+
146
+ const username = selectUsername(getState)
147
+ const password = selectPassword(getState)
148
+ // logic ...
149
+ dispatch(setUsername("newUsername"))
150
+ // ...
151
+
152
+ }
153
+ )
154
+ ```
155
+ &nbsp;
156
+
157
+ Astroglide also provides some of its internal helper functions you may find useful:
158
+
159
+ ```jsx
160
+ const configure = "redux-astroglide";
161
+
162
+ export const {
163
+ store,
164
+ createSlice,
165
+ injectReducer,
166
+ injectSlice,
167
+ injectMiddleware
168
+ } = configure();
169
+ ```
170
+ &nbsp;
171
+
172
+ ## Plugins
173
+
174
+ Astroglide ships with a handful of plugins you can use for things like typechecking and data persistence:
175
+
176
+ ```jsx
177
+ // Login/slice.js
178
+ const slice = createSlice("Login", {
179
+ username: type(PropType.string, ""),
180
+ password: type(PropTypes.string, "", { shouldPreventUpdate: true })
181
+ });
182
+
183
+ // Nav/slice.js
184
+ const slice = createSlice("Nav", {
185
+ isOpen: type(PropTypes.bool, persist("", {
186
+ storageType: localStorage
187
+ })),
188
+ clickCount: type(PropTypes.number, set((value) => value + 1)),
189
+ });
190
+ ```
191
+ &nbsp;
192
+
193
+ These plugins can be loaded by adding this to your Astroglide configuration:
194
+
195
+
196
+ ```jsx
197
+ import configure, { addPlugins } from "redux-astroglide";
198
+
199
+ import setPlugin from "redux-astroglide/plugins/set";
200
+ import typePlugin from "redux-astroglide/plugins/type";
201
+ import persistPlugin from "redux-astroglide/plugins/persist";
202
+ // these can also be imported collectively like:
203
+ // import { set, type, persist } = "redux-astroglide/plugins";
204
+
205
+ export const [set, type, persist] = addPlugins(
206
+ setPlugin(),
207
+ typePlugin({ shouldPreventUpdate: false }),
208
+ persistPlugin()
209
+ );
210
+
211
+ export const { store, createSlice } = configure();
212
+ ```
213
+ &nbsp;
214
+
215
+
216
+ ## Custom Plugins
217
+
218
+ If you the find the need to write your own custom plugins you can do so via the following API:
219
+
220
+ ```jsx
221
+ import { addPlugin, addPlugins } from "redux-astroglide";
222
+ addPlugin({
223
+ constructor(constructorArgs) {
224
+ // here you can modify the running instance
225
+ },
226
+ setup(pluginInstance, { key, sliceConfig, }) {
227
+ // here you can return an object with variable to access later
228
+ return {};
229
+ },
230
+ getInitialValue(value, { key, plugin }) {
231
+ return value
232
+ }
233
+ update(value, { draft, key, plugin }) {
234
+ return value;
235
+ },
236
+ })
237
+ // or
238
+ addPlugins({
239
+ // plugin 1
240
+ }, {
241
+ // plugin 2
242
+ })
243
+
244
+ ```
245
+ &nbsp;
246
+
247
+ ## Contributing
248
+
249
+ Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
250
+
251
+ &nbsp;
252
+ ## License
253
+
254
+ [MIT](https://choosealicense.com/licenses/mit/)
255
+