* Adds actions, queries, db models and rest client * Adds Websocket handling for categories * fix merge * small refactor for categories websocket events * fix bad merge * Category delete handled * Handles deletion (prune on store) * Resolves feedback, adds team handling for prune * Minor fixes; param order and return, imports, type naming Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
36 lines
1 KiB
TypeScript
36 lines
1 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {storeCategories} from '@actions/local/category';
|
|
import NetworkManager from '@init/network_manager';
|
|
|
|
import {forceLogoutIfNecessary} from './session';
|
|
|
|
import type {Client} from '@client/rest';
|
|
|
|
export type CategoriesRequest = {
|
|
categories?: CategoryWithChannels[];
|
|
error?: unknown;
|
|
}
|
|
|
|
export const fetchCategories = async (serverUrl: string, teamId: string, prune = false, fetchOnly = false): Promise<CategoriesRequest> => {
|
|
let client: Client;
|
|
try {
|
|
client = NetworkManager.getClient(serverUrl);
|
|
} catch (error) {
|
|
return {error};
|
|
}
|
|
|
|
try {
|
|
const {categories} = await client.getCategories('me', teamId);
|
|
|
|
if (!fetchOnly) {
|
|
storeCategories(serverUrl, categories, prune);
|
|
}
|
|
|
|
return {categories};
|
|
} catch (error) {
|
|
forceLogoutIfNecessary(serverUrl, error as ClientErrorProps);
|
|
return {error};
|
|
}
|
|
};
|