Skip to content

Commit

Permalink
added Refresh delay and Refreshing animation
Browse files Browse the repository at this point in the history
  • Loading branch information
outsung committed Feb 13, 2021
1 parent 88d073b commit 6009c2b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
46 changes: 42 additions & 4 deletions src/components/UserProfile/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useMemo, useState } from 'react';
import {
ProfileBox,
ImageBox,
Expand All @@ -19,6 +19,7 @@ type userProfileProps = {
lane: string;
age: number;
gender: 'male' | 'female' | 'Private';
refreshTime: Date | undefined;
clickUpdateBtn: (_id: string) => void;
isFavorites: boolean;
addFavorites: (idx: string) => void;
Expand All @@ -32,11 +33,44 @@ function UserProfile({
lane,
age,
gender,
refreshTime,
clickUpdateBtn,
isFavorites,
addFavorites,
removeFavorites,
}: userProfileProps) {
const elapsedMSec = useMemo(() => {
const date = new Date();
if (!refreshTime) return 1000 * 150;
const refreshDate = new Date(refreshTime);
return date.getTime() - refreshDate.getTime();
}, [refreshTime]);

const [isRefreshing, setIsRefreshing] = useState(false);
const [updateBtnCheck, setUpdateBtnCheck] = useState(false);

useEffect(() => {
setUpdateBtnCheck(false);
setIsRefreshing(false);
}, [refreshTime]);

useEffect(() => {
const elapsedSec = elapsedMSec / 1000;
const is = elapsedSec > 120;

const afterTime = 120 - elapsedSec;

const timeoutHandle = setTimeout(
() => {
setUpdateBtnCheck(true);
},
is ? 1 : afterTime * 1000,
);
return () => {
clearTimeout(timeoutHandle);
};
}, [elapsedMSec]);

return (
<ProfileBox>
<ImageBox>
Expand All @@ -53,17 +87,21 @@ function UserProfile({
<Field>성별 : {GENDER_KR[gender]}</Field>
</FieldBox>
<Btn
isChcek={isFavorites}
isCheck={isFavorites}
onClick={() => (isFavorites ? removeFavorites(_id) : addFavorites(_id))}
>
</Btn>
<Btn
className={isRefreshing ? 'refreshing' : ''}
style={{ top: '3px', right: '33px' }}
onClick={() => {
clickUpdateBtn(_id);
if (updateBtnCheck && !isRefreshing) {
setIsRefreshing(true);
clickUpdateBtn(_id);
}
}}
isChcek
isCheck={updateBtnCheck}
>
</Btn>
Expand Down
20 changes: 16 additions & 4 deletions src/components/UserProfile/style.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import styled from 'styled-components';
import styled, { keyframes } from 'styled-components';

export const ProfileBox = styled.div`
display: flex;
Expand Down Expand Up @@ -65,15 +65,27 @@ export const FieldLink = styled.div`
cursor: pointer;
`;

export const Btn = styled.div<{ isChcek?: boolean }>`
const refreshing = keyframes`
from {
transform: rotateZ(0deg);
}
to {
transform: rotateZ(360deg);
}
`;
export const Btn = styled.div<{ isCheck?: boolean }>`
position: absolute;
top: 2px;
right: 5px;
font-size: 25px;
color: ${(props) => (props.isChcek ? '#FEB82E' : '#000')};
opacity: 0.3;
color: ${(props) => (props.isCheck ? '#FEB82E' : '#000')};
opacity: 0.5;
cursor: pointer;
&.refreshing {
animation: ${refreshing} 2s infinite;
}
`;
2 changes: 2 additions & 0 deletions src/container/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type allgetRes = {
lolLevel: string;
lolTear: string;
lolLane: string;
lolRefreshTime: Date;
};
export const allget = function () {
return callApi.get<{}, [allgetRes]>('users/test');
Expand Down Expand Up @@ -91,6 +92,7 @@ export type updateLolInfoRes = {
lolLevel: string;
lolLane: string;
lolChampion: string;
lolRefreshTime: Date;
};
export const updateLolInfo = function (_id: string) {
return callApi.get<{}, updateLolInfoRes>(`users/lolInfo/${_id}`);
Expand Down
2 changes: 2 additions & 0 deletions src/pages/Studio/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function Studio() {
if (!users) return;
const newUser = await updateLolInfo(_id);
if (!newUser) return;
// console.log('newUser', newUser);
setUsers(users.map((u) => (u._id === _id ? newUser : u)));
};

Expand Down Expand Up @@ -180,6 +181,7 @@ function Studio() {
nickname={user.nickname}
tear={user.lolTear}
lane={user.lolLane}
refreshTime={user.lolRefreshTime}
clickUpdateBtn={clickUpdateBtn}
isFavorites={favorites.includes(user._id)}
addFavorites={(idx) => {
Expand Down

0 comments on commit 6009c2b

Please sign in to comment.