From 9ee31ee1e57fa42ec6fe857b1d5e82e3f6eb6f3a Mon Sep 17 00:00:00 2001 From: Alex Stephen <1325798+rambleraptor@users.noreply.github.com> Date: Sun, 29 Dec 2024 21:23:01 -0800 Subject: [PATCH] Migrated headers over to Redux (#14) * still don't know why the headers aren't working * removed old state stuff --- src/App.tsx | 5 ---- src/app/explorer/form.tsx | 6 ++-- src/app/explorer/resource_list.tsx | 7 +++-- src/components/app-sidebar.tsx | 36 ++++++++++++++++-------- src/state/StateContext.tsx | 24 ---------------- src/state/store.ts | 44 +++++++++++++++++++++++++----- 6 files changed, 70 insertions(+), 52 deletions(-) delete mode 100644 src/state/StateContext.tsx diff --git a/src/App.tsx b/src/App.tsx index 183c749..e200c76 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -11,8 +11,6 @@ import UpdateForm from "./app/explorer/update_form"; import { selectResources, store } from './state/store'; import { Provider } from 'react-redux' import { useAppSelector } from "./hooks/store"; -import { HeadersContext } from "./state/StateContext"; -import { useState } from "react"; function createRoutes(resources: ResourceSchema[]): RouteObject[] { const routes = [{ @@ -52,12 +50,9 @@ function createRoutes(resources: ResourceSchema[]): RouteObject[] { } function App() { - const [headers, setHeaders] = useState(""); return ( - - ); } diff --git a/src/app/explorer/form.tsx b/src/app/explorer/form.tsx index ffecafc..461d3a3 100644 --- a/src/app/explorer/form.tsx +++ b/src/app/explorer/form.tsx @@ -5,7 +5,9 @@ import { useMemo, } from "react"; import { useForm } from "react-hook-form"; import { useNavigate } from "react-router-dom"; import { toast } from "@/hooks/use-toast"; -import { useHeaders } from "@/state/StateContext"; +import { useAppSelector } from "@/hooks/store"; +import { selectHeaders } from "@/state/store"; +import { ResourceSchema } from "@/state/openapi"; type CreateFormProps = { resource: ResourceSchema @@ -15,7 +17,7 @@ export default function CreateForm(props: CreateFormProps) { const form = useForm(); const navigate = useNavigate(); - const {headers, setHeaders} = useHeaders(); + const headers = useAppSelector(selectHeaders); const onSubmit = ((value) => { // Value is the properly formed JSON body. diff --git a/src/app/explorer/resource_list.tsx b/src/app/explorer/resource_list.tsx index 0c0cfb9..33aae2a 100644 --- a/src/app/explorer/resource_list.tsx +++ b/src/app/explorer/resource_list.tsx @@ -7,7 +7,8 @@ import { MoreHorizontal } from "lucide-react"; import { toast } from "@/hooks/use-toast"; import { ResourceSchema } from "@/state/openapi"; import { ResourceInstance } from "@/state/fetch"; -import { useHeaders } from "@/state/StateContext"; +import { selectHeaders } from "@/state/store"; +import { useAppSelector } from "@/hooks/store"; type ResourceListState = { resources: ResourceInstance[], @@ -57,7 +58,7 @@ export default function ResourceList(props: ResourceListProps) { function createColumns(r: ResourceSchema | undefined): object[] { if(r) { - let columns = r.properties().map((prop) => { + const columns = r.properties().map((prop) => { return {accessorKey: prop.name, header: prop.name} }); columns.push(dropDownMenuColumn); @@ -74,7 +75,7 @@ export default function ResourceList(props: ResourceListProps) { resources: [], }); - const {headers, setHeaders} = useHeaders(); + const headers = useAppSelector(selectHeaders); function deleteResource(r: object) { const result = state?.resources.find((result: ResourceInstance) => result.id === r.id); diff --git a/src/components/app-sidebar.tsx b/src/components/app-sidebar.tsx index b7005a4..262a632 100644 --- a/src/components/app-sidebar.tsx +++ b/src/components/app-sidebar.tsx @@ -3,6 +3,7 @@ import { Sidebar, SidebarContent, SidebarGroup, + SidebarGroupContent, SidebarGroupLabel, SidebarHeader, SidebarInput, @@ -12,16 +13,18 @@ import { SidebarRail, } from "@/components/ui/sidebar"; import { Link } from "react-router-dom"; -import { useAppSelector } from "@/hooks/store"; -import { selectResources } from "@/state/store"; -import { useHeaders } from "@/state/StateContext"; +import { useAppDispatch, useAppSelector } from "@/hooks/store"; +import { selectHeaders, selectResources, setHeaders } from "@/state/store"; +import { Label } from "./ui/label"; export function AppSidebar({ ...props }: React.ComponentProps) { const resources = useAppSelector(selectResources); return ( - + + + Resources @@ -37,23 +40,34 @@ export function AppSidebar({ ...props }: React.ComponentProps) { ))} - Headers - key:value, comma-deliniated - ); } -export function TextBoxComponent() { - const {headers, setHeaders} = useHeaders(); +export function HeadersInput() { + const headers = useAppSelector(selectHeaders); + const dispatch = useAppDispatch(); const handleTextChange = (event) => { - setHeaders(event.target.value); + dispatch(setHeaders(event.target.value)); }; return ( - + + + + + Headers + + + + + ); } diff --git a/src/state/StateContext.tsx b/src/state/StateContext.tsx deleted file mode 100644 index 2364647..0000000 --- a/src/state/StateContext.tsx +++ /dev/null @@ -1,24 +0,0 @@ -import { createContext, useContext } from "react"; -import { OpenAPI } from "./openapi"; - -interface StateContext { - spec: OpenAPI | null; - setSpec: ((oas: OpenAPI) => void) | null; -} - -export const StateContext = createContext({ - spec: null, - setSpec: null, -}); - -interface HeadersContext { - headers: string | null; - setHeaders: ((headers: string) => void) | null; -} - -export const HeadersContext = createContext({ - headers: null, - setHeaders: null, -}); - -export const useHeaders = () => useContext(HeadersContext); \ No newline at end of file diff --git a/src/state/store.ts b/src/state/store.ts index 8e44e88..1a7372c 100644 --- a/src/state/store.ts +++ b/src/state/store.ts @@ -2,6 +2,7 @@ import { configureStore } from '@reduxjs/toolkit' import { createSlice, PayloadAction } from '@reduxjs/toolkit' import { OpenAPI } from './openapi' +// Schema reducers + selectors. interface SchemaState { value: OpenAPI | null } @@ -23,12 +24,6 @@ const schemaSlice = createSlice({ const {setSchema} = schemaSlice.actions; const schemaReducer = schemaSlice.reducer; -const store = configureStore({ - reducer: { - schema: schemaReducer, - } -}) - export const selectResources = (state: RootState) => { if (state.schema.value != null) { return state.schema.value.resources(); @@ -37,7 +32,42 @@ export const selectResources = (state: RootState) => { } } -export {setSchema, schemaReducer, store} +// Headers reducers + selectors. +interface HeadersState { + value: string +} + +const initialHeadersState: HeadersState = { + value: "" +} + +const headersSlice = createSlice({ + name: 'headers', + initialState: initialHeadersState, + reducers: { + setHeaders: (state, action: PayloadAction) => { + state.value = action.payload + } + } +}) + +const {setHeaders} = headersSlice.actions; +const headersReducer = headersSlice.reducer; + +export const selectHeaders = (state: RootState) => state.headers.value; + + + +// Store +const store = configureStore({ + reducer: { + schema: schemaReducer, + headers: headersReducer + } +}) + + +export {setSchema, schemaReducer, store, setHeaders} // Infer the `RootState` and `AppDispatch` types from the store itself
Headers
key:value, comma-deliniated