filter data farmer portal
This commit is contained in:
parent
8ca1307c21
commit
2a22320b15
@ -1,11 +1,13 @@
|
|||||||
import { useState, useEffect, useCallback } from "react";
|
import { useState, useEffect, useCallback } from "react";
|
||||||
import { OfflineService, isOnline } from "./offlineStorage";
|
import { OfflineService, isOnline } from "./offlineStorage";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
|
import { useAuth } from "@/lib/AuthContext";
|
||||||
|
|
||||||
export function useSyncManager() {
|
export function useSyncManager() {
|
||||||
const [online, setOnline] = useState(isOnline());
|
const [online, setOnline] = useState(isOnline());
|
||||||
const [syncing, setSyncing] = useState(false);
|
const [syncing, setSyncing] = useState(false);
|
||||||
const [pendingCount, setPendingCount] = useState(0);
|
const [pendingCount, setPendingCount] = useState(0);
|
||||||
|
const { user } = useAuth();
|
||||||
|
|
||||||
const executeSync = useCallback(async () => {
|
const executeSync = useCallback(async () => {
|
||||||
if (!isOnline() || syncing) return;
|
if (!isOnline() || syncing) return;
|
||||||
|
|||||||
@ -149,11 +149,13 @@ export const OfflineService = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
downloadFromServer: async () => {
|
downloadFromServer: async (user) => {
|
||||||
|
|
||||||
const token = localStorage.getItem('access_token');
|
const token = localStorage.getItem('access_token');
|
||||||
const baseURL = import.meta.env.VITE_BASE44_API_URL.replace(/\/+$/, "");
|
const baseURL = import.meta.env.VITE_BASE44_API_URL.replace(/\/+$/, "");
|
||||||
|
console.log(user, "Userr")
|
||||||
const syncConfigs = [
|
const syncConfigs = [
|
||||||
{ endpoint: 'auth/profile', table: db.farmers },
|
{ endpoint: `auth/profile/1`, table: db.farmers },
|
||||||
{ endpoint: 'map/lahan', table: db.lands },
|
{ endpoint: 'map/lahan', table: db.lands },
|
||||||
{ endpoint: 'map/tanaman', table: db.plants },
|
{ endpoint: 'map/tanaman', table: db.plants },
|
||||||
{ endpoint: 'map/panen', table: db.harvest},
|
{ endpoint: 'map/panen', table: db.harvest},
|
||||||
|
|||||||
@ -28,6 +28,7 @@ import HarvestForm from "@/components/harvest/HarvestForm";
|
|||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { motion, sync } from "framer-motion";
|
import { motion, sync } from "framer-motion";
|
||||||
import { OfflineService } from "@/components/common/offlineStorage";
|
import { OfflineService } from "@/components/common/offlineStorage";
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
|
||||||
export default function FarmerPortal() {
|
export default function FarmerPortal() {
|
||||||
@ -87,6 +88,7 @@ export default function FarmerPortal() {
|
|||||||
path_ktp: '',
|
path_ktp: '',
|
||||||
role:'',
|
role:'',
|
||||||
user_id:1,
|
user_id:1,
|
||||||
|
ktp:'',
|
||||||
});
|
});
|
||||||
|
|
||||||
const { data: villages = [] } = useQuery({
|
const { data: villages = [] } = useQuery({
|
||||||
@ -110,7 +112,9 @@ export default function FarmerPortal() {
|
|||||||
queryFn: async() => {
|
queryFn: async() => {
|
||||||
try{
|
try{
|
||||||
const resp = await entity('distribusi','panen').list();
|
const resp = await entity('distribusi','panen').list();
|
||||||
const data = resp.data || [];
|
let data = resp.data || [];
|
||||||
|
data = data.filter((dt) => dt.farmer_id === farmer.id)
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}catch(err){
|
}catch(err){
|
||||||
console.error("data gagal diabbil")
|
console.error("data gagal diabbil")
|
||||||
@ -144,17 +148,21 @@ export default function FarmerPortal() {
|
|||||||
let lahans = [];
|
let lahans = [];
|
||||||
try{
|
try{
|
||||||
const listLahan = await entity('map', 'lahan').list();
|
const listLahan = await entity('map', 'lahan').list();
|
||||||
lahans = Array.isArray(listLahan.data) ? listLahan.data : []
|
lahans = Array.isArray(listLahan.data)
|
||||||
|
? listLahan.data.filter(l => l.profile_id === profile.id)
|
||||||
|
: [];
|
||||||
|
|
||||||
}catch(err){
|
}catch(err){
|
||||||
console.warn("Online data is un reachable")
|
console.warn("Online data is un reachable")
|
||||||
}
|
}
|
||||||
try{
|
try{
|
||||||
const offlineData = await OfflineService.getEntities('lands');
|
const offlineData = await OfflineService.getEntities('lands');
|
||||||
|
const offlineLahan = safeArray(offlineData).filter(lh => lh.profile_id === profile.id);
|
||||||
const combined = new Map();
|
const combined = new Map();
|
||||||
safeArray(lahans)
|
|
||||||
.filter(l => l.sync_status === 'pending')
|
safeArray(offlineLahan).forEach(el => combined.set(el.id, el));
|
||||||
.forEach(element => combined.set(element.id, element));
|
safeArray(lahans).forEach(l => combined.set(l.id, l));
|
||||||
safeArray(offlineData).forEach(el => combined.set(el.id, el));
|
|
||||||
return Array.from(combined.values()).sort((a, b) =>
|
return Array.from(combined.values()).sort((a, b) =>
|
||||||
new Date(b.created_date || b.created_at) - new Date(a.created_date || a.created_at)
|
new Date(b.created_date || b.created_at) - new Date(a.created_date || a.created_at)
|
||||||
);
|
);
|
||||||
@ -290,7 +298,24 @@ export default function FarmerPortal() {
|
|||||||
const updateProfileMutation = useMutation({
|
const updateProfileMutation = useMutation({
|
||||||
mutationFn: async (data) => {
|
mutationFn: async (data) => {
|
||||||
console.log(profile)
|
console.log(profile)
|
||||||
return await entity('auth','profile').update(profile?.id, data);
|
const token = localStorage.getItem("access_token");
|
||||||
|
try{
|
||||||
|
const response = await axios.post(
|
||||||
|
`https://agro.pkc-dev.org/api/auth/profile/1`,
|
||||||
|
data,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}catch(err){
|
||||||
|
console.log(err)
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
// return await entity('auth','profile').update(profile?.id, data);
|
||||||
},
|
},
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
console.log("Berhasil mengubah data profile")
|
console.log("Berhasil mengubah data profile")
|
||||||
@ -329,7 +354,7 @@ export default function FarmerPortal() {
|
|||||||
try{
|
try{
|
||||||
const [rMe, rFar] = await Promise.all([
|
const [rMe, rFar] = await Promise.all([
|
||||||
entity('auth', 'me').list(),
|
entity('auth', 'me').list(),
|
||||||
entity('auth', 'profile').list()
|
entity('auth', 'profile/1').list()
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const dataMe = rMe.data || [];
|
const dataMe = rMe.data || [];
|
||||||
@ -379,7 +404,7 @@ export default function FarmerPortal() {
|
|||||||
email: profile?.email || currentProfile.email || ''
|
email: profile?.email || currentProfile.email || ''
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Data error: ", error)
|
console.error("Data error: ", error)
|
||||||
} finally {
|
} finally {
|
||||||
@ -402,17 +427,24 @@ export default function FarmerPortal() {
|
|||||||
|
|
||||||
// profile control
|
// profile control
|
||||||
const handleToggleEdit = async () => {
|
const handleToggleEdit = async () => {
|
||||||
const [file, setFile] = useState();
|
const formDataBaru = new FormData();
|
||||||
|
|
||||||
if (isEditing) {
|
if (isEditing) {
|
||||||
|
formDataBaru.append('_method', 'PUT');
|
||||||
|
formDataBaru.append('nama', farmerFormData.nama);
|
||||||
|
formDataBaru.append('telepon', farmerFormData.telepon);
|
||||||
|
formDataBaru.append('alamat', farmerFormData.alamat);
|
||||||
|
formDataBaru.append('kk', farmerFormData.kk);
|
||||||
|
formDataBaru.append('file_ktp', selectedKtp);
|
||||||
|
formDataBaru.append('ktp', farmerFormData.ktp);
|
||||||
|
formDataBaru.append('email', farmerFormData.email);
|
||||||
|
formDataBaru.append('desa_kelurahan_id', '1');
|
||||||
|
console.log("Isi FormData:", Object.fromEntries(formDataBaru.entries()));
|
||||||
try {
|
try {
|
||||||
if(selectedKtp){
|
updateProfileMutation.mutate(formDataBaru);
|
||||||
farmerFormData.append('file_ktp', selectedKtp)
|
|
||||||
}
|
|
||||||
updateProfileMutation.mutate(farmerFormData);
|
|
||||||
toast.success("Profil berhasil diperbarui");
|
toast.success("Profil berhasil diperbarui");
|
||||||
setIsEditing(false);
|
setIsEditing(false);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error("Gagal Update Data : ", error)
|
||||||
toast.error("Gagal menyimpan perubahan");
|
toast.error("Gagal menyimpan perubahan");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -423,13 +455,14 @@ export default function FarmerPortal() {
|
|||||||
const handleCancel = () => {
|
const handleCancel = () => {
|
||||||
// Reset form ke data asli dari prop 'farmer'
|
// Reset form ke data asli dari prop 'farmer'
|
||||||
setFarmerFormData({
|
setFarmerFormData({
|
||||||
nama: currentUser.profile?.nama || currentUser.nama || currentUser.name || "",
|
nama: profile?.nama || "",
|
||||||
telepon: currentUser.profile?.telepon || currentUser.telepon || currentUser.phone || "",
|
telepon: profile?.telepon || "",
|
||||||
alamat: currentUser.profile?.alamat || currentUser.alamat || "",
|
alamat: profile?.alamat || "",
|
||||||
kk: currentUser.profile?.kk || currentUser.kk || "",
|
kk: profile?.kk || "",
|
||||||
ktp: currentUser.profile?.ktp || currentUser.ktp || "",
|
ktp: profile?.ktp || profile.ktp || "",
|
||||||
desa_kelurahan_id: currentUser.profile?.desa_kelurahan_id || currentUser.kelurahan_desa_id || 0,
|
desa_kelurahan_id: profile?.desa_kelurahan_id || 0,
|
||||||
email: profile?.email || currentProfile.email || ''
|
email: profile?.email || '',
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Bersihkan preview foto jika ada
|
// Bersihkan preview foto jika ada
|
||||||
@ -450,9 +483,10 @@ export default function FarmerPortal() {
|
|||||||
|
|
||||||
const handleFileChange = (e) => {
|
const handleFileChange = (e) => {
|
||||||
const fileKtp = e.target.files ? e.target.files[0] : null;
|
const fileKtp = e.target.files ? e.target.files[0] : null;
|
||||||
|
|
||||||
if(fileKtp){
|
if(fileKtp){
|
||||||
if(fileKtp.size > 2 * 1024 * 1024){
|
if(fileKtp.size > 3 * 1024 * 1024){
|
||||||
toast.error("Ukuran file terlalu besar. Maksimal 2MB.");
|
toast.error("Ukuran file terlalu besar. Maksimal 3MB.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setSelectedKtp(fileKtp);
|
setSelectedKtp(fileKtp);
|
||||||
@ -528,6 +562,7 @@ export default function FarmerPortal() {
|
|||||||
// Main portal view
|
// Main portal view
|
||||||
const alivePlants = plants.length;
|
const alivePlants = plants.length;
|
||||||
const totalArea = lands.reduce((sum, l) => sum + parseFloat(l.area_hectares || l.luas_lahan || 0), 0);
|
const totalArea = lands.reduce((sum, l) => sum + parseFloat(l.area_hectares || l.luas_lahan || 0), 0);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen bg-gradient-to-br from-emerald-50 to-slate-100">
|
<div className="min-h-screen bg-gradient-to-br from-emerald-50 to-slate-100">
|
||||||
<div className="max-w-4xl mx-auto p-4 md:p-6 space-y-6">
|
<div className="max-w-4xl mx-auto p-4 md:p-6 space-y-6">
|
||||||
@ -1030,10 +1065,10 @@ export default function FarmerPortal() {
|
|||||||
<div className="p-3 bg-slate-50 rounded-lg border border-slate-100">
|
<div className="p-3 bg-slate-50 rounded-lg border border-slate-100">
|
||||||
<p className="text-xs text-slate-500 mb-2">Lampiran KTP</p>
|
<p className="text-xs text-slate-500 mb-2">Lampiran KTP</p>
|
||||||
<img
|
<img
|
||||||
src={farmer.path_ktp}
|
src={profile.path_ktp}
|
||||||
alt="KTP"
|
alt="KTP"
|
||||||
className="w-full h-32 object-cover rounded-md cursor-pointer hover:opacity-90 transition-opacity"
|
className="w-full h-32 object-cover rounded-md cursor-pointer hover:opacity-90 transition-opacity"
|
||||||
onClick={() => window.open(farmer.path_ktp, '_blank')}
|
onClick={() => window.open(profile.ktp, '_blank')}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user