-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
927fdae
commit f91e497
Showing
2 changed files
with
9 additions
and
14 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,28 @@ | ||
import { ReactNode, createContext, useEffect, useState } from "react"; | ||
import { ShopItemSchemaType } from "../../../../shared/schemas/shop-item.schema"; | ||
import CyclicArray from "../../../../shared/types/ds/CyclicArray.ds"; | ||
import responseHandler from "../../apis/responseHandler"; | ||
import RestAPI from "../../apis/server.endpoints"; | ||
|
||
type ShopDataCtxProperties = { | ||
data: CyclicArray<ShopItemSchemaType> | null; | ||
items: CyclicArray<ShopItemSchemaType> | null; | ||
}; | ||
|
||
export const ShopDataCtx = createContext<ShopDataCtxProperties | null>(null); | ||
|
||
/* | ||
NOTE: Write small note about implementing a reducer to manage sections | ||
*/ | ||
|
||
export const ShopDataProvider = ({ children }: { children?: ReactNode }) => { | ||
const [data, setData] = useState<CyclicArray<ShopItemSchemaType> | null>( | ||
const [items, setItems] = useState<CyclicArray<ShopItemSchemaType> | null>( | ||
null | ||
); | ||
|
||
useEffect(() => { | ||
RestAPI.getShopItems().then((res: any) => { | ||
setData(new CyclicArray(...res)); | ||
}); | ||
responseHandler(() => RestAPI.getShopItems(), async (res: Response) => { | ||
setItems(new CyclicArray(...(await res.json()).data)) | ||
}) | ||
|
||
}, []); | ||
|
||
return ( | ||
<ShopDataCtx.Provider value={{ data }}>{children}</ShopDataCtx.Provider> | ||
<ShopDataCtx.Provider value={{ items }}>{children}</ShopDataCtx.Provider> | ||
); | ||
}; |