update iot panel will now listen to push from panel
This commit is contained in:
1 parent
20d04acb3e
commit
520c4bcaad
10 files changed
+158
-754
No files matched your search
+22
-44
@@ -57,7 +57,6 @@ const SettingsPage: React.FC = () => {
|
||||
const [kandangName, setKandangName] = useState('');
|
||||
const [feedInButtonUrls, setFeedInButtonUrls] = useState<FeedInButtonUrl[]>([emptyFeedInButtonUrl()]);
|
||||
const [flockName, setFlockName] = useState('');
|
||||
const [flockIotUrls, setFlockIotUrls] = useState<Record<number, string>>({});
|
||||
const [editingCycleId, setEditingCycleId] = useState<number | null>(null);
|
||||
const [startDate, setStartDate] = useState(defaultCycleForm().startDate);
|
||||
const [docInCount, setDocInCount] = useState(defaultCycleForm().docInCount);
|
||||
@@ -139,14 +138,6 @@ const SettingsPage: React.FC = () => {
|
||||
setFeedInButtonUrls(urls.length > 0 ? urls.map((entry) => ({ ...entry })) : [emptyFeedInButtonUrl()]);
|
||||
}, [selectedKandang?.id, selectedKandang?.feed_in_button_urls]);
|
||||
|
||||
React.useEffect(() => {
|
||||
const next: Record<number, string> = {};
|
||||
for (const flock of flocks) {
|
||||
next[flock.id] = flock.iot_api_url ?? '';
|
||||
}
|
||||
setFlockIotUrls(next);
|
||||
}, [flocks]);
|
||||
|
||||
const wrap = async (fn: () => Promise<void>) => {
|
||||
setSaving(true);
|
||||
setError(null);
|
||||
@@ -516,7 +507,9 @@ const SettingsPage: React.FC = () => {
|
||||
<p className="text-xs text-gray-500">Pilih kandang terlebih dahulu.</p>
|
||||
) : (
|
||||
<ul className="divide-y">
|
||||
{flocks.map((flock) => (
|
||||
{flocks.map((flock) => {
|
||||
const webhookUrl = `${window.location.origin}/api/v1/panel-iot/push/${flock.id}/`;
|
||||
return (
|
||||
<li key={flock.id} className="py-3 space-y-2">
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<span className="text-sm font-medium text-gray-700">{flock.flock_name}</span>
|
||||
@@ -536,42 +529,27 @@ const SettingsPage: React.FC = () => {
|
||||
)}
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<label className="block text-xs text-gray-500">API Panel IoT</label>
|
||||
{isSuperAdmin ? (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<input
|
||||
className={inputClass}
|
||||
type="text"
|
||||
placeholder="https://…/api/iot/…"
|
||||
value={flockIotUrls[flock.id] ?? ''}
|
||||
onChange={(e) =>
|
||||
setFlockIotUrls((prev) => ({ ...prev, [flock.id]: e.target.value }))
|
||||
}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="bg-red-600 text-white px-3 py-2 rounded-lg text-sm font-semibold"
|
||||
disabled={saving}
|
||||
onClick={() =>
|
||||
void wrap(async () => {
|
||||
await api.flocks.update(flock.id, {
|
||||
iot_api_url: (flockIotUrls[flock.id] ?? '').trim(),
|
||||
});
|
||||
flash(`URL IoT ${flock.flock_name} disimpan.`);
|
||||
})
|
||||
}
|
||||
>
|
||||
Simpan URL IoT
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<p className="break-all text-xs text-gray-600">
|
||||
{(flock.iot_api_url || '').trim() || 'Belum diset (admin).'}
|
||||
</p>
|
||||
)}
|
||||
<label className="block text-xs text-gray-500">Webhook URL (IoT Panel → Dashboard)</label>
|
||||
<div className="flex items-center gap-2 rounded-lg bg-gray-50 border border-gray-200 px-3 py-2">
|
||||
<code className="flex-1 text-xs text-gray-700 break-all font-mono">{webhookUrl}</code>
|
||||
<button
|
||||
type="button"
|
||||
className="shrink-0 text-xs text-red-600 hover:underline font-semibold"
|
||||
onClick={() => {
|
||||
void navigator.clipboard.writeText(webhookUrl);
|
||||
flash('Webhook URL disalin.');
|
||||
}}
|
||||
>
|
||||
Salin
|
||||
</button>
|
||||
</div>
|
||||
<p className="text-[11px] text-gray-400">
|
||||
Konfigurasikan URL ini sebagai tujuan push di Panel IoT.
|
||||
</p>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
|
||||
Reference in new issue
Block a user