import { DropdownItem } from '@/components'; import { githubActions, RootState } from '@/store'; import { AppLog } from '@/utils'; import { createAsyncThunk } from '@reduxjs/toolkit'; import { GithubClient } from '../github-client'; type FetchBranches = { owner: string; repository: string; }; export const fetchBranchesThunk = createAsyncThunk( 'github/fetchBranches', async ({ owner, repository }, { dispatch, getState }) => { const { token, queryLoading } = (getState() as RootState).github; if (queryLoading === 'loading') return; try { dispatch(githubActions.setQueryState('loading')); const githubClient = new GithubClient(token); const branches = await githubClient.fetchBranches(owner, repository); dispatch(githubActions.setBranches(branches as DropdownItem[])); } catch (error) { AppLog.errorToast( 'We have a problem trying to get your branches. Please try again later.' ); dispatch(githubActions.setQueryState('failed')); } } );