Skip to content

Commit

Permalink
[Simplyfy] Feed 데이터 불러오는 부분에 필요 없는 부분삭제 #32
Browse files Browse the repository at this point in the history
why
사용하지 않는 코드가 있음

how
cursor based 말고 offset 에 해당하는 부분 코드 삭제
  • Loading branch information
WooYeonSeo committed Nov 23, 2019
1 parent 34ff701 commit 6a1430d
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions client/src/composition/feed/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useState, useMemo } from "react";
import React, { useState, useMemo, useEffect } from "react";
import Feed from "./Feed";
import { useQuery } from "@apollo/react-hooks";
import gql from "graphql-tag";
import useScrollEnd from "../../hooks/useScrollEnd";

interface IFeed {
content: string;
createdAt: string;
Expand All @@ -13,8 +14,8 @@ interface Feeds {
}

const GET_FEEDS = gql`
query getfeeds($first: Int, $after: Int, $currentCursor: String) {
feeds(first: $first, after: $after, cursor: $currentCursor) {
query getfeeds($first: Int, $currentCursor: String) {
feeds(first: $first, cursor: $currentCursor) {
content
createdAt
}
Expand All @@ -23,39 +24,38 @@ const GET_FEEDS = gql`

interface FeedVars {
first: number;
after: number;
currentCursor: string;
}
const OFFSET = 2;
const FeedContainer = () => {
const [feeds, setFeeds] = useState<IFeed[]>([]);
const [cursor, setCursor] = useState<string>("");
const [cursorIdx, setCursorIdx] = useState<number>(0);
const [cursor, setCursor] = useState<string>("9999-12-31T09:29:26.050Z");

const checkEnd = useScrollEnd();
// hooks 에서 useQuery 1 부터 시작
const { loading, data, fetchMore } = useQuery<Feeds, FeedVars>(GET_FEEDS, {
variables: { first: cursorIdx, after: OFFSET, currentCursor: cursor }
variables: { first: OFFSET, currentCursor: cursor }
});

useMemo(() => {
useEffect(() => {
if (data) {
// setFeeds([...feeds, ...data.feeds]);
setFeeds(data.feeds);
}
}, [data]);
}, []);

const fetchMoreFeed = async () => {
const { data: value } = await fetchMore({
variables: {
first: OFFSET,
after: cursorIdx,
currentCursor: cursor
},
updateQuery: (prev, { fetchMoreResult }) => {
if (!fetchMoreResult) {
return prev;
} else {
setCursorIdx(OFFSET + cursorIdx);
const { feeds: feedItems } = fetchMoreResult;
const lastFeedItem = feedItems[feedItems.length - 1];
setCursor(lastFeedItem.createdAt);
}

return Object.assign({}, prev, {
Expand All @@ -68,7 +68,6 @@ const FeedContainer = () => {

const checkEndFeed = () => {
fetchMoreFeed();

return null;
};

Expand Down

0 comments on commit 6a1430d

Please sign in to comment.