After improving the faq fonctionality management in adding the pdf upload1
This commit is contained in:
parent
d272c49f46
commit
1ef445f3ee
@ -286,10 +286,20 @@ export const supportConversationUpdateBodySchema = atLeastOneField(
|
||||
|
||||
// —— FAQ ——
|
||||
|
||||
const faqCategoryDescriptionSchema = z
|
||||
.union([z.string().max(4000), z.null()])
|
||||
.optional()
|
||||
.transform((v) => {
|
||||
if (v === undefined) return undefined;
|
||||
if (v === null) return null;
|
||||
const trimmed = v.trim();
|
||||
return trimmed.length ? trimmed : null;
|
||||
});
|
||||
|
||||
export const faqCategoryCreateBodySchema = z.object({
|
||||
name: z.string().trim().min(1).max(200),
|
||||
slug: z.string().trim().max(200).optional(),
|
||||
description: z.string().trim().max(4000).nullable().optional(),
|
||||
description: faqCategoryDescriptionSchema,
|
||||
sortOrder: z.number().int().optional(),
|
||||
isActive: z.boolean().optional(),
|
||||
});
|
||||
@ -298,7 +308,7 @@ export const faqCategoryUpdateBodySchema = atLeastOneField(
|
||||
z.object({
|
||||
name: z.string().trim().min(1).max(200).optional(),
|
||||
slug: z.string().trim().max(200).optional(),
|
||||
description: z.string().trim().max(4000).nullable().optional(),
|
||||
description: faqCategoryDescriptionSchema,
|
||||
sortOrder: z.number().int().optional(),
|
||||
isActive: z.boolean().optional(),
|
||||
})
|
||||
|
||||
@ -14,6 +14,7 @@ import {
|
||||
MenuItem,
|
||||
Paper,
|
||||
Select,
|
||||
Snackbar,
|
||||
Stack,
|
||||
Switch,
|
||||
Tab,
|
||||
@ -159,6 +160,9 @@ export function FaqAdminPage() {
|
||||
});
|
||||
|
||||
const [docBusy, setDocBusy] = useState<'fr' | 'en' | null>(null);
|
||||
const [catToast, setCatToast] = useState<string | null>(null);
|
||||
/** After first save of a brand-new category, keep dialog open so PDFs can be attached. */
|
||||
const [catJustCreated, setCatJustCreated] = useState(false);
|
||||
|
||||
const saveCatM = useMutation({
|
||||
mutationFn: async () => {
|
||||
@ -166,14 +170,24 @@ export function FaqAdminPage() {
|
||||
const body = {
|
||||
name: catForm.name.trim(),
|
||||
slug: catForm.slug.trim() || undefined,
|
||||
description: catForm.description.trim() || null,
|
||||
sortOrder: catForm.sortOrder,
|
||||
description: catForm.description.trim() ? catForm.description.trim() : null,
|
||||
sortOrder: Number(catForm.sortOrder) || 0,
|
||||
isActive: catForm.isActive,
|
||||
};
|
||||
if (editingCat) return adminUpdateFaqCategory(editingCat.id, body);
|
||||
return adminCreateFaqCategory(body);
|
||||
// Capture create-vs-edit before the request; editingCat may flip after create.
|
||||
const creating = !editingCat;
|
||||
if (editingCat) {
|
||||
const updated = await adminUpdateFaqCategory(editingCat.id, body);
|
||||
return { saved: updated, creating };
|
||||
}
|
||||
const created = await adminCreateFaqCategory(body);
|
||||
return { saved: created, creating };
|
||||
},
|
||||
onSuccess: (saved) => {
|
||||
onSuccess: ({ saved, creating }) => {
|
||||
invalidate();
|
||||
setCatToast(t('admin.faqAdmin.categorySaved'));
|
||||
if (creating) {
|
||||
// Stay open once so the admin can attach optional PDFs.
|
||||
setEditingCat(saved);
|
||||
setCatForm({
|
||||
name: saved.name,
|
||||
@ -182,8 +196,14 @@ export function FaqAdminPage() {
|
||||
sortOrder: saved.sortOrder,
|
||||
isActive: saved.isActive !== false,
|
||||
});
|
||||
invalidate();
|
||||
// Keep dialog open after create/save so PDFs can be managed immediately.
|
||||
setCatJustCreated(true);
|
||||
return;
|
||||
}
|
||||
setCatDialogOpen(false);
|
||||
setEditingCat(null);
|
||||
setCatForm(emptyCategory());
|
||||
setDocBusy(null);
|
||||
setCatJustCreated(false);
|
||||
},
|
||||
});
|
||||
|
||||
@ -192,6 +212,8 @@ export function FaqAdminPage() {
|
||||
setEditingCat(null);
|
||||
setCatForm(emptyCategory());
|
||||
setDocBusy(null);
|
||||
setCatJustCreated(false);
|
||||
saveCatM.reset();
|
||||
};
|
||||
|
||||
const deleteCatM = useMutation({
|
||||
@ -251,6 +273,8 @@ export function FaqAdminPage() {
|
||||
const openCreateCat = () => {
|
||||
setEditingCat(null);
|
||||
setCatForm(emptyCategory());
|
||||
setCatJustCreated(false);
|
||||
saveCatM.reset();
|
||||
setCatDialogOpen(true);
|
||||
};
|
||||
|
||||
@ -263,6 +287,8 @@ export function FaqAdminPage() {
|
||||
sortOrder: cat.sortOrder,
|
||||
isActive: cat.isActive !== false,
|
||||
});
|
||||
setCatJustCreated(false);
|
||||
saveCatM.reset();
|
||||
setCatDialogOpen(true);
|
||||
};
|
||||
|
||||
@ -578,20 +604,36 @@ export function FaqAdminPage() {
|
||||
</Stack>
|
||||
</Box>
|
||||
|
||||
{catJustCreated && (
|
||||
<Alert severity="success">{t('admin.faqAdmin.categorySavedAttachPdfs')}</Alert>
|
||||
)}
|
||||
{saveCatM.error && <Alert severity="error">{(apiErrorMessage(saveCatM.error))}</Alert>}
|
||||
</Stack>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={closeCatDialog}>{t('common.cancel')}</Button>
|
||||
<DialogActions sx={{ px: 3, pb: 2, gap: 1, flexWrap: 'wrap' }}>
|
||||
{saveCatM.error && (
|
||||
<Typography variant="caption" color="error" sx={{ flex: '1 1 100%', mr: 'auto' }}>
|
||||
{apiErrorMessage(saveCatM.error)}
|
||||
</Typography>
|
||||
)}
|
||||
<Button onClick={closeCatDialog}>{catJustCreated ? t('common.close') : t('common.cancel')}</Button>
|
||||
<Button
|
||||
variant="contained"
|
||||
onClick={() => saveCatM.mutate()}
|
||||
disabled={saveCatM.isPending || docBusy !== null}
|
||||
>
|
||||
{t('common.save')}
|
||||
{saveCatM.isPending ? t('common.saving') : t('common.save')}
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
|
||||
<Snackbar
|
||||
open={Boolean(catToast)}
|
||||
autoHideDuration={3500}
|
||||
onClose={() => setCatToast(null)}
|
||||
message={catToast}
|
||||
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
@ -2793,6 +2793,8 @@ export const en = {
|
||||
orderField: 'Order',
|
||||
categoryRequired: 'Category and question are required',
|
||||
nameRequired: 'Name is required',
|
||||
categorySaved: 'Category saved',
|
||||
categorySavedAttachPdfs: 'Category saved. You can attach PDF guides below, then close.',
|
||||
documentsTitle: 'Documents to download',
|
||||
documentsHint: 'Optional. Attach a French and/or English PDF guide for this category.',
|
||||
pdfFrLabel: 'French PDF (FR)',
|
||||
|
||||
@ -2817,6 +2817,8 @@ export const fr = {
|
||||
orderField: 'Ordre',
|
||||
categoryRequired: 'Catégorie et question requises',
|
||||
nameRequired: 'Nom requis',
|
||||
categorySaved: 'Catégorie enregistrée',
|
||||
categorySavedAttachPdfs: 'Catégorie enregistrée. Vous pouvez joindre les PDF ci-dessous, puis fermer.',
|
||||
documentsTitle: 'Documents à télécharger',
|
||||
documentsHint: 'Optionnel. Joignez un guide PDF en français et/ou en anglais pour cette catégorie.',
|
||||
pdfFrLabel: 'PDF français (FR)',
|
||||
|
||||
Loading…
Reference in New Issue
Block a user