add flatastic api
This commit is contained in:
@@ -1,48 +1,38 @@
|
||||
import { configureStore } from "@reduxjs/toolkit";
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
import { createAsyncThunk } from "@reduxjs/toolkit";
|
||||
import { callApi } from "@/api/api";
|
||||
import { type DepartureList } from "@/types/types";
|
||||
import { persistReducer } from "redux-persist";
|
||||
import timetableSlice from "./slices/timetableSlice";
|
||||
import flatasticChoresSlice from "./slices/flatastiucChoresSlice";
|
||||
import fetchTimetable from "./thunks/fetchTimetable";
|
||||
import persistConfig from "./persist/persistConfig";
|
||||
|
||||
const fetchTimetable = createAsyncThunk("timetable/phillipStreet", async () => {
|
||||
const hStreetStopId = 7000044;
|
||||
const pStreetStopId = 7000045;
|
||||
const persistedTimetableReducer = persistReducer(
|
||||
persistConfig,
|
||||
timetableSlice.reducer
|
||||
);
|
||||
|
||||
const hStreetData = await callApi(hStreetStopId);
|
||||
const pStreetData = await callApi(pStreetStopId);
|
||||
|
||||
const hStreetJson = await hStreetData.json();
|
||||
const pStreetJson = await pStreetData.json();
|
||||
|
||||
return {
|
||||
hStreet: hStreetJson,
|
||||
pStreet: pStreetJson,
|
||||
};
|
||||
});
|
||||
|
||||
const timetableSlice = createSlice({
|
||||
name: "timetable",
|
||||
initialState: {
|
||||
hStreet: {
|
||||
departureList: [] as DepartureList[],
|
||||
},
|
||||
pStreet: {
|
||||
departureList: [] as DepartureList[],
|
||||
},
|
||||
},
|
||||
reducers: {},
|
||||
extraReducers: (builder) => {
|
||||
builder.addCase(fetchTimetable.fulfilled, (state, action) => {
|
||||
state.hStreet = action.payload.hStreet;
|
||||
state.pStreet = action.payload.pStreet;
|
||||
});
|
||||
},
|
||||
});
|
||||
const persistedFlatasticChoresReducer = persistReducer(
|
||||
persistConfig,
|
||||
flatasticChoresSlice.reducer
|
||||
);
|
||||
|
||||
const store = configureStore({
|
||||
reducer: {
|
||||
timetable: timetableSlice.reducer,
|
||||
timetable: persistedTimetableReducer,
|
||||
flatasticChores: persistedFlatasticChoresReducer
|
||||
},
|
||||
middleware: (getDefaultMiddleware) =>
|
||||
getDefaultMiddleware({
|
||||
serializableCheck: {
|
||||
ignoredActions: [
|
||||
"persist/PERSIST",
|
||||
"persist/REHYDRATE",
|
||||
"persist/PAUSE",
|
||||
"persist/FLUSH",
|
||||
"persist/REGISTER",
|
||||
"persist/PURGE",
|
||||
],
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
export { store, fetchTimetable };
|
||||
@@ -51,4 +41,3 @@ export type RootState = ReturnType<typeof store.getState>;
|
||||
export type AppState = ReturnType<typeof store.getState>;
|
||||
export type AppDispatch = typeof store.dispatch;
|
||||
export type ThunkDispatch = typeof store.dispatch;
|
||||
export type fetchTimetableType = typeof fetchTimetable;
|
||||
|
||||
8
src/store/persist/persistConfig.ts
Normal file
8
src/store/persist/persistConfig.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import storage from 'redux-persist/lib/storage';
|
||||
|
||||
const persistConfig = {
|
||||
key: 'root',
|
||||
storage,
|
||||
};
|
||||
|
||||
export default persistConfig;
|
||||
20
src/store/slices/flatastiucChoresSlice.ts
Normal file
20
src/store/slices/flatastiucChoresSlice.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
import fetchFlatasticChores from "../thunks/fetchFlatasticChores";
|
||||
|
||||
const timetableSlice = createSlice({
|
||||
name: "chores",
|
||||
initialState: {
|
||||
chores: [] as any[],
|
||||
},
|
||||
reducers: {},
|
||||
extraReducers: (builder) => {
|
||||
builder.addCase(fetchFlatasticChores.fulfilled, (state, action) => {
|
||||
// Filter out timetable-related entries
|
||||
state.chores = Array.isArray(action.payload)
|
||||
? action.payload.filter((item) => item.name !== "hstreeet" && item.name !== "pstreet")
|
||||
: [];
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
export default timetableSlice;
|
||||
24
src/store/slices/timetableSlice.ts
Normal file
24
src/store/slices/timetableSlice.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
import fetchTimetable from "@thunks/fetchTimetable";
|
||||
import { type DepartureList } from "@/types/types";
|
||||
|
||||
const timetableSlice = createSlice({
|
||||
name: "timetable",
|
||||
initialState: {
|
||||
hStreet: {
|
||||
departureList: [] as DepartureList[],
|
||||
},
|
||||
pStreet: {
|
||||
departureList: [] as DepartureList[],
|
||||
},
|
||||
},
|
||||
reducers: {},
|
||||
extraReducers: (builder) => {
|
||||
builder.addCase(fetchTimetable.fulfilled, (state, action) => {
|
||||
state.hStreet = action.payload.hStreet;
|
||||
state.pStreet = action.payload.pStreet;
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
export default timetableSlice;
|
||||
22
src/store/thunks/fetchFlatasticChores.ts
Normal file
22
src/store/thunks/fetchFlatasticChores.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { createAsyncThunk } from "@reduxjs/toolkit";
|
||||
import { Flatastic } from "@/api/flatastic";
|
||||
|
||||
const fetchFlatasticChores = createAsyncThunk("flatastic/chores", async () => {
|
||||
if (!import.meta.env.VITE_FLATTASTIC_API_KEY) {
|
||||
throw new Error("Flatastic API Key is not set");
|
||||
}
|
||||
const flatastic = new Flatastic(import.meta.env.VITE_FLATTASTIC_API_KEY);
|
||||
const data = await new Promise((resolve, reject) => {
|
||||
flatastic.getTaskList((info) => {
|
||||
if (info.error) {
|
||||
reject(new Error(info.error));
|
||||
} else {
|
||||
resolve(info);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return data;
|
||||
});
|
||||
|
||||
export default fetchFlatasticChores;
|
||||
18
src/store/thunks/fetchTimetable.ts
Normal file
18
src/store/thunks/fetchTimetable.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { createAsyncThunk } from "@reduxjs/toolkit";
|
||||
import { callApi } from "@/api/api";
|
||||
|
||||
const fetchTimetable = createAsyncThunk("timetable/fetchTimeTable", async () => {
|
||||
const hStreetStopId = 7000044;
|
||||
const pStreetStopId = 7000045;
|
||||
const hStreetData = await callApi(hStreetStopId);
|
||||
const pStreetData = await callApi(pStreetStopId);
|
||||
const hStreetJson = await hStreetData.json();
|
||||
const pStreetJson = await pStreetData.json();
|
||||
|
||||
return {
|
||||
hStreet: hStreetJson,
|
||||
pStreet: pStreetJson,
|
||||
};
|
||||
});
|
||||
|
||||
export default fetchTimetable;
|
||||
Reference in New Issue
Block a user