mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
refactor(FE): Refactor useEffect hooks and loadAssignments function
This commit is contained in:
@@ -301,8 +301,93 @@ export function DailyChecklistContent() {
|
||||
checkAndLoadChecklist();
|
||||
}, [date, kandangId, selectedCategory]);
|
||||
|
||||
// Load employees when kandang changes
|
||||
useEffect(() => {
|
||||
if (kandangId) {
|
||||
// ✅ Clear selected employees ketika kandang berubah (reset ABK assignment)
|
||||
setSelectedEmployees([]);
|
||||
setAssignments({});
|
||||
} else {
|
||||
setSelectedEmployees([]);
|
||||
setAssignments({});
|
||||
}
|
||||
}, [kandangId]);
|
||||
|
||||
// Load activities and tasks when phases change
|
||||
useEffect(() => {
|
||||
const loadAssignments = async (taskIds: string[]) => {
|
||||
if (taskIds.length === 0) return;
|
||||
|
||||
try {
|
||||
const existingDailyChecklist =
|
||||
await DailyChecklistApi.getOneDailyChecklist(
|
||||
String(dailyChecklistId)
|
||||
);
|
||||
|
||||
if (isResponseError(existingDailyChecklist)) {
|
||||
console.error(
|
||||
'Error loading assignments:',
|
||||
existingDailyChecklist.message
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// set existing document
|
||||
setExistingDocuments(existingDailyChecklist?.data.document_urls || []);
|
||||
|
||||
// Build assignments map
|
||||
const assignmentMap: {
|
||||
[taskId: string]: {
|
||||
[employeeId: string]: { checked: boolean; note: string };
|
||||
};
|
||||
} = {};
|
||||
|
||||
(existingDailyChecklist?.data.tasks || []).forEach(
|
||||
(dailyChecklistTask) => {
|
||||
if (!assignmentMap[dailyChecklistTask.id]) {
|
||||
assignmentMap[dailyChecklistTask.id] = {};
|
||||
}
|
||||
|
||||
dailyChecklistTask.assignments.forEach((assignment) => {
|
||||
if (!assignmentMap[dailyChecklistTask.id]) {
|
||||
assignmentMap[dailyChecklistTask.id] = {};
|
||||
}
|
||||
assignmentMap[dailyChecklistTask.id][assignment.employee.id] = {
|
||||
checked: assignment.checked,
|
||||
note: assignment.note || '',
|
||||
};
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
setAssignments(assignmentMap);
|
||||
|
||||
// Load employees from assignments
|
||||
const employeeIds = Array.from(
|
||||
new Set(
|
||||
(existingDailyChecklist?.data.assigned_employees || []).map(
|
||||
(a) => a.id
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
if (employeeIds.length > 0) {
|
||||
const existingDailyChecklist =
|
||||
await DailyChecklistApi.getOneDailyChecklist(
|
||||
String(dailyChecklistId)
|
||||
);
|
||||
|
||||
if (isResponseSuccess(existingDailyChecklist)) {
|
||||
setSelectedEmployees(
|
||||
existingDailyChecklist.data.assigned_employees
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading assignments:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const loadActivitiesAndTasks = async () => {
|
||||
if (!dailyChecklistId || selectedPhaseIds.length === 0) {
|
||||
setActivitiesByPhase({});
|
||||
@@ -377,87 +462,6 @@ export function DailyChecklistContent() {
|
||||
loadActivitiesAndTasks();
|
||||
}, [dailyChecklistId, selectedPhaseIds]);
|
||||
|
||||
// Load employees when kandang changes
|
||||
useEffect(() => {
|
||||
if (kandangId) {
|
||||
// ✅ Clear selected employees ketika kandang berubah (reset ABK assignment)
|
||||
setSelectedEmployees([]);
|
||||
setAssignments({});
|
||||
} else {
|
||||
setSelectedEmployees([]);
|
||||
setAssignments({});
|
||||
}
|
||||
}, [kandangId]);
|
||||
|
||||
const loadAssignments = async (taskIds: string[]) => {
|
||||
if (taskIds.length === 0) return;
|
||||
|
||||
try {
|
||||
const existingDailyChecklist =
|
||||
await DailyChecklistApi.getOneDailyChecklist(String(dailyChecklistId));
|
||||
|
||||
if (isResponseError(existingDailyChecklist)) {
|
||||
console.error(
|
||||
'Error loading assignments:',
|
||||
existingDailyChecklist.message
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// set existing document
|
||||
setExistingDocuments(existingDailyChecklist?.data.document_urls || []);
|
||||
|
||||
// Build assignments map
|
||||
const assignmentMap: {
|
||||
[taskId: string]: {
|
||||
[employeeId: string]: { checked: boolean; note: string };
|
||||
};
|
||||
} = {};
|
||||
|
||||
(existingDailyChecklist?.data.tasks || []).forEach(
|
||||
(dailyChecklistTask) => {
|
||||
if (!assignmentMap[dailyChecklistTask.id]) {
|
||||
assignmentMap[dailyChecklistTask.id] = {};
|
||||
}
|
||||
|
||||
dailyChecklistTask.assignments.forEach((assignment) => {
|
||||
if (!assignmentMap[dailyChecklistTask.id]) {
|
||||
assignmentMap[dailyChecklistTask.id] = {};
|
||||
}
|
||||
assignmentMap[dailyChecklistTask.id][assignment.employee.id] = {
|
||||
checked: assignment.checked,
|
||||
note: assignment.note || '',
|
||||
};
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
setAssignments(assignmentMap);
|
||||
|
||||
// Load employees from assignments
|
||||
const employeeIds = Array.from(
|
||||
new Set(
|
||||
(existingDailyChecklist?.data.assigned_employees || []).map(
|
||||
(a) => a.id
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
if (employeeIds.length > 0) {
|
||||
const existingDailyChecklist =
|
||||
await DailyChecklistApi.getOneDailyChecklist(
|
||||
String(dailyChecklistId)
|
||||
);
|
||||
|
||||
if (isResponseSuccess(existingDailyChecklist)) {
|
||||
setSelectedEmployees(existingDailyChecklist.data.assigned_employees);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading assignments:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// Phase selection modal
|
||||
const handleAddPhase = () => {
|
||||
if (!selectedCategory) {
|
||||
|
||||
Reference in New Issue
Block a user