Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Convert CreateChannelWithData #25667

Merged
merged 3 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions apps/meteor/client/sidebar/header/CreateChannel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import React, { ReactElement, useEffect, useMemo, useState } from 'react';

import UserAutoCompleteMultiple from '../../components/UserAutoCompleteMultiple';

type CreateChannelProps = {
export type CreateChannelProps = {
values: {
name: string;
type?: boolean;
type: boolean;
readOnly?: boolean;
encrypted?: boolean;
broadcast?: boolean;
users?: string[];
description?: string;
};
handlers: {
handleName?: () => void;
Expand All @@ -21,10 +22,10 @@ type CreateChannelProps = {
handleReadOnly?: () => void;
};
hasUnsavedChanges: boolean;
onChangeUsers: () => void;
onChangeType: () => void;
onChangeBroadcast: () => void;
canOnlyCreateOneType?: boolean;
onChangeUsers: (value: string, action: string) => void;
onChangeType: React.FormEventHandler<HTMLElement>;
onChangeBroadcast: React.FormEventHandler<HTMLElement>;
canOnlyCreateOneType?: false | 'p' | 'c';
e2eEnabledForPrivateByDefault?: boolean;
onCreate: () => void;
onClose: () => void;
Expand Down Expand Up @@ -76,8 +77,8 @@ const CreateChannel = ({
checkName(values.name);
}, [checkName, values.name]);

const e2edisabled = useMemo(
() => !values.type || values.broadcast || !e2eEnabled || e2eEnabledForPrivateByDefault,
const e2edisabled = useMemo<boolean>(
() => !values.type || values.broadcast || Boolean(!e2eEnabled) || Boolean(e2eEnabledForPrivateByDefault),
[e2eEnabled, e2eEnabledForPrivateByDefault, values.broadcast, values.type],
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { RoomType } from '@rocket.chat/core-typings';
import { useMutableCallback } from '@rocket.chat/fuselage-hooks';
import { useSetting, usePermission } from '@rocket.chat/ui-contexts';
import React, { memo, useCallback, useMemo } from 'react';
import React, { memo, ReactElement, useCallback, useMemo } from 'react';

import { useEndpointActionExperimental } from '../../hooks/useEndpointActionExperimental';
import { useForm } from '../../hooks/useForm';
import { goToRoomById } from '../../lib/utils/goToRoomById';
import CreateChannel from './CreateChannel';
import CreateChannel, { CreateChannelProps } from './CreateChannel';

const CreateChannelWithData = ({ onClose, teamId = '', reload }) => {
type CreateChannelWithDataProps = {
onClose: () => void;
teamId?: string;
reload: () => void;
};

type UseFormValues = {
users: string[];
name: string;
type: RoomType;
description: string;
readOnly: boolean;
encrypted: boolean;
broadcast: boolean;
};

const CreateChannelWithData = ({ onClose, teamId = '', reload }: CreateChannelWithDataProps): ReactElement => {
const createChannel = useEndpointActionExperimental('POST', 'channels.create');
const createPrivateChannel = useEndpointActionExperimental('POST', 'groups.create');
const canCreateChannel = usePermission('create-c');
Expand All @@ -24,7 +41,7 @@ const CreateChannelWithData = ({ onClose, teamId = '', reload }) => {
}, [canCreateChannel, canCreatePrivateChannel]);

const initialValues = {
users: [],
users: [''],
name: '',
description: '',
type: canOnlyCreateOneType ? canOnlyCreateOneType === 'p' : true,
Expand All @@ -34,7 +51,7 @@ const CreateChannelWithData = ({ onClose, teamId = '', reload }) => {
};
const { values, handlers, hasUnsavedChanges } = useForm(initialValues);

const { users, name, description, type, readOnly, broadcast, encrypted } = values;
const { users, name, description, type, readOnly, broadcast, encrypted } = values as UseFormValues;
const { handleUsers, handleEncrypted, handleType, handleBroadcast, handleReadOnly } = handlers;

const onChangeUsers = useMutableCallback((value, action) => {
Expand All @@ -59,7 +76,7 @@ const CreateChannelWithData = ({ onClose, teamId = '', reload }) => {
});

const onCreate = useCallback(async () => {
const goToRoom = (rid) => {
const goToRoom = (rid: string): void => {
goToRoomById(rid);
};

Expand All @@ -74,14 +91,15 @@ const CreateChannelWithData = ({ onClose, teamId = '', reload }) => {
...(teamId && { teamId }),
},
};
let roomData;

if (type) {
roomData = await createPrivateChannel(params);
!teamId && goToRoom(roomData.group._id);
const roomData = await createPrivateChannel(params);
console.log(roomData);
!teamId && goToRoom(roomData.group._id as string);
} else {
roomData = await createChannel(params);
!teamId && goToRoom(roomData.channel._id);
const roomData = await createChannel(params);
console.log(roomData);
!teamId && goToRoom((roomData as any).channel._id);
}

onClose();
Expand All @@ -90,14 +108,14 @@ const CreateChannelWithData = ({ onClose, teamId = '', reload }) => {

return (
<CreateChannel
values={values}
values={values as CreateChannelProps['values']}
handlers={handlers}
hasUnsavedChanges={hasUnsavedChanges}
onChangeUsers={onChangeUsers}
onChangeType={onChangeType}
onChangeBroadcast={onChangeBroadcast}
canOnlyCreateOneType={canOnlyCreateOneType}
e2eEnabledForPrivateByDefault={e2eEnabledForPrivateByDefault}
e2eEnabledForPrivateByDefault={Boolean(e2eEnabledForPrivateByDefault)}
onClose={onClose}
onCreate={onCreate}
/>
Expand Down
2 changes: 2 additions & 0 deletions packages/core-typings/src/IRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export interface IRoom extends IRocketChatRecord {
description?: string;
createdOTR?: boolean;
e2eKeyId?: string;

channel?: { _id: string };
}

export interface ICreatedRoom extends IRoom {
Expand Down