|
| 1 | +import { toast } from 'react-toastify'; |
| 2 | +import { ENDPOINTS } from '~/utils/URL'; |
| 3 | +import httpService from '../services/httpService'; |
| 4 | +import { updateStudentTask } from './studentTasks'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Action types for intermediate tasks |
| 8 | + */ |
| 9 | +export const FETCH_INTERMEDIATE_TASKS_START = 'FETCH_INTERMEDIATE_TASKS_START'; |
| 10 | +export const FETCH_INTERMEDIATE_TASKS_SUCCESS = 'FETCH_INTERMEDIATE_TASKS_SUCCESS'; |
| 11 | +export const FETCH_INTERMEDIATE_TASKS_ERROR = 'FETCH_INTERMEDIATE_TASKS_ERROR'; |
| 12 | +export const CREATE_INTERMEDIATE_TASK_SUCCESS = 'CREATE_INTERMEDIATE_TASK_SUCCESS'; |
| 13 | +export const UPDATE_INTERMEDIATE_TASK_SUCCESS = 'UPDATE_INTERMEDIATE_TASK_SUCCESS'; |
| 14 | +export const DELETE_INTERMEDIATE_TASK_SUCCESS = 'DELETE_INTERMEDIATE_TASK_SUCCESS'; |
| 15 | +export const MARK_INTERMEDIATE_TASK_DONE = 'MARK_INTERMEDIATE_TASK_DONE'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Fetch intermediate tasks for a parent task |
| 19 | + */ |
| 20 | +export const fetchIntermediateTasks = (taskId) => { |
| 21 | + return async (dispatch) => { |
| 22 | + try { |
| 23 | + const response = await httpService.get(ENDPOINTS.INTERMEDIATE_TASKS_BY_PARENT(taskId)); |
| 24 | + return response.data; |
| 25 | + } catch (error) { |
| 26 | + console.error('Error fetching intermediate tasks:', error); |
| 27 | + toast.error('Failed to fetch sub-tasks'); |
| 28 | + throw error; |
| 29 | + } |
| 30 | + }; |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Calculate total expected hours from intermediate tasks |
| 35 | + */ |
| 36 | +const calculateTotalExpectedHours = (intermediateTasks) => { |
| 37 | + return intermediateTasks.reduce((total, task) => { |
| 38 | + return total + (task.expected_hours || 0); |
| 39 | + }, 0); |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * Update parent task's expected hours based on intermediate tasks |
| 44 | + */ |
| 45 | +const updateParentTaskExpectedHours = async (dispatch, getState, parentTaskId) => { |
| 46 | + try { |
| 47 | + // Fetch all intermediate tasks for this parent |
| 48 | + const intermediateTasks = await dispatch(fetchIntermediateTasks(parentTaskId)); |
| 49 | + |
| 50 | + // Calculate total expected hours |
| 51 | + const totalExpectedHours = calculateTotalExpectedHours(intermediateTasks); |
| 52 | + |
| 53 | + // Get the parent task from state |
| 54 | + const state = getState(); |
| 55 | + const parentTask = state.studentTasks.taskItems.find(t => t.id === parentTaskId); |
| 56 | + |
| 57 | + if (parentTask) { |
| 58 | + // Update the parent task with new expected hours |
| 59 | + dispatch(updateStudentTask(parentTaskId, { |
| 60 | + ...parentTask, |
| 61 | + suggested_total_hours: totalExpectedHours |
| 62 | + })); |
| 63 | + } |
| 64 | + } catch (error) { |
| 65 | + console.error('Error updating parent task expected hours:', error); |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * Create a new intermediate task |
| 71 | + */ |
| 72 | +export const createIntermediateTask = (taskData) => { |
| 73 | + return async (dispatch, getState) => { |
| 74 | + try { |
| 75 | + const response = await httpService.post(ENDPOINTS.INTERMEDIATE_TASKS(), taskData); |
| 76 | + toast.success('Sub-task created successfully'); |
| 77 | + |
| 78 | + // Update parent task expected hours |
| 79 | + if (taskData.parentTaskId) { |
| 80 | + await updateParentTaskExpectedHours(dispatch, getState, taskData.parentTaskId); |
| 81 | + } |
| 82 | + |
| 83 | + return response.data; |
| 84 | + } catch (error) { |
| 85 | + console.error('Error creating intermediate task:', error); |
| 86 | + const errorMessage = error.response?.data?.error || error.message || 'Failed to create sub-task'; |
| 87 | + toast.error(`Error: ${errorMessage}`); |
| 88 | + throw error; |
| 89 | + } |
| 90 | + }; |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * Update an intermediate task |
| 95 | + */ |
| 96 | +export const updateIntermediateTask = (id, taskData) => { |
| 97 | + return async (dispatch, getState) => { |
| 98 | + try { |
| 99 | + const response = await httpService.put(ENDPOINTS.INTERMEDIATE_TASK_BY_ID(id), taskData); |
| 100 | + toast.success('Sub-task updated successfully'); |
| 101 | + |
| 102 | + // Update parent task expected hours |
| 103 | + if (taskData.parentTaskId) { |
| 104 | + await updateParentTaskExpectedHours(dispatch, getState, taskData.parentTaskId); |
| 105 | + } |
| 106 | + |
| 107 | + return response.data; |
| 108 | + } catch (error) { |
| 109 | + console.error('Error updating intermediate task:', error); |
| 110 | + const errorMessage = error.response?.data?.error || error.message || 'Failed to update sub-task'; |
| 111 | + toast.error(`Error: ${errorMessage}`); |
| 112 | + throw error; |
| 113 | + } |
| 114 | + }; |
| 115 | +}; |
| 116 | + |
| 117 | +/** |
| 118 | + * Delete an intermediate task |
| 119 | + */ |
| 120 | +export const deleteIntermediateTask = (id, parentTaskId = null) => { |
| 121 | + return async (dispatch, getState) => { |
| 122 | + try { |
| 123 | + await httpService.delete(ENDPOINTS.INTERMEDIATE_TASK_BY_ID(id)); |
| 124 | + toast.success('Sub-task deleted successfully'); |
| 125 | + |
| 126 | + // Update parent task expected hours |
| 127 | + if (parentTaskId) { |
| 128 | + await updateParentTaskExpectedHours(dispatch, getState, parentTaskId); |
| 129 | + } |
| 130 | + |
| 131 | + return true; |
| 132 | + } catch (error) { |
| 133 | + console.error('Error deleting intermediate task:', error); |
| 134 | + const errorMessage = error.response?.data?.error || error.message || 'Failed to delete sub-task'; |
| 135 | + toast.error(`Error: ${errorMessage}`); |
| 136 | + throw error; |
| 137 | + } |
| 138 | + }; |
| 139 | +}; |
| 140 | + |
| 141 | +/** |
| 142 | + * Mark an intermediate task as done (for students) |
| 143 | + */ |
| 144 | +export const markIntermediateTaskAsDone = (id, parentTaskId) => { |
| 145 | + return async (dispatch) => { |
| 146 | + try { |
| 147 | + // First, fetch the current task data |
| 148 | + const currentTask = await httpService.get(ENDPOINTS.INTERMEDIATE_TASK_BY_ID(id)); |
| 149 | + |
| 150 | + // Update with the completed status while preserving all required fields |
| 151 | + const response = await httpService.put(ENDPOINTS.INTERMEDIATE_TASK_BY_ID(id), { |
| 152 | + ...currentTask.data, |
| 153 | + status: 'completed' |
| 154 | + }); |
| 155 | + toast.success('Sub-task marked as done'); |
| 156 | + return response.data; |
| 157 | + } catch (error) { |
| 158 | + console.error('Error marking intermediate task as done:', error); |
| 159 | + const errorMessage = error.response?.data?.error || error.message || 'Failed to mark sub-task as done'; |
| 160 | + toast.error(`Error: ${errorMessage}`); |
| 161 | + throw error; |
| 162 | + } |
| 163 | + }; |
| 164 | +}; |
| 165 | + |
0 commit comments