stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
199 lines • 5.99 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useUserActions = void 0;
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = require("react");
var _useChannelActions = require("./useChannelActions");
var _hooks = require("../../components/Notifications/hooks");
var _contexts = require("../../contexts");
var _useStableCallback = require("../useStableCallback");
var useUserActions = user => {
var _useChatContext = (0, _contexts.useChatContext)(),
client = _useChatContext.client;
var _useNotificationApi = (0, _hooks.useNotificationApi)(),
addNotification = _useNotificationApi.addNotification;
var _useTranslationContex = (0, _contexts.useTranslationContext)(),
t = _useTranslationContex.t;
var muteUser = (0, _useStableCallback.useStableCallback)(function () {
var _ref = (0, _asyncToGenerator2.default)(function* (options) {
if (!(user != null && user.id)) {
return;
}
try {
yield client.muteUser(user.id);
addNotification({
message: t('{{ user }} has been muted', {
user: user.name || user.id
}),
options: {
severity: 'success',
type: 'api:user:mute:success'
},
origin: {
context: {
user
},
emitter: 'UserActions'
}
});
yield options == null || options.onSuccess == null ? void 0 : options.onSuccess();
} catch (error) {
addNotification({
message: t('Error muting a user ...'),
options: {
...(0, _useChannelActions.getNotificationErrorOptions)(error),
severity: 'error',
type: 'api:user:mute:failed'
},
origin: {
context: {
user
},
emitter: 'UserActions'
}
});
}
});
return function (_x) {
return _ref.apply(this, arguments);
};
}());
var unmuteUser = (0, _useStableCallback.useStableCallback)(function () {
var _ref2 = (0, _asyncToGenerator2.default)(function* (options) {
if (!(user != null && user.id)) {
return;
}
try {
yield client.unmuteUser(user.id);
addNotification({
message: t('{{ user }} has been unmuted', {
user: user.name || user.id
}),
options: {
severity: 'success',
type: 'api:user:unmute:success'
},
origin: {
context: {
user
},
emitter: 'UserActions'
}
});
yield options == null || options.onSuccess == null ? void 0 : options.onSuccess();
} catch (error) {
addNotification({
message: t('Error unmuting a user ...'),
options: {
...(0, _useChannelActions.getNotificationErrorOptions)(error),
severity: 'error',
type: 'api:user:unmute:failed'
},
origin: {
context: {
user
},
emitter: 'UserActions'
}
});
}
});
return function (_x2) {
return _ref2.apply(this, arguments);
};
}());
var blockUser = (0, _useStableCallback.useStableCallback)(function () {
var _ref3 = (0, _asyncToGenerator2.default)(function* (options) {
if (!(user != null && user.id)) {
return;
}
try {
yield client.blockUser(user.id);
addNotification({
message: t('User blocked'),
options: {
severity: 'success',
type: 'api:user:block:success'
},
origin: {
context: {
user
},
emitter: 'UserActions'
}
});
yield options == null || options.onSuccess == null ? void 0 : options.onSuccess();
} catch (error) {
addNotification({
message: t('Failed to block user'),
options: {
...(0, _useChannelActions.getNotificationErrorOptions)(error),
severity: 'error',
type: 'api:user:block:failed'
},
origin: {
context: {
user
},
emitter: 'UserActions'
}
});
}
});
return function (_x3) {
return _ref3.apply(this, arguments);
};
}());
var unblockUser = (0, _useStableCallback.useStableCallback)(function () {
var _ref4 = (0, _asyncToGenerator2.default)(function* (options) {
if (!(user != null && user.id)) {
return;
}
try {
yield client.unBlockUser(user.id);
addNotification({
message: t('User unblocked'),
options: {
severity: 'success',
type: 'api:user:unblock:success'
},
origin: {
context: {
user
},
emitter: 'UserActions'
}
});
yield options == null || options.onSuccess == null ? void 0 : options.onSuccess();
} catch (error) {
addNotification({
message: t('Failed to block user'),
options: {
...(0, _useChannelActions.getNotificationErrorOptions)(error),
severity: 'error',
type: 'api:user:block:failed'
},
origin: {
context: {
user
},
emitter: 'UserActions'
}
});
}
});
return function (_x4) {
return _ref4.apply(this, arguments);
};
}());
return (0, _react.useMemo)(() => ({
blockUser,
muteUser,
unblockUser,
unmuteUser
}), [blockUser, muteUser, unblockUser, unmuteUser]);
};
exports.useUserActions = useUserActions;
//# sourceMappingURL=useUserActions.js.map