-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
annotationsModel.ts
85 lines (84 loc) 路 3.18 KB
/
annotationsModel.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
import { kea } from 'kea'
import api from 'lib/api'
import { toParams, deleteWithUndo } from 'lib/utils'
import dayjs, { Dayjs } from 'dayjs'
import { getNextKey } from 'lib/components/Annotations/utils'
import { annotationsModelType } from './annotationsModelType'
import { AnnotationScope, AnnotationType } from '~/types'
import { teamLogic } from '../scenes/teamLogic'
export const annotationsModel = kea<annotationsModelType>({
path: ['models', 'annotationsModel'],
actions: {
createGlobalAnnotation: (content: string, date_marker: string, dashboard_item?: number) => ({
content,
date_marker,
created_at: dayjs() as Dayjs,
dashboard_item,
}),
deleteGlobalAnnotation: (id) => ({ id }),
},
loaders: ({ values }) => ({
globalAnnotations: {
__default: [] as AnnotationType[],
loadGlobalAnnotations: async () => {
const response = await api.get(
`api/projects/${teamLogic.values.currentTeamId}/annotations/?${toParams({
scope: 'organization',
deleted: false,
})}`
)
return response.results
},
createGlobalAnnotation: async ({ dashboard_item, content, date_marker, created_at }) => {
await api.create(`api/projects/${teamLogic.values.currentTeamId}/annotations`, {
content,
date_marker: dayjs.isDayjs(date_marker) ? date_marker : dayjs(date_marker),
created_at,
dashboard_item,
scope: AnnotationScope.Organization,
})
return values.globalAnnotations || []
},
},
}),
reducers: {
globalAnnotations: {
createGlobalAnnotation: (state, { content, date_marker, created_at }) => [
...state,
{
id: getNextKey(state).toString(),
content,
date_marker: date_marker,
created_at: created_at.toISOString(),
updated_at: created_at.toISOString(),
created_by: 'local',
scope: AnnotationScope.Organization,
},
],
deleteGlobalAnnotation: (state, { id }) => {
return state.filter((a) => a.id !== id)
},
},
},
selectors: {
activeGlobalAnnotations: [
(s) => [s.globalAnnotations],
(globalAnnotations) => {
return globalAnnotations.filter((annotation) => !annotation.deleted)
},
],
},
listeners: ({ actions }) => ({
deleteGlobalAnnotation: ({ id }) => {
id >= 0 &&
deleteWithUndo({
endpoint: `projects/${teamLogic.values.currentTeamId}/annotations`,
object: { name: 'Annotation', id },
callback: () => actions.loadGlobalAnnotations(),
})
},
}),
events: ({ actions }) => ({
afterMount: () => actions.loadGlobalAnnotations(),
}),
})