@ruttwice/create-api-config-test
v1.4.4
Published
### Reducer
Downloads
146
Readme
Usage
Reducer
import { createPostsApi } from "@ruttwice/create-api-config-test";
const postsApi = createPostsApi({
getBackend: {
type: "query",
tagType: "config",
timeCache: 60,
},
sendBackend: {
type: "mutation",
tagType: "config",
},
getS3: {
type: "S3",
},
});
//how to renameFunction
//use + "GetBackend" uptocase G + Query
//result = useGetBackendQuery
export const {
useGetBackendQuery,
useSendBackendMutation,
useTestAQuery,
} = postsApi;
inject
//add param obj query: ["getBackend", "testA", "testB"],
injectReducer("tableConfigReducer", tableConfigReducer.reducer, {
getBackend: {
type: "query",
tagType: "config",
timeCache: 60,
},
sendBackend: {
type: "mutation",
tagType: "config",
},
getS3: {
type: "S3",
},
});
MainComponent
getBackend
import { useGetBackendQuery } from "reducer";
const baseUrl = "aws link";
//action link aws
useGetBackendQuery({
url: baseUrl + "/Get",
method: "POST",
data1: "User", //key
data2: "User", //value
});
sendBackend
import { useSendBackendMutation } from "reducer";
const baseUrl = "aws link";
//action submit sendBackend
const [sendbackend] = useSendBackendMutation();
const submit = async () => {
try {
await sendbackend({
url: baseUrl + "/Update",
method: "PUT",
data1: "User", //key
data2: "User", //value
updateValue: obj,//obj or param backend
}).unwrap(); // send backend
alert("sendbackend");
} catch (error) {
console.error("error: ", error);
}
};
jsx
<button onClick={testSubmit}>submit</button>
S3
import { useGetS3Query } from "reducer";
const baseUrl = "S3 link";
useGetS3Query({
url: baseUrl,
});
store
import { createPostsApi } from "@ruttwice/create-api-config-test";
export const store = configureStore({
reducer: {
mainStore: storeReducer,// lib
},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(createPostsApi().middleware),
});
export const injectReducer = (key, reducer, endpoint) => {
const postsApi = createPostsApi(endpoint);
if (!store.asyncReducers) {
store.asyncReducers = {
[postsApi.reducerPath]: postsApi.reducer,
};
}
store.asyncReducers[key] = reducer;
store.replaceReducer(combineReducers(store.asyncReducers));
};
storeReducer
import { createSlice } from "@reduxjs/toolkit";
export const baseStoreReducer = createSlice({
name: "storeReducer",
initialState: {},
reducers: {},
});
export const storeReducer = baseStoreReducer.reducer;