All files / pages PlayPage.js

100% Statements 33/33
100% Branches 17/17
100% Functions 10/10
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236                                    88x 88x 88x 88x 88x   88x 2x     88x 2x       88x                         88x                         88x                         88x                     88x       88x                       88x               88x 1x     88x     88x                       88x             88x 1x     88x   88x 3x     88x                     88x             88x         88x                                                                                                                                          
import React, { useState } from "react";
import { Container, CardGroup, Button } from "react-bootstrap";
import { useParams } from "react-router-dom";
 
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import CommonsOverview from "main/components/Commons/CommonsOverview";
import CommonsPlay from "main/components/Commons/CommonsPlay";
import FarmStats from "main/components/Commons/FarmStats";
import ManageCows from "main/components/Commons/ManageCows";
import Profits from "main/components/Commons/Profits";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import { hasRole } from "main/utils/currentUser";
import { useCurrentUser } from "main/utils/currentUser";
import Background from "../../assets/PlayPageBackground.jpg";
import ChatPanel from "main/components/Chat/ChatPanel";
import ManageCowsModal from "main/components/Commons/ManageCowsModal";
 
export default function PlayPage() {
    const { commonsId } = useParams();
    const { data: currentUser } = useCurrentUser();
    const [isModalOpen, setIsModalOpen] = useState(false);
    const [message, setMessage] = useState();
    const [numCows, setNumCows] = useState(1);
 
    const openModal = () => {
        setIsModalOpen(true);
    };
 
    const closeModal = () => {
        setIsModalOpen(false);
    };
 
    // Stryker disable all
    const { data: userCommons } = useBackend(
        [`/api/usercommons/forcurrentuser?commonsId=${commonsId}`],
        {
            method: "GET",
            url: "/api/usercommons/forcurrentuser",
            params: {
                commonsId: commonsId,
            },
        }
    );
    // Stryker restore all
 
    // Stryker disable all
    const { data: commonsPlus } = useBackend(
        [`/api/commons/plus?id=${commonsId}`],
        {
            method: "GET",
            url: "/api/commons/plus",
            params: {
                id: commonsId,
            },
        }
    );
    // Stryker restore all
 
    // Stryker disable all
    const { data: userCommonsProfits } = useBackend(
        [`/api/profits/all/commonsid?commonsId=${commonsId}`],
        {
            method: "GET",
            url: "/api/profits/all/commonsid",
            params: {
                commonsId: commonsId,
            },
        }
    );
    // Stryker restore all
 
    // Stryker disable all
    const { data: announcementsResponse } = useBackend(
        [`/api/announcements/getbycommonsid?commonsId=${commonsId}`],
        {
            method: "GET",
            url: "/api/announcements/getbycommonsid",
            params: {
                commonsId: commonsId,
            },
        }
    );
 
    const announcements = announcementsResponse ? announcementsResponse.content : null;
    // Stryker restore all
 
    // Stryker disable all (can't check if commonsId is null because it is mocked)
    const objectToAxiosParamsBuy = (newUserCommons) => ({
        url: "/api/usercommons/buy",
        method: "PUT",
        data: newUserCommons,
        params: {
            commonsId: commonsId,
            numCows: numCows,
        },
    });
    // Stryker restore all
 
    // Stryker disable all
    const mutationbuy = useBackendMutation(
        objectToAxiosParamsBuy,
        null,
        // Stryker disable next-line all : hard to set up test for caching
        [`/api/usercommons/forcurrentuser?commonsId=${commonsId}`]
    );
    // Stryker restore all
 
    const onBuy = (userCommons, numCows) => {
        mutationbuy.mutate(userCommons, numCows);
    };
 
    const onSuccessSell = () => {};
 
    // Stryker disable all
    const objectToAxiosParamsSell = (newUserCommons) => ({
        url: "/api/usercommons/sell",
        method: "PUT",
        data: newUserCommons,
        params: {
            commonsId: commonsId,
            numCows: numCows,
        },
    });
    // Stryker restore all
 
    // Stryker disable all
    const mutationsell = useBackendMutation(
        objectToAxiosParamsSell,
        { onSuccess: onSuccessSell },
        [`/api/usercommons/forcurrentuser?commonsId=${commonsId}`]
    );
    // Stryker restore all
 
    const onSell = (userCommons, numCows) => {
        mutationsell.mutate(userCommons, numCows);
    };
 
    const [isChatOpen, setIsChatOpen] = useState(false);
 
    const toggleChatWindow = () => {
        setIsChatOpen((prevState) => !prevState);
    };
 
    const chatButtonStyle = {
        width: "60px",
        height: "60px",
        borderRadius: "25%",
        backgroundColor: "lightblue",
        color: "black",
        position: "fixed",
        bottom: "30px",
        right: "30px",
    };
 
    const chatContainerStyle = {
        width: "550px",
        position: "fixed",
        bottom: "100px",
        right: "20px",
    };
 
    const emojiStyle = {
        fontFamily: "Arial, sans-serif",
        fontSize: "30px",
    };
 
    return (
        <div
            style={{
                backgroundSize: "cover",
                backgroundImage: `url(${Background})`,
            }}
            data-testid="playpage-div"
        >
            <BasicLayout>
                <Container>
                    {!!currentUser && <CommonsPlay currentUser={currentUser} />}
                    {!!commonsPlus && (
                        <CommonsOverview
                            commonsPlus={commonsPlus}
                            currentUser={currentUser}
                            announcements={announcements}
                        />
                    )}
                    <br />
                    {!!userCommons && !!commonsPlus && (
                        <CardGroup>
                            <ManageCows
                                userCommons={userCommons}
                                commons={commonsPlus.commons}
                                setMessage={setMessage}
                                openModal={openModal}
                            />
                            <FarmStats userCommons={userCommons} />
                            <Profits
                                userCommons={userCommons}
                                profits={userCommonsProfits}
                            />
                            <ManageCowsModal
                                number={numCows}
                                setNumber={setNumCows}
                                userCommons={userCommons}
                                isOpen={isModalOpen}
                                message={message}
                                onClose={closeModal}
                                onBuy={onBuy}
                                onSell={onSell}
                            />
                        </CardGroup>
                    )}
                </Container>
            </BasicLayout>
            { (hasRole(currentUser, "ROLE_ADMIN") || (!!commonsPlus && commonsPlus.commons.showChat)) &&
                <div style={chatContainerStyle} data-testid="playpage-chat-div">
                    {!!isChatOpen && <ChatPanel commonsId={commonsId} />}
                    <Button
                        style={chatButtonStyle}
                        onClick={toggleChatWindow}
                        data-testid="playpage-chat-toggle"
                    >
                        {!!isChatOpen ? (
                            <span style={emojiStyle} data-testid="close-icon">
                                ❌
                            </span>
                        ) : (
                            <span style={emojiStyle} data-testid="message-icon">
                                💬
                            </span>
                        )}
                    </Button>
                </div>
            }
        </div>
    );
}