Skip to content

Commit

Permalink
Merge branch 'feat/information' into carolineBda/list-modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
carolineBda committed Oct 19, 2023
2 parents e714a3f + 3406a87 commit 0d698d7
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 224 deletions.
2 changes: 0 additions & 2 deletions targets/frontend/src/modules/documents/api/documents.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const queryDocument = async (
client: ApiClient,
variables: DocumentsQueryProps
): Promise<Document | undefined> => {
console.log("queryDocument", query, variables);
const { data: result, error } = await client.query<QueryDocumentResult>(
query,
variables
Expand All @@ -43,7 +42,6 @@ export const queryDocument = async (
console.log(error);
throw error;
}
console.log("result", result);
const data = result?.documents[0];

return data;
Expand Down
58 changes: 28 additions & 30 deletions targets/frontend/src/modules/documents/api/documents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,34 @@ export class DocumentsService {
return {
name,
title,
blocks: blocks.map(
({
file,
img,
type,
content,
contentDisplayMode,
contents,
}) => {
return {
size: file?.size,
type,
imgUrl: img?.url,
fileUrl: file?.url,
markdown: content,
blockDisplayMode: contentDisplayMode,
contents: contents?.length
? contents.map(({ document }) => {
return {
title: document.title,
cdtnId: document.cdtnId,
source: document.source,
};
})
: undefined,
};
}
),
blocks: blocks.map((block) => {
return {
type: block.type,
markdown: block.content,
...(block.type === "graphic"
? {
size: block.file?.size,
imgUrl: block.img?.url,
altText: block.img?.altText,
fileUrl: block.file?.url,
}
: {}),
...(block.type === "content"
? {
blockDisplayMode: block.contentDisplayMode,
contents: block.contents?.length
? block.contents.map(({ document }) => {
return {
title: document.title,
cdtnId: document.cdtnId,
source: document.source,
};
})
: undefined,
}
: {}),
};
}),
references: references?.length
? [
{
Expand All @@ -95,12 +95,10 @@ export class DocumentsService {
}

public async publish(id: string, source: string) {
console.log("id", id);
let document = await this.documentsRepository.fetch({
source,
initialId: id,
});
console.log("currentDoc", document);
switch (source) {
case "information":
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export const informationsQuery = gql`
content
order
type
img {
url
altText
}
file {
id
url
Expand Down
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 @@ -127,7 +127,6 @@ export const InformationsContent = ({
appendBlock({
type: "markdown",
content: "",
contents: [],
})
}
>
Expand Down
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 Expand Up @@ -64,14 +65,19 @@ const mapInformationContentsBlocks = (
},
data:
blocks?.map((block, blockIndex) => {
const file = mapInformationContentsBlocksFile(block.file);
const img = mapInformationContentsBlocksFile(block.img);
const contents = mapInformationContentsBlocksContents(block.contents);
return {
...removeTypename(block),
file,
img,
contents,
...(block.type === "graphic"
? {
file: mapInformationContentsBlocksFile(block.file),
img: mapInformationContentsBlocksFile(block.img),
}
: {}),
...(block.type === "content"
? {
contents: mapInformationContentsBlocksContents(block.contents),
}
: {}),
order: blockIndex + 1,
};
}) ?? [],
Expand Down
Loading

0 comments on commit 0d698d7

Please sign in to comment.