1- import { InputText } from 'primereact/inputtext ' ;
1+ import { SelectButton } from 'primereact/selectbutton ' ;
22import { Button } from 'primereact/button' ;
3- import { SplitButton } from 'primereact/splitbutton ' ;
3+ import { AutoComplete } from 'primereact/autocomplete ' ;
44import React , { useState } from 'react' ;
55import { TransformWrapper , TransformComponent , MiniMap } from "react-zoom-pan-pinch" ;
66import { ImageElement } from './ImageCard' ;
77import { DomHandler } from 'primereact/utils' ;
88import { gql , useMutation } from '@apollo/client' ;
9- import { format } from 'date-fns' ;
109
1110const CREATE_COMMENT = gql `
1211mutation CreateOneComment($data: PhotoReactionInsertInput!) {
@@ -17,10 +16,11 @@ mutation CreateOneComment($data: PhotoReactionInsertInput!) {
1716}
1817` ;
1918
20- const CREATE_TAG = gql `
21- mutation CreateOneTAG($data: TagInsertInput!) {
22- tagCreateOne(data: $data) {
23- id
19+ const UPDATE_COMMENT = gql `
20+ mutation UpdateOneComment($data: PhotoReactionUpdateInput!, $filter: PhotoReactionFilterInput!) {
21+ photoReactionUpdate(data: $data, filter: $filter) {
22+ comment,
23+ isRecommended
2424 }
2525}
2626` ;
@@ -32,90 +32,54 @@ interface PreviewProps {
3232 onClose : ( ) => void ;
3333}
3434
35- function generateRandomString ( ) : string {
36- const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' ;
37- let result = '' ;
38- for ( let i = 0 ; i < 3 ; i ++ ) {
39- const randomIndex = Math . floor ( Math . random ( ) * letters . length ) ;
40- result += letters [ randomIndex ] ;
41- }
42- return result ;
43- }
44-
4535export const Preview : React . FC < PreviewProps > = ( props ) => {
4636 const { height } = DomHandler . getViewport ( ) ;
4737 const [ index , setIndex ] = useState < number > ( props . index ) ;
4838 const image = props . images [ index ] ;
49- const [ value , setValue ] = useState < string > ( '' ) ;
39+ const [ isRecommended , setIsRecommended ] = useState < string | null > (
40+ image . reaction ? ( image . reaction . isRecommended ? '正向' : '負向' ) : null
41+ ) ;
42+ const [ value , setValue ] = useState < string > ( image . reaction ?. comment ?? '' ) ;
43+ const [ searchItems , setSearchItems ] = useState < string [ ] > ( [ ] ) ;
5044 const [ createComment ] = useMutation ( CREATE_COMMENT ) ;
51- const [ createTag ] = useMutation ( CREATE_TAG ) ;
52- const goods = [
53- {
54- label : '構圖不錯' ,
55- command : async ( ) => {
56- await createComment ( {
57- variables : {
58- data : {
59- userId : props . userId ,
60- photoId : image . id ,
61- comment : '構圖不錯' ,
62- isRecommended : 1
63- }
64- }
65- } ) ;
66- }
67- } ,
68- {
69- label : '攝影技巧不錯' ,
70- command : async ( ) => {
71- await createTag ( {
72- variables : {
73- data : {
74- name : generateRandomString ( ) ,
75- description : '測試' ,
76- note : '測試' ,
77- tagType : 'photographer'
78- }
79- }
80- } )
81- }
82- } ,
83- {
84- label : '主題選擇不錯' ,
85- command : ( ) => {
86-
45+ const [ updateComment ] = useMutation ( UPDATE_COMMENT ) ;
46+ const handleCreateComment = async ( comment : string , isRecommended : number ) => {
47+ if ( image . reaction ) {
48+ await updateComment ( {
49+ variables : {
50+ data : {
51+ comment,
52+ isRecommended
53+ } ,
54+ filter : {
55+ userId : { eq : props . userId } ,
56+ photoId : { eq : image . id }
57+ }
8758 }
88- } ,
89- {
90- label : '內容充實' ,
91- command : ( ) => {
92-
93- }
94- }
95- ] ;
96- const bads = [
97- {
98- label : '構圖不好' ,
99- command : ( ) => {
100- }
101- } ,
102- {
103- label : '過曝' ,
104- command : ( ) => {
105- }
106- } ,
107- {
108- label : '失焦' ,
109- command : ( ) => {
110-
111- }
112- } ,
113- {
114- label : '光線不足' ,
115- command : ( ) => {
116-
59+ } ) ;
60+ } else {
61+ await createComment ( {
62+ variables : {
63+ data : {
64+ userId : props . userId ,
65+ photoId : image . id ,
66+ comment,
67+ isRecommended
68+ }
11769 }
70+ } ) ;
11871 }
72+ setValue ( '' ) ;
73+ } ;
74+ const suggestions = [
75+ '構圖不錯' ,
76+ '攝影技巧不錯' ,
77+ '主題選擇不錯' ,
78+ '內容充實' ,
79+ '構圖不好' ,
80+ '過曝' ,
81+ '失焦' ,
82+ '光線不足' ,
11983 ] ;
12084 return (
12185 < >
@@ -127,22 +91,26 @@ export const Preview: React.FC<PreviewProps> = (props) => {
12791 >
12892 < div className = 'flex h-full ml-2 min-w-[700px]' >
12993 < div className = 'flex h-full items-center gap-2' >
130- < SplitButton label = "我喜歡" icon = "pi pi-plus" model = { goods } size = "small" />
131- < SplitButton label = "影象模糊" icon = "pi pi-plus" model = { bads } size = "small" />
132- < InputText value = { value } onChange = { ( e ) => setValue ( e . target . value ) } placeholder = '其他想法' className = 'z-1002' />
94+ < AutoComplete
95+ suggestions = { searchItems }
96+ value = { value }
97+ onChange = { ( e ) => setValue ( e . value ) }
98+ completeMethod = { ( e ) => {
99+ setSearchItems ( suggestions . filter ( item => item . includes ( e . query ) ) ) ;
100+ } }
101+ dropdown
102+ placeholder = '建議'
103+ className = 'z-1002'
104+ />
133105 < span className = '!text-white' > 以</ span >
134- < Button
135- size = 'small'
136- className = '!text-white'
137- >
138- 正向
139- </ Button >
140- < Button
141- size = 'small'
142- className = '!text-white'
143- >
144- 負向
145- </ Button >
106+ < SelectButton
107+ value = { isRecommended }
108+ onChange = { ( e ) => {
109+ setIsRecommended ( e . value ) ;
110+ handleCreateComment ( value , e . value === '正向' ? 1 : 0 ) ;
111+ } }
112+ options = { [ '正向' , '負向' ] }
113+ />
146114 < span className = '!text-white' > 的評價送出</ span >
147115 </ div >
148116 < div className = 'flex flex-grow' > </ div >
0 commit comments