-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchSlice.js
107 lines (100 loc) · 3.03 KB
/
searchSlice.js
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
// src/slices/searchSlice.js
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { setMessage } from "./messageSlice";
import AuthService from "../services/auth.service";
export const searchSlice = createSlice({
name: "search",
initialState: {
results: [],
searches: [],
status: "idle",
error: null
},
reducers: {
setSearch: (state, action) => {
state.searches = action.payload
}
},
extraReducers: (builder) => {
builder
.addCase(advSeachBcodb.fulfilled, (state, action) => {
console.log(action.payload)
state.results = action.payload;
state.status = "fulfilled";
})
.addCase(advSeachBcodb.pending, (state) => {
state.status = "loading";
})
.addCase(advSeachBcodb.rejected, (state, action) => {
state.error = action.error.message
state.status = "failed";
})
.addCase(searchBcodb.fulfilled, (state, action) => {
state.results = action.payload;
state.status = "fulfilled";
})
.addCase(searchBcodb.pending, (state) => {
state.status = "loading";
})
.addCase(searchBcodb.rejected, (state, action) => {
state.error = action.error.message
state.status = "failed";
})
}
})
export const searchBcodbUser = createAsyncThunk(
"searchBcodbUser",
async ({username, public_hostname}, thunkAPI) => {
try {
const response = await AuthService.searchBcodbUser({username, public_hostname});
return response.data
} catch (error) {
const message = `${username} not found on ${public_hostname}`
thunkAPI.dispatch(setMessage(message));
return thunkAPI.rejectWithValue();
}
}
)
export const searchBcodb = createAsyncThunk(
"bcodb/searchBcodb",
async ({publicHostname, quickSearch}, thunkAPI) => {
try {
const results = await AuthService.searchBcodbAPI({publicHostname, quickSearch})
// thunkAPI.dispatch(setMessage(`Search (${quickSearch}) returned ${results.data.length} BCOs`));
return results.data
} catch (error) {
console.log(error.response)
const message =
(error.response &&
error.response.data &&
error.response.data.detail) ||
error.message ||
error.toString();
thunkAPI.dispatch(setMessage(message));
return thunkAPI.rejectWithValue(error.response);
}
}
)
export const advSeachBcodb = createAsyncThunk(
"bcodb/advSeachBcodb",
async (data, thunkAPI) => {
try {
const results = await AuthService.advSearchBcodbAPI(data);
thunkAPI.dispatch(setSearch(data));
return results.data
} catch (error) {
const message =
(error.response &&
error.response.data &&
error.response.data.message) ||
error.message ||
error.toString();
thunkAPI.dispatch(setMessage(message));
return thunkAPI.rejectWithValue();
}
}
)
export const searchReducer = searchSlice.reducer
export const {
setSearch
} = searchSlice.actions;