React Boilerplate Redux Saga Hoc

Search Results

No results for 'N/A'
1. Getting Started
2. Basic Concepts
3. Handlers
3.0.1 Introduction3.0.2 Data Handler3.0.3 Infinite Handler3.0.4 Update Handler# Update Handler without filter# Update Handler handling array of objects# Update Handler with filter# Update Handler with Multi-filter# Update Handler with Subkey# Update Handler Function - Api3.0.5 Update Key Handler3.0.6 Delete Handler3.0.7 Delete Key Handler3.0.8 Splice Handler3.0.9 Custom Handler3.1.0 Toggle Key Handler3.1.1 Dont Update Data Handler3.1.2 callback Handler3.1.3 Dont Update Handler
4. Advanced Concepts
5. Examples6. Advanced Examples7. ParamsApi ReferenceComplete DocumentationPrevious DocumentationReadme

Update Handler

# Update Handler without filter

import { HOC, commonConstants, store } from "react-boilerplate-redux-saga-hoc";
const { ON_SUCCESS } = commonConstants;
const { DEMO_API_CONFIGURATION_CUSTOM_TASK } = props;
DEMO_API_CONFIGURATION_CUSTOM_TASK(ON_SUCCESS, {
task: {
name: "Update-Handler",
},
});

# Update Handler handling array of objects

import { HOC, commonConstants, store } from "react-boilerplate-redux-saga-hoc";
const { ON_SUCCESS } = commonConstants;
const { DEMO_API_CONFIGURATION_CUSTOM_TASK } = props;
DEMO_API_CONFIGURATION_CUSTOM_TASK(ON_SUCCESS, {
task: {
name: "Update-Handler",
id: [1, 2],
key: "id",
},
});

# Update Handler with filter

import { HOC, commonConstants, store } from "react-boilerplate-redux-saga-hoc";
const { ON_SUCCESS } = commonConstants;
const { DEMO_API_CONFIGURATION_CUSTOM_TASK } = props;
DEMO_API_CONFIGURATION_CUSTOM_TASK(ON_SUCCESS, {
task: {
name: "Update-Handler",
id: [1, 2],
key: "id",
},
filter: ["name"],
});

# Update Handler with Multi-filter

import { HOC, commonConstants, store } from "react-boilerplate-redux-saga-hoc";
const { ON_SUCCESS } = commonConstants;
const { DEMO_API_CONFIGURATION_CUSTOM_TASK } = props;
DEMO_API_CONFIGURATION_CUSTOM_TASK(ON_SUCCESS, {
task: {
name: "Update-Handler",
id: [1, 2],
key: "id",
},
filter: [["filter-1"], ["filter-2"], ["filter-2"]],
});

# Update Handler with Subkey

Note:
- sub key for handling sub level of data in an object
import { HOC, commonConstants, store } from "react-boilerplate-redux-saga-hoc";
const { ON_SUCCESS } = commonConstants;
const { DEMO_API_CONFIGURATION_CUSTOM_TASK } = props;
DEMO_API_CONFIGURATION_CUSTOM_TASK(ON_SUCCESS, {
task: {
name: "Update-Handler",
subKey: ["data"],
id: [1, 2],
key: "id",
},
filter: [["filter-1"], ["filter-2"], ["filter-2"]],
});

# Update Handler Function - Api

Important:
- The code below are the built in handler function.
- Don't copy and paste this handler, it is already available with this hoc.
- You will learn how to create your own custom handler in below.
- If you want to customize this handler you can do it.
import {
generateTimeStamp,
updateIn,
getIn,
Safe,
} from "react-boilerplate-redux-saga-hoc";
const updateData = (data, successData, updateCallback) => {
if (updateCallback) return updateCallback(data, successData) || data;
if (
typeof successData === "object" &&
!Array.isArray(successData) &&
typeof data === "object" &&
!Array.isArray(data)
)
return newObject(data, successData);
return successData;
};
export const updateHandler = ({
task: { key, id, subKey = [], values = {} } = {},
callback: { updateCallback } = {},
successData = {},
}) => ({ data = [] } = {}) => ({
data:
subKey.length > 0
? updateIn(
{
...data,
...successData,
[subKey[0]]: data[subKey[0]],
},
subKey,
(_Data) =>
(() => {
let index = -1;
const _values = Array.isArray(values);
/** update data if old data is object */
if (!Array.isArray(_Data))
return updateData(
_Data,
Safe(successData, `.${subKey.join(".")}`),
updateCallback
);
else if (Array.isArray(id) && key && Array.isArray(_Data))
return _Data.reduce(
(acc, curr = {}) =>
id.includes(curr[key])
? (() => {
index = index + 1;
return acc.concat([
updateData(
curr,
values[_values ? index : curr[key]] || curr,
updateCallback
),
]);
})()
: acc.concat([curr]),
[]
);
else if ((id === 0 || id) && key)
return _Data.map((_data) =>
_data[key] === id
? (() => {
index = index + 1;
return updateData(
_data,
values[_values ? index : curr[key]] || _data,
updateCallback
);
})()
: _data
);
return updateData(
_Data,
Safe(successData, `.${subKey.join(".")}`),
updateCallback
);
})()
)
: (() => {
let index = -1;
const _values = Array.isArray(values);
if (!Array.isArray(data))
return updateData(data, successData, updateCallback);
else if (Array.isArray(id) && key)
return data.reduce(
(acc, curr = {}) =>
id.includes(curr[key])
? (() => {
index = index + 1;
return acc.concat([
updateData(
curr,
values[_values ? index : curr[key]] || curr,
updateCallback
),
]);
})()
: acc.concat([curr]),
[]
);
else if ((id === 0 || id) && key)
return data.map((_data) =>
_data[key] === id
? (() => {
index = index + 1;
return updateData(
_data,
values[_values ? index : _data[key]] || _data,
updateCallback
);
})()
: _data
);
return updateData(data, successData, updateCallback);
})(),
lastUpdated: generateTimeStamp(),
isError: false,
});