Skip to content

Instantly share code, notes, and snippets.

@tdreyno
Created November 6, 2019 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdreyno/98d487a92926da4b88ac9d577e03bca7 to your computer and use it in GitHub Desktop.
Save tdreyno/98d487a92926da4b88ac9d577e03bca7 to your computer and use it in GitHub Desktop.
export function loadStories(userId: string) {
return Task.map2(
myStories => storiesSharedWithMe =>
({
myStories,
storiesSharedWithMe,
}),
userStories(userId).map(listToLookup),
userSharedStories(userId).map(listToLookup),
);
}
function userStories(uid: string) {
return db.storiesCollection()
.where("owner", "==", uid)
.andThen(({ docs }) => docs.map(validateStory).andThen(Task.all));
}
function usersThatShareWithMe(uid: string) {
return db.usersCollection()
.where("editors", "array-contains", uid)
.map(({ docs }) => docs);
}
function userSharedStories(uid: string) {
return usersThatShareWithMe(uid).andThen(users =>
users
.map(({ id }) => userStories(id))
.andThen(Task.all)
.map(flatten),
);
}
function listToLookup<T extends { id: string }>(
items: T[],
): { [key: string]: T } {
return items.reduce(
(sum, story) => {
sum[story.id] = story;
return sum;
},
{} as {
[key: string]: T;
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment