Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(contributions): ajout d'un selecteur de references pour les fiches SP #1029

Merged
merged 10 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions targets/frontend/src/components/contributions/answers/Answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { statusesMapping } from "../status/data";
import { getNextStatus, getPrimaryButtonLabel } from "../status/utils";
import { SnackBar } from "../../utils/SnackBar";
import { FicheSpReferenceInput } from "./references/FicheSpReferenceInput";
import { Breadcrumb, BreadcrumbLink } from "src/components/utils";

export type ContributionsAnswerProps = {
Expand Down Expand Up @@ -62,6 +63,11 @@ export const ContributionsAnswer = ({
kaliReferences: [],
otherReferences: [],
cdtnReferences: [],
contentFichesSpReference: answer?.contentFichesSpDocument
? {
document: {},
}
: undefined,
},
});
const updateAnswer = useContributionAnswerUpdateMutation();
Expand Down Expand Up @@ -97,7 +103,8 @@ export const ContributionsAnswer = ({
otherAnswer: data.otherAnswer,
status: newStatus,
userId: user?.id,
urlSp: data.urlSp,
contentServicePublicCdtnId:
data.contentFichesSpReference?.document.cdtnId,
kaliReferences: data.kaliReferences,
legiReferences: data.legiReferences,
cdtnReferences: data.cdtnReferences,
Expand Down Expand Up @@ -206,17 +213,11 @@ export const ContributionsAnswer = ({
/>
)}
{answer && isCodeDuTravail(answer) && (
<FormControl>
<FormTextField
label="Url Service public"
name="urlSp"
disabled={isNotEditable(answer)}
control={control}
rules={{
required: answer && answer.otherAnswer === "SP",
}}
/>
</FormControl>
<FicheSpReferenceInput
name="contentFichesSpReference"
control={control}
disabled={isNotEditable(answer)}
/>
)}
{answer && !isCodeDuTravail(answer) && (
<KaliReferenceInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
} from "./answerReferences";

export const contributionAnswerUpdateMutation = `
mutation contributionAnswerUpdate($id: uuid!, $content: String, $otherAnswer: String, $status: statustype!, $userId: uuid!, $urlSp: String, $kaliReferences: [contribution_answer_kali_references_insert_input!]!, $legiReferences: [contribution_answer_legi_references_insert_input!]!, $otherReferences: [contribution_answer_other_references_insert_input!]!, $cdtnReferences: [contribution_answer_cdtn_references_insert_input!]!) {
update_contribution_answers_by_pk(pk_columns: {id: $id}, _set: {content: $content, other_answer: $otherAnswer, url_sp: $urlSp}) {
mutation contributionAnswerUpdate($id: uuid!, $content: String, $otherAnswer: String, $status: statustype!, $userId: uuid!, $contentServicePublicCdtnId: String, $kaliReferences: [contribution_answer_kali_references_insert_input!]!, $legiReferences: [contribution_answer_legi_references_insert_input!]!, $otherReferences: [contribution_answer_other_references_insert_input!]!, $cdtnReferences: [contribution_answer_cdtn_references_insert_input!]!) {
update_contribution_answers_by_pk(pk_columns: {id: $id}, _set: {content: $content, other_answer: $otherAnswer, content_service_public_cdtn_id: $contentServicePublicCdtnId}) {
__typename
}
insert_contribution_answer_statuses_one(object: {status: $status, user_id: $userId, answer_id: $id}) {
Expand Down Expand Up @@ -49,7 +49,7 @@ export type MutationProps = Pick<
Answer,
| "id"
| "otherAnswer"
| "urlSp"
| "contentServicePublicCdtnId"
| "content"
| "kaliReferences"
| "legiReferences"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useQuery } from "urql";
import { useMemo } from "react";
import { format, parseISO } from "date-fns";

import { Answer, AnswerStatus } from "../type";
import { Answer, AnswerStatus, CdtnReference, Document } from "../type";
m-maillot marked this conversation as resolved.
Show resolved Hide resolved
import { initStatus } from "../status/utils";

const contributionAnswerQuery = `
Expand All @@ -14,7 +14,7 @@ query contribution_answer($id: uuid) {
content
otherAnswer: other_answer
updatedAt: updated_at
urlSp: url_sp
contentServicePublicCdtnId: content_service_public_cdtn_id
question {
id
content
Expand Down Expand Up @@ -69,6 +69,12 @@ query contribution_answer($id: uuid) {
slug
}
}
contentFichesSpDocument: document {
cdtnId: cdtn_id
title
source
slug
}
}
}
`;
Expand Down Expand Up @@ -111,5 +117,10 @@ export const useContributionAnswerQuery = ({
...answer,
status: initStatus(answer),
updateDate: format(parseISO(answer.updatedAt), "dd/MM/yyyy"),
contentFichesSpReference: answer.contentFichesSpDocument
? {
document: answer.contentFichesSpDocument,
}
: undefined,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
LegiReference,
CdtnReference,
OtherReference,
Document,
m-maillot marked this conversation as resolved.
Show resolved Hide resolved
} from "../type";

export const formatKaliReferences = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const CdtnReferenceInput = ({
disabled = false,
}: Props): React.ReactElement => (
<ReferenceInput<CdtnReference>
isMultiple={true}
maxgfr marked this conversation as resolved.
Show resolved Hide resolved
label={`Contenus liés`}
color="info"
name={name}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { getRouteBySource } from "@socialgouv/cdtn-sources";
import { Control } from "react-hook-form";
import { CdtnReference } from "../../type";
import { ReferenceInput } from "./ReferenceInput";
import { useFicheSpSearchCdtnReferencesQuery } from "./ficheSpReferenceSearch.query";

type Props = {
name: string;
control: Control<any>;
disabled?: boolean;
};

export const FicheSpReferenceInput = ({
name,
control,
disabled = false,
}: Props): React.ReactElement => (
<ReferenceInput<CdtnReference>
label={`Fiche service-public`}
color="info"
name={name}
disabled={disabled}
control={control}
fetcher={useFicheSpSearchCdtnReferencesQuery}
isEqual={(option, value) =>
value.document.cdtnId === option.document.cdtnId
}
getLabel={(item) => `${item.document.title} (${item.document.slug})`}
onClick={(item) => {
const newWindow = window.open(
`https://code.travail.gouv.fr/${getRouteBySource(
item.document.source
)}/${item.document.slug}`,
"_blank",
"noopener,noreferrer"
);
if (newWindow) newWindow.opener = null;
}}
/>
);
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const LegiReferenceInput = ({
}: Props): React.ReactElement => {
return (
<ReferenceInput<LegiReference>
isMultiple={true}
maxgfr marked this conversation as resolved.
Show resolved Hide resolved
label={`Références liées au code du travail`}
color="success"
name={name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Props<Type> = {
| "success"
| "warning";
disabled: boolean;
isMultiple?: true;
};

export const ReferenceInput = <Type,>({
Expand All @@ -40,6 +41,7 @@ export const ReferenceInput = <Type,>({
onClick,
color,
disabled,
isMultiple,
}: Props<Type>): ReactElement | null => {
const [query, setQuery] = useState<string | undefined>();
const { data, fetching, error } = fetcher(query);
Expand All @@ -59,6 +61,7 @@ export const ReferenceInput = <Type,>({

return (
<FormAutocompleteMultiple<Type>
multiple={isMultiple}
maxgfr marked this conversation as resolved.
Show resolved Hide resolved
noOptionsText={"Aucun résultat trouvé"}
control={control}
getOptionLabel={getLabel}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useQuery } from "urql";

import { CdtnReference, Document } from "../../type";
import { Result } from "./ReferenceInput";

type QueryResult = {
documents: Document[];
};

export const useFicheSpSearchCdtnReferencesQuery = (
query: string | undefined
): Result<Pick<CdtnReference, "document">> => {
const [slug] = query?.split("/").reverse() ?? [""];
const title = `%${slug
?.split(/[\ \-\,]/gm)
?.map((text) => text.normalize().replace(/[\u0300-\u036f]/g, ""))
.join("%")}%`;
const [{ data, fetching, error }] = useQuery<QueryResult>({
query: `
query SearchCdtnReferences($sources: [String!], $slug: String, $title: String) {
documents(where: {
_or: [{
title: {_ilike: $title}
}, {
slug: {_eq: $slug}
}],
is_available: {_eq: true},
is_published: {_eq: true},
source: {_in: $sources}
},
order_by: {
created_at: asc
},
limit: 10
) {
title
cdtnId: cdtn_id
source
slug
}
}
`,
variables: {
sources: ["fiches_service_public"],
slug,
title,
},
});
return {
data: data?.documents.map((document) => ({ document })) ?? [],
error,
fetching,
};
};
4 changes: 3 additions & 1 deletion targets/frontend/src/components/contributions/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export type Answer = {
agreementId: string;
questionId: string;
otherAnswer?: OtherAnswer;
urlSp?: string;
contentServicePublicCdtnId?: string;
agreement: Agreement;
statuses: AnswerStatus[];
status: AnswerStatus;
Expand All @@ -110,4 +110,6 @@ export type Answer = {
legiReferences: LegiReference[];
otherReferences: OtherReference[];
cdtnReferences: CdtnReference[];
contentFichesSpDocument?: Document;
contentFichesSpReference?: CdtnReference;
maxgfr marked this conversation as resolved.
Show resolved Hide resolved
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type AutocompleteFormProps<T> = PropsWithChildren<
) => React.ReactNode;
filterOptions: (options: T[], state: FilterOptionsState<T>) => T[];
noOptionsText?: React.ReactNode;
multiple?: true;
}
>;

Expand All @@ -61,6 +62,7 @@ export const FormAutocompleteMultiple = <T,>({
filterOptions,
noOptionsText,
disabled,
multiple,
}: AutocompleteFormProps<T>) => {
return (
<Controller
Expand All @@ -74,7 +76,7 @@ export const FormAutocompleteMultiple = <T,>({
forcePopupIcon={false}
disabled={disabled}
disableClearable
multiple
multiple={multiple}
maxgfr marked this conversation as resolved.
Show resolved Hide resolved
id={`id-${label}`}
value={value}
onChange={(event, value) => onChange(value)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ object_relationships:
- name: agreement
using:
foreign_key_constraint_on: agreement_id
- name: document
using:
foreign_key_constraint_on: content_service_public_cdtn_id
- name: question
using:
foreign_key_constraint_on: question_id
Expand Down Expand Up @@ -63,7 +66,7 @@ select_permissions:
- other_answer
- question_id
- updated_at
- url_sp
- content_service_public_cdtn_id
filter: {}
allow_aggregations: true
update_permissions:
Expand All @@ -72,7 +75,7 @@ update_permissions:
columns:
- content
- other_answer
- url_sp
- content_service_public_cdtn_id
filter: {}
check: null
set:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "contribution"."answers" rename column "content_service_public_cdtn_id" to "url_sp";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "contribution"."answers" rename column "url_sp" to "content_service_public_cdtn_id";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "contribution"."answers" drop constraint "answers_content_service_public_cdtn_id_fkey";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "contribution"."answers"
add constraint "answers_content_service_public_cdtn_id_fkey"
foreign key ("content_service_public_cdtn_id")
references "public"."documents"
("cdtn_id") on update restrict on delete restrict;
Loading