To add a Remove button at the end of each filename in your Library component, you'll need to perform the following steps:
-
Extend Your Search Context:
- Ensure that your search context (
searchcontext) provides a method to remove individual items from the library. If it doesn't already exist, you'll need to add a removeFromLibrary function.
-
Update the Library Component:
- Incorporate the
removeFromLibrary function from the context.
- Add a Remove button next to each filename that, when clicked, removes the corresponding item from the library.
Below are the detailed steps and code modifications to achieve this.
1. Update Your Search Context
First, ensure that your search context has the ability to remove individual items from the library. Here's how you can modify your context:
// ../context/searchcontext.js
import React, { createContext, useContext, useState } from 'react';
const SearchContext = createContext();
export const useSearchContext = () => useContext(SearchContext);
export const SearchProvider = ({ children }) => {
const [library, setLibrary] = useState([]);
const addToLibrary = (item) => {
setLibrary((prevLibrary) => [...prevLibrary, item]);
};
const clearLibrary = () => {
setLibrary([]);
};
// **New Function to Remove Individual Items**
const removeFromLibrary = (index) => {
setLibrary((prevLibrary) => prevLibrary.filter((_, i) => i !== index));
};
return (
<SearchContext.Provider value={{ library, addToLibrary, clearLibrary, removeFromLibrary }}>
{children}
</SearchContext.Provider>
);
};
Explanation:
-
removeFromLibrary Function: This function takes an index as an argument and filters out the item at that specific index from the library array.
-
Providing the Function: The removeFromLibrary function is added to the context's value so that it can be accessed by any component that consumes the context.
2. Modify the Library Component
Now, update your Library component to include the Remove button for each library item.
// ../components/Library.js
import React from 'react';
import { useSearchContext } from '../context/searchcontext';
import Navbar from '../components/Navbar';
import Footer from '../components/Footer';
const Library = () => {
const { library, clearLibrary, removeFromLibrary } = useSearchContext();
const getRelativePath = (fullPath) => {
const basePath = '\\\\192.168.13.30\\c$\\GGN Office Repository\\';
if (fullPath.startsWith(basePath)) {
return fullPath.slice(basePath.length).replace(/\\/g, '/');
}
return fullPath.replace(/\\/g, '/');
};
return (
<div className="app-container">
<Navbar />
<main>
<h2>Your Library</h2>
{library.length === 0 ? (
<p>Your library is empty.</p>
) : (
<div className="scrollable-container">
<button onClick={clearLibrary} style={{ marginBottom: '10px' }}>
Clear Library
</button>
<ul style={{ listStyleType: 'none', padding: 0 }}>
{library.map((item, index) => {
const relativePath = getRelativePath(item.Filename);
const encodedPath = encodeURI(relativePath);
return (
<li
key={index}
style={{
margin: '5px 0',
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
}}
>
<a
href={`http://192.168.7.175:3005/files/${encodedPath}`}
target="_blank"
rel="noopener noreferrer"
style={{ color: 'blue', textDecoration: 'underline', flex: 1 }}
>
{relativePath}
</a>
{/* **Remove Button** */}
<button
onClick={() => removeFromLibrary(index)}
style={{
marginLeft: '10px',
padding: '5px 10px',
backgroundColor: '#ff4d4f',
color: '#fff',
border: 'none',
borderRadius: '4px',
cursor: 'pointer',
}}
>
Remove
</button>
</li>
);
})}
</ul>
</div>
)}
</main>
<Footer />
</div>
);
};
export default Library;
Explanation of Changes:
-
Destructure removeFromLibrary from Context:
const { library, clearLibrary, removeFromLibrary } = useSearchContext();
-
Add the Remove Button:
- Inside the
map function where each library item is rendered, add a Remove button next to the filename link.
<button
onClick={() => removeFromLibrary(index)}
style={{
marginLeft: '10px',
padding: '5px 10px',
backgroundColor: '#ff4d4f',
color: '#fff',
border: 'none',
borderRadius: '4px',
cursor: 'pointer',
}}
>
Remove
</button>
- Styling: The button is styled to have a red background to indicate a destructive action (removal). You can adjust the styles as per your application's design requirements.
-
Layout Adjustments:
- To ensure the Remove button aligns properly next to the filename, the
<li> element is styled with display: 'flex' and justifyContent: 'space-between'. This ensures that the filename link and the Remove button are spaced appropriately.
<li
key={index}
style={{
margin: '5px 0',
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
}}
>
{/* Filename Link */}
{/* Remove Button */}
</li>
3. Final Thoughts
With these changes:
-
Removing an Item: Clicking the Remove button next to a filename will invoke the removeFromLibrary function from the context, effectively removing that specific item from the library.
-
Clearing the Library: The existing Clear Library button will still function as intended, removing all items from the library.
Ensure that your context is correctly wrapped around your application (typically in a higher-level component like App.js) so that the Library component has access to the context functions.
Example Context Provider Setup:
// ../App.js
import React from 'react';
import { SearchProvider } from '../context/searchcontext';
import Library from '../components/Library';
function App() {
return (
<SearchProvider>
<Library />
{/* Other components */}
</SearchProvider>
);
}
export default App;
This setup ensures that all components within SearchProvider have access to the search context, including the newly added removeFromLibrary function.
Feel free to adjust the styling and functionality as per your application's specific requirements. If you encounter any issues or have further questions, don't hesitate to ask!