Skip to content

Instantly share code, notes, and snippets.

@baranovxyz
Last active October 4, 2017 11:43
Show Gist options
  • Save baranovxyz/10d3aae7891e6f743e73bf313cc40363 to your computer and use it in GitHub Desktop.
Save baranovxyz/10d3aae7891e6f743e73bf313cc40363 to your computer and use it in GitHub Desktop.
/**
* To avoid null state
*/
export const NullableComponent = (
component: Component,
vnodeForNull: VNode | null = null,
componentSinks: Array<string> = ["DOM", "onion"]
) => (sources: Sources): Sinks => {
const t$ = sources.onion.state$.map(
state =>
state === null
? {
DOM: xs.of(
vnodeForNull !== null ? vnodeForNull : <div>No data</div>
)
}
: component(sources)
);
return componentSinks
.map(sinkName => [
[sinkName],
t$.map(s => R.propOr(xs.of(), sinkName, s)).flatten()
])
.reduce((acc, val) => ({ ...acc, [val[0]]: val[1] }), {});
};
// lens
export const currentProjectLens = {
get: state => state.projects.find(p => p._id === state.app.projCurId) || null,
set: (state, childState) => ({
...state,
projects: state.projects.map(
(item: any) => (item._id === childState._id ? childState : item)
)
})
};
// connecting component
const projectContainer = isolate(
NullableComponent(ProjectContainer, null, ["DOM", "onion", "feathers"]),
{ onion: currentProjectLens }
)(sources);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment