From 3d71bc17c843a78b71f30a94eed55600a1d3f89d Mon Sep 17 00:00:00 2001 From: shuai Date: Mon, 12 Aug 2024 17:45:03 +0800 Subject: [PATCH] fix: badge detail list styles format --- i18n/en_US.yaml | 2 +- ui/src/common/interface.ts | 1 + ui/src/components/CardBadge/index.tsx | 23 +++++++++---- .../Badges/Detail/components/Badge/index.tsx | 6 ++-- .../Detail/components/UserCard/index.tsx | 31 ++++++++--------- .../Personal/components/Badges/index.tsx | 11 +++++-- .../Personal/components/Overview/index.tsx | 33 +++++++++++++------ ui/src/pages/Users/Personal/index.tsx | 7 +++- ui/src/services/client/badges.ts | 16 +++++++++ 9 files changed, 89 insertions(+), 41 deletions(-) diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml index 09596b78..420d25ba 100644 --- a/i18n/en_US.yaml +++ b/i18n/en_US.yaml @@ -1743,7 +1743,7 @@ ui: earned_x: Earned x{{ number }} x_awarded: "{{ number }} awarded" can_earn_multiple: You can earn this multiple times. - + earned: Earned admin: admin_header: diff --git a/ui/src/common/interface.ts b/ui/src/common/interface.ts index 45f957c2..608a2465 100644 --- a/ui/src/common/interface.ts +++ b/ui/src/common/interface.ts @@ -744,6 +744,7 @@ export interface BadgeListItem { earned: boolean; /** 1: bronze 2: silver 3:gold */ level: number; + earned_count?: number; } export interface BadgeListGroupItem { diff --git a/ui/src/components/CardBadge/index.tsx b/ui/src/components/CardBadge/index.tsx index 6fb4af7c..bb42630c 100644 --- a/ui/src/components/CardBadge/index.tsx +++ b/ui/src/components/CardBadge/index.tsx @@ -32,23 +32,34 @@ import './index.scss'; interface IProps { data: Type.BadgeListItem; showAwardedCount?: boolean; + urlSearchParams?: string; + badgePillType?: 'earned' | 'count'; } -const Index: FC = ({ data, showAwardedCount = false }) => { +const Index: FC = ({ + data, + badgePillType = 'earned', + showAwardedCount = false, + urlSearchParams, +}) => { const { t } = useTranslation('translation', { keyPrefix: 'badges' }); console.log(data); return ( - + - {data.earned && ( + {data.earned && badgePillType === 'earned' && ( {t('earned')} )} - {/* - {showEarned ? t('earned') : data.award_count} - */} + {badgePillType === 'count' && Number(data?.earned_count) > 0 && ( + + x{data.earned_count} + + )} {data.icon.startsWith('http') ? ( {data.name} ) : ( diff --git a/ui/src/pages/Badges/Detail/components/Badge/index.tsx b/ui/src/pages/Badges/Detail/components/Badge/index.tsx index 1fc5d43d..affb73e6 100644 --- a/ui/src/pages/Badges/Detail/components/Badge/index.tsx +++ b/ui/src/pages/Badges/Detail/components/Badge/index.tsx @@ -69,16 +69,16 @@ const Index: FC = ({ data }) => {
{t('can_earn_multiple')}
)} - {data.award_count > 0 && data.earned_count > 0 && ( + {(data.award_count > 0 || data.earned_count > 0) && (
{data.award_count > 0 && ( - + {t('x_awarded', { number: formatCount(data.award_count) })} )} {data.earned_count > 0 && ( - + {t('earned_x', { number: data.earned_count })} )} diff --git a/ui/src/pages/Badges/Detail/components/UserCard/index.tsx b/ui/src/pages/Badges/Detail/components/UserCard/index.tsx index d874ceb9..116a81b3 100644 --- a/ui/src/pages/Badges/Detail/components/UserCard/index.tsx +++ b/ui/src/pages/Badges/Detail/components/UserCard/index.tsx @@ -21,20 +21,17 @@ import { memo, FC } from 'react'; import { Link } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; -import classnames from 'classnames'; - import { Avatar } from '@/components'; import { formatCount } from '@/utils'; interface Props { data: any; - className?: string; } -const Index: FC = ({ data, className = '' }) => { +const Index: FC = ({ data }) => { const { t } = useTranslation('translation', { keyPrefix: 'badges' }); return ( -
+
{data?.status !== 'deleted' ? ( = ({ data, className = '' }) => { /> )} -
-
- {data?.status !== 'deleted' ? ( - - {data?.display_name} - - ) : ( - {data?.display_name} - )} -
+
+ {data?.status !== 'deleted' ? ( + + {data?.display_name} + + ) : ( + {data?.display_name} + )}
{formatCount(data?.rank)}{' '} {t('x_reputation', { keyPrefix: 'personal' })} diff --git a/ui/src/pages/Users/Personal/components/Badges/index.tsx b/ui/src/pages/Users/Personal/components/Badges/index.tsx index 0ef00f39..cd6830c9 100644 --- a/ui/src/pages/Users/Personal/components/Badges/index.tsx +++ b/ui/src/pages/Users/Personal/components/Badges/index.tsx @@ -24,10 +24,11 @@ import { CardBadge } from '@/components'; interface IProps { data: Type.BadgeListItem[]; + username: string; visible: boolean; } -const Index: FC = ({ data, visible }) => { +const Index: FC = ({ data, visible, username }) => { console.log(data); if (!visible) { return null; @@ -35,7 +36,13 @@ const Index: FC = ({ data, visible }) => { return (
{data.map((item) => { - return ; + return ( + + ); })}
); diff --git a/ui/src/pages/Users/Personal/components/Overview/index.tsx b/ui/src/pages/Users/Personal/components/Overview/index.tsx index b4dd548b..6b247b0c 100644 --- a/ui/src/pages/Users/Personal/components/Overview/index.tsx +++ b/ui/src/pages/Users/Personal/components/Overview/index.tsx @@ -22,16 +22,21 @@ import { useTranslation } from 'react-i18next'; import { Row, Col } from 'react-bootstrap'; // import * as Type from '@/common/interface'; -// import { CardBadge } from '@/components'; +import { CardBadge } from '@/components'; +import { useGetRecentAwardBadges } from '@/services'; import TopList from '../TopList'; interface Props { + username: string; visible: boolean; introduction: string; data; } -const Index: FC = ({ visible, introduction, data }) => { +const Index: FC = ({ visible, introduction, data, username }) => { const { t } = useTranslation('translation', { keyPrefix: 'personal' }); + const { data: recentBadges } = useGetRecentAwardBadges( + visible ? username : null, + ); if (!visible) { return null; } @@ -66,14 +71,22 @@ const Index: FC = ({ visible, introduction, data }) => { -
-
{t('recent_badges')}
- {/*
- {[0, 1, 2, 3, 4, 5, 6].map((item) => { - return ; - })} -
*/} -
+ {Number(recentBadges?.count) > 0 && ( +
+
{t('recent_badges')}
+
+ {recentBadges?.list?.map((item) => { + return ( + + ); + })} +
+
+ )}
); }; diff --git a/ui/src/pages/Users/Personal/index.tsx b/ui/src/pages/Users/Personal/index.tsx index 96704b85..e9516ace 100644 --- a/ui/src/pages/Users/Personal/index.tsx +++ b/ui/src/pages/Users/Personal/index.tsx @@ -102,6 +102,7 @@ const Personal: FC = () => { visible={tabName === 'overview'} introduction={userInfo?.bio_html || ''} data={topData} + username={username} /> { - + {!list?.length && !isLoading && } {count > 0 && ( diff --git a/ui/src/services/client/badges.ts b/ui/src/services/client/badges.ts index 476f85d1..7f3f8abd 100644 --- a/ui/src/services/client/badges.ts +++ b/ui/src/services/client/badges.ts @@ -64,3 +64,19 @@ export const useBadgeDetailList = (params: Type.BadgeDetailListReq) => { mutate, }; }; + +export const useGetRecentAwardBadges = (username) => { + const apiUrl = username + ? `/answer/api/v1/badge/user/awards/recent?username=${username}` + : null; + const { data, error, mutate } = useSWR< + { count: number; list: Array }, + Error + >(apiUrl, request.instance.get); + return { + data, + isLoading: !data && !error, + error, + mutate, + }; +};