1
Fork 0

The following components are migrated to apollo hooks instead of deprecated <Query> and <Mutation> components:

- AddUserRow.js
- AlbumGallery.js
- UserRow.js

LazyPhoto is migrated to functional component
This commit is contained in:
stz184 2020-11-15 01:25:21 +02:00
parent d5e21f6a5e
commit a0f7b4b135
4 changed files with 259 additions and 275 deletions

View File

@ -1,7 +1,6 @@
import { gql } from '@apollo/client' import { gql, useMutation } from '@apollo/client'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import React, { useState } from 'react' import React, { useState } from 'react'
import { Mutation } from '@apollo/client'
import { Button, Checkbox, Input, Table } from 'semantic-ui-react' import { Button, Checkbox, Input, Table } from 'semantic-ui-react'
const createUserMutation = gql` const createUserMutation = gql`
@ -28,6 +27,12 @@ const initialState = {
const AddUserRow = ({ setShow, show, onUserAdded }) => { const AddUserRow = ({ setShow, show, onUserAdded }) => {
const [state, setState] = useState(initialState) const [state, setState] = useState(initialState)
const [createUser, { loading }] = useMutation(createUserMutation, {
onCompleted: () => {
onUserAdded()
},
})
function updateInput(event, key) { function updateInput(event, key) {
setState({ setState({
...state, ...state,
@ -40,13 +45,6 @@ const AddUserRow = ({ setShow, show, onUserAdded }) => {
} }
return ( return (
<Mutation
mutation={createUserMutation}
onCompleted={() => {
onUserAdded()
}}
>
{(createUser, { loading }) => (
<Table.Row> <Table.Row>
<Table.Cell> <Table.Cell>
<Input <Input
@ -100,8 +98,6 @@ const AddUserRow = ({ setShow, show, onUserAdded }) => {
</Button.Group> </Button.Group>
</Table.Cell> </Table.Cell>
</Table.Row> </Table.Row>
)}
</Mutation>
) )
} }

View File

@ -1,7 +1,6 @@
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import React, { useState } from 'react' import React, { useState } from 'react'
import { Mutation } from '@apollo/client/react/components' import { gql, useMutation } from '@apollo/client'
import { gql } from '@apollo/client'
import { import {
Button, Button,
Checkbox, Checkbox,
@ -61,14 +60,13 @@ const scanUserMutation = gql`
const ChangePasswordModal = ({ onClose, user, ...props }) => { const ChangePasswordModal = ({ onClose, user, ...props }) => {
const [passwordInput, setPasswordInput] = useState('') const [passwordInput, setPasswordInput] = useState('')
return ( const [changePassword] = useMutation(changeUserPasswordMutation, {
<Mutation onCompleted: () => {
mutation={changeUserPasswordMutation}
onCompleted={() => {
onClose && onClose() onClose && onClose()
}} },
> })
{changePassword => (
return (
<Modal {...props}> <Modal {...props}>
<Modal.Header>Change password</Modal.Header> <Modal.Header>Change password</Modal.Header>
<Modal.Content> <Modal.Content>
@ -103,8 +101,6 @@ const ChangePasswordModal = ({ onClose, user, ...props }) => {
</Button> </Button>
</Modal.Actions> </Modal.Actions>
</Modal> </Modal>
)}
</Mutation>
) )
} }
@ -119,7 +115,7 @@ const UserRow = ({ user, refetchUsers }) => {
editing: false, editing: false,
}) })
const [showComfirmDelete, setConfirmDelete] = useState(false) const [showConfirmDelete, setConfirmDelete] = useState(false)
const [showChangePassword, setChangePassword] = useState(false) const [showChangePassword, setChangePassword] = useState(false)
function updateInput(event, key) { function updateInput(event, key) {
@ -129,19 +125,33 @@ const UserRow = ({ user, refetchUsers }) => {
}) })
} }
if (state.editing) { const [updateUser, { loading: updateUserLoading }] = useMutation(
return ( updateUserMutation,
<Mutation {
mutation={updateUserMutation} onCompleted: data => {
onCompleted={data => {
setState({ setState({
...data.updateUser, ...data.updateUser,
editing: false, editing: false,
}) })
refetchUsers() refetchUsers()
}} },
> }
{(updateUser, { loading }) => ( )
const [deleteUser] = useMutation(deleteUserMutation, {
onCompleted: () => {
refetchUsers()
},
})
const [scanUser, { called: scanUserCalled }] = useMutation(scanUserMutation, {
onCompleted: () => {
refetchUsers()
},
})
if (state.editing) {
return (
<Table.Row> <Table.Row>
<Table.Cell> <Table.Cell>
<Input <Input
@ -184,8 +194,8 @@ const UserRow = ({ user, refetchUsers }) => {
Cancel Cancel
</Button> </Button>
<Button <Button
loading={loading} loading={updateUserLoading}
disabled={loading} disabled={updateUserLoading}
positive positive
onClick={() => onClick={() =>
updateUser({ updateUser({
@ -203,19 +213,10 @@ const UserRow = ({ user, refetchUsers }) => {
</Button.Group> </Button.Group>
</Table.Cell> </Table.Cell>
</Table.Row> </Table.Row>
)}
</Mutation>
) )
} }
return ( return (
<Mutation
mutation={deleteUserMutation}
onCompleted={() => {
refetchUsers()
}}
>
{deleteUser => (
<Table.Row> <Table.Row>
<Table.Cell>{user.username}</Table.Cell> <Table.Cell>{user.username}</Table.Cell>
<Table.Cell>{user.rootPath}</Table.Cell> <Table.Cell>{user.rootPath}</Table.Cell>
@ -232,17 +233,13 @@ const UserRow = ({ user, refetchUsers }) => {
<Icon name="edit" /> <Icon name="edit" />
Edit Edit
</Button> </Button>
<Mutation mutation={scanUserMutation}>
{(scanUser, { called }) => (
<Button <Button
disabled={called} disabled={scanUserCalled}
onClick={() => scanUser({ variables: { userId: user.id } })} onClick={() => scanUser({ variables: { userId: user.id } })}
> >
<Icon name="sync" /> <Icon name="sync" />
Scan Scan
</Button> </Button>
)}
</Mutation>
<Button onClick={() => setChangePassword(true)}> <Button onClick={() => setChangePassword(true)}>
<Icon name="key" /> <Icon name="key" />
Change password Change password
@ -261,7 +258,7 @@ const UserRow = ({ user, refetchUsers }) => {
<Icon name="delete" /> <Icon name="delete" />
Delete Delete
</Button> </Button>
<Modal open={showComfirmDelete}> <Modal open={showConfirmDelete}>
<Modal.Header>Delete user</Modal.Header> <Modal.Header>Delete user</Modal.Header>
<Modal.Content> <Modal.Content>
<p> <p>
@ -271,9 +268,7 @@ const UserRow = ({ user, refetchUsers }) => {
<p>{`This action cannot be undone`}</p> <p>{`This action cannot be undone`}</p>
</Modal.Content> </Modal.Content>
<Modal.Actions> <Modal.Actions>
<Button onClick={() => setConfirmDelete(false)}> <Button onClick={() => setConfirmDelete(false)}>Cancel</Button>
Cancel
</Button>
<Button <Button
negative negative
onClick={() => { onClick={() => {
@ -292,8 +287,6 @@ const UserRow = ({ user, refetchUsers }) => {
</Button.Group> </Button.Group>
</Table.Cell> </Table.Cell>
</Table.Row> </Table.Row>
)}
</Mutation>
) )
} }

View File

@ -56,10 +56,8 @@ const AlbumGallery = ({
useEffect(() => { useEffect(() => {
const updateImageState = event => { const updateImageState = event => {
if (event.state.imageState) {
setImageState(event.state.imageState) setImageState(event.state.imageState)
} }
}
window.addEventListener('popstate', updateImageState) window.addEventListener('popstate', updateImageState)
return () => { return () => {

View File

@ -49,19 +49,16 @@ const PhotoImg = photoProps => {
) )
} }
class LazyPhoto extends React.Component { const LazyPhoto = React.memo(
shouldComponentUpdate(nextProps) { props => {
return nextProps.src != this.props.src
}
render() {
return ( return (
<LazyLoad scrollContainer="#layout-content"> <LazyLoad scrollContainer="#layout-content">
<PhotoImg {...this.props} /> <PhotoImg {...props} />
</LazyLoad> </LazyLoad>
) )
} },
} (prevProps, nextProps) => prevProps.src === nextProps.src
)
LazyPhoto.propTypes = { LazyPhoto.propTypes = {
src: PropTypes.string, src: PropTypes.string,