Skip to content

Commit

Permalink
feat(form): Display form status and last modification time
Browse files Browse the repository at this point in the history
  • Loading branch information
remiriv committed Jan 4, 2023
1 parent 463fcdc commit 2c43674
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Game() {
<>
<Box sx={{ mt: 2 }}>
<Typography variant="h3" sx={{ mb: 2 }}>
Atelier {game?.id}
Atelier {game?.code}
</Typography>
<GeneralInfo game={game} />
<Players game={game} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useEffect, useMemo, useState } from "react";
import { Button, Grid, Tooltip, Typography } from "@mui/material";
import { Button, Grid, Tooltip, Typography, useTheme } from "@mui/material";
import { CustomContainer } from "./styles/personalization";
import { BackArrow, BackArrowWithValidation } from "./common/BackArrow";
import { QuestionLine, QuestionText } from "./styles/form";
import { useForm } from "react-hook-form";
import { PersoFormInputList, PersoFormNumberInput } from "./common/FormInputs";
import {
formSections,
FormStatus,
formValues,
PersoForm,
persoFormInputs,
Expand All @@ -30,11 +31,13 @@ import { useQuery } from "react-query";
import { IGame } from "../../../utils/types";
import { ErrorAlert } from "../../alert";
import { Accordion } from "../../common/components/Accordion";
import { t } from "../../translations";

export { PersonalizationForm };

function PersonalizationForm() {
const gameId = useGameId();
const theme = useTheme();
const { profile, updateProfile } = usePlay();

const query = useQuery(`/api/games/${gameId}`, () => {
Expand Down Expand Up @@ -96,6 +99,22 @@ function PersonalizationForm() {
throw new Error("Unsupported question type");
};

const formatLastUpdateDate = (date: Date) => {
if (!date) {
return "Aucune action enregistrée";
}

const dateObj = new Date(date);
const day = dateObj.toLocaleDateString([], {
dateStyle: "short",
});
const time = new Date(date).toLocaleTimeString([], {
timeStyle: "short",
});

return `le ${day} à ${time}`;
};

const buildFormLine = (question: Question, display: boolean) => {
return (
<QuestionLine
Expand Down Expand Up @@ -174,6 +193,42 @@ function PersonalizationForm() {
<ErrorAlert alertPosition="top" message={formBlockText} />
)}
<form className="flex flex-col" onSubmit={handleSubmit(onSubmit)}>
{profile && profile.status && (
<Grid
container
direction="column"
justifyContent="space-between"
sx={{
border: `3px solid ${theme.palette.primary.contrastText}`,
borderRadius: 2,
p: 3,
width: "fit-content",
}}
>
<Typography
sx={{
color: theme.palette.primary.contrastText,
fontSize: "20px",
}}
>
<Typography component="span" sx={{ textDecoration: "underline" }}>
État du formulaire:
</Typography>{" "}
{t(`form.status.${profile.status as FormStatus}`)}
</Typography>
<Typography
sx={{
color: theme.palette.primary.contrastText,
fontSize: "20px",
}}
>
<Typography component="span" sx={{ textDecoration: "underline" }}>
{t(`form.last-action.${profile.status as FormStatus}`)}:
</Typography>{" "}
{formatLastUpdateDate(profile.lastStatusUpdate)}
</Typography>
</Grid>
)}
<Grid container direction="row" justifyContent="space-between">
{!isDirty && (
<BackArrow path={`/play/games/${gameId}/personalize/choice`} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { range } from "lodash";
import { buildChoices } from "../utils/choices";

export type FormStatus = "draft" | "pendingValidation" | "validated";

export const persoFormInputs = [
"numberAdults",
"numberKids",
Expand Down
11 changes: 10 additions & 1 deletion packages/client/src/modules/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,14 @@
"step.production-2.name": "Étape 4",
"step.production-2.title": "Choix de production 2",
"step.production-3.name": "Étape 5",
"step.production-3.title": "Choix de production 3"
"step.production-3.title": "Choix de production 3",

"form.status.draft": "Brouillon",
"form.status.pendingValidation": "Transmis à l'animateur - En attente de validation",
"form.status.validated": "Validé",

"form.last-action.draft": "Dernière sauvegarde",
"form.last-action.pendingValidation": "Dernier envoi à l'animateur",
"form.last-action.validated": "Validation"

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Profile" ADD COLUMN "lastStatusUpdate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
1 change: 1 addition & 0 deletions packages/server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ model Profile {
personalization Personalization @relation(fields: [personalizationId], references: [id])
personalizationId Int
status ProfileStatus @default(draft)
lastStatusUpdate DateTime @default(now())
}

enum ProfileStatus {
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/modules/players/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ async function setDefaultProfiles(
data: {
personalizationId: defaultPersonalization.id,
status: "validated",
lastStatusUpdate: new Date(),
},
});
await model.update({
Expand Down Expand Up @@ -134,6 +135,7 @@ async function validateProfiles(gameId: number): Promise<void> {
},
data: {
status: "validated",
lastStatusUpdate: new Date(),
},
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/modules/profiles/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ interface Profile {
personalization: Personalization;
personalizationId: number;
status: ProfileStatus;
lastStatusUpdate: Date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ function handleUpdateProfile(io: Server, socket: Socket) {
await update(personalizationId, personalizationData);
await profileServices.update(player?.profileId, {
status: profileStatus as ProfileStatus,
lastStatusUpdate: new Date(),
});
} else {
const personalization = await create(personalizationData);
const profile = await profileServices.create({
personalizationId: personalization.id,
status: profileStatus as ProfileStatus,
lastStatusUpdate: new Date(),
});
await playersServices.update(gameId, userId, {
profileId: profile?.id,
Expand Down

0 comments on commit 2c43674

Please sign in to comment.