mirror of
https://github.com/UniversityOfHelsinkiCS/oodikone.git
synced 2026-10-12 19:03:39 +03:00
[Course statistics] Use RTK Query for course search
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
import { callController } from '@/apiConnection/index'
|
||||
|
||||
const prefix = 'GET_COURSE_SEARCH_RESULT_'
|
||||
|
||||
export const clearCourses = () => ({ type: 'CLEAR_SEARCH_RESULTS' })
|
||||
|
||||
export const findCoursesV2 = ({ name, code, combineSubstitutions }) => {
|
||||
const route = '/v2/coursesmulti'
|
||||
const params = { name, code, combineSubstitutions }
|
||||
return callController(route, prefix, [], 'get', params, params)
|
||||
}
|
||||
|
||||
export const toggleOpenAndRegularCourses = showType => {
|
||||
return {
|
||||
type: 'TOGGLE_OPEN_AND_Regular_COURSES',
|
||||
showType,
|
||||
}
|
||||
}
|
||||
|
||||
export const reducer = (state = { data: {}, pending: false, openOrRegular: 'unifyStats' }, action) => {
|
||||
switch (action.type) {
|
||||
case 'GET_COURSE_SEARCH_RESULT_ATTEMPT':
|
||||
return {
|
||||
...state,
|
||||
pending: true,
|
||||
lastSearch: action.requestSettings.query,
|
||||
}
|
||||
case 'GET_COURSE_SEARCH_RESULT_FAILURE':
|
||||
return {
|
||||
...state,
|
||||
pending: false,
|
||||
error: true,
|
||||
}
|
||||
case 'GET_COURSE_SEARCH_RESULT_SUCCESS':
|
||||
return {
|
||||
...state,
|
||||
pending: false,
|
||||
error: false,
|
||||
data:
|
||||
action.query.code === state.lastSearch.code && action.query.name === state.lastSearch.name
|
||||
? action.response
|
||||
: state.data,
|
||||
}
|
||||
|
||||
case 'TOGGLE_OPEN_AND_Regular_COURSES':
|
||||
return {
|
||||
...state,
|
||||
openOrRegular: action.showType,
|
||||
}
|
||||
|
||||
case 'CLEAR_SEARCH_RESULTS':
|
||||
return {
|
||||
...state,
|
||||
data: {},
|
||||
}
|
||||
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { createSlice } from '@reduxjs/toolkit'
|
||||
|
||||
import { RTKApi } from '@/apiConnection'
|
||||
|
||||
const courseSearchApi = RTKApi.injectEndpoints({
|
||||
endpoints: builder => ({
|
||||
getCourseSearchResult: builder.query({
|
||||
query: ({ name, code, combineSubstitutions }) => ({
|
||||
url: '/v2/coursesmulti',
|
||||
params: { name, code, combineSubstitutions },
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
export const { useGetCourseSearchResultQuery } = courseSearchApi
|
||||
|
||||
const initialState: { openOrRegular: 'openStats' | 'regularStats' | 'unifyStats' } = { openOrRegular: 'unifyStats' }
|
||||
|
||||
const courseSearchSlice = createSlice({
|
||||
name: 'courseSearch',
|
||||
initialState,
|
||||
reducers: {
|
||||
toggleOpenAndRegularCourses: (state, { payload }) => {
|
||||
if (!['openStats', 'regularStats', 'unifyStats'].includes(payload)) {
|
||||
throw new Error(`Invalid payload ${payload} for toggleOpenAndRegularCourses`)
|
||||
}
|
||||
state.openOrRegular = payload
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
export const { toggleOpenAndRegularCourses } = courseSearchSlice.actions
|
||||
|
||||
export const { reducer } = courseSearchSlice
|
||||
Reference in New Issue
Block a user