mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 15:55:48 +00:00
refactor(FE): Refactor useEffect hooks and loadAssignments function
This commit is contained in:
@@ -301,8 +301,93 @@ export function DailyChecklistContent() {
|
|||||||
checkAndLoadChecklist();
|
checkAndLoadChecklist();
|
||||||
}, [date, kandangId, selectedCategory]);
|
}, [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
|
// Load activities and tasks when phases change
|
||||||
useEffect(() => {
|
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 () => {
|
const loadActivitiesAndTasks = async () => {
|
||||||
if (!dailyChecklistId || selectedPhaseIds.length === 0) {
|
if (!dailyChecklistId || selectedPhaseIds.length === 0) {
|
||||||
setActivitiesByPhase({});
|
setActivitiesByPhase({});
|
||||||
@@ -377,87 +462,6 @@ export function DailyChecklistContent() {
|
|||||||
loadActivitiesAndTasks();
|
loadActivitiesAndTasks();
|
||||||
}, [dailyChecklistId, selectedPhaseIds]);
|
}, [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
|
// Phase selection modal
|
||||||
const handleAddPhase = () => {
|
const handleAddPhase = () => {
|
||||||
if (!selectedCategory) {
|
if (!selectedCategory) {
|
||||||
|
|||||||
Reference in New Issue
Block a user