-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathcenterPanelSlice.ts
More file actions
191 lines (170 loc) · 6.66 KB
/
centerPanelSlice.ts
File metadata and controls
191 lines (170 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import httpUtility, { handleApiThunk } from '../../Services/httpUtility';
import { toast } from "react-toastify";
interface CenterPanelState {
contentData: any;
cLoader: boolean;
cError: string | null;
modified_result: any;
comments: string;
isSavingInProgress: boolean;
activeProcessId: string,
processStepsData: any[];
isJSONEditorSearchEnabled: boolean;
}
const getDisplayMessage = (text: string) => {
if (
text.startsWith('Processing of file with Process') &&
text.endsWith('not found.')
) {
return 'This record no longer exists. Please refresh.';
}
return text;
};
// Create the async thunk with the argument and return types
export const fetchContentJsonData = createAsyncThunk<any, { processId: string | null }>('/contentprocessor/processed/', async ({ processId }, { rejectWithValue }) => {
if (!processId) {
return rejectWithValue("Reset store");
}
const url = '/contentprocessor/processed/' + processId;
return handleApiThunk(
httpUtility.get<any>(url),
rejectWithValue,
'Failed to fetch content JSON data'
);
});
export const fetchProcessSteps = createAsyncThunk<any, { processId: string | null }>('/contentprocessor/processed/processId/steps', async ({ processId }, { rejectWithValue }) => {
if (!processId) {
return rejectWithValue("Reset store");
}
const url = `/contentprocessor/processed/${processId}/steps`;
return handleApiThunk(
httpUtility.get<any>(url),
rejectWithValue,
'Failed to fetch process steps'
);
});
export const saveContentJson = createAsyncThunk<any, { processId: string | null, contentJson: string, comments: string, savedComments: string }>('SaveContentJSON-Comments', async ({ processId, contentJson, comments, savedComments }, { rejectWithValue }) => {
try {
if (!processId) {
return rejectWithValue('Process ID is required');
}
const url = `/contentprocessor/processed/${processId}`;
const requests: Promise<any>[] = [];
// Add contentJson update if valid
if (contentJson && Object.keys(contentJson).length > 0) {
requests.push(
handleApiThunk(
httpUtility.put(url, {
process_id: processId,
modified_result: contentJson,
}),
rejectWithValue,
'Failed to save content JSON'
)
);
}
// Add comments update if applicable
if (comments.trim() !== '' || (savedComments !== '' && comments.trim() === '')) {
requests.push(
handleApiThunk(
httpUtility.put(url, {
process_id: processId,
comment: comments,
}),
rejectWithValue,
'Failed to save comments'
)
);
}
// If no changes, short-circuit
if (requests.length === 0) {
return { message: 'No updates were made' };
}
// Wait for all updates to complete
const responses = await Promise.all(requests);
return responses[0];
} catch (error) {
return Promise.reject(error);
}
});
const initialState: CenterPanelState = {
contentData: {},
cLoader: false,
cError: '',
modified_result: {},
comments: '',
isSavingInProgress: false,
activeProcessId: '',
processStepsData: [],
isJSONEditorSearchEnabled: true,
};
const centerPanelSlice = createSlice({
name: 'Center Panel',
initialState,
reducers: {
setModifiedResult: (state, action) => {
state.modified_result = action.payload;
},
setUpdateComments: (state, action) => {
state.comments = action.payload
},
setActiveProcessId: (state, action) => {
state.activeProcessId = action.payload
}
},
extraReducers: (builder) => {
//Fetch Dropdown values
builder
.addCase(fetchContentJsonData.pending, (state) => {
state.cLoader = true; // You can manage loading state if necessary
state.cError = null;
state.modified_result = {};
state.comments = '';
})
.addCase(fetchContentJsonData.fulfilled, (state, action) => { // Adjust `any` to the response data type
if (state.activeProcessId == action.payload.process_id) {
state.contentData = action.payload;
state.comments = action.payload.comment ?? "";
state.cLoader = false;
}
})
.addCase(fetchContentJsonData.rejected, (state, action: any) => {
state.cError = action.error.message || 'An error occurred';
state.cLoader = false;
state.contentData = {};
state.comments = "";
toast.error(getDisplayMessage(action.payload))
});
builder
.addCase(saveContentJson.pending, (state, action) => {
state.modified_result = {};
state.isSavingInProgress = true;
})
.addCase(saveContentJson.fulfilled, (state, action) => { // Adjust `any` to the response data type
toast.success("Data saved successfully!"); // Success toast
state.isSavingInProgress = false;
})
.addCase(saveContentJson.rejected, (state, action: any) => {
toast.error(getDisplayMessage(action.payload))
state.isSavingInProgress = false;
});
builder
.addCase(fetchProcessSteps.pending, (state) => {
state.processStepsData = [];
//state.isSavingInProgress = true;
})
.addCase(fetchProcessSteps.fulfilled, (state, action) => { // Adjust `any` to the response data type
state.processStepsData = action.payload;
})
.addCase(fetchProcessSteps.rejected, (state, action) => {
if (action.payload === "Reset store") {
state.processStepsData = []; // Reset store when processId is null
} else {
//console.error("Error fetching Process Steps Data:", action.error.message || 'An error occurred');
}
});
},
});
export const { setModifiedResult, setUpdateComments, setActiveProcessId } = centerPanelSlice.actions;
export default centerPanelSlice.reducer;