Skip to content

Commit

Permalink
fix: added better error-handling and made more logical naming of code
Browse files Browse the repository at this point in the history
  • Loading branch information
malmen237 committed Sep 5, 2024
1 parent 9998659 commit 88a796f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/api/manager/inventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function purgeInventorySourceItem(
.collection('inventory')
.updateOne({ _id: objectId, status: 'gone' }, { $set: { status: 'purge' } })
.catch((error) => {
Log().info(`Was not able to set source-id for ${id} to purge: ${error} `);
throw `Was not able to set source-id for ${id} to purge: ${error}`;
});

return result as UpdateResult<Document>;
Expand Down
12 changes: 6 additions & 6 deletions src/components/inventory/Inventory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

import { useEffect, useState } from 'react';
import { useSources } from '../../hooks/sources/useSources';
import { useSetSourceToPerge } from '../../hooks/sources/useSetSourceToPerge';
import FilterOptions from '../../components/filter/FilterOptions';
import SourceListItem from '../../components/sourceListItem/SourceListItem';
import { SourceWithId } from '../../interfaces/Source';
import EditView from './editView/EditView';
import FilterContext from './FilterContext';
import { useDeleteSource } from '../../hooks/sources/useDeleteSource';
import styles from './Inventory.module.scss';

export default function Inventory() {
const [deleteSource, deleteComplete] = useDeleteSource();
const [removeInventorySource, reloadList] = useSetSourceToPerge();
const [updatedSource, setUpdatedSource] = useState<
SourceWithId | undefined
>();
const [sources] = useSources(deleteComplete, updatedSource);
const [sources] = useSources(reloadList, updatedSource);
const [currentSource, setCurrentSource] = useState<SourceWithId | null>();
const [filteredSources, setFilteredSources] =
useState<Map<string, SourceWithId>>(sources);
Expand All @@ -29,10 +29,10 @@ export default function Inventory() {
}, [updatedSource]);

useEffect(() => {
if (deleteComplete) {
if (reloadList) {
setCurrentSource(null);
}
}, [deleteComplete]);
}, [reloadList]);

const editSource = (source: SourceWithId) => {
setCurrentSource(() => source);
Expand Down Expand Up @@ -97,7 +97,7 @@ export default function Inventory() {
source={currentSource}
updateSource={(source) => setUpdatedSource(source)}
close={() => setCurrentSource(null)}
deleteInventorySource={(source) => deleteSource(source)}
removeInventorySource={(source) => removeInventorySource(source)}
/>
</div>
) : null}
Expand Down
6 changes: 3 additions & 3 deletions src/components/inventory/editView/EditView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export default function EditView({
source,
updateSource,
close,
deleteInventorySource
removeInventorySource
}: {
source: SourceWithId;
updateSource: (source: SourceWithId) => void;
close: () => void;
deleteInventorySource: (source: SourceWithId) => void;
removeInventorySource: (source: SourceWithId) => void;
}) {
const [loaded, setLoaded] = useState(false);
const src = useMemo(() => getSourceThumbnail(source), [source]);
Expand Down Expand Up @@ -54,7 +54,7 @@ export default function EditView({
</div>
<UpdateButtons
close={close}
deleteInventorySource={deleteInventorySource}
removeInventorySource={removeInventorySource}
source={source}
/>
</EditViewContext>
Expand Down
6 changes: 3 additions & 3 deletions src/components/inventory/editView/UpdateButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { IconTrash } from '@tabler/icons-react';

export default function UpdateButtons({
close,
deleteInventorySource,
removeInventorySource,
source
}: {
close: () => void;
deleteInventorySource: (source: SourceWithId) => void;
removeInventorySource: (source: SourceWithId) => void;
source: SourceWithId;
}) {
const t = useTranslate();
Expand All @@ -37,7 +37,7 @@ export default function UpdateButtons({
state="warning"
disabled={source.status !== 'gone'}
className="mr-5 relative flex"
onClick={() => deleteInventorySource(source)}
onClick={() => removeInventorySource(source)}
>
<IconTrash className="text-p" />
</Button>
Expand Down
36 changes: 0 additions & 36 deletions src/hooks/sources/useDeleteSource.tsx

This file was deleted.

44 changes: 44 additions & 0 deletions src/hooks/sources/useSetSourceToPerge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useState } from 'react';
import { SourceWithId } from '../../interfaces/Source';
import { CallbackHook } from '../types';
import { Log } from '../../api/logger';

export function useSetSourceToPerge(): CallbackHook<
(source: SourceWithId) => void
> {
const [reloadList, setReloadList] = useState(false);

const removeInventorySource = (source: SourceWithId) => {
if (source && source.status === 'gone') {
setReloadList(false);

fetch(`/api/manager/inventory/${source._id}`, {
method: 'PUT',
// TODO: Implement api key
headers: [['x-api-key', `Bearer apisecretkey`]]
})
.then((response) => {
if (!response.ok) {
setReloadList(true);
Log().error(
`Failed to set ${source.name} with id: ${source._id} to purge`
);
} else {
console.log(
`${source.name} with id: ${source._id} is set to purge`
);
}
setReloadList(true);
})
.catch((e) => {
Log().error(
`Failed to set ${source.name} with id: ${source._id} to purge: ${e}`
);
throw `Failed to set ${source.name} with id: ${source._id} to purge: ${e}`;
});
} else {
setReloadList(false);
}
};
return [removeInventorySource, reloadList];
}

0 comments on commit 88a796f

Please sign in to comment.