Skip to content

Commit

Permalink
fix(export): affichage du nombre de documents mis à jour (#1295)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-maillot authored Feb 20, 2024
1 parent 770a3a8 commit 684db49
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 37 deletions.
10 changes: 10 additions & 0 deletions shared/utils/src/array-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const groupBy = <T>(
array: T[],
predicate: (value: T, index: number, array: T[]) => string
) =>
array.reduce((acc, value, index, array) => {
(acc[predicate(value, index, array)] ||= []).push(value);
return acc;
}, {} as { [key: string]: T[] });

export { groupBy };
59 changes: 29 additions & 30 deletions targets/frontend/src/modules/export/components/document-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
} from "@mui/material";
import { Check, Cross } from "../../../components/utils/icons";
import { sourceToRoute } from "../../../components/documents/List";
import { ShortDocument } from "@shared/types";
import { ResultUpdatedDocument } from "../document.query";

type Props = {
docs: ShortDocument<any>[];
docs: ResultUpdatedDocument;
isLoadingDocs: boolean;
};
export default function DocumentList({
Expand All @@ -34,34 +34,33 @@ export default function DocumentList({
</Stack>
) : (
<>
{docs.length ? (
<li>
<Typography mb={0}>Pages information :</Typography>

{docs.map((doc) => (
<Stack direction="row" key={doc.slug}>
<Link
href={sourceToRoute({
id: doc.initial_id,
cdtnId: doc.cdtn_id,
source: doc.source,
})}
target="_blank"
sx={{ fontSize: "0.8rem" }}
>
{doc.title}
</Link>
{doc.isPublished ? (
<Check text="Publié" />
) : (
<Cross text="Dépublié" />
)}
</Stack>
))}
</li>
) : (
<></>
)}
{Array.from(docs.keys()).map((source) => {
return (
<li key={source}>
<Typography mb={0}>Pages {source} :</Typography>
{docs.get(source)?.map((doc) => (
<Stack direction="row" key={doc.slug}>
<Link
href={sourceToRoute({
id: doc.initial_id,
cdtnId: doc.cdtn_id,
source: doc.source,
})}
target="_blank"
sx={{ fontSize: "0.8rem" }}
>
{doc.title}
</Link>
{doc.is_published ? (
<Check text="Publié" />
) : (
<Cross text="Dépublié" />
)}
</Stack>
))}
</li>
);
})}
</>
)}
</ul>
Expand Down
26 changes: 19 additions & 7 deletions targets/frontend/src/modules/export/document.query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useQuery } from "urql";
import { SOURCES } from "@socialgouv/cdtn-sources";
import { ShortDocument } from "@shared/types";
import { SourceRoute, SOURCES } from "@socialgouv/cdtn-sources";
import { Document } from "@shared/types";
import { groupBy } from "graphql/jsutils/groupBy";

export const getDocumentsUpdatedAfterDateQuery = `
query GetDocuments($updated_at: timestamptz!, $sources: [String!]) {
Expand All @@ -14,30 +15,41 @@ query GetDocuments($updated_at: timestamptz!, $sources: [String!]) {
slug
cdtn_id
initial_id
isPublished: is_published
is_published
}
}`;

type QueryProps = {
date: Date;
};

export type UpdatedDocument = Pick<
Document<unknown>,
"title" | "source" | "slug" | "cdtn_id" | "initial_id" | "is_published"
>;

export type ResultUpdatedDocument = Map<
SourceRoute,
readonly UpdatedDocument[]
>;

type QueryResult = {
documents: ShortDocument<any>[];
documents: UpdatedDocument[];
};

export const useDocumentsQuery = ({
date,
}: QueryProps): ShortDocument<any>[] => {
}: QueryProps): ResultUpdatedDocument => {
const [result] = useQuery<QueryResult>({
query: getDocumentsUpdatedAfterDateQuery,
variables: {
updated_at: date,
sources: [SOURCES.LETTERS, SOURCES.EDITORIAL_CONTENT],
},
requestPolicy: "network-only",
});
if (!result.data) {
return [];
return new Map();
}
return result.data.documents;
return groupBy(result.data.documents, (data) => data.source);
};
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ update_permissions:
- updated_at
filter: {}
check: {}
set:
updated_at: now()
- role: user
permission:
columns:
Expand All @@ -143,6 +145,8 @@ update_permissions:
- updated_at
filter: {}
check: {}
set:
updated_at: now()
delete_permissions:
- role: super
permission:
Expand Down

0 comments on commit 684db49

Please sign in to comment.