From 256b1ec005a914f888cf2cecf0cf53b153fc52f2 Mon Sep 17 00:00:00 2001 From: Avinash Lingaloo Date: Thu, 17 Feb 2022 20:14:57 +0400 Subject: [PATCH] added pin post option (#5982) --- app/actions/remote/post.ts | 33 +++++++++++++++++++ .../options/pin_channel_option.tsx | 22 ++++++++----- app/screens/post_options/post_options.tsx | 10 ++++-- package-lock.json | 1 + 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/app/actions/remote/post.ts b/app/actions/remote/post.ts index 426220ac30..2b2c1e8e50 100644 --- a/app/actions/remote/post.ts +++ b/app/actions/remote/post.ts @@ -519,3 +519,36 @@ export const fetchPostById = async (serverUrl: string, postId: string, fetchOnly return {error}; } }; + +export const togglePinPost = async (serverUrl: string, postId: string) => { + const database = DatabaseManager.serverDatabases[serverUrl]?.database; + if (!database) { + return {error: `${serverUrl} database not found`}; + } + + let client: Client; + try { + client = NetworkManager.getClient(serverUrl); + } catch (error) { + return {error}; + } + + try { + const post = await queryPostById(database, postId); + if (post) { + const isPinned = post.isPinned; + const request = isPinned ? client.unpinPost : client.pinPost; + + await request(postId); + await database.write(async () => { + await post.update((p) => { + p.isPinned = !isPinned; + }); + }); + } + return {post}; + } catch (error) { + forceLogoutIfNecessary(serverUrl, error as ClientErrorProps); + return {error}; + } +}; diff --git a/app/screens/post_options/options/pin_channel_option.tsx b/app/screens/post_options/options/pin_channel_option.tsx index e1c2799f7f..82d27f81bf 100644 --- a/app/screens/post_options/options/pin_channel_option.tsx +++ b/app/screens/post_options/options/pin_channel_option.tsx @@ -1,37 +1,41 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import React from 'react'; +import React, {useCallback} from 'react'; +import {togglePinPost} from '@actions/remote/post'; +import {Screens} from '@constants'; +import {useServerUrl} from '@context/server'; import {t} from '@i18n'; +import {dismissBottomSheet} from '@screens/navigation'; import BaseOption from './base_option'; type PinChannelProps = { isPostPinned: boolean; + postId: string; } -//fixme: wire up handlePinChannel -const PinChannelOption = ({isPostPinned}: PinChannelProps) => { - //todo: add useCallback for the handler callbacks - const handlePinPost = () => null; - const handleUnpinPost = () => null; +const PinChannelOption = ({isPostPinned, postId}: PinChannelProps) => { + const serverUrl = useServerUrl(); + + const onPress = useCallback(() => { + togglePinPost(serverUrl, postId); + dismissBottomSheet(Screens.POST_OPTIONS); + }, [postId, serverUrl]); let defaultMessage; let id; let key; - let onPress; if (isPostPinned) { defaultMessage = 'Unpin from Channel'; id = t('mobile.post_info.unpin'); key = 'unpin'; - onPress = handleUnpinPost; } else { defaultMessage = 'Pin to Channel'; id = t('mobile.post_info.pin'); key = 'pin'; - onPress = handlePinPost; } return ( diff --git a/app/screens/post_options/post_options.tsx b/app/screens/post_options/post_options.tsx index af6363256c..c098459fd7 100644 --- a/app/screens/post_options/post_options.tsx +++ b/app/screens/post_options/post_options.tsx @@ -80,9 +80,15 @@ const PostOptions = ({ } + /> + } {canCopyText && } - {canPin && } + {canPin && + + } {canEdit && } {canDelete && } diff --git a/package-lock.json b/package-lock.json index e9f3e3db8c..45bf05da91 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "mattermost-mobile", "version": "2.0.0", "hasInstallScript": true, "license": "Apache 2.0",