Skip to content

Commit

Permalink
Rows are disabled after Remove or Connect clicking -- rartly resolves #…
Browse files Browse the repository at this point in the history
…1014 (#1018)

* cache.evict

* Moved update(cache)

* Fix

* useState

* getEntity

* getEntity

* Fix

* withApollo

* async

* reRender

* reFetching

* queryLexicalEntries

* Fix

* reRender

* console.log

* Minor

* refetch

* Cleanup

* Cleanup

* TableComponent

* TableComponent2

* Fix

* Next step

* Next step

* Cleanup

* is_connectable

* Refetched 'queryPerspective'

* reRenderWrapper

* Fix

* Cleanup
  • Loading branch information
vmonakhov authored Aug 24, 2023
1 parent 9cec3e3 commit 82d0c27
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 78 deletions.
9 changes: 8 additions & 1 deletion src/components/GroupingTagModal/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ class SearchLexicalEntries extends React.Component {

render() {
const { joinGroup } = this.props;
const actions = [{ title: this.context("Connect"), className: "lingvo-button-greenest", action: entry => joinGroup(entry) }];

const actions = [
{
title: this.context("Connect"),
className: "lingvo-button-greenest",
action: entry => joinGroup(entry)
}
];
return (
<div>
<Segment className="lingvo-segment-grouptag-search">
Expand Down
8 changes: 5 additions & 3 deletions src/components/LexicalEntry/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import LinkModal from "components/LinkModal";
import { openModal } from "ducks/modals";

const DirectedLink = props => {
const { entry, column, mode, entitiesMode, as: Component = "div", openModal, disabled } = props;
const { reRender, entry, column, mode, entitiesMode, as: Component = "div", openModal, disabled } = props;

const count = entry.entities.filter(e => isEqual(e.field_id, column.id)).length;
const content = `${T(column.translations)} (${count})`;
Expand All @@ -32,7 +32,8 @@ const DirectedLink = props => {
lexicalEntry: entry,
fieldId: column.id,
mode,
entitiesMode
entitiesMode,
reRender
})
}
/>
Expand All @@ -46,7 +47,8 @@ DirectedLink.propTypes = {
mode: PropTypes.string.isRequired,
entitiesMode: PropTypes.string.isRequired,
as: PropTypes.string,
openModal: PropTypes.func.isRequired
openModal: PropTypes.func.isRequired,
reRender: PropTypes.func
};

DirectedLink.defaultProps = {
Expand Down
3 changes: 2 additions & 1 deletion src/components/LexicalEntry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ Entities.propTypes = {
updateEntity: PropTypes.func.isRequired,
resetCheckedRow: PropTypes.func,
resetCheckedColumn: PropTypes.func,
resetCheckedAll: PropTypes.func
resetCheckedAll: PropTypes.func,
reRender: PropTypes.func
};

Entities.defaultProps = {
Expand Down
8 changes: 8 additions & 0 deletions src/components/LinkModal/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@ export const languageTreeSourceQuery = gql`
}
}
`;

export const entityQuery = gql`
query getEntity($id: LingvodocID!) {
entity(id: $id) {
marked_for_deletion
}
}
`;
62 changes: 49 additions & 13 deletions src/components/LinkModal/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useContext } from "react";
import { InMemoryCache } from '@apollo/client';
import { Button, Checkbox, Dimmer, Header, Icon, Message, Modal, Segment, Tab } from "semantic-ui-react";
import { graphql } from "@apollo/client/react/hoc";
import { graphql, withApollo } from "@apollo/client/react/hoc";
import { isEqual } from "lodash";
import PropTypes from "prop-types";
import { compose } from "recompose";
Expand All @@ -12,7 +13,7 @@ import Tree from "components/GroupingTagModal/Tree";
import { LexicalEntryByIds, queryLexicalEntries, queryPerspective } from "components/PerspectiveView";
import TranslationContext from "Layout/TranslationContext";

import { acceptMutation, createMutation, languageTreeSourceQuery, publishMutation, removeMutation } from "./graphql";
import { acceptMutation, createMutation, languageTreeSourceQuery, publishMutation, removeMutation, entityQuery } from "./graphql";

const ModalContentWrapper = styled("div")`
min-height: 60vh;
Expand Down Expand Up @@ -67,23 +68,46 @@ ViewLink.propTypes = {
};

const EditLink = props => {
const { lexicalEntry, column, allLanguages, allDictionaries, allPerspectives, create, remove } = props;
const { client, lexicalEntry, column, allLanguages, allDictionaries, allPerspectives, create, remove } = props;

const getTranslation = useContext(TranslationContext);

const tree = buildTree(lexicalEntry, column, allLanguages, allDictionaries, allPerspectives);

const get_link = async entry => {
const entity = lexicalEntry.entities.find(
e => isEqual(e.link_id, entry.id) && isEqual(e.field_id, column.field_id)
);

if (!entity) return null;

//Checking in db
const result = await client.query({
query: entityQuery,
variables: { id: entity.id },
fetchPolicy: 'network-only'
});

if (!result.errors &&
result.data.entity &&
result.data.entity.marked_for_deletion === false) {
return entity;
}
return null;
}

const actions = [
{
title: getTranslation("Remove"),
action: entry => {
const entity = lexicalEntry.entities.find(
e => isEqual(e.link_id, entry.id) && isEqual(e.field_id, column.field_id)
);
if (entity) {
remove(entity);
}
get_link(entry).then(entity => {
if (entity) {
remove(entity);
console.log("Query result: ", lexicalEntry.entities);
}
});
},
enabled: entry => get_link(entry),
className: "lingvo-button-redder"
}
];
Expand Down Expand Up @@ -323,9 +347,14 @@ class LinkModalContent extends React.PureComponent {
}

removeEntity(entity) {
const { remove, lexicalEntry, entitiesMode } = this.props;
const { reRender, remove, lexicalEntry, entitiesMode } = this.props;

remove({
variables: { id: entity.id },
update(cache) {
cache.evict({ id: cache.identify(entity) });
cache.gc();
},
refetchQueries: [
{
// XXX: Expensive operation!
Expand All @@ -337,6 +366,9 @@ class LinkModalContent extends React.PureComponent {
}
]
});

// Refetching queryPerspective directly in PerspectiveView component
// reRender();
}

render() {
Expand Down Expand Up @@ -392,6 +424,7 @@ class LinkModalContent extends React.PureComponent {
remove={this.removeEntity}
publish={this.changePublished}
accept={this.changeAccepted}
client={this.props.client}
/>
</ModalContentWrapper>
);
Expand All @@ -414,15 +447,17 @@ LinkModalContent.propTypes = {
create: PropTypes.func.isRequired,
publish: PropTypes.func.isRequired,
accept: PropTypes.func.isRequired,
remove: PropTypes.func.isRequired
remove: PropTypes.func.isRequired,
reRender: PropTypes.func
};

const Content = compose(
graphql(languageTreeSourceQuery),
graphql(createMutation, { name: "create" }),
graphql(publishMutation, { name: "publish" }),
graphql(acceptMutation, { name: "accept" }),
graphql(removeMutation, { name: "remove" })
graphql(removeMutation, { name: "remove" }),
withApollo
)(LinkModalContent);

const LinkModal = props => {
Expand Down Expand Up @@ -453,7 +488,8 @@ LinkModal.propTypes = {
fieldId: PropTypes.array,
mode: PropTypes.string.isRequired,
entitiesMode: PropTypes.string.isRequired,
onClose: PropTypes.func.isRequired
onClose: PropTypes.func.isRequired,
reRender: PropTypes.func
};

LinkModal.defaultProps = {
Expand Down
7 changes: 5 additions & 2 deletions src/components/PerspectiveView/Cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const Cell = ({
resetCheckedAll,
mode,
entitiesMode,
disabled
disabled,
reRender
// eslint-disable-next-line arrow-body-style
}) => {
return (
Expand All @@ -44,6 +45,7 @@ const Cell = ({
mode={mode}
entitiesMode={entitiesMode}
disabled={disabled}
reRender={reRender}
/>
</Table.Cell>
);
Expand All @@ -63,7 +65,8 @@ Cell.propTypes = {
disabled: PropTypes.bool,
resetCheckedRow: PropTypes.func,
resetCheckedColumn: PropTypes.func,
resetCheckedAll: PropTypes.func
resetCheckedAll: PropTypes.func,
reRender: PropTypes.func
};

Cell.defaultProps = {
Expand Down
45 changes: 32 additions & 13 deletions src/components/PerspectiveView/Row.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useState } from "react";
import { Button, Checkbox, Table } from "semantic-ui-react";
import { isEmpty, isEqual, sortBy } from "lodash";
import PropTypes from "prop-types";
Expand Down Expand Up @@ -28,6 +28,7 @@ const Row = ({
resetCheckedRow,
resetCheckedColumn,
resetCheckedAll,
reRender,
/* eslint-disable react/prop-types */
showEntryId,
selectDisabled,
Expand All @@ -38,7 +39,9 @@ const Row = ({
}) => {
const entry_id_str = id2str(entry.id);

const disabled_flag = disabledEntrySet && disabledEntrySet.hasOwnProperty(entry_id_str);
const [ disabled, setDisabled ] = useState(false);

const disabled_flag = disabledEntrySet && disabledEntrySet.hasOwnProperty(entry_id_str) || disabled;

const remove_selection_flag = removeSelectionEntrySet && removeSelectionEntrySet.hasOwnProperty(entry_id_str);

Expand Down Expand Up @@ -93,20 +96,34 @@ const Row = ({
mode={mode}
entitiesMode={entitiesMode}
disabled={disabled_flag}
reRender={reRender}
/>
))}

{!isEmpty(actions) && (
<Table.Cell>
{actions.map(action => (
<Button
disabled={disabled_flag}
key={action.title}
content={action.title}
onClick={() => action.action(entry)}
className={action.className}
/>
))}
{actions.map(action => {
let reRenderWrapper;
if (action.enabled) {
action.enabled(entry).then(value => setDisabled(!value));
reRenderWrapper = reRender;
} else {
reRenderWrapper = () => setDisabled(true);
}

return(
<Button
disabled={disabled_flag}
key={action.title}
content={action.title}
onClick={() => {
action.action(entry);
reRenderWrapper();
}}
className={action.className}
/>
);
})}
</Table.Cell>
)}
</Table.Row>
Expand All @@ -131,7 +148,8 @@ Row.propTypes = {
onCheckRow: PropTypes.func,
resetCheckedRow: PropTypes.func,
resetCheckedColumn: PropTypes.func,
resetCheckedAll: PropTypes.func
resetCheckedAll: PropTypes.func,
reRender: PropTypes.func
};

Row.defaultProps = {
Expand All @@ -147,7 +165,8 @@ Row.defaultProps = {
onCheckRow: () => {},
resetCheckedRow: () => {},
resetCheckedColumn: () => {},
resetCheckedAll: () => {}
resetCheckedAll: () => {},
reRender: () => console.debug('Fake refetch')
};

export default onlyUpdateForKeys([
Expand Down
6 changes: 4 additions & 2 deletions src/components/PerspectiveView/TableBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ TableBody.propTypes = {
onCheckRow: PropTypes.func,
resetCheckedRow: PropTypes.func,
resetCheckedColumn: PropTypes.func,
resetCheckedAll: PropTypes.func
resetCheckedAll: PropTypes.func,
reRender: PropTypes.func
};

TableBody.defaultProps = {
Expand All @@ -47,7 +48,8 @@ TableBody.defaultProps = {
onCheckRow: () => {},
resetCheckedRow: () => {},
resetCheckedColumn: () => {},
resetCheckedAll: () => {}
resetCheckedAll: () => {},
reRender: () => console.log('Fake refetch')
};

export default onlyUpdateForKeys([
Expand Down
Loading

0 comments on commit 82d0c27

Please sign in to comment.