Integrated Redux for state management with auth and settings slices. (#117)

* feat: Implement DatasetFileTransfer component for file selection and management

* feat: Add pagination support to file list in Overview component

* feat: add DatasetFileTransfer and TagManagement components

- Added DatasetFileTransfer component for managing dataset files.
- Introduced TagManagement component for handling tags.
- Integrated Redux for state management with auth and settings slices.
- Updated package.json to include @reduxjs/toolkit and react-redux dependencies.
- Refactored existing components to utilize new DatasetFileTransfer and TagManagement components.
- Implemented hooks for typed dispatch and selector in Redux.
- Enhanced CreateKnowledgeBase and SynthesisTask components to support new features.
This commit is contained in:
chenghh-9609
2025-11-29 17:37:36 +08:00
committed by GitHub
parent 2e13bb9b4c
commit 5c178d5274
16 changed files with 305 additions and 22 deletions

View File

@@ -0,0 +1,6 @@
import { useDispatch, useSelector, TypedUseSelectorHook } from 'react-redux';
import type { RootState, AppDispatch } from './index';
// 类型化的 hooks
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

View File

@@ -0,0 +1,15 @@
import { configureStore } from "@reduxjs/toolkit";
import authSlice from "./slices/authSlice";
import settingsSlice from "./slices/settingsSlice";
// 创建 Store
export const store = configureStore({
reducer: {
auth: authSlice,
settings: settingsSlice,
},
});
// 导出类型
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

View File

@@ -0,0 +1,75 @@
// store/slices/authSlice.js
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
// 异步 thunk
export const loginUser = createAsyncThunk(
'auth/login',
async (credentials, { rejectWithValue }) => {
try {
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(credentials),
});
if (!response.ok) {
throw new Error('Login failed');
}
const data = await response.json();
return data;
} catch (error) {
return rejectWithValue(error.message);
}
}
);
const authSlice = createSlice({
name: 'auth',
initialState: {
user: null,
token: localStorage.getItem('token'),
isAuthenticated: false,
loading: false,
error: null,
},
reducers: {
logout: (state) => {
state.user = null;
state.token = null;
state.isAuthenticated = false;
localStorage.removeItem('token');
},
clearError: (state) => {
state.error = null;
},
setToken: (state, action) => {
state.token = action.payload;
localStorage.setItem('token', action.payload);
},
},
extraReducers: (builder) => {
builder
.addCase(loginUser.pending, (state) => {
state.loading = true;
state.error = null;
})
.addCase(loginUser.fulfilled, (state, action) => {
state.loading = false;
state.user = action.payload.user;
state.token = action.payload.token;
state.isAuthenticated = true;
localStorage.setItem('token', action.payload.token);
})
.addCase(loginUser.rejected, (state, action) => {
state.loading = false;
state.error = action.payload;
state.isAuthenticated = false;
});
},
});
export const { logout, clearError, setToken } = authSlice.actions;
export default authSlice.reducer;

View File

@@ -0,0 +1,23 @@
// Settings Slice
import { createSlice } from '@reduxjs/toolkit';
const settingsSlice = createSlice({
name: "settings",
initialState: {
visible: false,
},
reducers: {
showSettings: (state) => {
state.visible = true;
},
hideSettings: (state) => {
state.visible = false;
},
toggleSettings: (state) => {
state.visible = !state.visible;
},
},
});
export const { showSettings, hideSettings, toggleSettings } = settingsSlice.actions;
export default settingsSlice.reducer;