Skip to content

Commit

Permalink
Fixed a bug due to adding and deleting pages and sections.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamGodwin2 committed Aug 5, 2019
1 parent 50ecd6a commit 491b5b0
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 78 deletions.
2 changes: 1 addition & 1 deletion eq-author-api/schema/resolvers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ const Resolvers = {
const section = find(ctx.questionnaire.sections, { id: input.id });
remove(ctx.questionnaire.sections, section);
onPageDeleted(ctx, input.id);
return section;
return ctx.questionnaire;
}),
moveSection: createMutation((_, { input }, ctx) => {
const removedSection = first(
Expand Down
2 changes: 1 addition & 1 deletion eq-author-api/schema/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ type Mutation {
duplicateQuestionnaire(input: DuplicateQuestionnaireInput!): Questionnaire
createSection(input: CreateSectionInput!): Section
updateSection(input: UpdateSectionInput!): Section
deleteSection(input: DeleteSectionInput!): Section
deleteSection(input: DeleteSectionInput!): Questionnaire
moveSection(input: MoveSectionInput!): Section
duplicateSection(input: DuplicateSectionInput!): Section
updatePage(input: UpdatePageInput!): Page
Expand Down
6 changes: 3 additions & 3 deletions eq-author/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@
"svgo-loader": "latest",
"terser-webpack-plugin": "latest",
"url-loader": "latest",
"wait-for-expect": "latest",
"webpack": "latest",
"webpack-dev-server": "latest",
"webpack-manifest-plugin": "latest",
"wait-for-expect": "latest"
"webpack-manifest-plugin": "latest"
},
"dependencies": {
"@sentry/browser": "latest",
Expand Down Expand Up @@ -104,7 +104,7 @@
"lodash": "latest",
"polished": "latest",
"react": "latest",
"react-apollo": "latest",
"react-apollo": "^2.5.8",
"react-dom": "latest",
"react-firebaseui": "latest",
"react-hot-loader": "latest",
Expand Down
6 changes: 5 additions & 1 deletion eq-author/src/App/page/Design/MovePageModal/MovePageQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import query from "graphql/getQuestionnaire.graphql";
import PropTypes from "prop-types";

const MovePageQuery = ({ questionnaireId, children }) => (
<Query query={query} variables={{ input: { questionnaireId } }}>
<Query
query={query}
partialRefetch
variables={{ input: { questionnaireId } }}
>
{({ loading, data }) => children({ loading, data })}
</Query>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import PropTypes from "prop-types";
const MoveSectionQuery = ({ questionnaireId, children }) => (
<Query
query={query}
partialRefetch
variables={{
input: {
questionnaireId,
Expand Down
39 changes: 7 additions & 32 deletions eq-author/src/App/section/Design/withDeleteSection.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { graphql } from "react-apollo";
import { remove, find, flowRight } from "lodash";
import { find, flowRight } from "lodash";
import gql from "graphql-tag";

import { withShowToast } from "components/Toasts";

import deleteSectionMutation from "graphql/deleteSection.graphql";
import fragment from "graphql/questionnaireFragment.graphql";

import getNextSection from "utils/getNextOnDelete";
import { buildPagePath } from "utils/UrlUtils";
Expand All @@ -27,15 +26,17 @@ const pluralize = (count, word, plural = word + "s") => {

export const handleDeletion = (
{ history, onAddSection, match: { params } },
questionnaire
{ data },
oldQuestionnaire
) => {
const questionnaire = data.deleteSection;
const { sectionId, questionnaireId } = params;

if (questionnaire.sections.length === 1) {
if (questionnaire.sections.length === 0) {
return onAddSection();
}

const nextSection = getNextSection(questionnaire.sections, sectionId);
const nextSection = getNextSection(oldQuestionnaire.sections, sectionId);
const nextPage = nextSection.pages[0];

history.push(
Expand All @@ -47,30 +48,6 @@ export const handleDeletion = (
);
};

export const deleteUpdater = (questionnaireId, sectionId) => (
proxy,
result
) => {
const id = `Questionnaire${questionnaireId}`;
const questionnaire = proxy.readFragment({ id, fragment });

remove(questionnaire.sections, { id: sectionId });

const sections = questionnaire.sections.map(section => ({
...section,
questionnaire: result.data.deleteSection.questionnaire,
}));

proxy.writeFragment({
id,
fragment,
data: {
...questionnaire,
sections,
},
});
};

export const displayToast = (ownProps, questionnaire) => {
const {
match: { params },
Expand All @@ -94,7 +71,6 @@ export const mapMutateToProps = ({ ownProps, mutate }) => ({
client,
} = ownProps;
const section = { id: sectionId };
const update = deleteUpdater(params.questionnaireId, sectionId);

const questionnaire = client.readFragment({
id: `Questionnaire${params.questionnaireId}`,
Expand All @@ -103,11 +79,10 @@ export const mapMutateToProps = ({ ownProps, mutate }) => ({

const mutation = mutate({
variables: { input: section },
update,
});

return mutation
.then(() => handleDeletion(ownProps, questionnaire))
.then(data => handleDeletion(ownProps, data, questionnaire))
.then(() => displayToast(ownProps, questionnaire))
.then(() => mutation);
},
Expand Down
42 changes: 10 additions & 32 deletions eq-author/src/App/section/Design/withDeleteSection.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
mapMutateToProps,
deleteUpdater,
handleDeletion,
} from "./withDeleteSection";
import fragment from "graphql/questionnaireFragment.graphql";
import { mapMutateToProps, handleDeletion } from "./withDeleteSection";

describe("withDeleteSection", () => {
let history, mutate, result, ownProps, onAddSection, showToast;
Expand Down Expand Up @@ -43,7 +38,10 @@ describe("withDeleteSection", () => {

result = {
data: {
deleteSection: deletedPage,
deleteSection: {
id: "questionnaire",
sections: [],
},
},
};

Expand All @@ -70,25 +68,6 @@ describe("withDeleteSection", () => {
mutate = jest.fn(() => Promise.resolve(result));
});

describe("deleteUpdater", () => {
it("should remove the section from the cache", () => {
const id = `Questionnaire${questionnaire.id}`;
const readFragment = jest.fn(() => questionnaire);
const writeFragment = jest.fn();

const updater = deleteUpdater(questionnaire.id, currentSection.id);
updater({ readFragment, writeFragment }, result);

expect(readFragment).toHaveBeenCalledWith({ id, fragment });
expect(writeFragment).toHaveBeenCalledWith({
id,
fragment,
data: questionnaire,
});
expect(questionnaire.sections).not.toContain(currentSection);
});
});

describe("mapMutateToProps", () => {
let props;

Expand Down Expand Up @@ -152,19 +131,18 @@ describe("withDeleteSection", () => {

describe("handleDeletion", () => {
describe("when only one section in questionnaire", () => {
beforeEach(() => {
questionnaire.sections = [currentSection];
});

it("should add new section", () => {
handleDeletion(ownProps, questionnaire);
handleDeletion(ownProps, result, {});
expect(onAddSection).toHaveBeenCalled();
});
});

describe("when more than one section in questionnaire", () => {
it("should redirect to another section", () => {
handleDeletion(ownProps, questionnaire);
result.data.deleteSection.sections = [
{ id: "section 1", pages: [{ id: "page 1" }] },
];
handleDeletion(ownProps, result, questionnaire);
expect(history.push).toHaveBeenCalled();
});
});
Expand Down
8 changes: 4 additions & 4 deletions eq-author/src/graphql/deleteSection.graphql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mutation DeleteSection($input: DeleteSectionInput!) {
deleteSection(input: $input) {
id
questionnaire {
sections {
id
questionnaireInfo {
totalSectionCount
}
}
questionnaireInfo {
totalSectionCount
}
}
}
9 changes: 5 additions & 4 deletions eq-author/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9467,12 +9467,13 @@ rc@^1.2.7:
minimist "^1.2.0"
strip-json-comments "~2.0.1"

react-apollo@latest:
version "2.5.6"
resolved "https://registry.yarnpkg.com/react-apollo/-/react-apollo-2.5.6.tgz#98a59d0eea31432ed001e6a033e11a58139ffc31"
integrity sha512-WWX5UykTtmW6+awjqEsSWSdvVyZv/vsavUgpdI4ddn4CBdz47INC+iTdJBnYaUFMB24GmqjFFSoSd98gu1xqKA==
react-apollo@^2.5.8:
version "2.5.8"
resolved "https://registry.yarnpkg.com/react-apollo/-/react-apollo-2.5.8.tgz#c7a593b027efeefdd8399885e0ac6bec3b32623c"
integrity sha512-60yOQrnNosxU/tRbOxGDaYNLFcOKmQqxHPhxyvKTlGIaF/rRCXQRKixUgWVffpEupSHHD7psY5k5ZOuZsdsSGQ==
dependencies:
apollo-utilities "^1.3.0"
fast-json-stable-stringify "^2.0.0"
hoist-non-react-statics "^3.3.0"
lodash.isequal "^4.5.0"
prop-types "^15.7.2"
Expand Down

0 comments on commit 491b5b0

Please sign in to comment.