Skip to content

Commit

Permalink
fix: simplified the delete-source-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
malmen237 committed Aug 30, 2024
1 parent eb19449 commit b14a6f7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
12 changes: 6 additions & 6 deletions src/components/inventory/Inventory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { useDeleteSource } from '../../hooks/sources/useDeleteSource';
import styles from './Inventory.module.scss';

export default function Inventory() {
const [itemToDelete, setItemToDelete] = useState<SourceWithId | null>(null);
const [loading, deleteComplete] = useDeleteSource(itemToDelete);
const [deleteSource, deleteComplete] = useDeleteSource();
const [updatedSource, setUpdatedSource] = useState<
SourceWithId | undefined
>();
Expand All @@ -31,7 +30,6 @@ export default function Inventory() {

useEffect(() => {
if (deleteComplete) {
setItemToDelete(null);
setCurrentSource(null);
}
}, [deleteComplete]);
Expand Down Expand Up @@ -86,18 +84,20 @@ export default function Inventory() {
<ul
className={`flex flex-col border-t border-gray-600 overflow-scroll h-[95%] ${styles.no_scrollbar}`}
>
{loading ? '' : getSourcesToDisplay(filteredSources)}
{getSourcesToDisplay(filteredSources)}
</ul>
</div>
</div>

{currentSource ? (
<div className={`p-3 ml-2 mt-2 bg-container rounded h-[60%] min-w-max`}>
<div
className={`p-3 ml-2 mt-2 bg-container rounded h-[60%] min-w-max`}
>
<EditView
source={currentSource}
updateSource={(source) => setUpdatedSource(source)}
close={() => setCurrentSource(null)}
deleteInventorySource={(source) => setItemToDelete(source)}
deleteInventorySource={(source) => deleteSource(source)}
/>
</div>
) : null}
Expand Down
18 changes: 8 additions & 10 deletions src/hooks/sources/useDeleteSource.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useEffect, useState } from 'react';
import { useState } from 'react';
import { SourceWithId } from '../../interfaces/Source';
import { CallbackHook } from '../types';

export function useDeleteSource(source: SourceWithId | null) {
const [loading, setLoading] = useState(true);
export function useDeleteSource(): CallbackHook<
(source: SourceWithId) => void
> {
const [deleteComplete, setDeleteComplete] = useState(false);

useEffect(() => {
const setSourceToPurge = (source: SourceWithId) => {
if (source && source.status === 'gone') {
setLoading(true);
setDeleteComplete(false);

fetch(`/api/manager/inventory/${source._id}`, {
Expand All @@ -17,22 +18,19 @@ export function useDeleteSource(source: SourceWithId | null) {
})
.then((response) => {
if (!response.ok) {
setLoading(false);
setDeleteComplete(true);
return response.text().then((message) => {
throw new Error(`Error ${response.status}: ${message}`);
});
}
setLoading(false);
setDeleteComplete(true);
})
.catch((e) => {
console.log(`Failed to delete source-item: ${e}`);
});
} else {
setLoading(false);
setDeleteComplete(false);
}
}, [source]);
return [loading, deleteComplete];
};
return [setSourceToPurge, deleteComplete];
}

0 comments on commit b14a6f7

Please sign in to comment.