|
|
|
@ -1,4 +1,5 @@ |
|
|
|
import React, { useState, useEffect, useMemo } from 'react' |
|
|
|
import { useTranslation } from '@/i18n' |
|
|
|
import { Ic } from '@/icons' |
|
|
|
import { Button } from '@/components/ui/button' |
|
|
|
import { Input } from '@/components/ui/input' |
|
|
|
@ -60,6 +61,7 @@ function normalizeCityName(rawName: string): string { |
|
|
|
} |
|
|
|
|
|
|
|
export function DuasServiceView() { |
|
|
|
const { t } = useTranslation() |
|
|
|
const [duas, setDuas] = useState<DuaItem[]>([]) |
|
|
|
const [isLoading, setIsLoading] = useState(true) |
|
|
|
const [isRefreshing, setIsRefreshing] = useState(false) |
|
|
|
@ -95,6 +97,14 @@ export function DuasServiceView() { |
|
|
|
const [formNameEn, setFormNameEn] = useState('') |
|
|
|
const [formNameRu, setFormNameRu] = useState('') |
|
|
|
const [formNameId, setFormNameId] = useState('') |
|
|
|
const [formDescriptions, setFormDescriptions] = useState<Record<'fa' | 'ar' | 'en' | 'ru' | 'id', string>>({ |
|
|
|
fa: '', |
|
|
|
ar: '', |
|
|
|
en: '', |
|
|
|
ru: '', |
|
|
|
id: '', |
|
|
|
}) |
|
|
|
const [activeDescLang, setActiveDescLang] = useState<'fa' | 'ar' | 'en' | 'ru' | 'id'>('fa') |
|
|
|
const [formCityIds, setFormCityIds] = useState<number[]>([]) |
|
|
|
// نگهداری دستهبندیهای انتخابشده به تفکیک هر شهر (cityId -> categoryIds[]) جهت استقلال کامل هر شهر
|
|
|
|
const [cityCategoryMap, setCityCategoryMap] = useState<Record<number, number[]>>({}) |
|
|
|
@ -125,8 +135,19 @@ export function DuasServiceView() { |
|
|
|
// Tab 2 Verses states
|
|
|
|
const [verseSearch, setVerseSearch] = useState('') |
|
|
|
const [activeTranslationLang, setActiveTranslationLang] = useState<'fa' | 'en' | 'ru' | 'id'>('fa') |
|
|
|
const [activeVerseDescLang, setActiveVerseDescLang] = useState<'fa' | 'ar' | 'en' | 'ru' | 'id'>('fa') |
|
|
|
const [copiedVerseIndex, setCopiedVerseIndex] = useState<number | null>(null) |
|
|
|
const [verseDrafts, setVerseDrafts] = useState<Record<number, { text: string; translations: Record<string, string> }>>({}) |
|
|
|
const [verseDrafts, setVerseDrafts] = useState< |
|
|
|
Record< |
|
|
|
number, |
|
|
|
{ |
|
|
|
text: string |
|
|
|
translations: Record<string, string> |
|
|
|
descriptions: Record<string, string> |
|
|
|
repeat_count: number | null |
|
|
|
} |
|
|
|
> |
|
|
|
>({}) |
|
|
|
const [savingVerseIndex, setSavingVerseIndex] = useState<number | null>(null) |
|
|
|
const [savedVerseSuccessIndex, setSavedVerseSuccessIndex] = useState<number | null>(null) |
|
|
|
|
|
|
|
@ -352,6 +373,14 @@ export function DuasServiceView() { |
|
|
|
setFormNameEn(enVal) |
|
|
|
setFormNameRu(ruVal) |
|
|
|
setFormNameId(idVal) |
|
|
|
const initDesc = (dua.description && typeof dua.description === 'object') ? dua.description : {} |
|
|
|
setFormDescriptions({ |
|
|
|
fa: initDesc.fa || '', |
|
|
|
ar: initDesc.ar || '', |
|
|
|
en: initDesc.en || '', |
|
|
|
ru: initDesc.ru || '', |
|
|
|
id: initDesc.id || '', |
|
|
|
}) |
|
|
|
setHasAttemptedSaveGeneral(false) |
|
|
|
|
|
|
|
// Cities: فقط شهرهای معتبر که در سامانه دستهبندی دارند به صورت اولیه انتخاب شوند
|
|
|
|
@ -405,12 +434,37 @@ export function DuasServiceView() { |
|
|
|
const detail = await fetchDuaDetail(dua.index) |
|
|
|
setDuaDetail(detail) |
|
|
|
|
|
|
|
const detDesc = (detail.description && typeof detail.description === 'object') ? detail.description : {} |
|
|
|
setFormDescriptions({ |
|
|
|
fa: detDesc.fa || '', |
|
|
|
ar: detDesc.ar || '', |
|
|
|
en: detDesc.en || '', |
|
|
|
ru: detDesc.ru || '', |
|
|
|
id: detDesc.id || '', |
|
|
|
}) |
|
|
|
|
|
|
|
// Initialize verse drafts
|
|
|
|
const drafts: Record<number, { text: string; translations: Record<string, string> }> = {} |
|
|
|
const drafts: Record< |
|
|
|
number, |
|
|
|
{ |
|
|
|
text: string |
|
|
|
translations: Record<string, string> |
|
|
|
descriptions: Record<string, string> |
|
|
|
repeat_count: number | null |
|
|
|
} |
|
|
|
> = {} |
|
|
|
detail.verses?.forEach((v) => { |
|
|
|
drafts[v.index] = { |
|
|
|
text: v.text, |
|
|
|
translations: { ...(v.translations || {}) }, |
|
|
|
descriptions: { |
|
|
|
fa: v.descriptions?.fa || '', |
|
|
|
ar: v.descriptions?.ar || '', |
|
|
|
en: v.descriptions?.en || '', |
|
|
|
ru: v.descriptions?.ru || '', |
|
|
|
id: v.descriptions?.id || '', |
|
|
|
}, |
|
|
|
repeat_count: v.repeat_count ?? null, |
|
|
|
} |
|
|
|
}) |
|
|
|
setVerseDrafts(drafts) |
|
|
|
@ -617,8 +671,15 @@ export function DuasServiceView() { |
|
|
|
]) |
|
|
|
) |
|
|
|
|
|
|
|
const descriptionObj: Record<string, string> = {} |
|
|
|
;(['fa', 'ar', 'en', 'ru', 'id'] as const).forEach((l) => { |
|
|
|
const val = (formDescriptions[l] || '').trim() |
|
|
|
if (val) descriptionObj[l] = val |
|
|
|
}) |
|
|
|
|
|
|
|
const updated = await updateDua(selectedDuaForModal.index, { |
|
|
|
name: nameObj, |
|
|
|
description: descriptionObj, |
|
|
|
city_ids: activeCityIds, |
|
|
|
category_ids: allActiveCategoryIds, |
|
|
|
city_allocations: cityAllocations, |
|
|
|
@ -636,6 +697,7 @@ export function DuasServiceView() { |
|
|
|
...d, |
|
|
|
name: updated.name, |
|
|
|
name_display: updated.name_display || formNameFa || formNameAr, |
|
|
|
description: updated.description || descriptionObj, |
|
|
|
cities: updated.cities, |
|
|
|
city_ids: Array.isArray(updated.city_ids) ? updated.city_ids : activeCityIds, |
|
|
|
categories: updated.categories, |
|
|
|
@ -647,7 +709,7 @@ export function DuasServiceView() { |
|
|
|
) |
|
|
|
// بستن مدال پس از ذخیرهسازی موفقیتآمیز
|
|
|
|
setSelectedDuaForModal(null) |
|
|
|
toast.success('اطلاعات عمومی دعا با موفقیت در دیتابیس ذخیره شد.') |
|
|
|
toast.success(t('duasAdmin.saveGeneralSuccess')) |
|
|
|
|
|
|
|
// تازهسازی خودکار دادههای صفحه از سرور
|
|
|
|
await loadDuas(true) |
|
|
|
@ -667,9 +729,11 @@ export function DuasServiceView() { |
|
|
|
await updateVerse(verseIndex, { |
|
|
|
text: draft.text, |
|
|
|
translations: draft.translations, |
|
|
|
descriptions: draft.descriptions, |
|
|
|
repeat_count: draft.repeat_count && draft.repeat_count > 0 ? draft.repeat_count : null, |
|
|
|
}) |
|
|
|
setSavedVerseSuccessIndex(verseIndex) |
|
|
|
toast.success(`فراز با موفقیت در دیتابیس ذخیره شد.`) |
|
|
|
toast.success(t('duasAdmin.saveVerseSuccess')) |
|
|
|
setTimeout(() => setSavedVerseSuccessIndex(null), 2500) |
|
|
|
} catch (err: any) { |
|
|
|
toast.error(err?.message || 'خطا در ذخیرهسازی فراز در سرور.') |
|
|
|
@ -1455,6 +1519,50 @@ export function DuasServiceView() { |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
|
|
|
|
{/* ۱.۵. توضیحات و فضیلت مفاتیحالجنان (۵ زبانه) */} |
|
|
|
<div className="rounded-2xl border border-border-soft bg-card/50 p-4 space-y-3"> |
|
|
|
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2"> |
|
|
|
<div> |
|
|
|
<span className="text-xs font-bold text-foreground flex items-center gap-1.5"> |
|
|
|
<Ic name="book" className="size-3.5 text-primary" /> |
|
|
|
<span>{t('duasAdmin.mafatihIntroTitle')}</span> |
|
|
|
</span> |
|
|
|
<span className="text-[11px] text-muted-foreground block mt-0.5"> |
|
|
|
{t('duasAdmin.mafatihIntroHint')} |
|
|
|
</span> |
|
|
|
</div> |
|
|
|
<div className="flex items-center gap-1 flex-wrap shrink-0"> |
|
|
|
{(['fa', 'ar', 'en', 'ru', 'id'] as const).map((lang) => ( |
|
|
|
<button |
|
|
|
key={lang} |
|
|
|
type="button" |
|
|
|
onClick={() => setActiveDescLang(lang)} |
|
|
|
className={`text-[11px] px-2.5 py-1 rounded-lg font-mono font-bold transition cursor-pointer ${ |
|
|
|
activeDescLang === lang |
|
|
|
? 'bg-primary text-primary-foreground shadow-xs' |
|
|
|
: 'bg-card text-muted-foreground hover:text-foreground border border-border-soft' |
|
|
|
}`}
|
|
|
|
> |
|
|
|
{lang.toUpperCase()} |
|
|
|
</button> |
|
|
|
))} |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
<textarea |
|
|
|
value={formDescriptions[activeDescLang] || ''} |
|
|
|
onChange={(e) => |
|
|
|
setFormDescriptions((prev) => ({ |
|
|
|
...prev, |
|
|
|
[activeDescLang]: e.target.value, |
|
|
|
})) |
|
|
|
} |
|
|
|
dir={activeDescLang === 'fa' || activeDescLang === 'ar' ? 'rtl' : 'ltr'} |
|
|
|
rows={4} |
|
|
|
placeholder={t('duasAdmin.mafatihIntroPlaceholder')} |
|
|
|
className="w-full p-3 rounded-xl bg-card border border-border-soft text-xs text-foreground leading-relaxed focus:outline-hidden focus:ring-1 focus:ring-primary resize-y" |
|
|
|
/> |
|
|
|
</div> |
|
|
|
|
|
|
|
{/* ۲. انتساب شهر و دستهبندیها (انتخابگونه و خاموش/روشن) */} |
|
|
|
<div className="space-y-3 pt-2.5 border-t border-border-soft/70"> |
|
|
|
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2"> |
|
|
|
@ -1889,8 +1997,17 @@ export function DuasServiceView() { |
|
|
|
const draft = verseDrafts[verse.index] || { |
|
|
|
text: verse.text, |
|
|
|
translations: { ...(verse.translations || {}) }, |
|
|
|
descriptions: { |
|
|
|
fa: verse.descriptions?.fa || '', |
|
|
|
ar: verse.descriptions?.ar || '', |
|
|
|
en: verse.descriptions?.en || '', |
|
|
|
ru: verse.descriptions?.ru || '', |
|
|
|
id: verse.descriptions?.id || '', |
|
|
|
}, |
|
|
|
repeat_count: verse.repeat_count ?? null, |
|
|
|
} |
|
|
|
const activeTranslation = draft.translations?.[activeTranslationLang] || '' |
|
|
|
const activeVerseDesc = draft.descriptions?.[activeVerseDescLang] || '' |
|
|
|
const isSavingThis = savingVerseIndex === verse.index |
|
|
|
const isSavedSuccess = savedVerseSuccessIndex === verse.index |
|
|
|
|
|
|
|
@ -1900,10 +2017,17 @@ export function DuasServiceView() { |
|
|
|
className="p-4 rounded-2xl border border-border-soft bg-card/30 space-y-3 hover:border-primary/30 transition shadow-2xs" |
|
|
|
> |
|
|
|
{/* هدر فراز */} |
|
|
|
<div className="flex items-center justify-between"> |
|
|
|
<span className="text-[11px] font-mono font-bold text-primary"> |
|
|
|
فراز {toFa(index + 1)} از {toFa(duaDetail?.verses?.length || 0)} (کد: #{verse.index}) |
|
|
|
</span> |
|
|
|
<div className="flex flex-wrap items-center justify-between gap-2"> |
|
|
|
<div className="flex items-center gap-2"> |
|
|
|
<span className="text-[11px] font-mono font-bold text-primary"> |
|
|
|
فراز {toFa(index + 1)} از {toFa(duaDetail?.verses?.length || 0)} (کد: #{verse.index}) |
|
|
|
</span> |
|
|
|
{draft.repeat_count && draft.repeat_count > 1 ? ( |
|
|
|
<Badge variant="outline" className="text-[10px] font-mono px-2 py-0 border-primary/30 text-primary bg-primary/5"> |
|
|
|
×{draft.repeat_count} {t('duasAdmin.repeatCountSuffix')} |
|
|
|
</Badge> |
|
|
|
) : null} |
|
|
|
</div> |
|
|
|
<div className="flex items-center gap-1.5"> |
|
|
|
<Button |
|
|
|
variant="ghost" |
|
|
|
@ -1938,6 +2062,77 @@ export function DuasServiceView() { |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
|
|
|
|
{/* بخش توضیحات بینفرازی مفاتیح (۵ زبانه شامل عربی) و شمارنده تکرار */} |
|
|
|
<div className="p-3 rounded-xl border border-border-soft bg-muted/20 space-y-2"> |
|
|
|
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2"> |
|
|
|
<label className="text-[11px] font-bold text-foreground"> |
|
|
|
{t('duasAdmin.verseDescLabel')} ({activeVerseDescLang.toUpperCase()}): |
|
|
|
</label> |
|
|
|
<div className="flex items-center gap-2 flex-wrap"> |
|
|
|
<div className="flex items-center gap-1"> |
|
|
|
{(['fa', 'ar', 'en', 'ru', 'id'] as const).map((lang) => ( |
|
|
|
<button |
|
|
|
key={lang} |
|
|
|
type="button" |
|
|
|
onClick={() => setActiveVerseDescLang(lang)} |
|
|
|
className={`text-[10px] px-2 py-0.5 rounded-md font-mono font-bold transition cursor-pointer ${ |
|
|
|
activeVerseDescLang === lang |
|
|
|
? 'bg-primary text-primary-foreground' |
|
|
|
: 'bg-card text-muted-foreground hover:text-foreground border border-border-soft' |
|
|
|
}`}
|
|
|
|
> |
|
|
|
{lang.toUpperCase()} |
|
|
|
</button> |
|
|
|
))} |
|
|
|
</div> |
|
|
|
<div className="flex items-center gap-1.5"> |
|
|
|
<span className="text-[10px] text-muted-foreground font-medium"> |
|
|
|
{t('duasAdmin.repeatCountLabel')}: |
|
|
|
</span> |
|
|
|
<Input |
|
|
|
type="number" |
|
|
|
min={0} |
|
|
|
value={draft.repeat_count ?? ''} |
|
|
|
onChange={(e) => { |
|
|
|
const raw = e.target.value.trim() |
|
|
|
const parsed = raw === '' ? null : Math.max(0, parseInt(raw, 10) || 0) |
|
|
|
setVerseDrafts((prev) => ({ |
|
|
|
...prev, |
|
|
|
[verse.index]: { |
|
|
|
...draft, |
|
|
|
repeat_count: parsed && parsed > 0 ? parsed : null, |
|
|
|
}, |
|
|
|
})) |
|
|
|
}} |
|
|
|
placeholder={t('duasAdmin.repeatCountPlaceholder')} |
|
|
|
className="w-24 h-7 text-xs font-mono bg-card border-border-soft rounded-lg text-center" |
|
|
|
dir="ltr" |
|
|
|
/> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
<textarea |
|
|
|
value={activeVerseDesc} |
|
|
|
onChange={(e) => { |
|
|
|
const newDesc = e.target.value |
|
|
|
setVerseDrafts((prev) => ({ |
|
|
|
...prev, |
|
|
|
[verse.index]: { |
|
|
|
...draft, |
|
|
|
descriptions: { |
|
|
|
...(draft.descriptions || {}), |
|
|
|
[activeVerseDescLang]: newDesc, |
|
|
|
}, |
|
|
|
}, |
|
|
|
})) |
|
|
|
}} |
|
|
|
dir={activeVerseDescLang === 'fa' || activeVerseDescLang === 'ar' ? 'rtl' : 'ltr'} |
|
|
|
rows={2} |
|
|
|
placeholder={t('duasAdmin.verseDescPlaceholder')} |
|
|
|
className="w-full p-2.5 rounded-xl bg-card border border-border-soft text-xs text-foreground leading-relaxed focus:outline-hidden focus:ring-1 focus:ring-primary resize-y" |
|
|
|
/> |
|
|
|
</div> |
|
|
|
|
|
|
|
{/* فیلد ادیت متن اصلی عربی */} |
|
|
|
<div className="space-y-1"> |
|
|
|
<label className="block text-[11px] font-bold text-foreground"> |
|
|
|
@ -1950,9 +2145,8 @@ export function DuasServiceView() { |
|
|
|
setVerseDrafts((prev) => ({ |
|
|
|
...prev, |
|
|
|
[verse.index]: { |
|
|
|
...prev[verse.index], |
|
|
|
...draft, |
|
|
|
text: newText, |
|
|
|
translations: prev[verse.index]?.translations || {}, |
|
|
|
}, |
|
|
|
})) |
|
|
|
}} |
|
|
|
@ -1974,9 +2168,9 @@ export function DuasServiceView() { |
|
|
|
setVerseDrafts((prev) => ({ |
|
|
|
...prev, |
|
|
|
[verse.index]: { |
|
|
|
text: prev[verse.index]?.text || verse.text, |
|
|
|
...draft, |
|
|
|
translations: { |
|
|
|
...(prev[verse.index]?.translations || {}), |
|
|
|
...(draft.translations || {}), |
|
|
|
[activeTranslationLang]: newTr, |
|
|
|
}, |
|
|
|
}, |
|
|
|
|