mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-04 00:18:01 +01:00
refactor: project archive card (#7859)
- Refactored "favorites" action, and fixed issue with "favorite" prop - Refactored "owners" - simplified footer
This commit is contained in:
parent
9f0fd7e200
commit
ccb29a5acf
@ -12,9 +12,9 @@ import {
|
||||
} from './NewProjectCard.styles';
|
||||
import { ProjectCardFooter } from './ProjectCardFooter/ProjectCardFooter';
|
||||
import { ProjectModeBadge } from './ProjectModeBadge/ProjectModeBadge';
|
||||
import { ProjectOwners } from './ProjectOwners/ProjectOwners';
|
||||
import type { ProjectSchemaOwners } from 'openapi';
|
||||
import { ProjectIcon } from 'component/common/ProjectIcon/ProjectIcon';
|
||||
import { FavoriteAction } from './ProjectCardFooter/FavoriteAction/FavoriteAction';
|
||||
|
||||
interface IProjectCardProps {
|
||||
name: string;
|
||||
@ -23,7 +23,7 @@ interface IProjectCardProps {
|
||||
memberCount?: number;
|
||||
id: string;
|
||||
onHover: () => void;
|
||||
isFavorite?: boolean;
|
||||
favorite?: boolean;
|
||||
mode: string;
|
||||
owners?: ProjectSchemaOwners;
|
||||
}
|
||||
@ -36,7 +36,7 @@ export const ProjectCard = ({
|
||||
onHover,
|
||||
id,
|
||||
mode,
|
||||
isFavorite = false,
|
||||
favorite = false,
|
||||
owners,
|
||||
}: IProjectCardProps) => (
|
||||
<StyledProjectCard onMouseEnter={onHover}>
|
||||
@ -78,8 +78,8 @@ export const ProjectCard = ({
|
||||
</div>
|
||||
</StyledDivInfo>
|
||||
</StyledProjectCardBody>
|
||||
<ProjectCardFooter id={id} isFavorite={isFavorite}>
|
||||
<ProjectOwners owners={owners} />
|
||||
<ProjectCardFooter id={id} owners={owners}>
|
||||
<FavoriteAction id={id} isFavorite={favorite} />
|
||||
</ProjectCardFooter>
|
||||
</StyledProjectCard>
|
||||
);
|
||||
|
@ -12,7 +12,6 @@ import {
|
||||
} from './NewProjectCard.styles';
|
||||
import { ProjectCardFooter } from './ProjectCardFooter/ProjectCardFooter';
|
||||
import { ProjectModeBadge } from './ProjectModeBadge/ProjectModeBadge';
|
||||
import { ProjectOwners } from './ProjectOwners/ProjectOwners';
|
||||
import type { ProjectSchemaOwners } from 'openapi';
|
||||
import { ProjectIcon } from 'component/common/ProjectIcon/ProjectIcon';
|
||||
import { formatDateYMDHM } from 'utils/formatDate';
|
||||
@ -132,8 +131,13 @@ export const ProjectArchiveCard: FC<IProjectArchiveCardProps> = ({
|
||||
/>
|
||||
</StyledDivInfo>
|
||||
</StyledProjectCardBody>
|
||||
<ProjectCardFooter id={id} Actions={Actions} disabled>
|
||||
<ProjectOwners owners={owners} />
|
||||
<ProjectCardFooter
|
||||
id={id}
|
||||
Actions={Actions}
|
||||
disabled
|
||||
owners={owners}
|
||||
>
|
||||
<Actions id={id} />
|
||||
</ProjectCardFooter>
|
||||
</StyledProjectCard>
|
||||
);
|
||||
|
@ -0,0 +1,40 @@
|
||||
import type { FC } from 'react';
|
||||
import useToast from 'hooks/useToast';
|
||||
import { useFavoriteProjectsApi } from 'hooks/api/actions/useFavoriteProjectsApi/useFavoriteProjectsApi';
|
||||
import useProjects from 'hooks/api/getters/useProjects/useProjects';
|
||||
import { styled } from '@mui/material';
|
||||
import { FavoriteIconButton } from 'component/common/FavoriteIconButton/FavoriteIconButton';
|
||||
|
||||
type FavoriteActionProps = { id: string; isFavorite?: boolean };
|
||||
|
||||
const StyledFavoriteIconButton = styled(FavoriteIconButton)(({ theme }) => ({
|
||||
margin: theme.spacing(1, 2, 0, 0),
|
||||
}));
|
||||
|
||||
export const FavoriteAction: FC<FavoriteActionProps> = ({ id, isFavorite }) => {
|
||||
const { setToastApiError } = useToast();
|
||||
const { favorite, unfavorite } = useFavoriteProjectsApi();
|
||||
const { refetch } = useProjects();
|
||||
|
||||
const onFavorite = async (e: React.SyntheticEvent) => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
if (isFavorite) {
|
||||
await unfavorite(id);
|
||||
} else {
|
||||
await favorite(id);
|
||||
}
|
||||
refetch();
|
||||
} catch (error) {
|
||||
setToastApiError('Something went wrong, could not update favorite');
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<StyledFavoriteIconButton
|
||||
onClick={onFavorite}
|
||||
isFavorite={Boolean(isFavorite)}
|
||||
size='medium'
|
||||
/>
|
||||
);
|
||||
};
|
@ -1,10 +1,10 @@
|
||||
import type React from 'react';
|
||||
import type { FC } from 'react';
|
||||
import { Box, styled } from '@mui/material';
|
||||
import { FavoriteIconButton } from 'component/common/FavoriteIconButton/FavoriteIconButton';
|
||||
import useToast from 'hooks/useToast';
|
||||
import { useFavoriteProjectsApi } from 'hooks/api/actions/useFavoriteProjectsApi/useFavoriteProjectsApi';
|
||||
import useProjects from 'hooks/api/getters/useProjects/useProjects';
|
||||
import {
|
||||
type IProjectOwnersProps,
|
||||
ProjectOwners,
|
||||
} from '../ProjectOwners/ProjectOwners';
|
||||
|
||||
interface IProjectCardFooterProps {
|
||||
id: string;
|
||||
@ -12,6 +12,7 @@ interface IProjectCardFooterProps {
|
||||
children?: React.ReactNode;
|
||||
Actions?: FC<{ id: string; isFavorite?: boolean }>;
|
||||
disabled?: boolean;
|
||||
owners: IProjectOwnersProps['owners'];
|
||||
}
|
||||
|
||||
const StyledFooter = styled(Box)<{ disabled: boolean }>(
|
||||
@ -27,58 +28,15 @@ const StyledFooter = styled(Box)<{ disabled: boolean }>(
|
||||
}),
|
||||
);
|
||||
|
||||
const StyledContainer = styled(Box)(({ theme }) => ({
|
||||
padding: theme.spacing(1.5, 0, 2.5, 3),
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
}));
|
||||
|
||||
const StyledFavoriteIconButton = styled(FavoriteIconButton)(({ theme }) => ({
|
||||
margin: theme.spacing(1, 2, 0, 0),
|
||||
}));
|
||||
|
||||
const FavoriteAction: FC<{ id: string; isFavorite?: boolean }> = ({
|
||||
id,
|
||||
isFavorite,
|
||||
}) => {
|
||||
const { setToastApiError } = useToast();
|
||||
const { favorite, unfavorite } = useFavoriteProjectsApi();
|
||||
const { refetch } = useProjects();
|
||||
|
||||
const onFavorite = async (e: React.SyntheticEvent) => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
if (isFavorite) {
|
||||
await unfavorite(id);
|
||||
} else {
|
||||
await favorite(id);
|
||||
}
|
||||
refetch();
|
||||
} catch (error) {
|
||||
setToastApiError('Something went wrong, could not update favorite');
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<StyledFavoriteIconButton
|
||||
onClick={onFavorite}
|
||||
isFavorite={Boolean(isFavorite)}
|
||||
size='medium'
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export const ProjectCardFooter: FC<IProjectCardFooterProps> = ({
|
||||
children,
|
||||
id,
|
||||
isFavorite = false,
|
||||
Actions = FavoriteAction,
|
||||
owners,
|
||||
disabled = false,
|
||||
}) => {
|
||||
return (
|
||||
<StyledFooter disabled={disabled}>
|
||||
<StyledContainer>{children}</StyledContainer>
|
||||
<Actions id={id} isFavorite={isFavorite} />
|
||||
<ProjectOwners owners={owners} />
|
||||
{children}
|
||||
</StyledFooter>
|
||||
);
|
||||
};
|
||||
|
@ -5,7 +5,7 @@ import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
|
||||
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
|
||||
import { AvatarGroup } from 'component/common/AvatarGroup/AvatarGroup';
|
||||
|
||||
interface IProjectOwnersProps {
|
||||
export interface IProjectOwnersProps {
|
||||
owners?: ProjectSchema['owners'];
|
||||
}
|
||||
|
||||
@ -59,12 +59,18 @@ const StyledHeader = styled('h3')(({ theme }) => ({
|
||||
fontWeight: theme.typography.fontWeightRegular,
|
||||
}));
|
||||
|
||||
const StyledWrapper = styled('div')(({ theme }) => ({
|
||||
padding: theme.spacing(1.5, 0, 2.5, 3),
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
}));
|
||||
|
||||
export const ProjectOwners: FC<IProjectOwnersProps> = ({ owners = [] }) => {
|
||||
const ownersMap = useOwnersMap();
|
||||
const users = owners.map(ownersMap);
|
||||
|
||||
return (
|
||||
<>
|
||||
<StyledWrapper>
|
||||
<StyledContainer>
|
||||
<StyledHeader>
|
||||
{owners.length === 1 ? 'Owner' : 'Owners'}
|
||||
@ -76,6 +82,6 @@ export const ProjectOwners: FC<IProjectOwnersProps> = ({ owners = [] }) => {
|
||||
show={<StyledUserName>{users[0]?.name}</StyledUserName>}
|
||||
elseShow={<div />}
|
||||
/>
|
||||
</>
|
||||
</StyledWrapper>
|
||||
);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user