-
Notifications
You must be signed in to change notification settings - Fork 324
/
UpdateTaskMutation.ts
134 lines (127 loc) · 4.18 KB
/
UpdateTaskMutation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import graphql from 'babel-plugin-relay/macro'
import {commitMutation} from 'react-relay'
import ITask from '../../server/database/types/Task'
import {
OnNextHandler,
OnNextHistoryContext,
OptionalHandlers,
SharedUpdater,
StandardMutation
} from '../types/relayMutations'
import extractTextFromDraftString from '../utils/draftjs/extractTextFromDraftString'
import getTagsFromEntityMap from '../utils/draftjs/getTagsFromEntityMap'
import getInProxy from '../utils/relay/getInProxy'
import updateProxyRecord from '../utils/relay/updateProxyRecord'
import {UpdateTaskMutation as TUpdateTaskMutation} from '../__generated__/UpdateTaskMutation.graphql'
import {UpdateTaskMutation_task} from '../__generated__/UpdateTaskMutation_task.graphql'
import handleAddNotifications from './handlers/handleAddNotifications'
import handleRemoveTasks from './handlers/handleRemoveTasks'
import handleUpsertTasks from './handlers/handleUpsertTasks'
import popInvolvementToast from './toasts/popInvolvementToast'
graphql`
fragment UpdateTaskMutation_task on UpdateTaskPayload {
task {
# Entire frag needed in case it is deprivatized
...CompleteTaskFrag @relay(mask: false)
id
editors {
userId
preferredName
}
}
addedNotification {
type
changeAuthor {
preferredName
}
team {
id
}
...TaskInvolves_notification @relay(mask: false)
}
privatizedTaskId
}
`
const mutation = graphql`
mutation UpdateTaskMutation($updatedTask: UpdateTaskInput!, $area: AreaEnum) {
updateTask(updatedTask: $updatedTask, area: $area) {
error {
message
}
...UpdateTaskMutation_task @relay(mask: false)
}
}
`
export const updateTaskTaskOnNext: OnNextHandler<UpdateTaskMutation_task, OnNextHistoryContext> = (
payload,
{atmosphere, history}
) => {
if (!payload || !payload.addedNotification) return
popInvolvementToast(payload.addedNotification, {atmosphere, history})
}
export const updateTaskTaskUpdater: SharedUpdater<UpdateTaskMutation_task> = (payload, {store}) => {
const task = payload.getLinkedRecord('task')!
handleUpsertTasks(task as any, store as any)
const addedNotification = payload.getLinkedRecord('addedNotification')
handleAddNotifications(addedNotification as any, store)
const viewer = store.getRoot().getLinkedRecord('viewer')
const viewerId = viewer && viewer.getDataID()
const privatizedTaskId = payload.getValue('privatizedTaskId')
const taskUserId = getInProxy(task, 'userId')
if (taskUserId !== viewerId && privatizedTaskId) {
handleRemoveTasks(privatizedTaskId, store)
}
}
const UpdateTaskMutation: StandardMutation<TUpdateTaskMutation, OptionalHandlers> = (
atmosphere,
{updatedTask, area},
{onError, onCompleted}
) => {
return commitMutation<TUpdateTaskMutation>(atmosphere, {
mutation,
variables: {
area,
updatedTask
},
updater: (store) => {
const payload = store.getRootField('updateTask')
if (!payload) return
const error = payload.getLinkedRecord('error')
if (error) {
const {id: taskId} = updatedTask
const task = store.get<ITask>(taskId)
if (task) {
const message = error.getValue('message')
task.setValue(message, 'error')
}
}
updateTaskTaskUpdater(payload, {atmosphere, store: store as any})
},
optimisticUpdater: (store) => {
const {id, content, userId} = updatedTask
const task = store.get(id)
if (!task) return
const now = new Date()
const optimisticTask = {
...updatedTask,
updatedAt: now.toJSON()
}
updateProxyRecord(task, optimisticTask)
if (userId) {
task.setValue(userId, 'userId')
task.setLinkedRecord(store.get(userId)!, 'user')
}
if (content) {
const {entityMap} = JSON.parse(content)
const nextTags = getTagsFromEntityMap(entityMap)
task.setValue(nextTags, 'tags')
const plaintextContent = extractTextFromDraftString(content)
task.setValue(plaintextContent, 'plaintextContent')
}
handleUpsertTasks(task as any, store)
},
onError,
onCompleted
})
}
export default UpdateTaskMutation