-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNFTContext.tsx
51 lines (41 loc) · 1.28 KB
/
NFTContext.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// src/components/Contexts/NFTContext.tsx
import React, { createContext, useState, ReactNode } from 'react';
export interface NFTContent {
id: number;
file: File;
}
export interface NFTContextType {
nfts: NFTContent[];
addNFT: (nft: NFTContent) => void;
updateNFTs: (nfts: NFTContent[]) => void;
removeNFT: (id: number) => void;
removeAllNFTs: () => void;
}
export const NFTContext = createContext<NFTContextType | undefined>(undefined);
interface ProviderProps {
children: ReactNode;
}
export const NFTProvider: React.FC<ProviderProps> = ({ children }) => {
const [nfts, setNfts] = useState<NFTContent[]>([]);
// Adds a new NFT to the list
const addNFT = (nft: NFTContent) => {
setNfts((prevNfts) => [...prevNfts, nft]);
};
// Updates the entire NFT list
const updateNFTs = (nfts: NFTContent[]) => {
setNfts(nfts);
};
// Removes an NFT by ID
const removeNFT = (id: number) => {
setNfts((prevNfts) => prevNfts.filter((nft) => nft.id !== id));
};
// Removes all NFTs
const removeAllNFTs = () => {
setNfts([]);
};
return (
<NFTContext.Provider value={{ nfts, addNFT, updateNFTs, removeNFT, removeAllNFTs }}>
{children}
</NFTContext.Provider>
);
};