re-added ids to images to handle edge case

This commit is contained in:
erik-sn committed 2020-09-25 03:30:52 -04:00
1 parent 0789f69e2f
commit a01993e178
5 files changed
+56 -28

No files matched your search

@@ -1,11 +1,28 @@
import { useNuiEvent } from "../../../os/nui-events/hooks/useNuiEvent";
import { useSetRecoilState } from "recoil";
import { v4 as uuidv4 } from "uuid";
import { useNuiEvent } from "../../../os/nui-events/hooks/useNuiEvent";
import { IMAGE_DELIMITER } from "../utils/images";
import types from "../types";
import { twitterState } from "./state";
import { useTweets } from "./useTweets";
/**
* Perform all necessary processing/transforms from the raw database
* data to what the frontend expects
* @param {object} tweet - full JSON returned from the server/database
*/
function processTweet(tweet) {
// we store images in the database as a varchar field with
// comma separated links to images, so here we split them
// back into their distinct members
console.log(tweet);
const imageLinks = tweet.images ? tweet.images.split(IMAGE_DELIMITER) : [];
console.log(imageLinks);
const images = imageLinks.map((link) => ({ id: uuidv4(), link }));
return { ...tweet, images };
}
export const useTwitterService = () => {
const setTweets = useSetRecoilState(twitterState.tweets);
const setCreateLoading = useSetRecoilState(twitterState.createTweetLoading);
@@ -17,16 +34,7 @@ export const useTwitterService = () => {
};
const _setTweets = (tweets) => {
setTweets(
tweets.map((tweet) => {
return {
...tweet,
// we store images in the database as a varchar field with
// comma separated links to images
images: tweet.images ? tweet.images.split(IMAGE_DELIMITER) : [],
};
})
);
setTweets(tweets.map(processTweet));
};
useNuiEvent(types.APP_TWITTER, "fetchTweets", _setTweets);