Move loading button logic out.

This commit is contained in:
Brian Cao 2023-04-02 12:24:50 -07:00
parent 2713e3316a
commit 6805a8978a
4 changed files with 84 additions and 72 deletions

View File

@ -0,0 +1,29 @@
import useApi from 'hooks/useApi';
import useMessages from 'hooks/useMessages';
import { Icon, Icons, LoadingButton, Text } from 'react-basics';
export default function TeamMemberRemoveButton({ teamUserId, disabled, onSave }) {
const { formatMessage, labels } = useMessages();
const { del, useMutation } = useApi();
const { mutate, isLoading } = useMutation(() => del(`/teamUsers/${teamUserId}`));
const handleRemoveTeamMember = () => {
mutate(
{ teamUserId },
{
onSuccess: () => {
onSave();
},
},
);
};
return (
<LoadingButton onClick={() => handleRemoveTeamMember()} disabled={disabled} loading={isLoading}>
<Icon>
<Icons.Close />
</Icon>
<Text>{formatMessage(labels.remove)}</Text>
</LoadingButton>
);
}

View File

@ -1,28 +1,20 @@
import useMessages from 'hooks/useMessages';
import useUser from 'hooks/useUser';
import { ROLES } from 'lib/constants';
import { import {
Flexbox,
Table, Table,
TableHeader,
TableBody, TableBody,
TableRow,
TableCell, TableCell,
TableColumn, TableColumn,
LoadingButton, TableHeader,
Icon, TableRow,
Icons,
Flexbox,
Text,
} from 'react-basics'; } from 'react-basics';
import { ROLES } from 'lib/constants'; import TeamMemberRemoveButton from './TeamMemberRemoveButton';
import useUser from 'hooks/useUser';
import useApi from 'hooks/useApi';
import useMessages from 'hooks/useMessages';
import { useState } from 'react';
export default function TeamMembersTable({ data = [], onSave, readOnly }) { export default function TeamMembersTable({ data = [], onSave, readOnly }) {
const { formatMessage, labels } = useMessages(); const { formatMessage, labels } = useMessages();
const { user } = useUser(); const { user } = useUser();
const { del, useMutation } = useApi();
const { mutate, isLoading } = useMutation(data => del(`/teamUsers/${data.teamUserId}`));
const [loadingIds, setLoadingIds] = useState([]);
const columns = [ const columns = [
{ name: 'username', label: formatMessage(labels.username), style: { flex: 2 } }, { name: 'username', label: formatMessage(labels.username), style: { flex: 2 } },
@ -30,23 +22,6 @@ export default function TeamMembersTable({ data = [], onSave, readOnly }) {
{ name: 'action', label: '', style: { flex: 1 } }, { name: 'action', label: '', style: { flex: 1 } },
]; ];
const handleRemoveTeamMember = teamUserId => {
setLoadingIds(prev => [...prev, teamUserId]);
mutate(
{ teamUserId },
{
onSuccess: () => {
setLoadingIds(loadingIds.filter(a => a !== teamUserId));
onSave();
},
onError: () => {
setLoadingIds(loadingIds.filter(a => a !== teamUserId));
},
},
);
};
return ( return (
<Table columns={columns} rows={data}> <Table columns={columns} rows={data}>
<TableHeader> <TableHeader>
@ -67,16 +42,11 @@ export default function TeamMembersTable({ data = [], onSave, readOnly }) {
), ),
action: !readOnly && ( action: !readOnly && (
<Flexbox flex={1} justifyContent="end"> <Flexbox flex={1} justifyContent="end">
<LoadingButton <TeamMemberRemoveButton
onClick={() => handleRemoveTeamMember(row.id)} teamUserId={row.id}
disabled={user.id === row?.user?.id || row.role === ROLES.teamOwner} disabled={user.id === row?.user?.id || row.role === ROLES.teamOwner}
loading={isLoading && loadingIds.some(a => a === row.id)} onSave={onSave}
> ></TeamMemberRemoveButton>
<Icon>
<Icons.Close />
</Icon>
<Text>{formatMessage(labels.remove)}</Text>
</LoadingButton>
</Flexbox> </Flexbox>
), ),
}; };

View File

@ -0,0 +1,29 @@
import useApi from 'hooks/useApi';
import useMessages from 'hooks/useMessages';
import { Icon, Icons, LoadingButton, Text } from 'react-basics';
export default function TeamWebsiteRemoveButton({ teamWebsiteId, onSave }) {
const { formatMessage, labels } = useMessages();
const { del, useMutation } = useApi();
const { mutate, isLoading } = useMutation(() => del(`/teamWebsites/${teamWebsiteId}`));
const handleRemoveTeamMember = () => {
mutate(
{ teamWebsiteId },
{
onSuccess: () => {
onSave();
},
},
);
};
return (
<LoadingButton onClick={() => handleRemoveTeamMember()} loading={isLoading}>
<Icon>
<Icons.Close />
</Icon>
<Text>{formatMessage(labels.remove)}</Text>
</LoadingButton>
);
}

View File

@ -1,44 +1,30 @@
import useMessages from 'hooks/useMessages';
import useUser from 'hooks/useUser';
import Link from 'next/link'; import Link from 'next/link';
import { import {
Table,
TableHeader,
TableBody,
TableRow,
TableCell,
TableColumn,
Button, Button,
Text, Flexbox,
Icon, Icon,
Icons, Icons,
Flexbox, Table,
TableBody,
TableCell,
TableColumn,
TableHeader,
TableRow,
Text,
} from 'react-basics'; } from 'react-basics';
import useUser from 'hooks/useUser'; import TeamWebsiteRemoveButton from './TeamWebsiteRemoveButton';
import useApi from 'hooks/useApi';
import useMessages from 'hooks/useMessages';
export default function TeamWebsitesTable({ data = [], onSave }) { export default function TeamWebsitesTable({ data = [], onSave }) {
const { formatMessage, labels } = useMessages(); const { formatMessage, labels } = useMessages();
const { user } = useUser(); const { user } = useUser();
const { del, useMutation } = useApi();
const { mutate } = useMutation(({ teamWebsiteId }) => del(`/teamWebsites/${teamWebsiteId}`));
const columns = [ const columns = [
{ name: 'name', label: formatMessage(labels.name) }, { name: 'name', label: formatMessage(labels.name) },
{ name: 'domain', label: formatMessage(labels.domain) }, { name: 'domain', label: formatMessage(labels.domain) },
{ name: 'action', label: ' ' }, { name: 'action', label: ' ' },
]; ];
const handleRemoveWebsite = teamWebsiteId => {
mutate(
{ teamWebsiteId },
{
onSuccess: async () => {
onSave();
},
},
);
};
return ( return (
<Table columns={columns} rows={data}> <Table columns={columns} rows={data}>
<TableHeader> <TableHeader>
@ -72,12 +58,10 @@ export default function TeamWebsitesTable({ data = [], onSave }) {
</Button> </Button>
</Link> </Link>
{canRemove && ( {canRemove && (
<Button onClick={() => handleRemoveWebsite(teamWebsiteId)}> <TeamWebsiteRemoveButton
<Icon> teamWebsiteId={teamWebsiteId}
<Icons.Trash /> onSave={onSave}
</Icon> ></TeamWebsiteRemoveButton>
<Text>{formatMessage(labels.remove)}</Text>
</Button>
)} )}
</Flexbox> </Flexbox>
); );