-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathrightPanelSlice.ts
More file actions
85 lines (73 loc) · 2.64 KB
/
rightPanelSlice.ts
File metadata and controls
85 lines (73 loc) · 2.64 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
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
import httpUtility from '../../Services/httpUtility';
interface T {
headers: any,
blobURL: string,
processId: string
}
interface RightPanelState {
fileHeaders: any;
rLoader: boolean;
blobURL: string;
rError: string | null;
fileResponse: Array<T>
}
export const fetchContentFileData = createAsyncThunk<any, { processId: string | null }>('/contentprocessor/processed/files/', async ({ processId }, { rejectWithValue }): Promise<any> => {
const url = '/contentprocessor/processed/files/' + processId;
try {
const response = await httpUtility.headers(url);
if (!response.ok) throw new Error("Failed to fetch file");
const blob = await response.blob();
const blobURL = URL.createObjectURL(blob);
const headers = response.headers;
if (!headers) {
throw new Error("Failed to fetch headers");
}
const headersObject = Object.fromEntries(headers.entries());
return { headers: headersObject, blobURL: blobURL, processId: processId };
}
catch (error) {
return rejectWithValue({
success: false,
message: JSON.parse('Failed to fetch file')
});
}
});
const initialState: RightPanelState = {
fileHeaders: {},
blobURL: '',
rLoader: false,
rError: '',
fileResponse: []
};
const rightPanelSlice = createSlice({
name: 'Right Panel',
initialState,
reducers: {
},
extraReducers: (builder) => {
//Fetch Dropdown values
builder
.addCase(fetchContentFileData.pending, (state) => {
state.fileHeaders = {}
state.blobURL = '';
state.rLoader = true;
state.rError = '';
})
.addCase(fetchContentFileData.fulfilled, (state, action: PayloadAction<any>) => {
state.fileHeaders = action.payload.headers;
state.blobURL = action.payload.blobURL;
state.rLoader = false;
const isItemExists = state.fileResponse.find(i => i.processId === action.payload.processId)
if (!isItemExists)
state.fileResponse.push(action.payload)
})
.addCase(fetchContentFileData.rejected, (state, action) => {
state.rLoader = false;
state.rError = action.error.message || 'An error occurred';
//toast.error(action.error.message || 'Failed to fetch file');
});
},
});
export const { } = rightPanelSlice.actions;
export default rightPanelSlice.reducer;