import { createSlice , PayloadAction } from '@reduxjs/toolkit' ; import { ApiBackgroundImagesResult } from 'pages/api/vendor/editor/search-images' ; import { getRandomWordList } from './parts/randomTerms' ; const initialState = { data : [ ] , pagination : { query : getRandomWordList ( ) , current : 1 , history : [ ] , hasNext : false , } , } ; const { reducer : backgroundsStateReducer , actions : backgroundsStateActions , } = createSlice ( { initialState , name : 'backgrounds' , reducers : { set ( state , action ) { return { ... state , data : [ ... ( state . data ?? [ ] ) , ... ( action . payload ?? [ ] ) ] , } ; } , goToNextPage ( state ) { return { ... state , pagination : { ... state . pagination , current : state . pagination . next ?? 0 , } , } ; } , setQuery ( _state , action ) { return { ... initialState , pagination : { ... initialState . pagination , query : action . payload , } , } ; } , setRandomQuery ( ) { return { ... initialState , pagination : { ... initialState . pagination , query : getRandomWordList ( ) , } , } ; } , setNextPageOffset ( state , action ) { return { ... state , pagination : { ... state . pagination , hasNext : action . payload . hasNext , next : action . payload . next , history : [ ... state . pagination . history , action . payload . past ] , } , } ; } , } , } ) ; export { backgroundsStateReducer , backgroundsStateActions } ;