|
1 | | -import * as SpotifyApi from 'spotify-web-api-js'; |
| 1 | +import { User } from '@firebase/auth-types'; |
2 | 2 |
|
3 | | -import { ErrorAction, PayloadAction, Types } from '.'; |
| 3 | +import { UserCredentials } from '../state'; |
| 4 | + |
| 5 | +import { PayloadAction, Types } from '.'; |
4 | 6 |
|
5 | 7 | export type Actions = |
6 | 8 | | CheckSpotifyLoginStatusAction |
7 | 9 | | ExchangeCodeFailAction |
8 | | - | ExchangeCodeFinishAction |
9 | 10 | | ExchangeCodeStartAction |
10 | 11 | | NotifyAuthStatusKnownAction |
11 | | - | TriggerSpotifyOAuthLoginAction; |
| 12 | + | TriggerOAuthLoginAction; |
12 | 13 |
|
13 | 14 | export interface CheckSpotifyLoginStatusAction { |
14 | 15 | type: Types.CHECK_SPOTIFY_LOGIN_STATUS; |
15 | 16 | } |
16 | 17 |
|
17 | | -export interface ExchangeCodeFailAction extends ErrorAction { |
| 18 | +export interface ExchangeCodeFailAction extends PayloadAction<ProviderObject<Error>> { |
| 19 | + error: true; |
18 | 20 | type: Types.EXCHANGE_CODE_Fail; |
19 | 21 | } |
20 | 22 |
|
21 | | -export interface ExchangeCodeFinishAction { |
22 | | - type: Types.EXCHANGE_CODE_Finish; |
23 | | -} |
24 | | - |
25 | | -export interface ExchangeCodeStartAction { |
| 23 | +export interface ExchangeCodeStartAction extends PayloadAction<keyof UserCredentials> { |
26 | 24 | type: Types.EXCHANGE_CODE_Start; |
27 | 25 | } |
28 | 26 |
|
29 | | -export interface NotifyAuthStatusKnownAction extends PayloadAction<[ |
30 | | - 'spotify', |
31 | | - SpotifyApi.UserObjectPrivate | null |
32 | | -]> { |
| 27 | +export interface NotifyAuthStatusKnownAction extends PayloadAction<ProviderObject< |
| 28 | + User | SpotifyApi.UserObjectPrivate | null |
| 29 | +>> { |
33 | 30 | type: Types.NOTIFY_AUTH_STATUS_KNOWN; |
34 | 31 | } |
35 | 32 |
|
36 | | -export interface TriggerSpotifyOAuthLoginAction { |
37 | | - type: Types.TRIGGER_SPOTIFY_OAUTH_LOGIN; |
| 33 | +export type OAuthLoginProviders = Exclude<keyof UserCredentials, 'firebase'>; |
| 34 | + |
| 35 | +export interface TriggerOAuthLoginAction extends PayloadAction<OAuthLoginProviders> { |
| 36 | + type: Types.TRIGGER_OAUTH_LOGIN; |
| 37 | +} |
| 38 | + |
| 39 | +export interface ProviderObject<T> { |
| 40 | + data: T; |
| 41 | + provider: keyof UserCredentials; |
38 | 42 | } |
39 | 43 |
|
40 | 44 | export function checkSpotifyLoginStatus(): CheckSpotifyLoginStatusAction { |
41 | 45 | return { type: Types.CHECK_SPOTIFY_LOGIN_STATUS }; |
42 | 46 | } |
43 | 47 |
|
44 | | -export function exchangeCodeFail(err: Error): ExchangeCodeFailAction { |
| 48 | +export function exchangeCodeFail(provider: keyof UserCredentials, err: Error): ExchangeCodeFailAction { |
45 | 49 | return { |
46 | 50 | type: Types.EXCHANGE_CODE_Fail, |
47 | 51 | error: true, |
48 | | - payload: err, |
| 52 | + payload: { provider, data: err }, |
49 | 53 | }; |
50 | 54 | } |
51 | 55 |
|
52 | | -export function exchangeCodeFinish(): ExchangeCodeFinishAction { |
53 | | - return { type: Types.EXCHANGE_CODE_Finish }; |
54 | | -} |
55 | | - |
56 | | -export function exchangeCodeStart(): ExchangeCodeStartAction { |
57 | | - return { type: Types.EXCHANGE_CODE_Start }; |
| 56 | +export function exchangeCodeStart(provider: keyof UserCredentials): ExchangeCodeStartAction { |
| 57 | + return { |
| 58 | + type: Types.EXCHANGE_CODE_Start, |
| 59 | + payload: provider, |
| 60 | + }; |
58 | 61 | } |
59 | 62 |
|
60 | 63 | export function notifyAuthStatusKnown( |
61 | | - provider: 'spotify', |
62 | | - user: SpotifyApi.UserObjectPrivate | null, |
| 64 | + provider: keyof UserCredentials, |
| 65 | + user: User | SpotifyApi.UserObjectPrivate | null, |
63 | 66 | ): NotifyAuthStatusKnownAction { |
64 | 67 | return { |
65 | 68 | type: Types.NOTIFY_AUTH_STATUS_KNOWN, |
66 | | - payload: [provider, user], |
| 69 | + payload: { provider, data: user }, |
67 | 70 | }; |
68 | 71 | } |
69 | 72 |
|
70 | | -export function loginWithSpotify(): TriggerSpotifyOAuthLoginAction { |
71 | | - return { type: Types.TRIGGER_SPOTIFY_OAUTH_LOGIN }; |
| 73 | +export function triggerOAuthLogin(provider: OAuthLoginProviders): TriggerOAuthLoginAction { |
| 74 | + return { |
| 75 | + type: Types.TRIGGER_OAUTH_LOGIN, |
| 76 | + payload: provider, |
| 77 | + }; |
72 | 78 | } |
0 commit comments