MM-41963 Gekidou Post Options Delete and Edit (#5997)

This commit is contained in:
Avinash Lingaloo
2022-02-22 19:55:58 +04:00
committed by GitHub
parent bcb78c499c
commit b2b7a724de
5 changed files with 76 additions and 13 deletions

View File

@@ -552,3 +552,26 @@ export const togglePinPost = async (serverUrl: string, postId: string) => {
return {error};
}
};
export const deletePost = 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 {
await client.deletePost(postId);
const post = await removePost(serverUrl, {id: postId} as Post);
return {post};
} catch (error) {
forceLogoutIfNecessary(serverUrl, error as ClientErrorProps);
return {error};
}
};

View File

@@ -1,22 +1,52 @@
// 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 {useIntl} from 'react-intl';
import {Alert} from 'react-native';
import {deletePost} 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';
//fixme: wire up handleDeletePost
const DeletePostOption = () => {
const handleDeletePost = () => null;
type Props = {
postId: string ;
}
const DeletePostOption = ({postId}: Props) => {
const serverUrl = useServerUrl();
const {formatMessage} = useIntl();
const onPress = useCallback(() => {
Alert.alert(
formatMessage({id: 'mobile.post.delete_title', defaultMessage: 'Delete Post'}),
formatMessage({
id: 'mobile.post.delete_question',
defaultMessage: 'Are you sure you want to delete this post?',
}),
[{
text: formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'}),
style: 'cancel',
}, {
text: formatMessage({id: 'post_info.del', defaultMessage: 'Delete'}),
style: 'destructive',
onPress: () => {
deletePost(serverUrl, postId);
dismissBottomSheet(Screens.POST_OPTIONS);
},
}],
);
}, [postId, serverUrl]);
return (
<BaseOption
i18nId={t('post_info.del')}
defaultMessage='Delete'
iconName='trash-can-outline'
onPress={handleDeletePost}
onPress={onPress}
testID='post.options.delete.post'
isDestructive={true}
/>

View File

@@ -1,22 +1,30 @@
// 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 {Screens} from '@constants';
import {t} from '@i18n';
import {dismissBottomSheet, goToScreen} from '@screens/navigation';
import PostModel from '@typings/database/models/servers/post';
import BaseOption from './base_option';
const EditOption = () => {
const handleEdit = () => {
//todo:
};
type Props = {
post: PostModel;
}
const EditOption = ({post}: Props) => {
const onPress = useCallback(async () => {
// https://mattermost.atlassian.net/browse/MM-41991
await dismissBottomSheet(Screens.POST_OPTIONS);
goToScreen('EDIT_SCREEN_NOT_IMPLEMENTED_YET', '', {post});
}, [post]);
return (
<BaseOption
i18nId={t('post_info.edit')}
defaultMessage='Edit'
onPress={handleEdit}
onPress={onPress}
iconName='pencil-outline'
testID='post.options.edit'
/>

View File

@@ -89,8 +89,8 @@ const PostOptions = ({
postId={post.id}
/>
}
{canEdit && <EditOption/>}
{canDelete && <DeletePostOption/>}
{canEdit && <EditOption post={post}/>}
{canDelete && <DeletePostOption postId={post.id}/>}
</>
);
};