Skip to content

Commit

Permalink
Merge pull request #369 from scottbenton:feat/allow-gms-to-remove-pla…
Browse files Browse the repository at this point in the history
…yers

Feat/allow-gms-to-remove-players
  • Loading branch information
scottbenton authored Feb 24, 2024
2 parents 69986e1 + 8b98969 commit 469cde9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Changed the layout of the app
- Added ability to add extra experience points past 30 (Ironsworn)
- Added the ability to delete Rolls from the log (AnnB)
- Added ability for GMs to remove other players and their characters from campaigns

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export function CharacterSection(props: CharacterSectionProps) {
const { campaign, campaignId } = props;

const uid = useStore((store) => store.auth.uid);
const isUserGM = useStore((store) =>
store.campaigns.currentCampaign.currentCampaign?.gmIds?.includes(
store.auth.uid
)
);

const [addCharacterDialogOpen, setAddCharacterDialogOpen] =
useState<boolean>(false);
Expand Down Expand Up @@ -78,27 +83,32 @@ export function CharacterSection(props: CharacterSectionProps) {
<CharacterList
usePlayerNameAsSecondaryText
characters={characters}
actions={(characterId) =>
getUidFromCharacterId(characterId) === uid ? (
<>
actions={(characterId) => (
<>
{getUidFromCharacterId(characterId) === uid && (
<Button
color={"inherit"}
component={Link}
to={constructCharacterSheetPath(characterId)}
>
View
</Button>
)}
{(getUidFromCharacterId(characterId) === uid || isUserGM) && (
<Button
color={"error"}
onClick={() => removeCharacterFromCampaign(characterId)}
onClick={() =>
removeCharacterFromCampaign(
getUidFromCharacterId(characterId) ?? "",
characterId
)
}
>
Remove from Campaign
</Button>
</>
) : (
<></>
)
}
)}
</>
)}
/>
<AddCharacterDialog
open={addCharacterDialogOpen}
Expand Down
37 changes: 29 additions & 8 deletions src/pages/Campaign/CampaignSheetPage/components/PlayerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,28 @@ export function PlayerCard(props: PlayerCardProps) {
const { playerId } = props;
const player = useStore((store) => store.users.userMap[playerId]?.doc);
const loadPlayer = useStore((store) => store.users.loadUserDocument);
const [gmCallLoading, setGMCallLoading] = useState(false);

useEffect(() => {
loadPlayer(playerId);
}, [playerId, loadPlayer]);

const isUserGM = useStore((store) =>
store.campaigns.currentCampaign.currentCampaign?.gmIds?.includes(
store.auth.uid
)
);

const isPlayerGM = useStore((store) =>
store.campaigns.currentCampaign.currentCampaign?.gmIds?.includes(playerId)
);
const addGm = useStore(
(store) => store.campaigns.currentCampaign.updateCampaignGM
);
const [gmCallLoading, setGMCallLoading] = useState(false);

const isPlayerGM = useStore((store) =>
store.campaigns.currentCampaign.currentCampaign?.gmIds?.includes(playerId)
const removePlayer = useStore(
(store) => store.campaigns.currentCampaign.removePlayerFromCampaign
);
const [removePlayerCallLoading, setRemovePlayerCallLoading] = useState(false);

useEffect(() => {
loadPlayer(playerId);
}, [playerId, loadPlayer]);

const handleAddGm = () => {
setGMCallLoading(true);
Expand All @@ -40,6 +43,15 @@ export function PlayerCard(props: PlayerCardProps) {
});
};

const handleRemovePlayer = () => {
setRemovePlayerCallLoading(true);
removePlayer(playerId)
.catch(() => {})
.finally(() => {
setRemovePlayerCallLoading(false);
});
};

return (
<Card variant={"outlined"}>
<Box display={"flex"} alignItems={"center"} p={2}>
Expand All @@ -63,6 +75,15 @@ export function PlayerCard(props: PlayerCardProps) {
>
Add as GM
</Button>
{!isPlayerGM && (
<Button
color={"error"}
disabled={removePlayerCallLoading}
onClick={handleRemovePlayer}
>
Remove from Campaign
</Button>
)}
</Box>
)}
</Card>
Expand Down
14 changes: 12 additions & 2 deletions src/stores/campaign/currentCampaign/currentCampaign.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ export const createCurrentCampaignSlice: CreateSliceType<

return leaveCampaign({ uid, campaignId, campaign });
},
removePlayerFromCampaign: (uid: string) => {
const state = getState();
const campaignId = state.campaigns.currentCampaign.currentCampaignId;
const campaign = state.campaigns.currentCampaign.currentCampaign;

if (!campaign || !campaignId) {
return new Promise((res, reject) => reject("Campaign is not open"));
}

return leaveCampaign({ uid, campaignId, campaign });
},
addCharacter: (characterId) => {
const state = getState();
const uid = state.auth.uid;
Expand All @@ -127,9 +138,8 @@ export const createCurrentCampaignSlice: CreateSliceType<
}
return addCharacterToCampaign({ uid, characterId, campaignId });
},
removeCharacter: (characterId) => {
removeCharacter: (uid, characterId) => {
const state = getState();
const uid = state.auth.uid;
const campaignId = state.campaigns.currentCampaign.currentCampaignId;

if (!campaignId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export interface CurrentCampaignSliceActions {
updateCampaignGM: (gmId: string, shouldRemove?: boolean) => Promise<void>;
deleteCampaign: () => Promise<void>;
leaveCampaign: () => Promise<void>;
removePlayerFromCampaign: (uid: string) => Promise<void>;
addCharacter: (characterId: string) => Promise<void>;
removeCharacter: (characterId: string) => Promise<void>;
removeCharacter: (userId: string, characterId: string) => Promise<void>;
updateCampaignSupply: (supply: number) => Promise<void>;
updateCampaign: (campaign: Partial<StoredCampaign>) => Promise<void>;

Expand Down

0 comments on commit 469cde9

Please sign in to comment.