-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(webext): ensure that videos are correctly added when using contex…
…tmenu actions
- Loading branch information
Showing
8 changed files
with
150 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,41 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
import { RootState } from 'store'; | ||
import { Channel } from 'types'; | ||
import { addChannel } from './reducers/channels'; | ||
import { extendedApi } from './services/youtube'; | ||
import { addChannel } from '../reducers/channels'; | ||
import { extendedApi } from '../services/youtube'; | ||
|
||
export const fetchChannelById = createAsyncThunk< | ||
Channel | undefined, | ||
{ id: string }, | ||
{ state: RootState } | ||
>('channels/fetchChannelById', async ({ id }, { getState, dispatch }) => { | ||
const { channels } = getState(); | ||
// check for existing channel | ||
const found = channels.list.find((channel: Channel) => channel.id === id); | ||
if (found) { | ||
return found; | ||
} | ||
|
||
// fetch channel by id | ||
>('channels/fetchChannelById', async ({ id }, { dispatch }) => { | ||
const result = dispatch( | ||
extendedApi.endpoints.findChannelById.initiate({ id }), | ||
); | ||
result.unsubscribe(); | ||
const response = await result; | ||
const channel = response.data?.items[0]; | ||
|
||
return channel; | ||
}); | ||
|
||
export const addChannelById = createAsyncThunk< | ||
void, | ||
{ id: string }, | ||
{ state: RootState } | ||
>('channels/addChannelById', async ({ id }, { getState, dispatch }) => { | ||
// check if channel exists | ||
const { channels } = getState(); | ||
const found = channels.list.find((channel: Channel) => channel.id === id); | ||
if (found) { | ||
return; | ||
} | ||
|
||
// fetch channel by id | ||
const channel = await dispatch(fetchChannelById({ id })).unwrap(); | ||
|
||
// save to channels list | ||
if (channel) { | ||
// save to channels list | ||
dispatch(addChannel(channel)); | ||
} | ||
return channel; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
import { RootState } from 'store'; | ||
import { Video, VideoCache, VideoFlags } from 'types'; | ||
import { addVideo } from '../reducers/videos'; | ||
import { extendedApi, GetVideosByIdArgs } from '../services/youtube'; | ||
import { addChannelById } from './channels'; | ||
|
||
export const fetchVideosById = createAsyncThunk< | ||
Video[], | ||
GetVideosByIdArgs, | ||
{ state: RootState } | ||
>('videos/fetchVideosById', async (payload, { dispatch }) => { | ||
const result = dispatch( | ||
extendedApi.endpoints.getVideosById.initiate(payload), | ||
); | ||
result.unsubscribe(); | ||
const response = await result; | ||
const videos = response.data?.items || []; | ||
|
||
return videos; | ||
}); | ||
|
||
export const addVideoById = createAsyncThunk< | ||
void, | ||
{ id: string; flags?: VideoFlags }, | ||
{ state: RootState } | ||
>('videos/addVideoById', async ({ id, flags }, { getState, dispatch }) => { | ||
// check if video exists | ||
const { videos } = getState(); | ||
let video: Video | VideoCache | undefined = videos.list.find( | ||
(video) => video.id === id, | ||
); | ||
|
||
if (!video) { | ||
// fetch video by id | ||
const result = await dispatch( | ||
fetchVideosById({ ids: [id], maxResults: 1 }), | ||
).unwrap(); | ||
video = result[0]; | ||
} | ||
|
||
// save to videos list | ||
if (video) { | ||
dispatch( | ||
addVideo({ | ||
video, | ||
flags, | ||
}), | ||
); | ||
// ensure to add channel too (if it does not exist) | ||
dispatch(addChannelById({ id: video.channelId })); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.