Skip to content

Commit

Permalink
fix: issues with mapping and requests
Browse files Browse the repository at this point in the history
  • Loading branch information
m-maillot committed Oct 12, 2023
1 parent 41ce871 commit 8c987ff
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,85 +1,74 @@
import { CombinedError, useQuery } from "urql";
import { CombinedError, OperationContext, useQuery } from "urql";
import { format, parseISO } from "date-fns";

import { Information } from "../../type";
import { gql } from "@urql/core";

const informationsQuery = `query informations($id: uuid) {
information_informations(
where: {
id: { _eq: $id }
}
) {
description
const informationsQuery = gql`
query informations($id: uuid) {
information_informations(where: { id: { _eq: $id } }) {
description
id
intro
metaDescription
metaTitle
referenceLabel
sectionDisplayMode
title
updatedAt
dismissalProcess
contents(order_by: { order: asc }) {
id
intro
metaDescription
metaTitle
referenceLabel
sectionDisplayMode
name
title
updatedAt
dismissalProcess
contents(
order_by: {order: asc}
) {
referenceLabel
order
blocks(order_by: { order: asc }) {
id
name
title
referenceLabel
content
order
blocks(
order_by: {order: asc}
) {
type
file {
id
content
order
type
file {
id
url
altText
size
}
img {
id
url
altText
size
}
contentDisplayMode
contents(
order_by: {order: asc}
) {
id
document {
cdtnId: cdtn_id
source
title
slug
}
}
url
altText
size
}
references(
order_by: {order: asc}
) {
img {
id
url
type
title
order
altText
size
}
contentDisplayMode
contents(order_by: { order: asc }) {
id
document {
cdtnId: cdtn_id
source
title
slug
}
}
}
references(
order_by: {order: asc}
) {
references(order_by: { order: asc }) {
id
url
type
title
order
}
}
references(order_by: { order: asc }) {
id
url
type
title
order
}
}
}`;
}
`;

export type QueryInformation = Information;

Expand All @@ -99,12 +88,13 @@ export type InformationsQueryResult = {
data?: InformationsResult;
error?: CombinedError;
fetching: boolean;
reexecuteQuery: (opts?: Partial<OperationContext> | undefined) => void;
};

export const useInformationsQuery = ({
id,
}: InformationsQueryProps): InformationsQueryResult => {
const [{ data, error, fetching }] = useQuery<QueryResult>({
const [{ data, error, fetching }, reexecuteQuery] = useQuery<QueryResult>({
query: informationsQuery,
requestPolicy: "cache-and-network",
variables: {
Expand All @@ -124,5 +114,6 @@ export const useInformationsQuery = ({
: undefined,
error,
fetching,
reexecuteQuery,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export type EditInformationProps = {
};

export const InformationsEdit = ({ id }: EditInformationProps): JSX.Element => {
const { data: information, fetching } = useInformationsQuery({ id });
const {
data: information,
fetching,
reexecuteQuery,
} = useInformationsQuery({ id });
const router = useRouter();

const [snack, setSnack] = useState<{
Expand Down Expand Up @@ -74,7 +78,7 @@ export const InformationsEdit = ({ id }: EditInformationProps): JSX.Element => {
onUpsert={async (upsertData) => {
try {
const idUpsert = await onUpsert(upsertData);
await router.push(`/informations/${idUpsert}`);
reexecuteQuery({ requestPolicy: "network-only" });
setSnack({
open: true,
severity: "success",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { getRawColumns } from "../editInformation.mapping";

test("All rows should be included (with null value also)", () => {
const rows = getRawColumns({
id: "58d8f41e-b31b-47a2-a9d9-f079773ae86a",
name: "Cas 1",
title: "Cas 1",
referenceLabel: null,
order: 1,
blocks: [
{
id: "f0e86b70-475d-4156-9df7-06a9fdf6e6e1",
content: "Toto",
type: "markdown",
order: 1,
},
{
id: "ebe5f760-4907-47be-88bb-edc244a7849d",
content: "test",
type: "markdown",
order: 2,
},
{
id: "cfadf03c-f540-4904-bc27-69877f9d9be9",
content: "Test 3",
type: "markdown",
order: 3,
},
{
id: "73223a87-e142-44e8-9cfb-d1353e1bf71d",
content: "Test 4",
type: "markdown",
order: 4,
},
],
references: [],
});

expect(rows).toEqual(["id", "name", "title", "referenceLabel", "order"]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ const removeTypename = (obj: any) => {
return obj;
};

const getRawColumns = (obj?: any): string[] => {
export const getRawColumns = (obj?: any): string[] => {
if (!obj) return [];
return Object.entries(obj).reduce<string[]>((result, [key, value]) => {
if (key === "__typename" || typeof value === "object") return result;
if (key === "__typename" || (value && typeof value === "object"))
return result;
return [...result, key];
}, []);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,59 @@ import { Information } from "../../type";

import { mapInformation } from "./editInformation.mapping";
import { getElementsToDelete } from "src/lib/mutationUtils";
import { gql } from "@urql/core";

export const informationMutation = `
mutation edit_information(
$upsert: information_informations_insert_input!,
$contentIdsToDelete: [uuid!],
$referenceIdsToDelete: [uuid!],
$contentBlockIdsToDelete: [uuid!],
$contentBlockContentIdsToDelete: [uuid!],
$contentReferenceIdsToDelete: [uuid!]
) {
insert_information_informations_one(
object: $upsert,
on_conflict: {
constraint: informations_pkey,
update_columns: [description,intro, metaTitle, metaDescription,referenceLabel,sectionDisplayMode]
}
) {
id
}
delete_information_informations_references (
where: {id: {_in: $referenceIdsToDelete}}
) {
affectedRows: affected_rows
}
delete_information_informations_contents (
where: {id: {_in: $contentIdsToDelete}}
) {
affectedRows: affected_rows
}
delete_information_informations_contents_references (
where: {id: {_in: $contentReferenceIdsToDelete}}
) {
affectedRows: affected_rows
}
delete_information_informations_contents_blocks (
where: {id: {_in: $contentBlockIdsToDelete}}
export const informationMutation = gql`
mutation edit_information(
$upsert: information_informations_insert_input!
$contentIdsToDelete: [uuid!]
$referenceIdsToDelete: [uuid!]
$contentBlockIdsToDelete: [uuid!]
$contentBlockContentIdsToDelete: [uuid!]
$contentReferenceIdsToDelete: [uuid!]
) {
affectedRows: affected_rows
}
delete_information_informations_contents_blocks_contents (
where: {id: {_in: $contentBlockContentIdsToDelete}}
) {
affectedRows: affected_rows
delete_information_informations_references(
where: { id: { _in: $referenceIdsToDelete } }
) {
affectedRows: affected_rows
}
delete_information_informations_contents(
where: { id: { _in: $contentIdsToDelete } }
) {
affectedRows: affected_rows
}
delete_information_informations_contents_references(
where: { id: { _in: $contentReferenceIdsToDelete } }
) {
affectedRows: affected_rows
}
delete_information_informations_contents_blocks(
where: { id: { _in: $contentBlockIdsToDelete } }
) {
affectedRows: affected_rows
}
delete_information_informations_contents_blocks_contents(
where: { id: { _in: $contentBlockContentIdsToDelete } }
) {
affectedRows: affected_rows
}
insert_information_informations_one(
object: $upsert
on_conflict: {
constraint: informations_pkey
update_columns: [
description
intro
metaTitle
metaDescription
referenceLabel
sectionDisplayMode
]
}
) {
id
}
}
}
`;

export type EditInformationMutationResult = {
Expand Down

0 comments on commit 8c987ff

Please sign in to comment.