Skip to content

Commit

Permalink
reorderPlayers reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
wyne committed Apr 4, 2024
1 parent 6f64c47 commit 3819b22
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
12 changes: 12 additions & 0 deletions redux/GamesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ const gamesSlice = createSlice({
},
gameDelete(state, action: PayloadAction<string>) {
gamesAdapter.removeOne(state, action.payload);
},
reorderPlayers(state, action: PayloadAction<{ gameId: string, playerIds: string[]; }>) {
const game = state.entities[action.payload.gameId];
if (!game) { return; }

gamesAdapter.updateOne(state, {
id: action.payload.gameId,
changes: {
playerIds: action.payload.playerIds,
}
});
}
}
});
Expand Down Expand Up @@ -179,6 +190,7 @@ export const {
roundPrevious,
gameSave,
gameDelete,
reorderPlayers,
} = gamesSlice.actions;

export default gamesSlice.reducer;
Expand Down
9 changes: 3 additions & 6 deletions src/screens/EditPlayerScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import { RouteProp } from '@react-navigation/native';
import { StyleSheet, Text, View } from 'react-native';

Check failure on line 4 in src/screens/EditPlayerScreen.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

'Text' is defined but never used
import { Input } from 'react-native-elements';
import { Button, Input } from 'react-native-elements';

import { palette } from '../constants';

Expand Down Expand Up @@ -30,11 +30,6 @@ const EditPlayerScreen: React.FC<EditPlayerScreenProps> = ({

return (
<View>
<Text>Edit Player Screen</Text>
<Text style={styles.playerNumber}>
{index + 1}
</Text>

<View style={[
styles.colorBadge,
{ backgroundColor: "#" + palette[index % palette.length] }
Expand All @@ -55,6 +50,8 @@ const EditPlayerScreen: React.FC<EditPlayerScreenProps> = ({
inputContainerStyle={{ borderBottomWidth: 0 }}
/>

<Button title="Delete" onPress={() => { }} />

</View>
);
};
Expand Down
14 changes: 11 additions & 3 deletions src/screens/SettingsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import DraggableFlatList, { ScaleDecorator } from 'react-native-draggable-flatli
import { Button, Icon } from 'react-native-elements';
import Animated, { LinearTransition } from 'react-native-reanimated';

import { selectSortedPlayers, updateGame } from '../../redux/GamesSlice';
import { reorderPlayers, selectSortedPlayers, updateGame } from '../../redux/GamesSlice';
import { useAppDispatch, useAppSelector } from '../../redux/hooks';
import { playerAdd } from '../../redux/PlayersSlice';
import { selectCurrentGame } from '../../redux/selectors';
import EditGame from '../components/EditGame';
import PlayerListItem from '../components/PlayerListItem';
import { systemBlue } from '../constants';
import logger from '../Logger';

type RouteParams = {
Settings: {
Expand Down Expand Up @@ -95,8 +96,15 @@ const SettingsScreen: React.FunctionComponent<Props> = ({ navigation }) => {
)}
keyExtractor={(player) => player.id}
onDragEnd={({ data }) => {
// Update your state with the new order of players
console.log('End player reoder', data.map((player) => player.id));
// Reorder players
dispatch(
reorderPlayers({
gameId: currentGameId,
playerIds: data.map((player) => player.id)
})
);

logger.info('Reorder players');
}}
/>
</Animated.View>
Expand Down

0 comments on commit 3819b22

Please sign in to comment.