Skip to content

Commit

Permalink
refactor: 유저 데이터에 담겨있는 lastUploadedAt을 이용해 헤더 표시
Browse files Browse the repository at this point in the history
  • Loading branch information
wildcatco committed Aug 21, 2023
1 parent ab1c2b2 commit df48ad7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/features/user/queries/dto/get-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@ export interface GetUserResponse {
residence: Residence;
job: string;
role: string;
worries: {
expirationTime: string;
id: number;
status: string;
createdAt: string;
updatedAt: string | null;
etc: null;
title: string;
content: string;
}[];
}
4 changes: 3 additions & 1 deletion src/features/user/queries/useUser.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useQuery } from '@tanstack/react-query';
import { useEffect, useMemo } from 'react';
import { axiosPrivate } from '@/common/libs/axios';
import { User } from '@/features/user/types';
Expand Down Expand Up @@ -45,6 +45,8 @@ export function useUser() {
label: category.worryCategory.label,
})),
image: data.profileImageUrl,
lastUploadedAt:
data.worries?.length > 0 ? data.worries[0].createdAt : null,
}
: null,
[data, isError],
Expand Down
1 change: 1 addition & 0 deletions src/features/user/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface User {
worryCategories: OptionType[] | null;
image: string | null;
joinStatus: string;
lastUploadedAt: string | null;
}

export type UserInformation = {
Expand Down
14 changes: 5 additions & 9 deletions src/pages/home/components/HomeHeader/HomeHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { POST_TYPE } from '@/common/constants/post-type';
import { ONE_DAY_IN_MILLISECOND } from '@/common/constants/time';
import { Z_INDEX } from '@/common/constants/z-index';
import { useGetPosts } from '@/features/posts/queries';
import { useUser } from '@/features/user/queries';
import HeaderLogo from './HeaderLogo';

Expand All @@ -11,18 +9,16 @@ interface HomeHeaderProps {

export default function HomeHeader({ onClickLogo }: HomeHeaderProps) {
const { user, isLoggedIn } = useUser();
const { posts } = useGetPosts({
userId: user?.id,
type: POST_TYPE.MY,
});

const hasUploadedIn24 = () => {
if (!isLoggedIn || !posts || posts.length === 0) {
if (!isLoggedIn || !user?.lastUploadedAt) {
return false;
}
const latestUploadedAt = new Date(posts[0].createdAt).getTime();
const lastUploadedAt = new Date(
user.lastUploadedAt.replace('Z', ''),
).getTime();
const now = new Date().getTime();
return now - latestUploadedAt < ONE_DAY_IN_MILLISECOND;
return now - lastUploadedAt < ONE_DAY_IN_MILLISECOND;
};

const uploadedIn24 = hasUploadedIn24();
Expand Down

0 comments on commit df48ad7

Please sign in to comment.