Skip to content

Commit

Permalink
[Simplyfy] client 쿼리 조회하는 부분에서 필요 없는 코드 삭제
Browse files Browse the repository at this point in the history
why
offset조회 부분과 코드가 섞여있어서 사용하지 않는 offset관련 부분 코드 삭제

how
사용하지 않는 부분 코드 삭제
  • Loading branch information
WooYeonSeo committed Nov 22, 2019
1 parent f31e4e0 commit bdbfdee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 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
2 changes: 0 additions & 2 deletions client/src/hooks/useScrollEnd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ const useScrollEnd = () => {
document.documentElement.clientHeight ===
document.documentElement.scrollHeight
) {
console.log("scroll Y : ", window.scrollY);
setState(true);
} else {
console.log("scroll Y! : ", window.scrollY);
setState(false);
}
};
Expand Down

0 comments on commit bdbfdee

Please sign in to comment.