Skip to content

Commit

Permalink
fix: make changes for PR
Browse files Browse the repository at this point in the history
Add enum from sdk
  • Loading branch information
spaenleh committed Aug 22, 2022
1 parent a368036 commit e653a57
Show file tree
Hide file tree
Showing 9 changed files with 929 additions and 670 deletions.
10 changes: 10 additions & 0 deletions cypress/fixtures/mockHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ export const mockUseAvatar = (): SpyHookType => ({
.as(USE_AVATAR_HOOK_NAME),
name: USE_AVATAR_HOOK_NAME,
});

// todo
// export const mockUseMentions = (): QueryObserverResult<List<MemberRecord>> => ({
// id: '123',
// mentions: [
// {
// id: '1234',
// },
// ],
// });
99 changes: 46 additions & 53 deletions cypress/integration/mention-button.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,54 @@
/// <reference types="./cypress"/>
import { List } from 'immutable';

import { QueryObserverResult } from 'react-query';

import {
MemberMentionsRecord,
MemberRecord,
} from '@graasp/query-client/dist/src/types';

import MentionButton from '../../src/components/Mentions/MentionButton';
import {
dataCyWrapper,
exportChatButtonCypress,
} from '../../src/config/selectors';
import { spyMethod } from '../fixtures/chat_messages';

// import { List } from 'immutable';
//
// import { QueryObserverResult } from 'react-query';
//
// import {
// MemberMentionsRecord,
// MemberRecord,
// } from '@graasp/query-client/dist/src/types';
//
// import MentionButton from '../../src/components/Mentions/MentionButton';
// import {
// dataCyWrapper,
// exportChatButtonCypress,
// } from '../../src/config/selectors';
// import { spyMethod } from '../fixtures/chat_messages';
//
// const useMentionsMock = (): RecordOf<MemberMentions> => ({
// id: '123',
// mentions: [
// {
// id: '1234',
// },
// ],
// });
// const patchMentionSpy = spyMethod('patchMention');
// const deleteMentionSpy = spyMethod('deleteMention');
// const clearMentionsSpy = spyMethod('clearMentions');

const patchMentionSpy = spyMethod('patchMention');
const deleteMentionSpy = spyMethod('deleteMention');
const clearMentionsSpy = spyMethod('clearMentions');
// todo: remove when tests are completed
export {};

describe('Mention Button', () => {
it('should show mention button', () => {
cy.mount(
<MentionButton
color="primary"
clearAllMentionsFunction={clearMentionsSpy}
deleteMentionFunction={deleteMentionSpy}
patchMentionFunction={patchMentionSpy}
useMentions={(): QueryObserverResult<MemberMentionsRecord> =>
null as unknown as QueryObserverResult<MemberMentionsRecord>
}
useMembers={(): QueryObserverResult<List<MemberRecord>> =>
null as unknown as QueryObserverResult<List<MemberRecord>>
}
// useMentions={function (
// options?: { getUpdates?: boolean | undefined } | undefined,
// ): { data?: RecordOf<MemberMentions> | undefined } {
// throw new Error('Function not implemented.');
// }}
// useMembers={function (memberIds: string[]): {
// data?: List<Member> | undefined;
// } {
// throw new Error('Function not implemented.');
// }}
/>,
).then(() =>
cy.get(dataCyWrapper(exportChatButtonCypress)).should('exist'),
);
// cy.mount(
// <MentionButton
// color="primary"
// clearAllMentionsFunction={clearMentionsSpy}
// deleteMentionFunction={deleteMentionSpy}
// patchMentionFunction={patchMentionSpy}
// useMentions={(): QueryObserverResult<MemberMentionsRecord> =>
// null as unknown as QueryObserverResult<MemberMentionsRecord>
// }
// useMembers={(): QueryObserverResult<List<MemberRecord>> =>
// null as unknown as QueryObserverResult<List<MemberRecord>>
// }
// // useMentions={function (
// // options?: { getUpdates?: boolean | undefined } | undefined,
// // ): { data?: RecordOf<MemberMentions> | undefined } {
// // throw new Error('Function not implemented.');
// // }}
// // useMembers={function (memberIds: string[]): {
// // data?: List<Member> | undefined;
// // } {
// // throw new Error('Function not implemented.');
// // }}
// />,
// ).then(() =>
// cy.get(dataCyWrapper(exportChatButtonCypress)).should('exist'),
// );
});
});
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@graasp/translations": "github:graasp/graasp-translations.git",
"@graasp/ui": "github:graasp/graasp-ui",
"clsx": "1.1.1",
"i18next": "21.6.11",
"i18next": "21.8.1",
"immutable": "4.0.0",
"lodash.truncate": "4.4.2",
"moment": "2.29.4",
Expand All @@ -75,10 +75,12 @@
"@cypress/react": "5.12.4",
"@cypress/webpack-dev-server": "1.8.4",
"@graasp/query-client": "github:graasp/graasp-query-client#183/updateChatMessageTypes",
"@graasp/sdk": "github:graasp/graasp-sdk#38/addMentionStatusEnum",
"@material-ui/core": "4.12.4",
"@material-ui/icons": "4.11.2",
"@material-ui/lab": "4.0.0-alpha.60",
"@rollup/plugin-commonjs": "21.0.1",
"@rollup/plugin-json": "4.1.0",
"@rollup/plugin-node-resolve": "13.1.3",
"@rollup/plugin-typescript": "8.3.0",
"@testing-library/jest-dom": "5.16.1",
Expand Down
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import peerDepsExternal from 'rollup-plugin-peer-deps-external';

import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';

Expand All @@ -11,6 +12,7 @@ export default {
format: 'cjs',
},
plugins: [
json(),
peerDepsExternal(),
resolve({
mainFields: ['module', 'main', 'jsnext:main', 'browser'],
Expand Down
30 changes: 17 additions & 13 deletions src/components/Chatbox/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import IconButton from '@material-ui/core/IconButton';
import { makeStyles } from '@material-ui/core/styles';
import SendIcon from '@material-ui/icons/Send';

import { MessageBodyType } from '@graasp/query-client/dist/src/types';
import {
MemberRecord,
MessageBodyType,
} from '@graasp/query-client/dist/src/types';
import { CHATBOX } from '@graasp/translations';

import {
Expand Down Expand Up @@ -58,6 +61,7 @@ const useStyles = makeStyles((theme) => ({
}));

const inputStyle = {
width: '100%',
// mentions
control: {
minHeight: '63px',
Expand Down Expand Up @@ -111,15 +115,15 @@ const Input: FC<Props> = ({
const { id: currentMemberId } = useCurrentMemberContext();
const { t } = useTranslation();

const memberSuggestions = [
// exclude self from suggestions
const membersExcludingSelf: MemberRecord[] =
members?.filter((m) => m.id !== currentMemberId)?.toArray() || [];
const memberSuggestions: SuggestionDataItem[] = [
{ id: ALL_MEMBERS_ID, display: ALL_MEMBERS_DISPLAY },
...(members
// exclude self from suggestions
?.filter((m) => m.id !== currentMemberId)
.map((m) => ({ id: m.id, display: m.name }))
.toJS() || []),
] as SuggestionDataItem[];
// add mention to all
...membersExcludingSelf.map((m) => ({ id: m.id, display: m.name })),
];

// compute if message exceeds max length
const isMessageTooLong = textInput.length > HARD_MAX_MESSAGE_LENGTH;

// autofocus on first render
Expand All @@ -130,10 +134,10 @@ const Input: FC<Props> = ({
const onSend = (): void => {
if (textInput) {
const mentions = getAllMentions(textInput).map(({ id }) => id);
// expand '@all' to all members in mentions array
let expandedMentions = mentions;
if (mentions.includes(ALL_MEMBERS_ID)) {
expandedMentions = members?.map((m) => m.id).toJS() as string[];
let expandedMentions: string[] = mentions;
// expand '@all' to all members in mentions array (skip if there are no members)
if (mentions.includes(ALL_MEMBERS_ID) && members?.size) {
expandedMentions = members.map((m) => m.id).toArray();
}
sendMessageFunction?.({ message: textInput, mentions: expandedMentions });
// reset input content
Expand Down
7 changes: 4 additions & 3 deletions src/components/Mentions/MentionButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
MemberMentionsRecord,
MemberRecord,
} from '@graasp/query-client/dist/src/types';
import { Member } from '@graasp/ui/dist/types';
import { MentionStatus } from '@graasp/sdk';

import MentionsDialog from './MentionsDialog';
import MentionsTable from './MentionsTable';
Expand Down Expand Up @@ -52,7 +52,7 @@ const MentionButton: FC<Props> = ({

// get member ids from the mentions
const memberIds = Array.from(new Set(mentions?.map((m) => m.creator)));
const { data: members = List<Member>() } = useMembers(memberIds);
const { data: members = List<MemberRecord>() } = useMembers(memberIds);

// add member names to mentions
const mentionsWithMembers = mentions?.map((m) => {
Expand All @@ -72,7 +72,8 @@ const MentionButton: FC<Props> = ({
overlap="circular"
color={color}
badgeContent={
mentions?.filter((m) => m.status === 'unread')?.size || 0
mentions?.filter((m) => m.status === MentionStatus.UNREAD)?.size ||
0
}
>
<Notifications color={color} />
Expand Down
15 changes: 8 additions & 7 deletions src/components/Mentions/MentionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { Check, Close, FiberManualRecord } from '@material-ui/icons';

import { ChatMentionRecord } from '@graasp/query-client/dist/src/types';
import { MentionStatus } from '@graasp/sdk';
import { Button } from '@graasp/ui';

import { normalizeMentions } from '../../utils/mentions';
Expand All @@ -35,7 +36,7 @@ const MentionsTable: FC<Props> = ({
}) => {
const [openConfirmation, setOpenConfirmation] = useState(false);
const markAsRead = (id: string): void => {
patchMention({ id: id, status: 'read' });
patchMention({ id: id, status: MentionStatus.READ });
};

const renderMentionTableContent = (): ReactElement | ReactElement[] => {
Expand All @@ -53,7 +54,7 @@ const MentionsTable: FC<Props> = ({
onClick={(): void => console.log('go to item', m.itemPath)}
>
<TableCell>
{m.status === 'unread' ? (
{m.status === MentionStatus.UNREAD ? (
<FiberManualRecord fontSize={'small'} color={'primary'} />
) : null}
</TableCell>
Expand Down Expand Up @@ -94,20 +95,20 @@ const MentionsTable: FC<Props> = ({
</TableCell>
</TableRow>
))
.toJS() as ReactElement[];
.toArray();
};

console.log(mentions?.toJS());

return (
<Grid container direction="column">
<Grid container item direction="row" justifyContent="space-between">
<Button
variant="outlined"
onClick={(): void => {
mentions
?.filter((m) => m.status === 'unread')
.map((m) => patchMention({ id: m.id, status: 'read' }));
?.filter((m) => m.status === MentionStatus.UNREAD)
.map((m) =>
patchMention({ id: m.id, status: MentionStatus.READ }),
);
}}
>
Mark All As Read
Expand Down
8 changes: 5 additions & 3 deletions src/context/MessagesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { List } from 'immutable';

import React, { FC, ReactElement } from 'react';

import { ChatMessageList, Member } from '../types';
import { MemberRecord } from '@graasp/query-client/dist/src/types';

import { ChatMessageList } from '../types';

export const MessagesContext = React.createContext({
chatId: '',
Expand All @@ -11,14 +13,14 @@ export const MessagesContext = React.createContext({
export type MessagesContextType = {
messages?: ChatMessageList;
chatId: string;
members?: List<Member>;
members?: List<MemberRecord>;
};

type Props = {
children: ReactElement;
messages?: ChatMessageList;
chatId: string;
members?: List<Member>;
members?: List<MemberRecord>;
};

export const MessagesContextProvider: FC<Props> = ({
Expand Down
Loading

0 comments on commit e653a57

Please sign in to comment.