Can update family settings

This commit is contained in:
Pierre HUBERT 2023-07-12 17:44:39 +02:00
parent 6cd9b29178
commit 4be127f58e
4 changed files with 130 additions and 0 deletions

View File

@ -16,6 +16,7 @@ import { BaseAuthenticatedPage } from "./widgets/BaseAuthenticatedPage";
import { BaseFamilyRoute } from "./widgets/BaseFamilyRoute";
import { BaseLoginPage } from "./widgets/BaseLoginpage";
import { FamilyUsersListRoute } from "./routes/family/FamilyUsersListRoute";
import { FamilySettingsRoute } from "./routes/family/FamilySettingsRoute";
interface AuthContext {
signedIn: boolean;
@ -46,6 +47,7 @@ export function App(): React.ReactElement {
<Route path="profile" element={<ProfileRoute />} />
<Route path="family/:familyId/*" element={<BaseFamilyRoute />}>
<Route path="" element={<FamilyHomeRoute />} />
<Route path="settings" element={<FamilySettingsRoute />} />
<Route path="users" element={<FamilyUsersListRoute />} />
<Route path="*" element={<NotFoundRoute />} />
</Route>

View File

@ -186,4 +186,20 @@ export class FamilyApi {
uri: `/family/${user.family_id}/user/${user.user_id}`,
});
}
/**
* Update a family settings
*/
static async UpdateFamily(settings: {
id: number;
name: string;
}): Promise<void> {
await APIClient.exec({
method: "PATCH",
uri: `/family/${settings.id}`,
jsonData: {
name: settings.name,
},
});
}
}

View File

@ -0,0 +1,105 @@
import {
Alert,
Box,
Button,
Card,
CardActions,
CardContent,
TextField,
Typography,
} from "@mui/material";
import React from "react";
import { FamilyApi } from "../../api/FamilyApi";
import { ServerApi } from "../../api/ServerApi";
import { useAlert } from "../../context_providers/AlertDialogProvider";
import { useFamily } from "../../widgets/BaseFamilyRoute";
import { formatDate } from "../../widgets/TimeWidget";
export function FamilySettingsRoute(): React.ReactElement {
const family = useFamily();
const alert = useAlert();
const [newName, setNewName] = React.useState(family.family.name);
const canEdit = family.family.is_admin;
const [error, setError] = React.useState<string | null>(null);
const [success, setSuccess] = React.useState<string | null>(null);
const updateFamily = async () => {
try {
setError(null);
setSuccess(null);
await FamilyApi.UpdateFamily({
id: family.family.family_id,
name: newName,
});
family.reloadFamilyInfo();
alert("Les paramètres de la famille ont été mis à jour avec succès !");
} catch (e) {
console.error(e);
setError("Echec de la mise à jour des paramètres de la famille !");
}
};
return (
<>
<Card style={{ margin: "10px auto", maxWidth: "450px" }}>
{error && <Alert severity="error">{error}</Alert>}
{success && <Alert severity="success">{success}</Alert>}
<CardContent>
<Typography gutterBottom variant="h5" component="div">
Paramètres de la famille
</Typography>
<Box
component="form"
sx={{
"& .MuiTextField-root": { my: 1 },
}}
noValidate
autoComplete="off"
>
<TextField
disabled
fullWidth
label="Identifiant"
value={family.family.family_id}
/>
<TextField
disabled
fullWidth
label="Création de la famille"
value={formatDate(family.family.time_create)}
/>
<TextField
fullWidth
label="Nom de la famille"
value={newName}
disabled={!canEdit}
onChange={(e) => setNewName(e.target.value)}
inputProps={{
maxLength: ServerApi.Config.constraints.family_name_len.max,
}}
/>
</Box>
</CardContent>
<CardActions>
<Button
onClick={updateFamily}
disabled={!canEdit}
style={{ marginLeft: "auto" }}
>
Enregistrer
</Button>
</CardActions>
</Card>
</>
);
}

View File

@ -98,6 +98,13 @@ pub async fn update(
f: FamilyInPathWithAdminMembership,
req: web::Json<UpdateFamilyBody>,
) -> HttpResult {
if !StaticConstraints::default()
.family_name_len
.validate(&req.name)
{
return Ok(HttpResponse::BadRequest().body("Invalid family name!"));
}
let mut family = families_service::get_by_id(f.family_id()).await?;
family.name = req.0.name;
families_service::update_family(&family).await?;