main

mattermost/focalboard

Last updated at: 29/12/2023 09:41

initialLoad.ts

TLDR

This file, initialLoad.ts, contains functions related to the initial loading and fetching of data for the Mattermost application. The file includes methods for fetching initial data, loading board data, loading boards, loading board memberships, and getting user block subscriptions.

Methods

initialLoad

This method is an asynchronous thunk that fetches initial data for the Mattermost application. It makes multiple API calls to retrieve data such as the user's information, team information, board information, etc. If any necessary data is missing or there is an error, the method throws an appropriate error.

initialReadOnlyLoad

This method is an asynchronous thunk that fetches data for a read-only board. It retrieves the board and all its blocks by making corresponding API calls. If the board is not found or there is an error, the method throws an appropriate error.

loadBoardData

This method is an asynchronous thunk that loads data for a specific board identified by boardID. It retrieves all the blocks belonging to the board by making an API call.

loadBoards

This method is an asynchronous thunk that loads all the boards by making an API call.

loadMyBoardsMemberships

This method is an asynchronous thunk that loads the board memberships for the current user by making an API call.

getUserBlockSubscriptions

This method is a selector that returns the block subscriptions for a user. It takes the application's state as input and retrieves the block subscriptions from the state.

getUserBlockSubscriptionList

This method is a selector that returns the list of user block subscriptions. It uses the getUserBlockSubscriptions selector to get the block subscriptions.

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {createAsyncThunk, createSelector} from '@reduxjs/toolkit'

import {default as client} from '../octoClient'
import {Subscription} from '../wsclient'
import {ErrorId} from '../errors'

import {RootState} from './index'

export const initialLoad = createAsyncThunk(
    'initialLoad',
    async () => {
        const [me, myConfig, team, teams, boards, boardsMemberships, boardTemplates, limits] = await Promise.all([
            client.getMe(),
            client.getMyConfig(),
            client.getTeam(),
            client.getTeams(),
            client.getBoards(),
            client.getMyBoardMemberships(),
            client.getTeamTemplates(),
            client.getBoardsCloudLimits(),
        ])

        // if no me, normally user not logged in
        if (!me) {
            throw new Error(ErrorId.NotLoggedIn)
        }

        // if no team, either bad id, or user doesn't have access
        if (!team) {
            throw new Error(ErrorId.TeamUndefined)
        }
        return {
            team,
            teams,
            boards,
            boardsMemberships,
            boardTemplates,
            limits,
            myConfig,
        }
    },
)

export const initialReadOnlyLoad = createAsyncThunk(
    'initialReadOnlyLoad',
    async (boardId: string) => {
        const [board, blocks] = await Promise.all([
            client.getBoard(boardId),
            client.getAllBlocks(boardId),
        ])

        // if no board, read_token invalid
        if (!board) {
            throw new Error(ErrorId.InvalidReadOnlyBoard)
        }

        return {board, blocks}
    },
)

export const loadBoardData = createAsyncThunk(
    'loadBoardData',
    async (boardID: string) => {
        const blocks = await client.getAllBlocks(boardID)
        return {
            blocks,
        }
    },
)

export const loadBoards = createAsyncThunk(
    'loadBoards',
    async () => {
        const boards = await client.getBoards()
        return {
            boards,
        }
    },
)

export const loadMyBoardsMemberships = createAsyncThunk(
    'loadMyBoardsMemberships',
    async () => {
        const boardsMemberships = await client.getMyBoardMemberships()
        return {
            boardsMemberships,
        }
    },
)

export const getUserBlockSubscriptions = (state: RootState): Subscription[] => state.users.blockSubscriptions

export const getUserBlockSubscriptionList = createSelector(
    getUserBlockSubscriptions,
    (subscriptions) => subscriptions,
)