forked from OwnZones/orchestration-gui
-
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.
feat: first commit of source-delete, with basic styling and fetches
- Loading branch information
Showing
6 changed files
with
135 additions
and
3 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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { isAuthenticated } from '../../../../../api/manager/auth'; | ||
import { Params } from 'next/dist/shared/lib/router/utils/route-matcher'; | ||
import { deleteInventorySourceItem } from '../../../../../api/manager/inventory'; | ||
|
||
export async function DELETE( | ||
request: NextRequest, | ||
{ params }: { params: Params } | ||
): Promise<NextResponse> { | ||
if (!(await isAuthenticated())) { | ||
return new NextResponse(`Not Authorized!`, { | ||
status: 403 | ||
}); | ||
} | ||
|
||
try { | ||
await deleteInventorySourceItem(params._id); | ||
return new NextResponse(null, { | ||
status: 200 | ||
}); | ||
} catch (error) { | ||
return new NextResponse( | ||
`Error occurred while posting to DB! Error: ${error}`, | ||
{ | ||
status: 500 | ||
} | ||
); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { SourceWithId } from '../../interfaces/Source'; | ||
|
||
export function useDeleteSource(source: SourceWithId | null) { | ||
const [loading, setLoading] = useState(true); | ||
const [deleteComplete, setDeleteComplete] = useState(false); | ||
|
||
useEffect(() => { | ||
if (source && source.status === 'gone') { | ||
setLoading(true); | ||
setDeleteComplete(false); | ||
// Source to be deleted: | ||
console.log('source._id', source); | ||
fetch(`/api/manager/inventory/${source._id}`, { | ||
method: 'DELETE', | ||
// TODO: Implement api key | ||
headers: [['x-api-key', `Bearer apisecretkey`]] | ||
}) | ||
.then((response) => { | ||
if (response.ok) { | ||
setLoading(false); | ||
setDeleteComplete(true); | ||
} | ||
}) | ||
.catch((e) => { | ||
console.log(`Failed to delete source-item: ${e}`); | ||
}); | ||
} else { | ||
setLoading(false); | ||
setDeleteComplete(false); | ||
} | ||
}, [source]); | ||
return [loading, deleteComplete]; | ||
} |