Created
November 6, 2019 21:25
-
-
Save tdreyno/98d487a92926da4b88ac9d577e03bca7 to your computer and use it in GitHub Desktop.
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
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