Hook up saving parts to backend.
This commit is contained in:
@@ -6,11 +6,11 @@ import PartProfile from 'src/components/PartProfile'
|
|||||||
export const QUERY = gql`
|
export const QUERY = gql`
|
||||||
query FIND_PART_BY_USERNAME_TITLE($userName: String!, $partTitle: String!) {
|
query FIND_PART_BY_USERNAME_TITLE($userName: String!, $partTitle: String!) {
|
||||||
userPart: userName(userName: $userName) {
|
userPart: userName(userName: $userName) {
|
||||||
|
id
|
||||||
name
|
name
|
||||||
userName
|
userName
|
||||||
bio
|
bio
|
||||||
image
|
image
|
||||||
id
|
|
||||||
Part(partTitle: $partTitle) {
|
Part(partTitle: $partTitle) {
|
||||||
id
|
id
|
||||||
title
|
title
|
||||||
@@ -25,6 +25,19 @@ export const QUERY = gql`
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const UPDATE_PART_MUTATION = gql`
|
||||||
|
mutation UpdatePartMutation($id: String!, $input: UpdatePartInput!) {
|
||||||
|
updatePart:updatePart(id: $id, input: $input) {
|
||||||
|
id
|
||||||
|
title
|
||||||
|
user {
|
||||||
|
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>
|
||||||
@@ -32,5 +45,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 = ({ userPart, variables: {isEditable} }) => {
|
export const Success = ({ userPart, variables: {isEditable} }) => {
|
||||||
return <PartProfile userPart={userPart} isEditable={isEditable} />
|
const { addMessage } = useFlash()
|
||||||
|
const [updateUser, { loading, error }] = useMutation(UPDATE_PART_MUTATION, {
|
||||||
|
onCompleted: ({updatePart}) => {
|
||||||
|
navigate(routes.part2({userName: updatePart.user.userName, partTitle: updatePart.title}))
|
||||||
|
addMessage('Part updated.', { classes: 'rw-flash-success' })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const onSave = (id, input) => {
|
||||||
|
updateUser({ variables: { id, input } })
|
||||||
|
}
|
||||||
|
|
||||||
|
return <PartProfile
|
||||||
|
userPart={userPart}
|
||||||
|
onSave={onSave}
|
||||||
|
loading={loading}
|
||||||
|
error={error}
|
||||||
|
isEditable={isEditable}
|
||||||
|
/>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,36 @@
|
|||||||
import {useState} from 'react'
|
import {useState, useEffect} from 'react'
|
||||||
import Editor from "rich-markdown-editor";
|
import Editor from "rich-markdown-editor";
|
||||||
|
import { navigate, routes } from '@redwoodjs/router'
|
||||||
|
|
||||||
import ImageUploader from 'src/components/ImageUploader'
|
import ImageUploader from 'src/components/ImageUploader'
|
||||||
import Breadcrumb from 'src/components/Breadcrumb'
|
import Breadcrumb from 'src/components/Breadcrumb'
|
||||||
import EmojiReaction from 'src/components/EmojiReaction'
|
import EmojiReaction from 'src/components/EmojiReaction'
|
||||||
import Button from 'src/components/Button'
|
import Button from 'src/components/Button'
|
||||||
|
|
||||||
const PartProfile = ({userPart, isEditable}) => {
|
const PartProfile = ({userPart, isEditable, onSave, loading, error}) => {
|
||||||
const part = userPart?.Part
|
const part = userPart?.Part
|
||||||
const [input, setInput] = useState({
|
const [input, setInput] = useState({
|
||||||
title: part.title,
|
title: part.title,
|
||||||
mainImage: part.mainImage,
|
mainImage: part.mainImage,
|
||||||
description: part.description,
|
description: part.description,
|
||||||
})
|
})
|
||||||
const hi = () => {}
|
const setProperty = (property, value) => setInput({
|
||||||
|
...input,
|
||||||
|
[property]: value,
|
||||||
|
})
|
||||||
|
const onTitleChange = ({target}) => setProperty('title', target.value)
|
||||||
|
const onDescriptionChange = (description) => setProperty('description', description())
|
||||||
|
const onImageUpload = ({cloudinaryPublicId}) => setProperty('mainImage', cloudinaryPublicId)
|
||||||
|
const onEditSaveClick = () => {
|
||||||
|
if (isEditable) {
|
||||||
|
onSave(part.id, input)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
navigate(routes.editPart2({userName: userPart.userName, partTitle: part.title}))
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="grid mt-20 gap-8" style={{gridTemplateColumns: 'auto 12rem minmax(12rem, 42rem) 10rem auto'}}>
|
<div className="grid mt-20 gap-8" style={{gridTemplateColumns: 'auto 12rem minmax(12rem, 42rem) auto'}}>
|
||||||
|
|
||||||
{/* Side column */}
|
{/* Side column */}
|
||||||
<aside className="col-start-2 relative">
|
<aside className="col-start-2 relative">
|
||||||
@@ -55,7 +69,7 @@ const PartProfile = ({userPart, isEditable}) => {
|
|||||||
className="mt-4 ml-auto shadow-md hover:shadow-lg bg-indigo-200 relative z-20"
|
className="mt-4 ml-auto shadow-md hover:shadow-lg bg-indigo-200 relative z-20"
|
||||||
shouldAnimateHover
|
shouldAnimateHover
|
||||||
iconName={isEditable ? 'save' : 'pencil'}
|
iconName={isEditable ? 'save' : 'pencil'}
|
||||||
onClick={() => {}}
|
onClick={onEditSaveClick}
|
||||||
>
|
>
|
||||||
{isEditable ? 'Save Details' : 'Edit Details'}
|
{isEditable ? 'Save Details' : 'Edit Details'}
|
||||||
</Button>}
|
</Button>}
|
||||||
@@ -64,10 +78,10 @@ const PartProfile = ({userPart, isEditable}) => {
|
|||||||
|
|
||||||
{/* main project center column */}
|
{/* main project center column */}
|
||||||
<section className="col-start-3">
|
<section className="col-start-3">
|
||||||
<Breadcrumb className="inline" onPartTitleChange={isEditable && hi} userName={userPart.userName} partTitle={input?.title}/>
|
<Breadcrumb className="inline" onPartTitleChange={isEditable && onTitleChange} userName={userPart.userName} partTitle={input?.title}/>
|
||||||
{ input?.mainImage && <ImageUploader
|
{ input?.mainImage && <ImageUploader
|
||||||
className="rounded-lg shadow-md border-2 border-gray-200 border-solid mt-8"
|
className="rounded-lg shadow-md border-2 border-gray-200 border-solid mt-8"
|
||||||
onImageUpload={() => {}}
|
onImageUpload={onImageUpload}
|
||||||
aspectRatio={16/9}
|
aspectRatio={16/9}
|
||||||
isEditable={isEditable}
|
isEditable={isEditable}
|
||||||
imageUrl={input?.mainImage}
|
imageUrl={input?.mainImage}
|
||||||
@@ -76,10 +90,7 @@ const PartProfile = ({userPart, isEditable}) => {
|
|||||||
<Editor
|
<Editor
|
||||||
defaultValue={part?.description || ''}
|
defaultValue={part?.description || ''}
|
||||||
readOnly={!isEditable}
|
readOnly={!isEditable}
|
||||||
// onChange={(bioFn) => setInput({
|
onChange={onDescriptionChange}
|
||||||
// ...input,
|
|
||||||
// bio: bioFn(),
|
|
||||||
// })}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
Reference in New Issue
Block a user