set up rtk query
parent
c26227a631
commit
c6d8b35806
@ -0,0 +1,38 @@
|
||||
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
|
||||
import { Connection } from '../connection'
|
||||
|
||||
interface GameState {
|
||||
title: null|string,
|
||||
introduction: null|string,
|
||||
worlds: null|{nodes: string[], edges: string[][2]},
|
||||
authors: null|string[],
|
||||
conclusion: null|string,
|
||||
}
|
||||
|
||||
const customBaseQuery = async (
|
||||
args : {method: string, params?: any},
|
||||
{ signal, dispatch, getState, extra },
|
||||
extraOptions
|
||||
) => {
|
||||
const connection : Connection = extra.connection
|
||||
let leanClient = await connection.startLeanClient()
|
||||
console.log(`Sending request ${args.method}`)
|
||||
let res = await leanClient.sendRequest(args.method, args.params)
|
||||
console.log('Received response', res)
|
||||
return {'data': res}
|
||||
}
|
||||
|
||||
// Define a service using a base URL and expected endpoints
|
||||
export const gameApi = createApi({
|
||||
reducerPath: 'gameApi',
|
||||
baseQuery: customBaseQuery,
|
||||
endpoints: (builder) => ({
|
||||
getGameInfo: builder.query<GameState, void>({
|
||||
query: () => {return {method: 'info', params: {}}},
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
// Export hooks for usage in functional components, which are
|
||||
// auto-generated based on the defined endpoints
|
||||
export const { useGetGameInfoQuery } = gameApi
|
||||
@ -1,48 +0,0 @@
|
||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
||||
import { LeanClient } from 'lean4web/client/src/editor/leanclient'
|
||||
import { Connection } from '../connection'
|
||||
import type { RootState } from '../store'
|
||||
|
||||
interface GameState {
|
||||
title: null|string,
|
||||
introduction: null|string,
|
||||
worlds: null|{nodes: string[], edges: string[][2]},
|
||||
authors: null|string[],
|
||||
conclusion: null|string,
|
||||
}
|
||||
|
||||
const initialState : GameState = {
|
||||
title: null,
|
||||
introduction: null,
|
||||
worlds: null,
|
||||
authors: null,
|
||||
conclusion: null,
|
||||
}
|
||||
|
||||
export const gameSlice = createSlice({
|
||||
name: 'game',
|
||||
initialState,
|
||||
reducers: {
|
||||
loadedGame: (state, action: PayloadAction<any>) => {
|
||||
state.title = action.payload.title
|
||||
state.introduction = action.payload.introduction
|
||||
state.worlds = action.payload.worlds
|
||||
state.authors = action.payload.authors
|
||||
state.conclusion = action.payload.conclusion
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
export const { loadedGame } = gameSlice.actions
|
||||
|
||||
|
||||
export const fetchGame = (dispatch, getState, extraArgument) => {
|
||||
const connection : Connection = extraArgument.connection
|
||||
connection.whenLeanClientStarted(() => {
|
||||
connection.getLeanClient().sendRequest("info", {}).then((res) =>{
|
||||
dispatch(loadedGame(res))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default gameSlice.reducer
|
||||
Loading…
Reference in New Issue