Skip to content

Commit

Permalink
[feat] #87 아티클 즐겨찾기 함수 전역 상태로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
mymain838 committed Aug 7, 2024
1 parent 9b9a850 commit fc2188e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/components/article/ArticleFlatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { RootStackParamList } from "../router/Router";
import StarIcon from "@/assets/icons/Star.svg";

import UserIcon from "@/assets/icons/User.svg";
import useStore from "@/store/store";

const ArticleFlatList: React.FC<ArticleFlatListProps> = ({
articles,
myFavoriteArticles,
setFavorite,
}) => {
const navigation = useNavigation<NavigationProp<RootStackParamList>>();

const favoriteFunc = useStore().favoriteFunc;
return (
<>
{articles.length ? (
Expand All @@ -38,7 +38,6 @@ const ArticleFlatList: React.FC<ArticleFlatListProps> = ({
images: el.images,
favorite_count: el.favorite_count,
},
setFavorite,
})
}
>
Expand Down Expand Up @@ -75,7 +74,7 @@ const ArticleFlatList: React.FC<ArticleFlatListProps> = ({
</View>
<Text style={{ fontSize: 12 }}>{el.favorite_count}</Text>
<TouchableOpacity
onPress={() => setFavorite(el.id, !!isFavorite)}
onPress={() => favoriteFunc(el.id, !!isFavorite)}
>
<StarIcon
color={isFavorite ? "#FFD73F" : "#ddd"}
Expand Down
7 changes: 4 additions & 3 deletions src/pages/articles/ArticleDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ const { width: screenWidth } = Dimensions.get("window");

import StarIcon from "@/assets/icons/Star.svg";
import { ScrollView } from "react-native-gesture-handler";
import useStore from "@/store/store";
const backIcon = require("@/assets/icons/Back.png");

export const ArticleDetail = () => {
const navigation = useNavigation();
const route = useRoute();

const { id, iconState, data, setFavorite } = route.params as {
const { id, iconState, data } = route.params as {
id: number;
iconState: boolean;
data: ArticleDetail;
setFavorite: (articleId: number, mode: boolean) => void;
};

const [isFavorite, setIsFavorite] = useState(iconState);
const favoriteFunc = useStore().favoriteFunc;
const handleFavorite = async () => {
setFavorite(id, isFavorite);
favoriteFunc(id, isFavorite);
setIsFavorite(!isFavorite);
};

Expand Down
4 changes: 3 additions & 1 deletion src/pages/articles/Articles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getFavoriteArticleIdSpb,
setFavoriteSpb,
} from "@/supaBase/api/article";
import useStore from "@/store/store";

const menu = require("@/assets/icons/menu.png");
const profile = { uri: "https://picsum.photos/200/300" };
Expand All @@ -25,11 +26,13 @@ const Articles = () => {
{ title: "최신순", value: "LATEST" },
{ title: "좋아요순", value: "FAVORITE" },
];
const setFavoriteFunc = useStore().setFavoriteFunc;

useFocusEffect(
React.useCallback(() => {
getArticles();
getFavoriteArticles();
setFavoriteFunc(setFavorite);
}, [])
);

Expand Down Expand Up @@ -79,7 +82,6 @@ const Articles = () => {
<ArticleFlatList
articles={articles}
myFavoriteArticles={myFavoriteArticles}
setFavorite={setFavorite}
/>
</ScrollView>
</SafeAreaView>
Expand Down
2 changes: 2 additions & 0 deletions src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { create } from "zustand";

const useStore = create((set) => ({
userInfo: {},
favoriteFunc: null,
setFavoriteFunc: (func) => set({ favoriteFunc: func }),
setUserData: (newUserData) => set({ userInfo: newUserData }),
}));

Expand Down
1 change: 0 additions & 1 deletion src/types/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type ArticleDetail = {
interface ArticleFlatListProps {
articles: Article[];
myFavoriteArticles: ArticleFavoriteAId[];
setFavorite: (articleId: number, mode: boolean) => void;
}

interface ArticlesReponse {
Expand Down

0 comments on commit fc2188e

Please sign in to comment.