import { useCallback, useState } from 'react'; import { Alert, ScrollView, TextInput, View } from 'react-native'; import { useFocusEffect } from 'expo-router'; import { api } from '../src/api'; import { describe } from '../src/errors'; import { useStore } from '../src/session'; import { Banner, Button, Card, T } from '../src/ui'; import { font, useColors } from '../src/theme'; interface Overview { allowed: boolean; audience: { cards: number; appleDevices: number }; nextAllowedAt: string | null; items: { id: string; message: string; sentByName: string; cards: number; createdAt: string }[]; } const MAX = 160; const when = (d: string) => new Date(d).toLocaleString('fr-FR', { day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit' }); /** Campagnes (gérants, formule Pro) : un message envoyé en notification à toutes les cartes du magasin. */ export default function Campaigns() { const c = useColors(); const store = useStore(); const [data, setData] = useState(null); const [message, setMessage] = useState(''); const [error, setError] = useState(null); const [sent, setSent] = useState(false); const [busy, setBusy] = useState(false); const load = useCallback(async () => { try { setData(await api.get('/api/staff/campaigns')); } catch (e) { setError(describe(e).detail); } }, []); useFocusEffect( useCallback(() => { load(); }, [load]), ); const text = message.trim(); const blocked = !data || !data.allowed || !!data.nextAllowedAt; const send = () => Alert.alert('Envoyer la campagne ?', `${data!.audience.cards} cartes de ${store.name} recevront :\n\n« ${text} »`, [ { text: 'Annuler', style: 'cancel' }, { text: 'Envoyer', onPress: async () => { setBusy(true); setError(null); try { await api.post('/api/staff/campaigns', { message: text }); setMessage(''); setSent(true); await load(); } catch (e) { setError(describe(e).detail); } finally { setBusy(false); } }, }, ]); return ( {error && {error}} {sent && Campagne envoyée. Les cartes de vos clients se mettent à jour dans les minutes qui suivent.} {data && !data.allowed && Les campagnes sont incluses dans la formule Pro. Contactez MonEspacePro pour changer de formule.} Votre message apparaît en notification sur le téléphone de vos clients et reste visible sur leur carte. {data ? ` ${data.audience.cards} cartes au total.` : ''} {text.length} / {MAX} · 1 campagne par jour maximum {data?.nextAllowedAt && Prochain envoi possible le {when(data.nextAllowedAt)}.}