Skip to content

Commit 890acb7

Browse files
committed
add error notification for failed pin requests
1 parent 1acdbf1 commit 890acb7

File tree

13 files changed

+80
-39
lines changed

13 files changed

+80
-39
lines changed

examples/team/src/App.js

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable */
21
import React, { Component } from 'react';
32
import { StreamChat } from 'stream-chat';
43
import {
@@ -18,18 +17,13 @@ import 'stream-chat-react/dist/css/index.css';
1817
import './App.css';
1918

2019
const urlParams = new URLSearchParams(window.location.search);
21-
// const user =
22-
// urlParams.get('user') || process.env.REACT_APP_CHAT_API_DEFAULT_USER;
20+
const user = urlParams.get('user') || process.env.REACT_APP_USER_ID;
2321
const theme = urlParams.get('theme') || 'light';
24-
// const userToken =
25-
// urlParams.get('user_token') ||
26-
// process.env.REACT_APP_CHAT_API_DEFAULT_USER_TOKEN;
22+
const userToken =
23+
urlParams.get('user_token') || process.env.REACT_APP_USER_TOKEN;
2724

28-
const filters = { type: 'team', example: 1 };
29-
const sort = {
30-
last_message_at: -1,
31-
cid: 1,
32-
};
25+
const filters = { type: 'team' };
26+
const sort = { last_message_at: -1, cid: 1 };
3327
const options = {
3428
member: true,
3529
watch: true,
@@ -43,16 +37,11 @@ const Paginator = (props) => (
4337
class App extends Component {
4438
constructor(props) {
4539
super(props);
46-
this.chatClient = new StreamChat('qk4nn7rpcn75');
40+
this.chatClient = new StreamChat(process.env.REACT_APP_STREAM_KEY);
4741
if (process.env.REACT_APP_CHAT_SERVER_ENDPOINT) {
4842
this.chatClient.setBaseURL(process.env.REACT_APP_CHAT_SERVER_ENDPOINT);
4943
}
50-
this.chatClient.setUser(
51-
{
52-
id: 'example-user',
53-
},
54-
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiZXhhbXBsZS11c2VyIn0.HlC0dMKL43y3K_XbfvQS_Yc3V314HU4Z7LrBLil777g',
55-
);
44+
this.chatClient.setUser({ id: user }, userToken);
5645
}
5746

5847
render() {

src/components/Message/Message.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const Message = (props) => {
4242
getFlagMessageSuccessNotification,
4343
getMuteUserErrorNotification,
4444
getMuteUserSuccessNotification,
45+
getPinMessageErrorNotification,
4546
groupStyles = [],
4647
Message: MessageUIComponent = MessageSimple,
4748
message,
@@ -64,7 +65,6 @@ const Message = (props) => {
6465
const handleDelete = useDeleteHandler(message);
6566
const { editing, setEdit, clearEdit } = useEditHandler();
6667
const handleOpenThread = useOpenThreadHandler(message, propOpenThread);
67-
const { canPin, handlePin } = usePinHandler(message, pinPermissions);
6868
const handleReaction = useReactionHandler(message);
6969
const handleRetry = useRetryHandler(propRetrySendMessage);
7070

@@ -85,6 +85,11 @@ const Message = (props) => {
8585
onMentionsHover: propOnMentionsHover,
8686
});
8787

88+
const { canPin, handlePin } = usePinHandler(message, pinPermissions, {
89+
notify: addNotification,
90+
getErrorNotification: getPinMessageErrorNotification,
91+
});
92+
8893
const { onUserClick, onUserHover } = useUserHandler(message, {
8994
onUserClickHandler: propOnUserClick,
9095
onUserHoverHandler: propOnUserHover,
@@ -259,14 +264,23 @@ Message.propTypes = {
259264
*
260265
* */
261266
getMuteUserErrorNotification: PropTypes.func,
267+
/**
268+
* Function that returns message/text as string to be shown as notification, when request for pinning a message runs into error
269+
*
270+
* This function should accept following params:
271+
*
272+
* @param message A [message object](https://getstream.io/chat/docs/#message_format)
273+
*
274+
* */
275+
getPinMessageErrorNotification: PropTypes.func,
262276
/** Latest message id on current channel */
263277
lastReceivedId: PropTypes.string,
264278
/** DOMRect object for parent MessageList component */
265279
messageListRect: /** @type {PropTypes.Validator<DOMRect>} */ (PropTypes.object),
266280
/** @see See [Channel Context](https://getstream.github.io/stream-chat-react/#channelcontext) */
267281
members: /** @type {PropTypes.Validator<import('seamless-immutable').ImmutableObject<{[user_id: string]: import('stream-chat').ChannelMemberResponse<import('types').StreamChatReactUserType>}> | null | undefined>} */ (PropTypes.object),
268282
/**
269-
* Function to add custom notification on messagelist
283+
* Function to add custom notification on message list
270284
*
271285
* @param text Notification text to display
272286
* @param type Type of notification. 'success' | 'error'

src/components/Message/hooks/usePinHandler.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { useContext } from 'react';
2-
import { ChannelContext } from '../../../context';
2+
import { validateAndGetMessage } from '../utils';
3+
import { ChannelContext, TranslationContext } from '../../../context';
34

45
/**
56
* @type {import('types').usePinHandler}
67
*/
7-
export const usePinHandler = (message, permissions) => {
8+
export const usePinHandler = (message, permissions, notifications) => {
9+
const { notify, getErrorNotification } = notifications;
10+
811
const { client, channel } = useContext(ChannelContext);
12+
const { t } = useContext(TranslationContext);
913

1014
const canPin = () => {
1115
if (!client || !channel?.state || !permissions) return false;
@@ -47,13 +51,21 @@ export const usePinHandler = (message, permissions) => {
4751
try {
4852
await client.pinMessage(message);
4953
} catch (e) {
50-
console.log('Cannot pin message:', e);
54+
const errorMessage =
55+
getErrorNotification &&
56+
validateAndGetMessage(getErrorNotification, [message.user]);
57+
58+
notify(errorMessage || t('Error pinning message'), 'error');
5159
}
5260
} else {
5361
try {
5462
await client.unpinMessage(message);
5563
} catch (e) {
56-
console.log('Cannot unpin message:', e);
64+
const errorMessage =
65+
getErrorNotification &&
66+
validateAndGetMessage(getErrorNotification, [message.user]);
67+
68+
notify(errorMessage || t('Error removing message pin'), 'error');
5769
}
5870
}
5971
};

src/components/MessageActions/MessageActions.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const MessageActions = (props) => {
2424
getFlagMessageSuccessNotification,
2525
getMuteUserErrorNotification,
2626
getMuteUserSuccessNotification,
27+
getPinMessageErrorNotification,
2728
handleDelete: propHandleDelete,
2829
handleFlag: propHandleFlag,
2930
handleMute: propHandleMute,
@@ -56,7 +57,10 @@ export const MessageActions = (props) => {
5657
getSuccessNotification: getMuteUserErrorNotification,
5758
});
5859

59-
const { handlePin } = usePinHandler(message, pinPermissions);
60+
const { handlePin } = usePinHandler(message, pinPermissions, {
61+
notify: addNotification,
62+
getErrorNotification: getPinMessageErrorNotification,
63+
});
6064

6165
const isMuted = useCallback(() => {
6266
return isUserMuted(message, mutes);

src/components/MessageList/MessageList.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ class MessageList extends PureComponent {
283283
.getMuteUserSuccessNotification,
284284
getMuteUserErrorNotification: this.props
285285
.getMuteUserErrorNotification,
286+
getPinMessageErrorNotification: this.props
287+
.getPinMessageErrorNotification,
286288
pinPermissions: this.props.pinPermissions,
287289
}}
288290
/>
@@ -371,6 +373,15 @@ MessageList.propTypes = {
371373
*
372374
* */
373375
getMuteUserErrorNotification: PropTypes.func,
376+
/**
377+
* Function that returns message/text as string to be shown as notification, when request for pinning a message runs into error
378+
*
379+
* This function should accept following params:
380+
*
381+
* @param message A [message object](https://getstream.io/chat/docs/#message_format)
382+
*
383+
* */
384+
getPinMessageErrorNotification: PropTypes.func,
374385
/** **Available from [chat context](https://getstream.github.io/stream-chat-react/#chat)** */
375386
client: PropTypes.object,
376387
/** **Available from [channel context](https://getstream.github.io/stream-chat-react/#channel)** */

src/i18n/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"Error adding flag: Either the flag already exist or there is issue with network connection ...": "Error adding flag: Either the flag already exist or there is issue with network connection ...",
1515
"Error connecting to chat, refresh the page to try again.": "Error connecting to chat, refresh the page to try again.",
1616
"Error muting a user ...": "Error muting a user ...",
17+
"Error pinning message": "Error pinning message",
18+
"Error removing message pin": "Error removing message pin",
1719
"Error unmuting a user ...": "Error unmuting a user ...",
1820
"Error · Unsent": "Error · Unsent",
1921
"Error: {{ errorMessage }}": "Error: {{ errorMessage }}",

src/i18n/fr.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"Error adding flag: Either the flag already exist or there is issue with network connection ...": "Erreur d'ajout du flag : le flag existe déjà ou vous rencontrez un problème de connexion au réseau ...",
1515
"Error connecting to chat, refresh the page to try again.": "Erreur de connexion au chat, rafraîchissez la page pour réessayer.",
1616
"Error muting a user ...": "Erreur de mise en sourdine d'un utilisateur ...",
17+
"Error pinning message": "Erreur d'épinglage du message",
18+
"Error removing message pin": "Erreur lors de la suppression du code PIN du message",
1719
"Error unmuting a user ...": "Erreur de désactivation de la fonction sourdine pour un utilisateur ...",
1820
"Error · Unsent": "Erreur - Non envoyé",
1921
"Error: {{ errorMessage }}": "Erreur : {{ errorMessage }}",

src/i18n/hi.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"Error adding flag: Either the flag already exist or there is issue with network connection ...": "फ़ैल: या तो यह मैसेज के ऊपर पहले से फ्लैग है या तो आपके इंटरनेट कनेक्शन में कुछ परेशानी है",
1515
"Error connecting to chat, refresh the page to try again.": "चैट से कनेक्ट करने में त्रुटि, पेज को रिफ्रेश करें",
1616
"Error muting a user ...": "यूजर को म्यूट करने का प्रयास फेल हुआ",
17+
"Error pinning message": "संदेश को पिन करने में त्रुटि",
18+
"Error removing message pin": "संदेश पिन निकालने में त्रुटि",
1719
"Error unmuting a user ...": "यूजर को अनम्यूट करने का प्रयास फेल हुआ",
1820
"Error · Unsent": "फेल",
1921
"Error: {{ errorMessage }}": "फेल: {{ errorMessage }}",

src/i18n/it.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"Error adding flag: Either the flag already exist or there is issue with network connection ...": "Errore durante la segnalazione: la segnalazione esiste giá o c'é un problema di connessione ...",
1515
"Error connecting to chat, refresh the page to try again.": "Errore di connessione alla chat, aggiorna la pagina per riprovare",
1616
"Error muting a user ...": "Errore silenziando un utente ...",
17+
"Error pinning message": "Errore durante il blocco del messaggio",
18+
"Error removing message pin": "Errore durante la rimozione del PIN del messaggio",
1719
"Error unmuting a user ...": "Errore riattivando le notifiche per l'utente ...",
1820
"Error · Unsent": "Errore · Non inviato",
1921
"Error: {{ errorMessage }}": "Errore: {{ errorMessage }}",

src/i18n/nl.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"Error adding flag: Either the flag already exist or there is issue with network connection ...": "Fout bij het markeren: of het bericht is al gemarkeerd of er is een probleem met de netwerk verbinding",
1515
"Error connecting to chat, refresh the page to try again.": "Fout bij het verbinden, ververs de pagina om nogmaals te proberen",
1616
"Error muting a user ...": "Fout bij het muten van de gebruiker",
17+
"Error pinning message": "Fout bij vastzetten van bericht",
18+
"Error removing message pin": "Fout bij verwijderen van berichtpin",
1719
"Error unmuting a user ...": "Fout bij het unmuten van de gebruiker",
1820
"Error · Unsent": "Error: · niet verzonden",
1921
"Error: {{ errorMessage }}": "Error: {{ errorMessage }}",

0 commit comments

Comments
 (0)