Skip to content

Commit

Permalink
implement remote project deletion client side
Browse files Browse the repository at this point in the history
  • Loading branch information
SillyFreak committed Jun 18, 2020
1 parent 3b0b391 commit 7fabe38
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/components/projects/ProjectList/ProjectList.js
Expand Up @@ -41,6 +41,7 @@ import useDeleteProjectDialog from './useDeleteProjectDialog';
import useRenameProjectDialog from './useRenameProjectDialog';
import useProjectIndex from './useProjectIndex';
import useCreateRemoteProject from './useCreateRemoteProject';
import useDeleteRemoteProject from './useDeleteRemoteProject';

import { useAuth } from '../../users/AuthProvider';

Expand Down Expand Up @@ -131,6 +132,7 @@ function ProjectList(_props: Props) {
const intl = useIntl();

const createProjectMutation = useCreateRemoteProject();
const deleteProjectMutation = useDeleteRemoteProject();

const [
{ localProjects, remoteProjects, localToRemoteMap, remoteToLocalMap },
Expand Down Expand Up @@ -447,7 +449,10 @@ function ProjectList(_props: Props) {
aria-label={intl.formatMessage(messages.deleteExerciseTooltip, {
name: exercise.name,
})}
// onClick={() => ...}
onClick={async () => {
await deleteProjectMutation(exercise.id);
projectIndexDispatch({ type: 'REFRESH_REMOTE' });
}}
>
<DeleteIcon />
</IconButton>
Expand Down
29 changes: 29 additions & 0 deletions src/components/projects/ProjectList/useDeleteRemoteProject.js
@@ -0,0 +1,29 @@
// @flow

import gql from 'graphql-tag';

import * as hooks from '../../misc/hooks';

import { type DeleteProject, type DeleteProjectVariables } from './__generated__/DeleteProject';

const useDeleteProjectMutation = hooks.makeMutation<DeleteProject, DeleteProjectVariables>(gql`
mutation DeleteProject($projectId: ID!) {
deleteProjectById(projectId: $projectId)
}
`);

export default function useCreateRemoteProject(): string => Promise<string | null> {
const [performDeleteProject, _deleteProjectResponse] = useDeleteProjectMutation();

async function deleteProject(projectId: string) {
const result = await performDeleteProject({ variables: { projectId } });

// we're not passing `ignoreResults`, so there will be a result
// eslint-disable-next-line no-throw-literal
if (!result.data) throw 'unreachable';

return result.data.deleteProjectById ?? null;
}

return deleteProject;
}

0 comments on commit 7fabe38

Please sign in to comment.