|
|
| import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'; |
| import linkedinAuthService from '../../services/linkedinAuthService'; |
|
|
| |
| const initialState = { |
| linkedinAccounts: [], |
| loading: false, |
| error: null, |
| oauthLoading: false, |
| oauthError: null, |
| deletingAccount: null, |
| deletingError: null, |
| settingPrimary: null, |
| settingPrimaryError: null |
| }; |
|
|
| |
| export const fetchLinkedInAccounts = createAsyncThunk( |
| 'linkedinAccounts/fetchLinkedInAccounts', |
| async (_, { rejectWithValue }) => { |
| try { |
| const response = await linkedinAuthService.getLinkedInAccounts(); |
| |
| |
| return response; |
| } catch (error) { |
| console.error('π [DEBUG] Async thunk - Error:', error); |
| return rejectWithValue(error.message); |
| } |
| } |
| ); |
|
|
| export const initiateLinkedInAuth = createAsyncThunk( |
| 'linkedinAccounts/initiateLinkedInAuth', |
| async (_, { rejectWithValue }) => { |
| try { |
| const response = await linkedinAuthService.initiateAuth(); |
| return response; |
| } catch (error) { |
| console.error('π [DEBUG] Initiate auth - Error:', error); |
| return rejectWithValue(error.message); |
| } |
| } |
| ); |
|
|
| export const handleLinkedInCallback = createAsyncThunk( |
| 'linkedinAccounts/handleLinkedInCallback', |
| async ({ code, state }, { rejectWithValue }) => { |
| try { |
| const response = await linkedinAuthService.handleCallback(code, state); |
| return response; |
| } catch (error) { |
| console.error('π [DEBUG] Handle callback - Error:', error); |
| return rejectWithValue(error.message); |
| } |
| } |
| ); |
|
|
| export const deleteLinkedInAccount = createAsyncThunk( |
| 'linkedinAccounts/deleteLinkedInAccount', |
| async (accountId, { rejectWithValue }) => { |
| try { |
| const response = await linkedinAuthService.deleteLinkedInAccount(accountId); |
| return { accountId, ...response }; |
| } catch (error) { |
| console.error('π [DEBUG] Delete account - Error:', error); |
| return rejectWithValue(error.message); |
| } |
| } |
| ); |
|
|
| export const setPrimaryLinkedInAccount = createAsyncThunk( |
| 'linkedinAccounts/setPrimaryLinkedInAccount', |
| async (accountId, { rejectWithValue }) => { |
| try { |
| const response = await linkedinAuthService.setPrimaryAccount(accountId); |
| return { accountId, ...response }; |
| } catch (error) { |
| return rejectWithValue(error.message); |
| } |
| } |
| ); |
|
|
| |
| const linkedinAccountsSlice = createSlice({ |
| name: 'linkedinAccounts', |
| initialState, |
| reducers: { |
| clearLinkedInError: (state) => { |
| state.error = null; |
| state.oauthError = null; |
| state.deletingError = null; |
| state.settingPrimaryError = null; |
| }, |
| resetOAuthState: (state) => { |
| state.oauthLoading = false; |
| state.oauthError = null; |
| }, |
| resetDeleteState: (state) => { |
| state.deletingAccount = null; |
| state.deletingError = null; |
| }, |
| resetPrimaryState: (state) => { |
| state.settingPrimary = null; |
| state.settingPrimaryError = null; |
| } |
| }, |
| extraReducers: (builder) => { |
| |
| builder |
| .addCase(fetchLinkedInAccounts.pending, (state) => { |
| state.loading = true; |
| state.error = null; |
| }) |
| .addCase(fetchLinkedInAccounts.fulfilled, (state, action) => { |
| state.loading = false; |
| |
| |
| if (!action.payload) { |
| state.linkedinAccounts = []; |
| return; |
| } |
| |
| |
| if (action.payload.success === false) { |
| state.error = action.payload.message; |
| state.linkedinAccounts = []; |
| return; |
| } |
| |
| |
| if (action.payload.accounts && Array.isArray(action.payload.accounts)) { |
| state.linkedinAccounts = action.payload.accounts; |
| return; |
| } |
| |
| |
| if (Array.isArray(action.payload)) { |
| state.linkedinAccounts = action.payload; |
| return; |
| } |
| |
| |
| state.linkedinAccounts = []; |
| }) |
| .addCase(fetchLinkedInAccounts.rejected, (state, action) => { |
| state.loading = false; |
| state.error = action.payload; |
| }) |
|
|
| |
| .addCase(initiateLinkedInAuth.pending, (state) => { |
| state.oauthLoading = true; |
| state.oauthError = null; |
| }) |
| .addCase(initiateLinkedInAuth.fulfilled, (state, action) => { |
| state.oauthLoading = false; |
| if (action.payload.success) { |
| |
| window.location.href = action.payload.authorization_url; |
| } else { |
| state.oauthError = action.payload.message; |
| } |
| }) |
| .addCase(initiateLinkedInAuth.rejected, (state, action) => { |
| state.oauthLoading = false; |
| state.oauthError = action.payload; |
| }) |
|
|
| |
| .addCase(handleLinkedInCallback.pending, (state) => { |
| state.oauthLoading = true; |
| state.oauthError = null; |
| }) |
| .addCase(handleLinkedInCallback.fulfilled, (state, action) => { |
| state.oauthLoading = false; |
| if (action.payload.success) { |
| |
| state.linkedinAccounts = [...state.linkedinAccounts]; |
| state.oauthError = null; |
| } else { |
| state.oauthError = action.payload.message; |
| } |
| }) |
| .addCase(handleLinkedInCallback.rejected, (state, action) => { |
| state.oauthLoading = false; |
| state.oauthError = action.payload; |
| }) |
|
|
| |
| .addCase(deleteLinkedInAccount.pending, (state, action) => { |
| state.deletingAccount = action.meta.arg; |
| state.deletingError = null; |
| }) |
| .addCase(deleteLinkedInAccount.fulfilled, (state, action) => { |
| state.deletingAccount = null; |
| if (action.payload.success) { |
| |
| state.linkedinAccounts = state.linkedinAccounts.filter( |
| account => account.id !== action.payload.accountId |
| ); |
| } else { |
| state.deletingError = action.payload.message; |
| } |
| }) |
| .addCase(deleteLinkedInAccount.rejected, (state, action) => { |
| state.deletingAccount = null; |
| state.deletingError = action.payload; |
| }) |
|
|
| |
| .addCase(setPrimaryLinkedInAccount.pending, (state, action) => { |
| state.settingPrimary = action.meta.arg; |
| state.settingPrimaryError = null; |
| }) |
| .addCase(setPrimaryLinkedInAccount.fulfilled, (state, action) => { |
| state.settingPrimary = null; |
| if (action.payload.success) { |
| |
| state.linkedinAccounts = state.linkedinAccounts.map(account => ({ |
| ...account, |
| is_primary: account.id === action.payload.accountId |
| })); |
| } else { |
| state.settingPrimaryError = action.payload.message; |
| } |
| }) |
| .addCase(setPrimaryLinkedInAccount.rejected, (state, action) => { |
| state.settingPrimary = null; |
| state.settingPrimaryError = action.payload; |
| }); |
| } |
| }); |
|
|
| export const { |
| clearLinkedInError, |
| resetOAuthState, |
| resetDeleteState, |
| resetPrimaryState |
| } = linkedinAccountsSlice.actions; |
|
|
| export default linkedinAccountsSlice.reducer; |