Skip to content

Commit

Permalink
Add delete user button
Browse files Browse the repository at this point in the history
  • Loading branch information
felmez committed Oct 21, 2021
1 parent 91fdf3b commit 655061b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
8 changes: 8 additions & 0 deletions client/src/components/CustomPopup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react'
import { Popup } from "semantic-ui-react";

export default function CustomPopup({ content, children }) {
return (
<Popup inverted content={content} trigger={children} />
)
}
52 changes: 52 additions & 0 deletions client/src/components/DeleteButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useState } from 'react'
import { Button, Confirm } from 'semantic-ui-react'
import { gql, useMutation } from '@apollo/client';

import { FETCH_USERS_QUERY } from '../util/GraphQL';
import CustomPopup from './CustomPopup'

const DELETE_USER_MUTATION = gql`
mutation deleteUser($userID: ID!) {
deleteUser(userID: $userID)
}
`

export default function DeleteButton({ userID, callback }) {
const [confirmOpen, setConfirmOpen] = useState(false);

const mutation = DELETE_USER_MUTATION;

const [deleteUser] = useMutation(mutation, {
update(proxy) {
setConfirmOpen(false);
const data = proxy.readQuery({
query: FETCH_USERS_QUERY
});
proxy.writeQuery({
query: FETCH_USERS_QUERY,
data: {
getUsers: data.getUsers.filter((a) => a.id !== userID)
},
});
if (callback) callback();
},
variables: {
userID
}
});

return (
<>
<CustomPopup content={'Delete User'}>
<Button circular icon="close" color="red" floated="right" onClick={() => setConfirmOpen(true)}>
</Button>
</CustomPopup>
<Confirm
open={confirmOpen}
onCancel={() => setConfirmOpen(false)}
onConfirm={deleteUser}
/>
</>
);
}

5 changes: 5 additions & 0 deletions client/src/components/UserTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Table } from 'semantic-ui-react'
import moment from "moment";
import trLocale from "moment/locale/tr";

import DeleteButton from '../components/DeleteButton';

export default function UserTable({ user: { id, name, email, role, createdAt } }) {
moment.locale('tr', [trLocale])

Expand All @@ -14,6 +16,9 @@ export default function UserTable({ user: { id, name, email, role, createdAt } }
<Table.Cell>{email}</Table.Cell>
<Table.Cell>{role}</Table.Cell>
<Table.Cell>{moment(createdAt).fromNow()}</Table.Cell>
<Table.Cell>
<DeleteButton userID={id} />
</Table.Cell>
</Table.Row>
</Table.Body>
)
Expand Down
3 changes: 2 additions & 1 deletion client/src/pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function Home() {
const { loading, data: { getUsers: users } = {} } = useQuery(FETCH_USERS_QUERY);

return (
<Grid columns={4} >
<Grid columns={6} >
<Grid.Column>
<UserForm></UserForm>
</Grid.Column>
Expand All @@ -26,6 +26,7 @@ export default function Home() {
<Table.HeaderCell>E-mail</Table.HeaderCell>
<Table.HeaderCell>Kayıt Tipi</Table.HeaderCell>
<Table.HeaderCell>Kayıt Tarihi</Table.HeaderCell>
<Table.HeaderCell>İşlemler</Table.HeaderCell>
</Table.Row>
</Table.Header>
{
Expand Down

0 comments on commit 655061b

Please sign in to comment.