File size: 9,711 Bytes
25f22bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import linkedinAuthService from '../../services/linkedinAuthService';
// Initial state
const initialState = {
linkedinAccounts: [],
loading: false,
error: null,
oauthLoading: false,
oauthError: null,
deletingAccount: null,
deletingError: null,
settingPrimary: null,
settingPrimaryError: null
};
// Async thunks
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 the response directly, not response.data
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);
}
}
);
// LinkedIn accounts slice
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) => {
// Fetch LinkedIn accounts
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;
// Handle different response formats robustly
if (!action.payload) {
console.warn('π [DEBUG] fetchLinkedInAccounts.fulfilled - Empty payload');
state.linkedinAccounts = [];
return;
}
// If service returns error object, handle it
if (action.payload.success === false) {
console.error('π [DEBUG] fetchLinkedInAccounts.fulfilled - Service error:', action.payload.message);
state.error = action.payload.message;
state.linkedinAccounts = [];
return;
}
// If service returns object with accounts property (expected format)
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 service returns array directly (fallback)
if (Array.isArray(action.payload)) {
console.log('π [DEBUG] fetchLinkedInAccounts.fulfilled - Using direct array format');
state.linkedinAccounts = action.payload;
return;
}
// Fallback to empty array
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;
})
// Initiate LinkedIn auth
.addCase(initiateLinkedInAuth.pending, (state) => {
state.oauthLoading = true;
state.oauthError = null;
})
.addCase(initiateLinkedInAuth.fulfilled, (state, action) => {
state.oauthLoading = false;
if (action.payload.success) {
// Redirect to LinkedIn authorization URL
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;
})
// Handle LinkedIn callback
.addCase(handleLinkedInCallback.pending, (state) => {
state.oauthLoading = true;
state.oauthError = null;
})
.addCase(handleLinkedInCallback.fulfilled, (state, action) => {
state.oauthLoading = false;
if (action.payload.success) {
// Refresh accounts list
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;
})
// Delete LinkedIn account
.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) {
// Remove account from list
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;
})
// Set primary LinkedIn account
.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) {
// Update primary account in list
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; |