3
\$\begingroup\$

currentUserPosts will have a post object with an id that either matches a tempId, an Id, or nothing.

If there's a tempId match there won't be an id match and vice versa.

This code works but there's something about it I don't like. How can it be refactored?

let postIndex;
postIndex = currentUserPosts.findIndex(post => post.id === upsertParams.tempId);
if (postIndex === -1) {
 postIndex = currentUserPosts.findIndex(post => post.id === upsertParams.id);
}
if (postIndex === -1) {
 currentUserPosts.push(upsertParams);
} else {
 currentUserPosts[postIndex] = upsertParams;
}

Complete function:

const upsertIntoApolloCache = (upsertParams) => {
 try {
 const data = client.readQuery({
 query: USER_POSTS_QUERY,
 });
 const currentUserPosts = data.currentUser.posts;
 const postIndex = currentUserPosts.findIndex(
 post => post.id === upsertParams.tempId || post.id === upsertParams.id
 );
 if (postIndex === -1) {
 currentUserPosts.push(upsertParams);
 } else {
 currentUserPosts[postIndex] = upsertParams;
 }
 client.writeQuery({
 query: USER_POSTS_QUERY,
 data,
 });
 } catch (error) {
 console.log('!!ERROR in upsertIntoApolloCache!!', error);
 }
};
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jun 24, 2018 at 0:54
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

You can group the two conditions for finding into a single || condition, since the post.id can match only one of tempId or id.

let postIndex = currentUserPosts.findIndex(post => {
 return post.id === upsertParams.id || post.id === upsertParams.tempId
});
if (postIndex === -1) {
 currentUserPosts.push(upsertParams);
} else {
 currentUserPosts[postIndex] = upsertParams;
}

You could, if possible, also change the currentUserPosts from array to a dictionary mapping your post.id to the post itself. This entirely depends on your further usage of the currentUserPosts across application.


Based on updated question content:

let currentUserPosts = {}
for (let post of data.currentUser.posts) {
 currentUserPosts[post.id] = post
}

This way, you'd only have to check against id using the in operation:

let newId = upsertParams.id || upsertParams.tempId
if (newId in currentUserPosts)
 // do unspeakable things
answered Jun 24, 2018 at 9:52
\$\endgroup\$
2
  • \$\begingroup\$ Looks to me like you are missing a return in your findIndex method. \$\endgroup\$ Commented Jun 24, 2018 at 16:02
  • \$\begingroup\$ Ha!! That was an easy one. Thanks @hjpotter92! Regarding your second suggestion. Don't completely understand what you're getting at. I'll update my questions with the full function so you can see. It probably doesn't matter because I don't have control of the structure. It's a cache object provided by Apollo. \$\endgroup\$ Commented Jun 24, 2018 at 16:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.