1
1
import React , { useContext } from "react" ;
2
+ import { InMemoryCache } from '@apollo/client' ;
2
3
import { Button , Checkbox , Dimmer , Header , Icon , Message , Modal , Segment , Tab } from "semantic-ui-react" ;
3
- import { graphql } from "@apollo/client/react/hoc" ;
4
+ import { graphql , withApollo } from "@apollo/client/react/hoc" ;
4
5
import { isEqual } from "lodash" ;
5
6
import PropTypes from "prop-types" ;
6
7
import { compose } from "recompose" ;
@@ -12,7 +13,7 @@ import Tree from "components/GroupingTagModal/Tree";
12
13
import { LexicalEntryByIds , queryLexicalEntries , queryPerspective } from "components/PerspectiveView" ;
13
14
import TranslationContext from "Layout/TranslationContext" ;
14
15
15
- import { acceptMutation , createMutation , languageTreeSourceQuery , publishMutation , removeMutation } from "./graphql" ;
16
+ import { acceptMutation , createMutation , languageTreeSourceQuery , publishMutation , removeMutation , entityQuery } from "./graphql" ;
16
17
17
18
const ModalContentWrapper = styled ( "div" ) `
18
19
min-height: 60vh;
@@ -67,23 +68,46 @@ ViewLink.propTypes = {
67
68
} ;
68
69
69
70
const EditLink = props => {
70
- const { lexicalEntry, column, allLanguages, allDictionaries, allPerspectives, create, remove } = props ;
71
+ const { client , lexicalEntry, column, allLanguages, allDictionaries, allPerspectives, create, remove } = props ;
71
72
72
73
const getTranslation = useContext ( TranslationContext ) ;
73
74
74
75
const tree = buildTree ( lexicalEntry , column , allLanguages , allDictionaries , allPerspectives ) ;
75
76
77
+ const get_link = async entry => {
78
+ const entity = lexicalEntry . entities . find (
79
+ e => isEqual ( e . link_id , entry . id ) && isEqual ( e . field_id , column . field_id )
80
+ ) ;
81
+
82
+ if ( ! entity ) return null ;
83
+
84
+ //Checking in db
85
+ const result = await client . query ( {
86
+ query : entityQuery ,
87
+ variables : { id : entity . id } ,
88
+ fetchPolicy : 'network-only'
89
+ } ) ;
90
+
91
+ if ( ! result . errors &&
92
+ result . data . entity &&
93
+ result . data . entity . marked_for_deletion === false ) {
94
+ return entity ;
95
+ }
96
+ return null ;
97
+ }
98
+
76
99
const actions = [
77
100
{
78
101
title : getTranslation ( "Remove" ) ,
79
102
action : entry => {
80
- const entity = lexicalEntry . entities . find (
81
- e => isEqual ( e . link_id , entry . id ) && isEqual ( e . field_id , column . field_id )
82
- ) ;
83
- if ( entity ) {
84
- remove ( entity ) ;
85
- }
103
+ get_link ( entry ) . then ( entity => {
104
+ if ( entity ) {
105
+ remove ( entity ) ;
106
+ console . log ( "Query result: " , lexicalEntry . entities ) ;
107
+ }
108
+ } ) ;
86
109
} ,
110
+ enabled : entry => get_link ( entry ) ,
87
111
className : "lingvo-button-redder"
88
112
}
89
113
] ;
@@ -323,9 +347,14 @@ class LinkModalContent extends React.PureComponent {
323
347
}
324
348
325
349
removeEntity ( entity ) {
326
- const { remove, lexicalEntry, entitiesMode } = this . props ;
350
+ const { reRender, remove, lexicalEntry, entitiesMode } = this . props ;
351
+
327
352
remove ( {
328
353
variables : { id : entity . id } ,
354
+ update ( cache ) {
355
+ cache . evict ( { id : cache . identify ( entity ) } ) ;
356
+ cache . gc ( ) ;
357
+ } ,
329
358
refetchQueries : [
330
359
{
331
360
// XXX: Expensive operation!
@@ -337,6 +366,9 @@ class LinkModalContent extends React.PureComponent {
337
366
}
338
367
]
339
368
} ) ;
369
+
370
+ // Refetching queryPerspective directly in PerspectiveView component
371
+ // reRender();
340
372
}
341
373
342
374
render ( ) {
@@ -392,6 +424,7 @@ class LinkModalContent extends React.PureComponent {
392
424
remove = { this . removeEntity }
393
425
publish = { this . changePublished }
394
426
accept = { this . changeAccepted }
427
+ client = { this . props . client }
395
428
/>
396
429
</ ModalContentWrapper >
397
430
) ;
@@ -414,15 +447,17 @@ LinkModalContent.propTypes = {
414
447
create : PropTypes . func . isRequired ,
415
448
publish : PropTypes . func . isRequired ,
416
449
accept : PropTypes . func . isRequired ,
417
- remove : PropTypes . func . isRequired
450
+ remove : PropTypes . func . isRequired ,
451
+ reRender : PropTypes . func
418
452
} ;
419
453
420
454
const Content = compose (
421
455
graphql ( languageTreeSourceQuery ) ,
422
456
graphql ( createMutation , { name : "create" } ) ,
423
457
graphql ( publishMutation , { name : "publish" } ) ,
424
458
graphql ( acceptMutation , { name : "accept" } ) ,
425
- graphql ( removeMutation , { name : "remove" } )
459
+ graphql ( removeMutation , { name : "remove" } ) ,
460
+ withApollo
426
461
) ( LinkModalContent ) ;
427
462
428
463
const LinkModal = props => {
@@ -453,7 +488,8 @@ LinkModal.propTypes = {
453
488
fieldId : PropTypes . array ,
454
489
mode : PropTypes . string . isRequired ,
455
490
entitiesMode : PropTypes . string . isRequired ,
456
- onClose : PropTypes . func . isRequired
491
+ onClose : PropTypes . func . isRequired ,
492
+ reRender : PropTypes . func
457
493
} ;
458
494
459
495
LinkModal . defaultProps = {
0 commit comments