MM-41889 Gekidou Post Options menu - save/unsave option (#5980)
* Added save/unsave option * code clean up * code correction * removed await
This commit is contained in:
parent
5b9492356b
commit
a83f40b41a
4 changed files with 102 additions and 20 deletions
|
|
@ -4,14 +4,17 @@
|
|||
import {Preferences} from '@constants';
|
||||
import DatabaseManager from '@database/manager';
|
||||
import NetworkManager from '@init/network_manager';
|
||||
import {queryPreferencesByCategoryAndName} from '@queries/servers/preference';
|
||||
import {queryCurrentUserId} from '@queries/servers/system';
|
||||
|
||||
import {forceLogoutIfNecessary} from './session';
|
||||
|
||||
const {CATEGORY_FAVORITE_CHANNEL, CATEGORY_SAVED_POST} = Preferences;
|
||||
|
||||
export type MyPreferencesRequest = {
|
||||
preferences?: PreferenceType[];
|
||||
error?: unknown;
|
||||
}
|
||||
};
|
||||
|
||||
export const fetchMyPreferences = async (serverUrl: string, fetchOnly = false): Promise<MyPreferencesRequest> => {
|
||||
let client;
|
||||
|
|
@ -48,6 +51,47 @@ export const saveFavoriteChannel = async (serverUrl: string, channelId: string,
|
|||
return {error: `${serverUrl} database not found`};
|
||||
}
|
||||
|
||||
try {
|
||||
// Todo: @shaz I think you'll need to add the category handler here so that the channel is added/removed from the favorites category
|
||||
const userId = await queryCurrentUserId(operator.database);
|
||||
const favPref: PreferenceType = {
|
||||
category: CATEGORY_FAVORITE_CHANNEL,
|
||||
name: channelId,
|
||||
user_id: userId,
|
||||
value: String(isFavorite),
|
||||
};
|
||||
return savePreference(serverUrl, [favPref]);
|
||||
} catch (error) {
|
||||
return {error};
|
||||
}
|
||||
};
|
||||
|
||||
export const savePostPreference = async (serverUrl: string, postId: string) => {
|
||||
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
|
||||
if (!operator) {
|
||||
return {error: `${serverUrl} database not found`};
|
||||
}
|
||||
|
||||
try {
|
||||
const userId = await queryCurrentUserId(operator.database);
|
||||
const pref: PreferenceType = {
|
||||
user_id: userId,
|
||||
category: CATEGORY_SAVED_POST,
|
||||
name: postId,
|
||||
value: 'true',
|
||||
};
|
||||
return savePreference(serverUrl, [pref]);
|
||||
} catch (error) {
|
||||
return {error};
|
||||
}
|
||||
};
|
||||
|
||||
export const savePreference = async (serverUrl: string, preferences: PreferenceType[]) => {
|
||||
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
|
||||
if (!operator) {
|
||||
return {error: `${serverUrl} database not found`};
|
||||
}
|
||||
|
||||
let client;
|
||||
try {
|
||||
client = NetworkManager.getClient(serverUrl);
|
||||
|
|
@ -56,15 +100,7 @@ export const saveFavoriteChannel = async (serverUrl: string, channelId: string,
|
|||
}
|
||||
|
||||
try {
|
||||
// Todo: @shaz I think you'll need to add the category handler here so that the channel is added/removed from the favorites category
|
||||
const userId = await queryCurrentUserId(operator.database);
|
||||
const favPref: PreferenceType = {
|
||||
category: Preferences.CATEGORY_FAVORITE_CHANNEL,
|
||||
name: channelId,
|
||||
user_id: userId,
|
||||
value: String(isFavorite),
|
||||
};
|
||||
const preferences = [favPref];
|
||||
client.savePreferences(userId, preferences);
|
||||
await operator.handlePreferences({
|
||||
preferences,
|
||||
|
|
@ -77,3 +113,39 @@ export const saveFavoriteChannel = async (serverUrl: string, channelId: string,
|
|||
return {error};
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteSavedPost = async (serverUrl: string, postId: string) => {
|
||||
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
|
||||
if (!operator) {
|
||||
return {error: `${serverUrl} database not found`};
|
||||
}
|
||||
let client;
|
||||
try {
|
||||
client = NetworkManager.getClient(serverUrl);
|
||||
} catch (error) {
|
||||
return {error};
|
||||
}
|
||||
try {
|
||||
const userId = await queryCurrentUserId(operator.database);
|
||||
const records = await queryPreferencesByCategoryAndName(operator.database, CATEGORY_SAVED_POST, postId);
|
||||
const postPreferenceRecord = records.find((r) => postId === r.name);
|
||||
const pref = {
|
||||
user_id: userId,
|
||||
category: CATEGORY_SAVED_POST,
|
||||
name: postId,
|
||||
value: 'true',
|
||||
};
|
||||
|
||||
if (postPreferenceRecord) {
|
||||
client.deletePreferences(userId, [pref]);
|
||||
await postPreferenceRecord.destroyPermanently();
|
||||
}
|
||||
|
||||
return {
|
||||
preference: pref,
|
||||
};
|
||||
} catch (error) {
|
||||
forceLogoutIfNecessary(serverUrl, error as ClientErrorProps);
|
||||
return {error};
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ export interface ClientPreferencesMix {
|
|||
|
||||
const ClientPreferences = (superclass: any) => class extends superclass {
|
||||
savePreferences = async (userId: string, preferences: PreferenceType[]) => {
|
||||
this.analytics.trackAPI('action_posts_flag');
|
||||
return this.doFetch(
|
||||
`${this.getPreferencesRoute(userId)}`,
|
||||
{method: 'put', body: preferences},
|
||||
|
|
@ -23,6 +24,7 @@ const ClientPreferences = (superclass: any) => class extends superclass {
|
|||
};
|
||||
|
||||
deletePreferences = async (userId: string, preferences: PreferenceType[]) => {
|
||||
this.analytics.trackAPI('action_posts_unflag');
|
||||
return this.doFetch(
|
||||
`${this.getPreferencesRoute(userId)}/delete`,
|
||||
{method: 'post', body: preferences},
|
||||
|
|
|
|||
|
|
@ -1,35 +1,39 @@
|
|||
// 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 {deleteSavedPost, savePostPreference} from '@actions/remote/preference';
|
||||
import {Screens} from '@constants';
|
||||
import {useServerUrl} from '@context/server';
|
||||
import {t} from '@i18n';
|
||||
import {dismissBottomSheet} from '@screens/navigation';
|
||||
|
||||
import BaseOption from './base_option';
|
||||
|
||||
type CopyTextProps = {
|
||||
isSaved: boolean;
|
||||
postId: string;
|
||||
}
|
||||
|
||||
const SaveOption = ({isSaved}: CopyTextProps) => {
|
||||
const handleUnsavePost = () => {
|
||||
//todo:
|
||||
};
|
||||
const SaveOption = ({isSaved, postId}: CopyTextProps) => {
|
||||
const serverUrl = useServerUrl();
|
||||
|
||||
const handleSavePost = () => {
|
||||
//todo:
|
||||
};
|
||||
const onHandlePress = useCallback(async () => {
|
||||
const remoteAction = isSaved ? deleteSavedPost : savePostPreference;
|
||||
remoteAction(serverUrl, postId);
|
||||
dismissBottomSheet(Screens.POST_OPTIONS);
|
||||
}, [postId, serverUrl]);
|
||||
|
||||
const id = isSaved ? t('mobile.post_info.unsave') : t('mobile.post_info.save');
|
||||
const defaultMessage = isSaved ? 'Unsave' : 'Save';
|
||||
const onPress = isSaved ? handleUnsavePost : handleSavePost;
|
||||
|
||||
return (
|
||||
<BaseOption
|
||||
i18nId={id}
|
||||
defaultMessage={defaultMessage}
|
||||
iconName='bookmark-outline'
|
||||
onPress={onPress}
|
||||
onPress={onHandlePress}
|
||||
testID='post.options.flag.unflag'
|
||||
/>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -76,7 +76,11 @@ const PostOptions = ({
|
|||
}
|
||||
{canMarkAsUnread && !isSystemPost && (<MarkAsUnreadOption/>)}
|
||||
{canCopyPermalink && <CopyLinkOption post={post}/>}
|
||||
{!isSystemPost && <SaveOption isSaved={isSaved}/>}
|
||||
{!isSystemPost &&
|
||||
<SaveOption
|
||||
isSaved={isSaved}
|
||||
postId={post.id}
|
||||
/>}
|
||||
{canCopyText && <CopyTextOption postMessage={post.message}/>}
|
||||
{canPin && <PinChannelOption isPostPinned={post.isPinned}/>}
|
||||
{canEdit && <EditOption/>}
|
||||
|
|
|
|||
Loading…
Reference in a new issue