Skip to content

Commit

Permalink
Migrated headers over to Redux (#14)
Browse files Browse the repository at this point in the history
* still don't know why the headers aren't working

* removed old state stuff
  • Loading branch information
rambleraptor authored Dec 30, 2024
1 parent 0111890 commit 9ee31ee
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 52 deletions.
5 changes: 0 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [{
Expand Down Expand Up @@ -52,12 +50,9 @@ function createRoutes(resources: ResourceSchema[]): RouteObject[] {
}

function App() {
const [headers, setHeaders] = useState("");
return (
<Provider store={store} >
<HeadersContext.Provider value={{ headers: headers, setHeaders: setHeaders}}>
<Routes />
</HeadersContext.Provider>
</Provider>
);
}
Expand Down
6 changes: 4 additions & 2 deletions src/app/explorer/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions src/app/explorer/resource_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
36 changes: 25 additions & 11 deletions src/components/app-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarHeader,
SidebarInput,
Expand All @@ -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<typeof Sidebar>) {
const resources = useAppSelector(selectResources);

return (
<Sidebar collapsible="icon" {...props}>
<SidebarHeader></SidebarHeader>
<SidebarHeader>
<HeadersInput />
</SidebarHeader>
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel>Resources</SidebarGroupLabel>
Expand All @@ -37,23 +40,34 @@ export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
))}
</SidebarMenu>
</SidebarGroup>
<p>Headers</p>
<p>key:value, comma-deliniated</p>
<TextBoxComponent />
</SidebarContent>
<SidebarRail />
</Sidebar>
);
}

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 (
<SidebarInput type="text" value={headers!} onChange={handleTextChange} />
<form>
<SidebarGroup className="py-0">
<SidebarGroupContent className="relative">
<Label htmlFor="headers" className="sr-only">
Headers
</Label>
<SidebarInput id="headers"
placeholder="Headers - Key:value, comma-delineated"
type="text"
value={headers!}
onChange={handleTextChange} />
</SidebarGroupContent>
</SidebarGroup>
</form>
);
}
24 changes: 0 additions & 24 deletions src/state/StateContext.tsx

This file was deleted.

44 changes: 37 additions & 7 deletions src/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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();
Expand All @@ -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<string>) => {
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
Expand Down

0 comments on commit 9ee31ee

Please sign in to comment.