Skip to content

Commit

Permalink
feat(modeles): ajout des meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
m-maillot committed Oct 9, 2023
1 parent 40b25ef commit 4ce7403
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 168 deletions.
49 changes: 36 additions & 13 deletions targets/frontend/src/components/models/Common/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type FormData = Partial<Omit<Model, "createdAt">> & {
newFile?: DropzoneFile[];
};

type FormDataResult = Required<Omit<FormData, "newFile">>;
type FormDataResult = Required<Omit<FormData, "newFile" | "updatedAt">>;

type Props = {
model?: Model;
Expand All @@ -46,7 +46,9 @@ export const ModelForm = ({ model, onUpsert }: Props): React.ReactElement => {
defaultValues: {
updatedAt: "",
title: "",
metaTitle: "",
description: "",
metaDescription: "",
fileName: "",
fileSize: 0,
previewHTML: "",
Expand Down Expand Up @@ -79,21 +81,24 @@ export const ModelForm = ({ model, onUpsert }: Props): React.ReactElement => {
});
};

const onSubmit = async (newData: FormData) => {
const onSubmit = async (data: FormData) => {
try {
if (newData.newFile && newData.newFile.length === 1) {
await uploadFile(newData.newFile[0]);
if (data.newFile && data.newFile.length === 1) {
await uploadFile(data.newFile[0]);
}
// Form has been validated by react-hook-form
const newData = data as Required<FormData>;
await onUpsert({
id: newData.id!,
title: newData.title!,
description: newData.description!,
type: newData.type!,
fileName: newData.newFile ? newData.newFile[0].path : newData.fileName!,
fileSize: newData.newFile ? newData.newFile[0].size : newData.fileSize!,
previewHTML: newData.previewHTML!,
updatedAt: newData.updatedAt!,
legiReferences: newData.legiReferences!,
id: newData.id,
title: newData.title,
metaTitle: newData.metaTitle,
description: newData.description,
metaDescription: newData.metaDescription,
type: newData.type,
fileName: newData.newFile ? newData.newFile[0].path : newData.fileName,
fileSize: newData.newFile ? newData.newFile[0].size : newData.fileSize,
previewHTML: newData.previewHTML,
legiReferences: newData.legiReferences,
});
setSnack({
open: true,
Expand Down Expand Up @@ -208,6 +213,24 @@ export const ModelForm = ({ model, onUpsert }: Props): React.ReactElement => {
<FormControl>
<LegiReferenceInput name="legiReferences" control={control} />
</FormControl>
<FormControl>
<FormTextField
name="metaTitle"
control={control}
label="Méta titre (SEO)"
rules={{ required: true }}
fullWidth
/>
</FormControl>
<FormControl>
<FormTextField
name="metaDescription"
control={control}
label="Méta description (SEO)"
rules={{ required: true }}
fullWidth
/>
</FormControl>
<FormControl>
<FormFileField
name="newFile"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { LegiReference } from "src/components/forms/LegiReferences/type";

type ModelsInsertInput = {
description?: String;
meta_description?: String;
file_name?: String;
file_size?: number;
id?: string;
preview_html?: String;
title?: String;
meta_title?: String;
type?: String;
models_legi_references: {
data: {
Expand All @@ -30,9 +32,10 @@ export type MutationProps = Pick<
Model,
| "id"
| "title"
| "metaTitle"
| "type"
| "updatedAt"
| "description"
| "metaDescription"
| "fileSize"
| "fileName"
| "previewHTML"
Expand All @@ -58,8 +61,10 @@ export const useModelInsertMutation = (): MutationFn => {
model: {
id: data.id,
title: data.title,
meta_title: data.metaTitle,
type: data.type,
description: data.description,
meta_description: data.metaDescription,
file_size: data.fileSize,
file_name: data.fileName,
preview_html: data.previewHTML,
Expand Down
13 changes: 9 additions & 4 deletions targets/frontend/src/components/models/Edition/model.mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { LegiReference } from "src/components/forms/LegiReferences/type";

type ModelsInsertInput = {
description?: String;
meta_description?: String;
file_name?: String;
file_size?: number;
preview_html?: String;
title?: String;
meta_title?: String;
type?: String;
};

Expand All @@ -21,7 +23,7 @@ const updateModelQuery = gql`
update_model_models_by_pk(pk_columns: { id: $id }, _set: $model) {
id
}
delete_model_models_legi_references(where: { letter_id: { _eq: $id } }) {
delete_model_models_legi_references(where: { model_id: { _eq: $id } }) {
affected_rows
}
insert_model_models_legi_references(
Expand All @@ -37,9 +39,10 @@ export type MutationProps = Pick<
Model,
| "id"
| "title"
| "metaTitle"
| "type"
| "updatedAt"
| "description"
| "metaDescription"
| "fileSize"
| "fileName"
| "previewHTML"
Expand All @@ -55,7 +58,7 @@ type MutationGraphQLProps = {
model: ModelsInsertInput;
legiReferences: {
article_id: string;
letter_id: string;
model_id: string;
}[];
};

Expand All @@ -72,8 +75,10 @@ export const useModelUpdateMutation = (): MutationFn => {
const result = await executeUpdate({
model: {
title: data.title,
meta_title: data.metaTitle,
type: data.type,
description: data.description,
meta_description: data.metaDescription,
file_size: data.fileSize,
file_name: data.fileName,
preview_html: data.previewHTML,
Expand All @@ -94,7 +99,7 @@ export const useModelUpdateMutation = (): MutationFn => {

const formatLegiReferences = (modelId: string, refs: LegiReference[]) => {
return refs.map((ref) => ({
letter_id: modelId,
model_id: modelId,
article_id: ref.legiArticle.id,
}));
};
2 changes: 2 additions & 0 deletions targets/frontend/src/components/models/Edition/model.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ export const listModelsQuery = gql`
model: model_models_by_pk(id: $id) {
id
title
metaTitle: meta_title
type
description
metaDescription: meta_description
fileName: file_name
fileSize: file_size
previewHTML: preview_html
Expand Down
2 changes: 2 additions & 0 deletions targets/frontend/src/components/models/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { LegiReference } from "src/components/contributions";
export type Model = {
id: string;
title: string;
metaTitle: string;
type: string;
updatedAt: string;
createdAt: string;
description: string;
metaDescription: string;
fileName: string;
fileSize: number;
previewHTML: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ array_relationships:
- name: models_legi_references
using:
foreign_key_constraint_on:
column: letter_id
column: model_id
table:
name: models_legi_references
schema: model
- name: models_other_references
using:
foreign_key_constraint_on:
column: letter_id
column: model_id
table:
name: models_other_references
schema: model
Expand All @@ -26,6 +26,8 @@ insert_permissions:
- file_name
- file_size
- id
- meta_description
- meta_title
- preview_html
- title
- type
Expand All @@ -37,6 +39,8 @@ select_permissions:
- file_size
- description
- file_name
- meta_description
- meta_title
- preview_html
- title
- type
Expand All @@ -51,6 +55,8 @@ update_permissions:
- description
- file_name
- file_size
- meta_description
- meta_title
- preview_html
- title
- type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ object_relationships:
foreign_key_constraint_on: article_id
- name: model
using:
foreign_key_constraint_on: letter_id
foreign_key_constraint_on: model_id
insert_permissions:
- role: super
permission:
check: {}
columns:
- article_id
- letter_id
- model_id
select_permissions:
- role: super
permission:
columns:
- article_id
- id
- letter_id
- model_id
filter: {}
delete_permissions:
- role: super
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ table:
object_relationships:
- name: model
using:
foreign_key_constraint_on: letter_id
foreign_key_constraint_on: model_id
insert_permissions:
- role: super
permission:
check: {}
columns:
- label
- letter_id
- model_id
- url
select_permissions:
- role: super
Expand All @@ -20,7 +20,7 @@ select_permissions:
- label
- url
- id
- letter_id
- model_id
filter: {}
update_permissions:
- role: super
Expand Down
Loading

0 comments on commit 4ce7403

Please sign in to comment.