Hook up edit user flow
This commit is contained in:
@@ -35,6 +35,7 @@ export const schema = gql`
|
|||||||
type Mutation {
|
type Mutation {
|
||||||
createUser(input: CreateUserInput!): User!
|
createUser(input: CreateUserInput!): User!
|
||||||
updateUser(id: String!, input: UpdateUserInput!): User!
|
updateUser(id: String!, input: UpdateUserInput!): User!
|
||||||
|
updateUserByUserName(userName: String!, input: UpdateUserInput!): User!
|
||||||
deleteUser(id: String!): User!
|
deleteUser(id: String!): User!
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|||||||
@@ -29,6 +29,13 @@ export const updateUser = ({ id, input }) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const updateUserByUserName = ({ userName, input }) => {
|
||||||
|
return db.user.update({
|
||||||
|
data: input,
|
||||||
|
where: { userName },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export const deleteUser = ({ id }) => {
|
export const deleteUser = ({ id }) => {
|
||||||
return db.user.delete({
|
return db.user.delete({
|
||||||
where: { id },
|
where: { id },
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { useMutation, useFlash } from '@redwoodjs/web'
|
||||||
|
import { navigate, routes } from '@redwoodjs/router'
|
||||||
import UserProfile from 'src/components/UserProfile'
|
import UserProfile from 'src/components/UserProfile'
|
||||||
|
|
||||||
export const QUERY = gql`
|
export const QUERY = gql`
|
||||||
@@ -14,6 +16,15 @@ export const QUERY = gql`
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const UPDATE_USER_MUTATION = gql`
|
||||||
|
mutation UpdateUserMutation($userName: String!, $input: UpdateUserInput!) {
|
||||||
|
updateUserByUserName(userName: $userName, input: $input) {
|
||||||
|
id
|
||||||
|
userName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
export const Loading = () => <div>Loading...</div>
|
export const Loading = () => <div>Loading...</div>
|
||||||
|
|
||||||
export const Empty = () => <div>Empty</div>
|
export const Empty = () => <div>Empty</div>
|
||||||
@@ -21,5 +32,23 @@ export const Empty = () => <div>Empty</div>
|
|||||||
export const Failure = ({ error }) => <div>Error: {error.message}</div>
|
export const Failure = ({ error }) => <div>Error: {error.message}</div>
|
||||||
|
|
||||||
export const Success = ({ user }) => {
|
export const Success = ({ user }) => {
|
||||||
return <UserProfile user={user} isEditable />
|
const { addMessage } = useFlash()
|
||||||
|
const [updateUser, { loading, error }] = useMutation(UPDATE_USER_MUTATION, {
|
||||||
|
onCompleted: ({updateUserByUserName}) => {
|
||||||
|
navigate(routes.user2({userName: updateUserByUserName.userName}))
|
||||||
|
addMessage('User updated.', { classes: 'rw-flash-success' })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const onSave = (userName, input) => {
|
||||||
|
updateUser({ variables: { userName, input } })
|
||||||
|
}
|
||||||
|
|
||||||
|
return <UserProfile
|
||||||
|
user={user}
|
||||||
|
onSave={onSave}
|
||||||
|
loading={loading}
|
||||||
|
error={error}
|
||||||
|
isEditable
|
||||||
|
/>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ export default function ImageUploader({ onImageUpload, imageUrl, aspectRatio, cl
|
|||||||
});
|
});
|
||||||
async function handleImageUpload() {
|
async function handleImageUpload() {
|
||||||
const croppedFile = await getCroppedImg(imageObj, crop, 'avatar')
|
const croppedFile = await getCroppedImg(imageObj, crop, 'avatar')
|
||||||
console.log(croppedFile)
|
|
||||||
const imageData = new FormData();
|
const imageData = new FormData();
|
||||||
imageData.append('upload_preset', CLOUDINARY_UPLOAD_PRESET);
|
imageData.append('upload_preset', CLOUDINARY_UPLOAD_PRESET);
|
||||||
imageData.append('file', croppedFile);
|
imageData.append('file', croppedFile);
|
||||||
@@ -54,7 +53,7 @@ export default function ImageUploader({ onImageUpload, imageUrl, aspectRatio, cl
|
|||||||
<div className={'relative overflow-hidden '+ (!imageUrl && isEditable ? 'border ' : '') + className} style={{paddingBottom: `${1/aspectRatio*100}%`}}>
|
<div className={'relative overflow-hidden '+ (!imageUrl && isEditable ? 'border ' : '') + className} style={{paddingBottom: `${1/aspectRatio*100}%`}}>
|
||||||
<div className="absolute w-full h-full" {...getRootProps()}>
|
<div className="absolute w-full h-full" {...getRootProps()}>
|
||||||
{cloudinaryId && isEditable && <button className="absolute z-10 w-full inset-0 bg-indigo-900 opacity-50 flex justify-center items-center">
|
{cloudinaryId && isEditable && <button className="absolute z-10 w-full inset-0 bg-indigo-900 opacity-50 flex justify-center items-center">
|
||||||
<Svg name="pencil" strokeWidth={2} className="text-gray-300 h-48 w-48" />
|
<Svg name="pencil" strokeWidth={2} className="text-gray-300 h-24 w-24" />
|
||||||
</button>}
|
</button>}
|
||||||
{isEditable && <input {...getInputProps()} />}
|
{isEditable && <input {...getInputProps()} />}
|
||||||
{(cloudinaryId || !isEditable) && <div className="relative overflow-hidden w-full h-full">
|
{(cloudinaryId || !isEditable) && <div className="relative overflow-hidden w-full h-full">
|
||||||
|
|||||||
@@ -1,14 +1,21 @@
|
|||||||
import {Fragment} from 'react'
|
import {useState, useEffect} from 'react'
|
||||||
|
import { navigate, routes } from '@redwoodjs/router'
|
||||||
|
import Editor from "rich-markdown-editor";
|
||||||
|
|
||||||
import ImageUploader from 'src/components/ImageUploader'
|
import ImageUploader from 'src/components/ImageUploader'
|
||||||
import Button from 'src/components/Button'
|
import Button from 'src/components/Button'
|
||||||
import Editor from "rich-markdown-editor";
|
|
||||||
import ProfileTextInput from 'src/components/ProfileTextInput'
|
import ProfileTextInput from 'src/components/ProfileTextInput'
|
||||||
|
|
||||||
|
|
||||||
const UserProfile = ({user, isEditable}) => {
|
const UserProfile = ({user, isEditable, loading, onSave, error}) => {
|
||||||
console.log(isEditable)
|
const [input, setInput] = useState({
|
||||||
const {userName, email} = user
|
userName: user.userName,
|
||||||
const editableTextFields = {userName, name: email} // TODO add name field to user
|
email: user.email,
|
||||||
|
bio: user.bio,
|
||||||
|
image: user.image,
|
||||||
|
})
|
||||||
|
const {userName, email} = input
|
||||||
|
const editableTextFields = {userName, email} // TODO add name field to user
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="max-w-2xl mx-auto mt-20 ">
|
<div className="max-w-2xl mx-auto mt-20 ">
|
||||||
@@ -16,16 +23,23 @@ const UserProfile = ({user, isEditable}) => {
|
|||||||
<div className="w-40 flex-shrink-0">
|
<div className="w-40 flex-shrink-0">
|
||||||
<ImageUploader
|
<ImageUploader
|
||||||
className="rounded-half rounded-br-lg shadow-md border-2 border-gray-200 border-solid"
|
className="rounded-half rounded-br-lg shadow-md border-2 border-gray-200 border-solid"
|
||||||
|
onImageUpload={({cloudinaryPublicId: image}) => setInput({
|
||||||
|
...input,
|
||||||
|
image,
|
||||||
|
})}
|
||||||
aspectRatio={1}
|
aspectRatio={1}
|
||||||
isEditable={isEditable}
|
isEditable={isEditable}
|
||||||
imageUrl={user.image === 'abc' ? '': user.image}
|
imageUrl={user.image === 'abc' ? '': user.image}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="ml-6 flex flex-col justify-between">
|
<div className="ml-6 flex flex-col justify-between">
|
||||||
<ProfileTextInput fields={editableTextFields} isEditable={isEditable}/>
|
<ProfileTextInput fields={editableTextFields} onChange={(textFields) => setInput({
|
||||||
|
...input,
|
||||||
|
...textFields,
|
||||||
|
})} isEditable={isEditable}/>
|
||||||
{isEditable ?
|
{isEditable ?
|
||||||
<Button iconName="plus">Save Profile</Button> : // TODO replace pencil with a save icon
|
<Button iconName="plus" onClick={() => onSave(user.userName, input)}>Save Profile</Button> : // TODO replace pencil with a save icon
|
||||||
<Button iconName="pencil">Edit Profile</Button>
|
<Button iconName="pencil" onClick={() => navigate(routes.editUser2({userName: user.userName}))}>Edit Profile</Button>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -35,6 +49,10 @@ const UserProfile = ({user, isEditable}) => {
|
|||||||
<Editor
|
<Editor
|
||||||
defaultValue={user.bio}
|
defaultValue={user.bio}
|
||||||
readOnly={!isEditable}
|
readOnly={!isEditable}
|
||||||
|
onChange={(bioFn) => setInput({
|
||||||
|
...input,
|
||||||
|
bio: bioFn(),
|
||||||
|
})}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user