Skip to content

Commit

Permalink
Use deleteDraftProject GQL mutation in admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
rmunn committed May 13, 2024
1 parent 7489e09 commit 35d2338
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
29 changes: 28 additions & 1 deletion frontend/src/lib/gql/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { $OpResult, DeleteUserByAdminOrSelfInput, DeleteUserByAdminOrSelfMutation, SoftDeleteProjectMutation } from './types';
import type { $OpResult, DeleteUserByAdminOrSelfInput, DeleteUserByAdminOrSelfMutation, SoftDeleteProjectMutation, DeleteDraftProjectMutation } from './types';

import { getClient } from './gql-client';
import { graphql } from './generated';
Expand Down Expand Up @@ -55,3 +55,30 @@ export async function _deleteProject(projectId: string): $OpResult<SoftDeletePro

return result;
}

export async function _deleteDraftProject(draftProjectId: string): $OpResult<DeleteDraftProjectMutation> {
//language=GraphQL
const result = await getClient()
.mutation(
graphql(`
mutation DeleteDraftProject($input: DeleteDraftProjectInput!) {
deleteDraftProject(input: $input) {
draftProject {
id,
}
errors {
... on Error {
message
}
}
}
}
`),
{
input: { draftProjectId }
},
{ additionalTypenames: ['Projects'] },
);

return result;
}
22 changes: 19 additions & 3 deletions frontend/src/routes/(authenticated)/admin/AdminProjects.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import {DialogResponse} from '$lib/components/modals';
import ConfirmDeleteModal from '$lib/components/modals/ConfirmDeleteModal.svelte';
import {Button} from '$lib/forms';
import {_deleteProject} from '$lib/gql/mutations';
import {_deleteProject, _deleteDraftProject} from '$lib/gql/mutations';
import t, {number} from '$lib/i18n';
import {TrashIcon} from '$lib/icons';
import {useNotifications} from '$lib/notify';
Expand Down Expand Up @@ -59,7 +59,7 @@
$: shownProjects = limitResults ? limit(filteredProjects, lastLoadUsedActiveFilter ? DEFAULT_PAGE_SIZE : 10) : filteredProjects;
let deleteProjectModal: ConfirmDeleteModal;
async function softDeleteProject(project: ProjectItemWithDraftStatus): Promise<void> {
async function softDeleteProject(project: ProjectItem): Promise<void> {
const result = await deleteProjectModal.open(project.name, async () => {
const { error } = await _deleteProject(project.id);
return error?.message;
Expand All @@ -68,6 +68,22 @@
notifyWarning($t('delete_project_modal.success', { name: project.name, code: project.code }));
}
}
async function deleteDraftProject(project: DraftProject): Promise<void> {
const result = await deleteProjectModal.open(project.name, async () => {
const { error } = await _deleteDraftProject(project.id);
return error?.message;
});
if (result.response === DialogResponse.Submit) {
notifyWarning($t('delete_project_modal.success', { name: project.name, code: project.code }));
}
}
function deleteProjectOrDraft(project: ProjectItemWithDraftStatus): Promise<void> {
if (project.isDraft) {
return deleteDraftProject(project)
} else {
return softDeleteProject(project);
}
}
</script>

<ConfirmDeleteModal bind:this={deleteProjectModal} i18nScope="delete_project_modal" />
Expand Down Expand Up @@ -117,7 +133,7 @@
</button>
<ul slot="content" class="menu">
<li>
<button class="text-error whitespace-nowrap" on:click={() => softDeleteProject(project)}>
<button class="text-error whitespace-nowrap" on:click={() => deleteProjectOrDraft(project)}>
<TrashIcon />
{$t('delete_project_modal.submit')}
</button>
Expand Down

0 comments on commit 35d2338

Please sign in to comment.