@@ -7,26 +7,49 @@ import JobForm from '@/components/job-form'
7
7
import { PollForm } from '@/components/poll-form'
8
8
import { BountyForm } from '@/components/bounty-form'
9
9
import { useState } from 'react'
10
- import { useQuery } from '@apollo/client'
10
+ import { gql , useQuery } from '@apollo/client'
11
11
import { useRouter } from 'next/router'
12
12
import PageLoading from '@/components/page-loading'
13
- import { FeeButtonProvider } from '@/components/fee-button'
13
+ import { FeeButtonProvider , postCommentBaseLineItems } from '@/components/fee-button'
14
14
import SubSelect from '@/components/sub-select'
15
15
import useCanShadowEdit from '@/components/use-can-edit'
16
+ import { useMe } from '@/components/me'
16
17
17
18
export const getServerSideProps = getGetServerSideProps ( {
18
19
query : ITEM ,
19
20
notFound : data => ! data . item
20
21
} )
21
22
23
+ // TODO: cleanup
24
+ const SUB_QUERY = gql `
25
+ query Sub($name: String!) {
26
+ sub(name: $name) {
27
+ name
28
+ baseCost
29
+ }
30
+ }
31
+ `
32
+
22
33
export default function PostEdit ( { ssrData } ) {
34
+ const { me } = useMe ( )
23
35
const router = useRouter ( )
24
36
const { data } = useQuery ( ITEM , { variables : { id : router . query . id } } )
25
37
if ( ! data && ! ssrData ) return < PageLoading />
26
38
27
39
const { item } = data || ssrData
28
40
const [ sub , setSub ] = useState ( item . subName )
29
41
42
+ // TODO: cleanup
43
+ const { data : oldSubData } = useQuery ( SUB_QUERY , {
44
+ variables : { name : item . subName } ,
45
+ skip : ! item . subName
46
+ } )
47
+
48
+ const { data : newSubData } = useQuery ( SUB_QUERY , {
49
+ variables : { name : sub } ,
50
+ skip : ! sub
51
+ } )
52
+
30
53
const [ , , shadowEditThreshold ] = useCanShadowEdit ( item )
31
54
32
55
let FormType = DiscussionForm
@@ -45,20 +68,26 @@ export default function PostEdit ({ ssrData }) {
45
68
itemType = 'BOUNTY'
46
69
}
47
70
48
- const existingBoostLineItem = item . boost
49
- ? {
50
- existingBoost : {
51
- label : 'old boost' ,
52
- term : `- ${ item . boost } ` ,
53
- op : '-' ,
54
- modifier : cost => cost - item . boost
71
+ function editLineItems ( oldSub , newSub ) {
72
+ const existingBoostLineItem = item . boost
73
+ ? {
74
+ existingBoost : {
75
+ label : 'old boost' ,
76
+ term : `- ${ item . boost } ` ,
77
+ op : '-' ,
78
+ modifier : cost => cost - item . boost
79
+ }
55
80
}
56
- }
57
- : undefined
81
+ : undefined
82
+ return {
83
+ ...( item . subName !== newSub ?. name ? postCommentBaseLineItems ( { baseCost : Math . max ( 0 , newSub ?. baseCost - oldSub ?. baseCost ) , me : ! ! me } ) : undefined ) ,
84
+ ...existingBoostLineItem
85
+ }
86
+ }
58
87
59
88
return (
60
89
< CenterLayout sub = { sub } >
61
- < FeeButtonProvider baseLineItems = { existingBoostLineItem } >
90
+ < FeeButtonProvider baseLineItems = { editLineItems ( oldSubData ?. sub , newSubData ?. sub ) } >
62
91
< FormType item = { item } shadowEditThreshold = { shadowEditThreshold } >
63
92
{ ! item . isJob &&
64
93
< SubSelect
0 commit comments