From 9f0a876cba47cbb7ad711325789af759b9551e3e Mon Sep 17 00:00:00 2001 From: Solareon <769465+solareon@users.noreply.github.com> Date: Wed, 26 Jun 2024 19:56:51 +0200 Subject: [PATCH] refactor: cleanup datastores --- web/src/App.tsx | 5 +- web/src/components/GroupDashboard.tsx | 140 ++++++++++++-------------- web/src/components/GroupJob.tsx | 9 +- web/src/components/PlayerList.tsx | 12 +-- web/src/storage/GroupStore.ts | 15 +-- web/src/storage/PlayerListStore.ts | 35 +++++++ 6 files changed, 118 insertions(+), 98 deletions(-) create mode 100644 web/src/storage/PlayerListStore.ts diff --git a/web/src/App.tsx b/web/src/App.tsx index e515818..a85f746 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -22,6 +22,7 @@ const App = () => { onSettingsChange, } = window as any; const [currentPage, setCurrentPage] = useState("GroupDashboard"); + const [inGroup, setInGroup] = useState(false); useEffect(() => { if (devMode) { @@ -91,10 +92,10 @@ const App = () => { )}
Groups
{currentPage === "GroupDashboard" && ( - + )} {currentPage === "GroupJob" && ( - + )} diff --git a/web/src/components/GroupDashboard.tsx b/web/src/components/GroupDashboard.tsx index 94067aa..af2b60c 100644 --- a/web/src/components/GroupDashboard.tsx +++ b/web/src/components/GroupDashboard.tsx @@ -15,70 +15,56 @@ import { faRightFromBracket, } from "@fortawesome/free-solid-svg-icons"; -const GroupDashboard = ({ setCurrentPage }) => { - const { currentGroups, getCurrentGroups } = useGroupStore(); +const GroupDashboard = ({ setCurrentPage, setInGroup, inGroup}) => { + const { currentGroups, refreshGroups } = useGroupStore(); const { playerData } = usePlayerDataStore(); - const [ groups, setGroups ] = useState([]); const [showCreateGroup, setShowCreateGroup] = useState(false); const [showPlayerList, setShowPlayerList] = useState(false); const [selectedGroup, setSelectedGroup] = useState(null); + const [currentGroup, setCurrentGroup] = useState(null); const [isDialogOpen, setIsDialogOpen] = useState(false); useEffect(() => { - getCurrentGroups(); - }, []); + if (currentGroups.length === 0) { + refreshGroups(); + } + }, []) useEffect(() => { - setGroups(currentGroups); - }, [currentGroups]); - - const inGroup = groups.some((group) => - group.members.some((member) => member.Player === playerData.source) - ); + setInGroup(currentGroups && currentGroups.length > 0 && currentGroups.some((group) => + group.members.some((member) => member.Player === playerData.source) + )) + setCurrentGroup(currentGroups.find(group => group.members.some(member => member.Player === playerData.source))); + }, [currentGroups]) const handleConfirm = () => { - // Add your confirm action here + fetchReactNui('leaveGroup') setIsDialogOpen(false); }; const createGroup = (groupData) => { console.log(groupData); - const newGroup = { - id: groups.length + 1, - status: "open", - GName: groupData.groupName, - GPass: groupData.password, - leader: playerData.source, - members: [ - { - name: playerData.name, - CID: playerData.citizenId, - Player: playerData.source, - }, - ], - stage: [], - ScriptCreated: false, - }; - console.log(newGroup); - setGroups([...groups, newGroup]); + fetchReactNui('createGroup', groupData) }; const joinGroup = (groupData) => { + fetchReactNui('joinGroup', groupData) console.log(groupData); }; const leaveGroup = () => { - console.log("Leaving current group"); setIsDialogOpen(true); }; const removeGroup = (groupData) => { + fetchReactNui('removeGroup', groupData) console.log(groupData); }; - const renderIcons = (isLeader, isMember, element) => { + const renderIcons = (isLeader, isMember, group) => { return ( <> +
{ removeGroup(element)} + onClick={() => removeGroup(group)} /> )} {isMember && !isLeader && ( leaveGroup(element)} + onClick={() => leaveGroup(group)} /> )} +
); }; @@ -131,49 +118,50 @@ const GroupDashboard = ({ setCurrentPage }) => { Leave Group -

Create a group or join an existing group below

-
- {Object.keys(groups).map((key) => { - const element = groups[key]; - let isLeader = element.leader === playerData.source; - let isMember = element.members.some( - (member) => member.Player === playerData.source - ); + {currentGroups?.length > 0 ? ( +

Create a group or join an existing group below

+ ) : ( +

Create a group to get started

+ )} + {currentGroups && currentGroups.length > 0 && ( +
+ {currentGroups.map((group) => { + let isLeader = group.leader === playerData.source; + let isMember = group.members.some( + (member) => member.Player === playerData.source + ); + if (isMember === true) {} - return ( -
{ - if (!inGroup) { - setSelectedGroup(element); - } - }} - > -
- - {element.GName} + return ( +
{ + if (!inGroup) { + setSelectedGroup(group); + } + }} + > +
+ + {group.GName} +
+
+ <> + {isMember && (renderIcons(isLeader, isMember, group))} + + + {group.members.length} + + +
-
- <> - {isLeader || - (isMember && ( -
- {renderIcons(isLeader, isMember, element)} -
- ))} - - - {element.members.length} - - -
-
- ); - })} -
+ ); + })} +
+ )} {showCreateGroup && ( { @@ -197,7 +185,7 @@ const GroupDashboard = ({ setCurrentPage }) => { /> )} {showPlayerList && ( - setShowPlayerList(false)} /> + setShowPlayerList(false)} currentGroup={currentGroup}/> )} {isDialogOpen && ( = ({ setCurrentPage }) => { +const GroupJob: React.FC = ({ setCurrentPage, inGroup }) => { const { groups } = useGroupStore(); const { playerData } = usePlayerDataStore(); const { groupJobSteps, getGroupJobSteps } = useGroupJobStepStore(); @@ -27,12 +28,8 @@ const GroupJob: React.FC = ({ setCurrentPage }) => { setSteps(groupJobSteps); }, [groupJobSteps]); - const inGroup = groups.some((group) => - group.members.some((member) => member.Player === playerData.source) - ); - const handleConfirm = () => { - // Add your confirm action here + fetchReactNui('leaveGroup') setIsDialogOpen(false); }; diff --git a/web/src/components/PlayerList.tsx b/web/src/components/PlayerList.tsx index 3bdb6cb..de9a26b 100644 --- a/web/src/components/PlayerList.tsx +++ b/web/src/components/PlayerList.tsx @@ -2,20 +2,16 @@ import React, { useContext } from 'react'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faUser, faTrash } from '@fortawesome/free-solid-svg-icons'; import { usePlayerDataStore } from '../storage/PlayerDataStore'; -import { useGroupStore } from '../storage/GroupStore'; -const PlayerList: React.FC = ({ onClose }) => { +const PlayerList: React.FC = ({ onClose, currentGroup }) => { const { playerData } = usePlayerDataStore(); - const { groups, setGroups } = useGroupStore(); - const currentGroup = groups.find((group) => - group.members.some((member) => member.Player === playerData.source) - ); - const isLeader = currentGroup.leader === playerData.source; const removeGroupMember = (member) => { console.log('Remove Member', member); }; + const isLeader = currentGroup.members.find(member => member.Player === playerData.source).isLeader; + return (
@@ -32,7 +28,7 @@ const PlayerList: React.FC = ({ onClose }) => { > <> - { isLeader && member.Player !== playerData.source && + { (isLeader && member.Player !== playerData.source) && void; + inGroup: boolean; + refreshGroups: () => void; setGroups: (currentGroups: Group[]) => void; } export const useGroupStore = create((set) => ({ - getCurrentGroups: async () => { - const currentGroups = await fetchReactNui("getCurrentGroups", {}, [ + currentGroups: [], + refreshGroups: async () => { + const currentGroups = await fetchReactNui("refreshGroups", {}, [ { id: 1, status: "open", @@ -43,7 +45,7 @@ export const useGroupStore = create((set) => ({ GPass: "password", leader: 3, members: [ - { name: "Larry", CID: "ABCD1234", Player: 1 }, + { name: "Larry", CID: "ABCD1234", Player: 2 }, { name: "Barry", CID: "EFGH5678", Player: 3 }, ], stage: [], @@ -60,7 +62,8 @@ export const useGroupStore = create((set) => ({ ScriptCreated: false, }, ]); - set({ currentGroups }); + set({ currentGroups }, true); + console.log("Fetched groups"); }, setGroups: (currentGroups) => set({ currentGroups }), -})); +})); \ No newline at end of file diff --git a/web/src/storage/PlayerListStore.ts b/web/src/storage/PlayerListStore.ts new file mode 100644 index 0000000..463a811 --- /dev/null +++ b/web/src/storage/PlayerListStore.ts @@ -0,0 +1,35 @@ +import { create } from 'zustand'; + +type PlayerListStoreState = { + members: string[]; + leader: number; + addMember: (member: string) => void; + setLeader: (leader: number) => void; +}; + +const usePlayerListStore = create((set) => ({ + members: [], + leader: 0, + addMember: (member) => set((state) => ({ members: [...state.members, member] })), + setLeader: (leader) => set({ leader }), +})); + +const PlayerListStore: React.FC = () => { + const { members, leader, addMember, setLeader } = usePlayerListStore(); + + return ( +
+

Player List

+
    + {members.map((member, index) => ( +
  • {member}
  • + ))} +
+

Leader: {leader}

+ + +
+ ); +}; + +export default PlayerListStore; \ No newline at end of file