-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ajouter une nouvelle contribution
- Loading branch information
Showing
13 changed files
with
268 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
targets/frontend/src/components/contributions/questions/common/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./Form"; |
56 changes: 56 additions & 0 deletions
56
targets/frontend/src/components/contributions/questions/creation/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Form, QuestionFormData } from "../common"; | ||
import { useQuestionCreationMutation } from "./question.mutation"; | ||
import { useQuestionCreationDataQuery } from "./question.query"; | ||
import { useRouter } from "next/router"; | ||
import { Link, Stack, Typography } from "@mui/material"; | ||
import SentimentVeryDissatisfiedIcon from "@mui/icons-material/SentimentVeryDissatisfied"; | ||
|
||
export const NewQuestion = (): JSX.Element => { | ||
const router = useRouter(); | ||
const { data } = useQuestionCreationDataQuery(); | ||
const create = useQuestionCreationMutation(); | ||
|
||
const onCreate = async (formData: QuestionFormData) => { | ||
console.log("On create new contrib"); | ||
if (!data) { | ||
throw new Error("Missing agreement list to create a new question"); | ||
} | ||
const result = await create({ | ||
order: formData.order, | ||
seo_title: formData.seo_title ? formData.seo_title : undefined, | ||
content: formData.content, | ||
message_id: formData.message_id ? formData.message_id : undefined, // use to transform empty string sent by the form to undefined | ||
answers: { | ||
data: data.agreementIds.map((item) => ({ | ||
display_date: "01/01/2025", | ||
agreement_id: item.id, | ||
content_type: item.unextended ? "NOTHING" : undefined, | ||
})), | ||
}, | ||
}); | ||
|
||
router.push( | ||
`/contributions/questions/${result.insert_contribution_questions_one.id}` | ||
); | ||
}; | ||
if (!data) { | ||
return ( | ||
<Stack alignItems="center" spacing={2}> | ||
<SentimentVeryDissatisfiedIcon color="error" sx={{ fontSize: 70 }} /> | ||
<Typography variant="h5" component="h3" color="error"> | ||
Une erreur est survenue | ||
</Typography> | ||
<Link href={"/contributions"}>Retour à la liste des contributions</Link> | ||
</Stack> | ||
); | ||
} | ||
return ( | ||
<> | ||
<Form | ||
messages={data.messages} | ||
onSubmit={onCreate} | ||
defaultOrder={data.nextOrder} | ||
/> | ||
</> | ||
); | ||
}; |
52 changes: 52 additions & 0 deletions
52
targets/frontend/src/components/contributions/questions/creation/question.mutation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { gql, useMutation } from "urql"; | ||
|
||
import { Answer, Question } from "../../type"; | ||
|
||
const questionCreationMutation = gql` | ||
mutation CreateQuestion($question: contribution_questions_insert_input!) { | ||
insert_contribution_questions_one(object: $question) { | ||
id | ||
} | ||
} | ||
`; | ||
|
||
type MutationProps = { | ||
question: Pick<Question, "content" | "message_id" | "seo_title" | "order"> & { | ||
answers: { | ||
data: { | ||
display_date: Answer["displayDate"]; | ||
agreement_id: Answer["agreementId"]; | ||
content_type?: Answer["contentType"]; | ||
}[]; | ||
}; | ||
}; | ||
}; | ||
|
||
type Result = { | ||
insert_contribution_questions_one: { | ||
id: string; | ||
}; | ||
}; | ||
|
||
export type MutationResult = ( | ||
props: MutationProps["question"] | ||
) => Promise<Result>; | ||
|
||
export const useQuestionCreationMutation = (): MutationResult => { | ||
const [, executeUpdate] = useMutation<Result, MutationProps>( | ||
questionCreationMutation | ||
); | ||
const resultFunction = async (question: MutationProps["question"]) => { | ||
const result = await executeUpdate({ question }); | ||
if (result.error) { | ||
throw new Error(result.error.message); | ||
} | ||
if (!result.data) { | ||
throw new Error( | ||
"No data returned from 'useQuestionCreationMutation' mutation" | ||
); | ||
} | ||
return result.data; | ||
}; | ||
return resultFunction; | ||
}; |
64 changes: 64 additions & 0 deletions
64
targets/frontend/src/components/contributions/questions/creation/question.query.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { CombinedError, gql, useQuery } from "urql"; | ||
import { Message } from "../../type"; | ||
|
||
export const contributionQuestionCreationDataQuery = gql` | ||
query SelectQuestionCreationData { | ||
messages: contribution_question_messages { | ||
contentAgreement | ||
contentLegal | ||
contentNotHandled | ||
contentNotHandledWithoutLegal | ||
contentAgreementWithoutLegal | ||
id | ||
label | ||
} | ||
agreements: agreement_agreements(where: { isSupported: { _eq: true } }) { | ||
id | ||
unextended | ||
} | ||
maxOrder: contribution_questions(order_by: { order: desc }, limit: 1) { | ||
order | ||
} | ||
} | ||
`; | ||
|
||
type QueryOutput = { | ||
messages: Message[]; | ||
agreements: { id: string; unextended: boolean }[]; | ||
maxOrder: [{ order: number }]; | ||
}; | ||
|
||
export type QueryResult = { | ||
messages: Message[]; | ||
agreementIds: { id: string; unextended: boolean }[]; | ||
nextOrder: number; | ||
}; | ||
|
||
type Result = { | ||
data?: QueryResult; | ||
error?: CombinedError; | ||
fetching: boolean; | ||
}; | ||
|
||
export const useQuestionCreationDataQuery = (): Result => { | ||
const [{ data, error, fetching }] = useQuery<QueryOutput>({ | ||
query: contributionQuestionCreationDataQuery, | ||
requestPolicy: "cache-and-network", | ||
}); | ||
|
||
let formattedData: Result["data"] = undefined; | ||
if (data) { | ||
formattedData = { | ||
messages: data.messages, | ||
agreementIds: data.agreements | ||
.flatMap((item) => item) | ||
.concat([{ id: "0000", unextended: false }]), | ||
nextOrder: data.maxOrder[0].order + 1, | ||
}; | ||
} | ||
return { | ||
data: formattedData, | ||
fetching, | ||
error, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.