-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding the ability to run loadData after setFilter
- Loading branch information
Alex Harding
committed
Sep 13, 2024
1 parent
805f3b3
commit 93a17ac
Showing
4 changed files
with
65 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,84 @@ | ||
import { capitalize } from "./utils.js"; | ||
|
||
export default function subscribeActions(store, pageDefinition) { | ||
store.$onAction(({ name, store, args, after }) => { | ||
store.$onAction(async ({ name, store, args, after }) => { | ||
//before action is called | ||
runSubscriptions(name, args, store, pageDefinition, "before"); | ||
after(() => { | ||
runSubscriptions(name, args, store, pageDefinition, "after"); | ||
after(async () => { | ||
await runSubscriptions(name, args, store, pageDefinition, "after"); | ||
await checkForLoadData(name, store); | ||
}); | ||
|
||
await runSubscriptions(name, args, store, pageDefinition, "before"); | ||
}); | ||
} | ||
|
||
function runSubscriptions(name, args, store, pageDefinition, hook) { | ||
async function runSubscriptions(name, args, store, pageDefinition, hook) { | ||
// for loadData actions, both toggle the dataLoading state variable | ||
// and also check for before/after loadData hooks | ||
if (name === "loadData") { | ||
store.toggleDataLoading(); | ||
if (pageDefinition[`${hook}LoadData`]) { | ||
pageDefinition[`${hook}LoadData`]({ name, args }, store); | ||
await pageDefinition[`${hook}LoadData`]({ name, args }, store); | ||
} | ||
} else if (name.includes("Filter") && name !== "setFilter") { | ||
// iterate over filters to find if any match the action name | ||
// and run their before/after hooks | ||
Object.keys(store.getFilters).forEach((filterKey) => { | ||
const filterActionString = `set${capitalize(filterKey)}Filter`; | ||
if ( | ||
name === filterActionString && | ||
store.getFilters[filterKey][`${hook}Set`] | ||
) { | ||
store.getFilters[filterKey][`${hook}Set`]({ name, args }, store); | ||
} | ||
}); | ||
await Promise.all( | ||
Object.keys(store.getFilters).map(async (filterKey) => { | ||
const filterActionString = `set${capitalize(filterKey)}Filter`; | ||
if ( | ||
name === filterActionString && | ||
store.getFilters[filterKey][`${hook}Set`] | ||
) { | ||
await store.getFilters[filterKey][`${hook}Set`]( | ||
{ name, args }, | ||
store, | ||
); | ||
} | ||
}), | ||
); | ||
} else if (name.includes("ChartData") && name !== "setChartData") { | ||
// iterate over hcarts to find if any match the action name | ||
// iterate over charts to find if any match the action name | ||
// and run their before/after hooks | ||
Object.keys(store.getCharts).forEach((chartKey) => { | ||
const chartActionString = `set${capitalize(chartKey)}ChartData`; | ||
if ( | ||
name === chartActionString && | ||
store.getCharts[chartKey][`${hook}Set`] | ||
) { | ||
store.getCharts[chartKey][`${hook}Set`]({ name, args }, store); | ||
} | ||
}); | ||
await Promise.all( | ||
Object.keys(store.getCharts).map(async (chartKey) => { | ||
const chartActionString = `set${capitalize(chartKey)}ChartData`; | ||
if ( | ||
name === chartActionString && | ||
store.getCharts[chartKey][`${hook}Set`] | ||
) { | ||
await store.getCharts[chartKey][`${hook}Set`]({ name, args }, store); | ||
} | ||
}), | ||
); | ||
} | ||
|
||
if (pageDefinition.extendSubscriptions) { | ||
pageDefinition.extendSubscriptions(name, args, store, pageDefinition, hook); | ||
} | ||
return Promise.resolve(); | ||
} | ||
|
||
// checks to see if the action is a filter action and if so, checks if the filter is synchronous | ||
// if not, it will trigger a loadData action | ||
async function checkForLoadData(name, store) { | ||
if (name.includes("Filter") && name !== "setFilter") { | ||
// get filter name from action | ||
const filterName = name.slice(3, -6); | ||
// find the matching filter key and retrieve definition | ||
const filterKey = store.getFilterKeys.find( | ||
(filter) => capitalize(filter) === filterName, | ||
); | ||
const filterObj = store.getFilterDefinition(filterKey); | ||
|
||
if ( | ||
store[`${filterKey}TriggerLoadData`] && | ||
!( | ||
Object.hasOwn(filterObj, "triggerLoadData") && | ||
filterObj.triggerLoadData == false | ||
) | ||
) { | ||
await store.loadData(); | ||
} | ||
} | ||
} |