JetUp
2 years ago
12 changed files with 281 additions and 34 deletions
@ -0,0 +1,8 @@ |
|||||||
|
import AsyncStorage from '@react-native-async-storage/async-storage' |
||||||
|
import Reactotron from 'reactotron-react-native' |
||||||
|
|
||||||
|
Reactotron |
||||||
|
.setAsyncStorageHandler(AsyncStorage) // AsyncStorage would either come from `react-native` or `@react-native-community/async-storage` depending on where you get it from
|
||||||
|
.configure() // controls connection & communication settings
|
||||||
|
.useReactNative() // add all built-in react native plugins
|
||||||
|
.connect() // let's connect!
|
@ -0,0 +1,65 @@ |
|||||||
|
import {createAsyncThunk, createSlice} from '@reduxjs/toolkit'; |
||||||
|
import {GameItem} from '../../shared/interfaces/game-item'; |
||||||
|
import firestore from '@react-native-firebase/firestore'; |
||||||
|
import {RootState} from '../../../store/store'; |
||||||
|
import { shuffle } from '../shuffle/shuffle'; |
||||||
|
|
||||||
|
export interface PostsState { |
||||||
|
posts: GameItem[]; |
||||||
|
shuffled: GameItem[]; |
||||||
|
loaded: boolean; |
||||||
|
hasError: boolean; |
||||||
|
} |
||||||
|
|
||||||
|
const initialState: PostsState = { |
||||||
|
posts: [], |
||||||
|
shuffled: [], |
||||||
|
loaded: false, |
||||||
|
hasError: false, |
||||||
|
}; |
||||||
|
|
||||||
|
export const fetchPostsAsync = createAsyncThunk( |
||||||
|
'posts/fetchPosts', |
||||||
|
async () => { |
||||||
|
const data = [] as GameItem[]; |
||||||
|
let querySnapshot = await firestore().collection('GameItems').get(); |
||||||
|
querySnapshot.forEach(doc => { |
||||||
|
const item = doc.data(); |
||||||
|
data.push(item as GameItem); |
||||||
|
}); |
||||||
|
return data; |
||||||
|
}, |
||||||
|
); |
||||||
|
|
||||||
|
export const postsSlice = createSlice({ |
||||||
|
name: 'posts', |
||||||
|
initialState, |
||||||
|
reducers: { |
||||||
|
shuffleItems: state => { |
||||||
|
state.shuffled = shuffle(state.posts); |
||||||
|
}, |
||||||
|
}, |
||||||
|
extraReducers(builder) { |
||||||
|
builder |
||||||
|
.addCase(fetchPostsAsync.fulfilled, (state, action) => { |
||||||
|
state.posts = action.payload; |
||||||
|
state.hasError = false; |
||||||
|
state.loaded = true; |
||||||
|
}) |
||||||
|
.addCase(fetchPostsAsync.pending, state => { |
||||||
|
state.loaded = false; |
||||||
|
}) |
||||||
|
.addCase(fetchPostsAsync.rejected, state => { |
||||||
|
state.hasError = true; |
||||||
|
}); |
||||||
|
}, |
||||||
|
}); |
||||||
|
|
||||||
|
export const {shuffleItems} = postsSlice.actions; |
||||||
|
|
||||||
|
export const selectPosts = (state: RootState) => state.gameItems.posts; |
||||||
|
export const selectHasError = (state: RootState) => state.gameItems.hasError; |
||||||
|
export const selectLoaded = (state: RootState) => state.gameItems.loaded; |
||||||
|
export const selectShuffled = (state: RootState) => state.gameItems.shuffled; |
||||||
|
|
||||||
|
export default postsSlice.reducer; |
@ -0,0 +1,7 @@ |
|||||||
|
export interface GameItem { |
||||||
|
id: number; |
||||||
|
isDare: boolean; |
||||||
|
en: string; |
||||||
|
ua: string; |
||||||
|
hi: string; |
||||||
|
} |
Loading…
Reference in new issue