added pin post option (#5982)

This commit is contained in:
Avinash Lingaloo
2022-02-17 20:14:57 +04:00
committed by GitHub
parent 2a2f103813
commit 256b1ec005
4 changed files with 55 additions and 11 deletions

View File

@@ -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};
}
};

View File

@@ -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 (

View File

@@ -80,9 +80,15 @@ const PostOptions = ({
<SaveOption
isSaved={isSaved}
postId={post.id}
/>}
/>
}
{canCopyText && <CopyTextOption postMessage={post.message}/>}
{canPin && <PinChannelOption isPostPinned={post.isPinned}/>}
{canPin &&
<PinChannelOption
isPostPinned={post.isPinned}
postId={post.id}
/>
}
{canEdit && <EditOption/>}
{canDelete && <DeletePostOption/>}
</>

1
package-lock.json generated
View File

@@ -5,6 +5,7 @@
"requires": true,
"packages": {
"": {
"name": "mattermost-mobile",
"version": "2.0.0",
"hasInstallScript": true,
"license": "Apache 2.0",