Skip to content

Commit

Permalink
Short-cut with an error throw
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Dec 24, 2024
1 parent 3177864 commit f9d217c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ import { useTranslation } from 'react-i18next';
import ConvertToChannelModal from './ConvertToChannelModal';
import { useEndpointAction } from '../../../../hooks/useEndpointAction';

export const useConvertToChannel = (room: IRoom) => {
export const useConvertToChannel = ({ _id, teamId }: IRoom) => {
const { t } = useTranslation();
const setModal = useSetModal();
const userId = useUserId();
const canEdit = usePermission('edit-team-channel', room._id);
const canEdit = usePermission('edit-team-channel', _id);
const dispatchToastMessage = useToastMessageDispatch();

const convertTeamToChannel = useEndpointAction('POST', '/v1/teams.convertToChannel');

const onClickConvertToChannel = useEffectEvent(() => {
if (!userId || !teamId) {
throw new Error('Invalid teamId or userId');
}

const onConfirm = async (roomsToRemove: { [key: string]: Serialized<IRoom> }) => {
try {
await convertTeamToChannel({
teamId: room.teamId!,
teamId,
roomsToRemove: Object.keys(roomsToRemove),
});

Expand All @@ -37,8 +41,8 @@ export const useConvertToChannel = (room: IRoom) => {
onClose={() => setModal(null)}
onCancel={() => setModal(null)}
onConfirm={onConfirm}
teamId={room.teamId!}
userId={userId!}
teamId={teamId}
userId={userId}
/>,
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useTranslation } from 'react-i18next';
import LeaveTeam from './LeaveTeam';
import { useEndpointAction } from '../../../../hooks/useEndpointAction';

export const useLeaveTeam = (room: IRoom) => {
export const useLeaveTeam = ({ teamId }: IRoom) => {
const { t } = useTranslation();
const setModal = useSetModal();
const dispatchToastMessage = useToastMessageDispatch();
Expand All @@ -17,13 +17,17 @@ export const useLeaveTeam = (room: IRoom) => {

// const canLeave = usePermission('leave-team'); /* && room.cl !== false && joined */
const handleLeaveTeam = useEffectEvent(() => {
if (!teamId) {
throw new Error('Invalid teamId');
}

const onConfirm = async (selectedRooms: { [key: string]: Serialized<IRoom> & { isLastOwner?: boolean } } = {}) => {
const roomsLeft = Object.keys(selectedRooms);
const roomsToLeave = Array.isArray(roomsLeft) && roomsLeft.length > 0 ? roomsLeft : [];

try {
await leaveTeam({
teamId: room.teamId!,
teamId,
...(roomsToLeave.length && { rooms: roomsToLeave }),
});
dispatchToastMessage({ type: 'success', message: t('Teams_left_team_successfully') });
Expand All @@ -35,7 +39,7 @@ export const useLeaveTeam = (room: IRoom) => {
}
};

setModal(<LeaveTeam onConfirm={onConfirm} onCancel={() => setModal(null)} teamId={room.teamId!} />);
setModal(<LeaveTeam onConfirm={onConfirm} onCancel={() => setModal(null)} teamId={teamId} />);
});

return handleLeaveTeam;
Expand Down

0 comments on commit f9d217c

Please sign in to comment.