Skip to content
Snippets Groups Projects
Commit ba7f1766 authored by mateusz-winiarczyk's avatar mateusz-winiarczyk
Browse files

refactor(plugins): adjust code to new changes

parent 4dbad7c8
No related branches found
No related tags found
3 merge requests!223reset the pin numbers before search results are fetch (so the results will be...,!122feat: add main plugins drawer with tabs (MIN-232),!116feat(plugins): Plugin loading management (MIN-233)
import { Button } from '@/shared/Button';
import { useState } from 'react';
import { useLoadPlugin } from '../hooks/useLoadPlugin';
export const LoadPluginFromUrl = (): JSX.Element => {
const [url, setUrl] = useState<string>('');
const handleLoadPlugin = (): void => {
// TODO: handleLoadPlugin
};
const { handleChangePluginUrl, handleLoadPlugin, isPending, pluginUrl } = useLoadPlugin();
return (
<div className="flex w-full">
......@@ -15,8 +11,8 @@ export const LoadPluginFromUrl = (): JSX.Element => {
<input
className="h-10 w-full bg-cultured p-3"
type="url"
value={url}
onChange={(e): void => setUrl(e.target.value)}
value={pluginUrl}
onChange={handleChangePluginUrl}
data-testid="load-plugin-input-url"
/>
</label>
......@@ -24,6 +20,7 @@ export const LoadPluginFromUrl = (): JSX.Element => {
variantStyles="secondary"
className="h-10 self-end rounded-e rounded-s"
onClick={handleLoadPlugin}
disabled={isPending}
>
Load
</Button>
......
import { Button } from '@/shared/Button';
import { Input } from '@/shared/Input';
import { useLoadPlugin } from './hooks/useLoadPlugin';
import { useLoadPlugin } from '../../AvailablePluginsDrawer/hooks/useLoadPlugin';
export const LoadPluginInput = (): React.ReactNode => {
const { handleChangePluginUrl, handleLoadPlugin, isPending, pluginUrl } = useLoadPlugin();
......
......@@ -6,4 +6,8 @@ export const PLUGINS_INITIAL_STATE: PluginsState = {
loading: 'idle',
error: { name: '', message: '' },
},
activePlugins: {
data: {},
pluginsId: [],
},
};
......@@ -10,14 +10,10 @@ import { apiPath } from '../apiPath';
import { PluginsState } from './plugins.types';
import pluginsReducer, { removePlugin } from './plugins.slice';
import { registerPlugin } from './plugins.thunk';
import { PLUGINS_INITIAL_STATE_MOCK } from './plugins.mock';
const mockedAxiosClient = mockNetworkResponse();
const INITIAL_STATE: PluginsState = {
data: {},
pluginsId: [],
};
describe('plugins reducer', () => {
let store = {} as ToolkitStoreWithSingleSlice<PluginsState>;
beforeEach(() => {
......@@ -27,7 +23,7 @@ describe('plugins reducer', () => {
it('should match initial state', () => {
const action = { type: 'unknown' };
expect(pluginsReducer(undefined, action)).toEqual(INITIAL_STATE);
expect(pluginsReducer(undefined, action)).toEqual(PLUGINS_INITIAL_STATE_MOCK);
});
it('should remove overlay from store properly', () => {
const { type, payload } = store.dispatch(
......@@ -53,7 +49,7 @@ describe('plugins reducer', () => {
);
expect(type).toBe('plugins/registerPlugin/fulfilled');
const { data, pluginsId } = store.getState().plugins;
const { data, pluginsId } = store.getState().plugins.activePlugins;
expect(data[pluginFixture.hash]).toEqual(pluginFixture);
expect(pluginsId).toContain(pluginFixture.hash);
......@@ -74,7 +70,7 @@ describe('plugins reducer', () => {
expect(type).toBe('plugins/registerPlugin/rejected');
expect(payload).toEqual(undefined);
const { data, pluginsId } = store.getState().plugins;
const { data, pluginsId } = store.getState().plugins.activePlugins;
expect(data).toEqual({});
......@@ -94,7 +90,7 @@ describe('plugins reducer', () => {
}),
);
const { data, pluginsId } = store.getState().plugins;
const { data, pluginsId } = store.getState().plugins.activePlugins;
expect(data).toEqual({});
expect(pluginsId).toContain(pluginFixture.hash);
......
import { createSelector } from '@reduxjs/toolkit';
import { Plugin } from '@/types/models';
import { MinervaPlugin } from '@/types/models';
import { rootSelector } from '../root/root.selectors';
export const pluginsSelector = createSelector(rootSelector, state => state.plugins);
export const activePluginsIdSelector = createSelector(pluginsSelector, state => state.pluginsId);
export const activePluginsIdSelector = createSelector(
pluginsSelector,
state => state.activePlugins.pluginsId,
);
export const pluginsDataSelector = createSelector(pluginsSelector, plugins => plugins.data);
export const pluginsDataSelector = createSelector(
pluginsSelector,
plugins => plugins.activePlugins.data,
);
export const activePluginsSelector = createSelector(
pluginsDataSelector,
activePluginsIdSelector,
(data, pluginsId) => {
const result: Plugin[] = [];
const result: MinervaPlugin[] = [];
pluginsId.forEach(pluginId => {
const element = data[pluginId];
......
......@@ -3,8 +3,9 @@ import axios from 'axios';
import { createAsyncThunk } from '@reduxjs/toolkit';
import { validateDataUsingZodSchema } from '@/utils/validateDataUsingZodSchema';
import { pluginSchema } from '@/models/pluginSchema';
import type { Plugin } from '@/types/models';
import type { MinervaPlugin } from '@/types/models';
import { axiosInstance } from '@/services/api/utils/axiosInstance';
import { z } from 'zod';
import { apiPath } from '../apiPath';
type RegisterPlugin = {
......@@ -23,7 +24,7 @@ export const registerPlugin = createAsyncThunk(
pluginName,
pluginUrl,
pluginVersion,
}: RegisterPlugin): Promise<Plugin | undefined> => {
}: RegisterPlugin): Promise<MinervaPlugin | undefined> => {
const payload = {
hash,
url: pluginUrl,
......@@ -32,7 +33,7 @@ export const registerPlugin = createAsyncThunk(
isPublic: isPublic.toString(),
} as const;
const response = await axiosInstance.post<Plugin>(
const response = await axiosInstance.post<MinervaPlugin>(
apiPath.registerPluign(),
new URLSearchParams(payload),
{
......@@ -66,7 +67,7 @@ export const getInitPlugins = createAsyncThunk<void, GetInitPluginsProps>(
async ({ pluginsId, setHashedPlugin }): Promise<void> => {
/* eslint-disable no-restricted-syntax, no-await-in-loop */
for (const pluginId of pluginsId) {
const res = await axiosInstance<Plugin>(apiPath.getPlugin(pluginId));
const res = await axiosInstance<MinervaPlugin>(apiPath.getPlugin(pluginId));
const isDataValid = validateDataUsingZodSchema(res.data, pluginSchema);
......@@ -83,3 +84,14 @@ export const getInitPlugins = createAsyncThunk<void, GetInitPluginsProps>(
}
},
);
export const getAllPlugins = createAsyncThunk(
'plugins/getAllPlugins',
async (): Promise<MinervaPlugin[]> => {
const response = await axiosInstance.get<MinervaPlugin[]>(apiPath.getAllPlugins());
const isDataValid = validateDataUsingZodSchema(response.data, z.array(pluginSchema));
return isDataValid ? response.data : [];
},
);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment