I'm a functional programming beginner. I'm working on a React Native app using Ramda. The app lets users maintain their houses.
I have written function called asyncPipe
which lets me pipe promises and normal functions. I use it for the loginFlow
which currently has a http request (getHouseList
) as its last function.
const asyncPipe = (...fns) => x => fns.reduce(async (y, f) => f(await y), x);
const loginFlow = asyncPipe(
// ... someFunctions
getHouseList
);
// used later like this in LoginForm.js's handleSubmit():
const list = await loginFlow(credentials);
So, after logging in, the app loads the user's houses. Now depending on whether he has only one or multiple houses I would like to send the user either to list view to choose a house or a detail view if he only has one house. Additionally, I would like to dispatch a Redux action to save the list in my reducer and another action to pick the house if there is only one.
Currently I do it like this:
const list = await loginFlow(credentials);
dispatch(addHouses(list));
if (list.length > 1) {
navigate('ListScreen')
} else {
dispatch(pickHouse(list[0]);
navigate('DetailScreen') ;
}
But as you can see that is super imperative. It seems like I have to 'fork' the list and use it twice in the pipe (because Redux' dispatch
does not have a return value).
My main question is:
How to do this more functional / declaratively (if there is a way)?
A little sub question I have would be, whether its okay to be imperative here / if doing it functional is a good idea.