52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
// src/store/slices/authModalSlice.js
|
||
// 认证弹窗状态管理 Redux Slice - 从 AuthModalContext 迁移
|
||
|
||
import { createSlice } from '@reduxjs/toolkit';
|
||
|
||
/**
|
||
* AuthModal Slice
|
||
* 管理统一的认证弹窗状态(登录/注册合并)
|
||
*/
|
||
const authModalSlice = createSlice({
|
||
name: 'authModal',
|
||
initialState: {
|
||
isOpen: false, // 弹窗开关状态
|
||
redirectUrl: null, // 认证成功后的重定向URL(可选)
|
||
},
|
||
reducers: {
|
||
/**
|
||
* 打开认证弹窗
|
||
* @param {object} action.payload - { redirectUrl?: string }
|
||
*/
|
||
openModal: (state, action) => {
|
||
state.isOpen = true;
|
||
state.redirectUrl = action.payload?.redirectUrl || null;
|
||
},
|
||
|
||
/**
|
||
* 关闭认证弹窗
|
||
*/
|
||
closeModal: (state) => {
|
||
state.isOpen = false;
|
||
state.redirectUrl = null;
|
||
},
|
||
|
||
/**
|
||
* 设置重定向URL(不打开弹窗)
|
||
*/
|
||
setRedirectUrl: (state, action) => {
|
||
state.redirectUrl = action.payload;
|
||
},
|
||
},
|
||
});
|
||
|
||
// 导出 actions
|
||
export const { openModal, closeModal, setRedirectUrl } = authModalSlice.actions;
|
||
|
||
// 导出 selectors
|
||
export const selectAuthModalOpen = (state) => state.authModal.isOpen;
|
||
export const selectRedirectUrl = (state) => state.authModal.redirectUrl;
|
||
|
||
// 导出 reducer
|
||
export default authModalSlice.reducer;
|