Skip to content

Commit

Permalink
fix(config-ui): missed some code for refactor redux connections
Browse files Browse the repository at this point in the history
  • Loading branch information
mintsweet committed Oct 20, 2023
1 parent 4f65bce commit 59afd9c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
18 changes: 17 additions & 1 deletion config-ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
*
*/

import { useEffect } from 'react';
import { createBrowserRouter, Navigate, RouterProvider, json } from 'react-router-dom';

import { useAppDispatch, useAppSelector } from '@/app/hook';
import { init, selectStatus } from '@/features';
import { PageLoading } from '@/components';
import {
ConnectionHomePage,
Expand Down Expand Up @@ -100,4 +103,17 @@ const router = createBrowserRouter([
},
]);

export const App = () => <RouterProvider router={router} fallbackElement={<PageLoading />} />;
export const App = () => {
const dispatch = useAppDispatch();
const status = useAppSelector(selectStatus);

useEffect(() => {
dispatch(init());
}, []);

if (['idle', 'loading'].includes(status)) {
return <PageLoading />;
}

return <RouterProvider router={router} fallbackElement={<PageLoading />} />;
};
36 changes: 34 additions & 2 deletions config-ui/src/features/connections/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import { flatten } from 'lodash';

import API from '@/api';
import type { ConnectionForm } from '@/api/connection/types';
import { RootState } from '@/app/store';
import { PluginConfig } from '@/plugins';
import { IConnection, IConnectionStatus } from '@/types';
Expand All @@ -29,8 +28,10 @@ import { transformConnection } from './utils';

const initialState: {
connections: IConnection[];
status: 'idle' | 'loading' | 'success' | 'failed';
} = {
connections: [],
status: 'idle',
};

export const init = createAsyncThunk('connections/init', async () => {
Expand Down Expand Up @@ -77,23 +78,52 @@ export const addConnection = createAsyncThunk('connections/addConnection', async
return transformConnection(plugin, connection);
});

export const updateConnection = createAsyncThunk('connections/updateConnection', async (payload: ConnectionForm) => {});
export const updateConnection = createAsyncThunk(
'connections/updateConnection',
async ({ plugin, connectionId, ...payload }: any) => {
const connection = await API.connection.update(plugin, connectionId, payload);
return transformConnection(plugin, connection);
},
);

export const removeConnection = createAsyncThunk(
'connections/removeConnection',
async ({ plugin, connectionId }: any) => {
await API.connection.remove(plugin, connectionId);
return `${plugin}-${connectionId}`;
},
);

export const slice = createSlice({
name: 'connections',
initialState,
reducers: {},
extraReducers(builder) {
builder
.addCase(init.pending, (state) => {
state.status = 'loading';
})
.addCase(init.fulfilled, (state, action) => {
state.connections = action.payload;
state.status = 'success';
})
.addCase(fetchConnections.fulfilled, (state, action) => {
state.connections = state.connections.concat(action.payload.connections);
})
.addCase(addConnection.fulfilled, (state, action) => {
state.connections.push(action.payload);
})
.addCase(updateConnection.fulfilled, (state, action) => {
state.connections = state.connections.map((cs) => {
if (cs.unique === action.payload.unique) {
return action.payload;
}
return cs;
});
})
.addCase(removeConnection.fulfilled, (state, action) => {
state.connections = state.connections.filter((cs) => cs.unique !== action.payload);
})
.addCase(testConnection.pending, (state, action) => {
const existingConnection = state.connections.find((cs) => cs.unique === action.meta.arg.unique);
if (existingConnection) {
Expand All @@ -113,6 +143,8 @@ export const {} = slice.actions;

export default slice.reducer;

export const selectStatus = (state: RootState) => state.connections.status;

export const selectAllConnections = (state: RootState) => state.connections.connections;

export const selectConnections = (state: RootState, plugin: string) =>
Expand Down
8 changes: 5 additions & 3 deletions config-ui/src/pages/connection/detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import { useParams, useNavigate, Link } from 'react-router-dom';
import { Button, Intent } from '@blueprintjs/core';

import API from '@/api';
import { useAppSelector } from '@/app/hook';
import { useAppDispatch, useAppSelector } from '@/app/hook';
import { PageHeader, Buttons, Dialog, IconButton, Table, Message, toast } from '@/components';
import { selectConnection } from '@/features';
import { selectConnection, removeConnection } from '@/features';
import { useTips, useRefreshData } from '@/hooks';
import ClearImg from '@/images/icons/clear.svg';
import {
Expand Down Expand Up @@ -64,7 +64,9 @@ export const ConnectionDetailPage = () => {
const { plugin, id } = useParams() as { plugin: string; id: string };
const connectionId = +id;

const dispatch = useAppDispatch();
const connection = useAppSelector((state) => selectConnection(state, `${plugin}-${connectionId}`)) as IConnection;

const navigate = useNavigate();
const { setTips } = useTips();
const { ready, data } = useRefreshData(
Expand Down Expand Up @@ -102,7 +104,7 @@ export const ConnectionDetailPage = () => {
const [, res] = await operator(
async () => {
try {
await API.connection.remove(plugin, connectionId);
await dispatch(removeConnection({ plugin, connectionId }));
return { status: 'success' };
} catch (err: any) {
const { status, data } = err.response;
Expand Down
5 changes: 4 additions & 1 deletion config-ui/src/plugins/components/connection-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { pick } from 'lodash';
import API from '@/api';
import { useAppDispatch, useAppSelector } from '@/app/hook';
import { ExternalLink, Buttons } from '@/components';
import { addConnection, updateConnection } from '@/features';
import { selectConnection } from '@/features/connections';
import { PluginConfig, PluginConfigType } from '@/plugins';
import { operator } from '@/utils';
Expand Down Expand Up @@ -82,7 +83,9 @@ export const ConnectionForm = ({ plugin, connectionId, onSuccess }: Props) => {
const handleSave = async () => {
const [success, res] = await operator(
() =>
!connectionId ? API.connection.create(plugin, values) : API.connection.update(plugin, connectionId, values),
!connectionId
? dispatch(addConnection({ plugin, ...values })).unwrap()
: dispatch(updateConnection({ plugin, connectionId, ...values })).unwrap(),
{
setOperating,
formatMessage: () => (!connectionId ? 'Create a New Connection Successful.' : 'Update Connection Successful.'),
Expand Down
10 changes: 1 addition & 9 deletions config-ui/src/routes/layout/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
*
*/

import { useEffect, useRef } from 'react';
import { useRef } from 'react';
import { useLoaderData, Outlet, useNavigate, useLocation } from 'react-router-dom';
import { CSSTransition } from 'react-transition-group';
import { Menu, MenuItem, Navbar, Alignment } from '@blueprintjs/core';

import { useAppDispatch } from '@/app/hook';
import { Logo, ExternalLink, IconButton } from '@/components';
import { init } from '@/features';
import { DOC_URL } from '@/release';
import { TipsContextProvider, TipsContextConsumer } from '@/store';

Expand All @@ -41,12 +39,6 @@ import './tips-transition.css';
export const Layout = () => {
const { version } = useLoaderData() as Awaited<ReturnType<typeof loader>>;

const dispatch = useAppDispatch();

useEffect(() => {
dispatch(init());
}, []);

const navigate = useNavigate();
const { pathname } = useLocation();

Expand Down

0 comments on commit 59afd9c

Please sign in to comment.