|
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(); |
|
console.log('π [DEBUG] Async thunk - Service response:', response); |
|
console.log('π [DEBUG] Async thunk - Service response type:', typeof response); |
|
console.log('π [DEBUG] Async thunk - Service response is array:', Array.isArray(response)); |
|
|
|
|
|
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(); |
|
console.log('π [DEBUG] Initiate auth - Service response:', response); |
|
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); |
|
console.log('π [DEBUG] Handle callback - Service response:', response); |
|
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); |
|
console.log('π [DEBUG] Delete account - Service response:', response); |
|
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); |
|
console.log('π [DEBUG] Set primary - Service response:', response); |
|
return { accountId, ...response }; |
|
} catch (error) { |
|
console.error('π [DEBUG] Set primary - Error:', 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) => { |
|
console.log('π [DEBUG] fetchLinkedInAccounts.pending - State:', state); |
|
state.loading = true; |
|
state.error = null; |
|
}) |
|
.addCase(fetchLinkedInAccounts.fulfilled, (state, action) => { |
|
console.log('π [DEBUG] fetchLinkedInAccounts.fulfilled - Action:', action); |
|
console.log('π [DEBUG] fetchLinkedInAccounts.fulfilled - Action payload:', action.payload); |
|
console.log('π [DEBUG] fetchLinkedInAccounts.fulfilled - Action payload type:', typeof action.payload); |
|
console.log('π [DEBUG] fetchLinkedInAccounts.fulfilled - Action payload is array:', Array.isArray(action.payload)); |
|
console.log('π [DEBUG] fetchLinkedInAccounts.fulfilled - State before update:', state); |
|
|
|
state.loading = false; |
|
|
|
|
|
if (!action.payload) { |
|
console.warn('π [DEBUG] fetchLinkedInAccounts.fulfilled - Empty payload'); |
|
state.linkedinAccounts = []; |
|
return; |
|
} |
|
|
|
|
|
if (action.payload.success === false) { |
|
console.error('π [DEBUG] fetchLinkedInAccounts.fulfilled - Service error:', action.payload.message); |
|
state.error = action.payload.message; |
|
state.linkedinAccounts = []; |
|
return; |
|
} |
|
|
|
|
|
if (action.payload.accounts && Array.isArray(action.payload.accounts)) { |
|
console.log('π [DEBUG] fetchLinkedInAccounts.fulfilled - Using accounts property format'); |
|
state.linkedinAccounts = action.payload.accounts; |
|
return; |
|
} |
|
|
|
|
|
if (Array.isArray(action.payload)) { |
|
console.log('π [DEBUG] fetchLinkedInAccounts.fulfilled - Using direct array format'); |
|
state.linkedinAccounts = action.payload; |
|
return; |
|
} |
|
|
|
|
|
console.warn('π [DEBUG] fetchLinkedInAccounts.fulfilled - Unexpected payload format, using empty array'); |
|
state.linkedinAccounts = []; |
|
|
|
console.log('π [DEBUG] fetchLinkedInAccounts.fulfilled - State after update:', state); |
|
}) |
|
.addCase(fetchLinkedInAccounts.rejected, (state, action) => { |
|
console.log('π [DEBUG] fetchLinkedInAccounts.rejected - Action:', action); |
|
console.log('π [DEBUG] fetchLinkedInAccounts.rejected - Error:', action.payload); |
|
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; |