-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Basic CRUD for userData structure, setup ListContext
- Loading branch information
1 parent
0243169
commit 238211f
Showing
3 changed files
with
204 additions
and
37 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
editor.planx.uk/src/@planx/components/List/Public/Context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React, { createContext, ReactNode,useContext, useState } from "react"; | ||
|
||
import { generateNewItem,Schema, UserData } from "../model"; | ||
|
||
interface ListContextValue { | ||
schema: Schema; | ||
activeIndex: number | undefined; | ||
userData: UserData; | ||
addNewItem: () => void; | ||
saveItem: (index: number, updatedItem: UserData[0]) => void; | ||
removeItem: (index: number) => void; | ||
editItem: (index: number) => void; | ||
cancelEditItem: () => void; | ||
} | ||
|
||
interface ListProviderProps { | ||
children: ReactNode; | ||
schema: Schema; | ||
} | ||
|
||
const ListContext = createContext<ListContextValue | undefined>(undefined); | ||
|
||
export const ListProvider: React.FC<ListProviderProps> = ({ | ||
children, | ||
schema, | ||
}) => { | ||
const [activeIndex, setActiveIndex] = useState<number | undefined>(0); | ||
const [userData, setUserData] = useState<UserData>( | ||
schema.min === 0 ? [] : [generateNewItem(schema)], | ||
); | ||
|
||
const addNewItem = () => { | ||
setUserData([...userData, generateNewItem(schema)]); | ||
setActiveIndex((prev) => (prev === undefined ? 0 : prev + 1)); | ||
}; | ||
|
||
const saveItem = (index: number, updatedItem: UserData[0]) => { | ||
setUserData((prev) => | ||
prev.map((item, i) => (i === index ? updatedItem : item)), | ||
); | ||
}; | ||
|
||
const editItem = (index: number) => setActiveIndex(index); | ||
|
||
const removeItem = (index: number) => { | ||
if (index === activeIndex || index === 0) cancelEditItem(); | ||
setUserData((prev) => prev.filter((_, i) => i !== index)); | ||
}; | ||
|
||
const cancelEditItem = () => setActiveIndex(undefined); | ||
|
||
return ( | ||
<ListContext.Provider | ||
value={{ | ||
activeIndex, | ||
userData, | ||
addNewItem, | ||
saveItem, | ||
schema, | ||
editItem, | ||
removeItem, | ||
cancelEditItem, | ||
}} | ||
> | ||
{children} | ||
</ListContext.Provider> | ||
); | ||
}; | ||
|
||
export const useListContext = (): ListContextValue => { | ||
const context = useContext(ListContext); | ||
if (!context) { | ||
throw new Error("useListContext must be used within a ListProvider"); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters