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 | import { styled } from "@mui/material"; import Button, { AppButtonProps } from "components/Button"; import TextField from "components/TextField"; import { useAuthContext } from "features/auth/AuthProvider"; import { useListAvailableReferences } from "features/profile/hooks/referencesHooks"; import { Empty } from "google-protobuf/google/protobuf/empty_pb"; import { RpcError } from "grpc-web"; import { useTranslation } from "i18n"; import { GLOBAL, MESSAGES } from "i18n/namespaces"; import Link from "next/link"; import { HostRequestStatus } from "proto/conversations_pb"; import { ReferenceType } from "proto/references_pb"; import { HostRequest, RespondHostRequestReq } from "proto/requests_pb"; import React from "react"; import { useForm } from "react-hook-form"; import { UseMutationResult } from "react-query"; import { referenceTypeRoute, routeToLeaveReference } from "routes"; import { theme } from "theme"; import FieldButton from "./FieldButton"; import HostRequestGuideLinks from "./HostRequestGuideLinks"; import HostRequestRespondButtons from "./HostRequestRespondButtons"; interface MessageFormData { text: string; } export interface HostRequestSendFieldProps { hostRequest: HostRequest.AsObject; sendMutation: UseMutationResult<string | undefined | Empty, RpcError, string>; respondMutation: UseMutationResult< unknown, RpcError, Required<RespondHostRequestReq.AsObject> >; } const StyledButtonContainer = styled("div")(({ theme }) => ({ "& > button": { marginInline: theme.spacing(2), }, display: "flex", flexWrap: "wrap", justifyContent: "center", })); const StyledButton = styled(Button)<AppButtonProps>({ display: "block", flexShrink: 0, marginInlineStart: theme.spacing(1), height: theme.spacing(5), marginBottom: 0, marginTop: "auto", alignItems: "center", }); const StyledContainer = styled("div")(({ theme }) => ({ alignItems: "flex-start", display: "flex", marginTop: theme.spacing(3), })); export default function HostRequestSendField({ hostRequest, sendMutation, respondMutation, }: HostRequestSendFieldProps) { const { t } = useTranslation([MESSAGES, GLOBAL]); const { authState } = useAuthContext(); const isHost = hostRequest.hostUserId === authState.userId; const { data: availableRefrences } = useListAvailableReferences( isHost ? hostRequest.surferUserId : hostRequest.hostUserId ); const { mutate: handleSend, isLoading } = sendMutation; const { mutate: handleRespond, isLoading: isResponseLoading } = respondMutation; const { register, handleSubmit, reset } = useForm<MessageFormData>(); const onSubmit = handleSubmit(async (data: MessageFormData) => { handleSend(data.text); reset(); }); const handleStatus = (status: HostRequestStatus) => handleSubmit(async (data: MessageFormData) => { handleRespond({ hostRequestId: hostRequest.hostRequestId, status, text: data.text, }); reset(); }); const isButtonLoading = isLoading || isResponseLoading; const isPast = hostRequest.toDate < new Date().toISOString().split("T")[0]; const isRequestClosed = isPast || hostRequest.status === HostRequestStatus.HOST_REQUEST_STATUS_CANCELLED || hostRequest.status === HostRequestStatus.HOST_REQUEST_STATUS_REJECTED; const isReferenceAvailable = (hostRequest.status === HostRequestStatus.HOST_REQUEST_STATUS_CONFIRMED || hostRequest.status === HostRequestStatus.HOST_REQUEST_STATUS_ACCEPTED) && availableRefrences && availableRefrences.availableWriteReferencesList.find( ({ hostRequestId }) => hostRequestId === hostRequest.hostRequestId ); const referenceRoute = routeToLeaveReference( referenceTypeRoute[ isHost ? ReferenceType.REFERENCE_TYPE_HOSTED : ReferenceType.REFERENCE_TYPE_SURFED ], isHost ? hostRequest.surferUserId : hostRequest.hostUserId, hostRequest.hostRequestId ); const handleKeyDown = (event: React.KeyboardEvent) => { Iif (event.key === "Enter" && event.ctrlKey) { event.preventDefault(); onSubmit(); } }; return ( <form onSubmit={onSubmit}> <HostRequestGuideLinks isPast={isPast} isHost={isHost} status={hostRequest.status} /> <StyledButtonContainer> {!isPast && ( <HostRequestRespondButtons isHost={isHost} status={hostRequest.status} isLoading={isButtonLoading} handleStatus={handleStatus} /> )} {isReferenceAvailable && ( <Link href={referenceRoute} passHref legacyBehavior> <StyledButton color="primary" component="a"> {t("messages:write_reference_button_text")} </StyledButton> </Link> )} </StyledButtonContainer> <StyledContainer> <TextField {...register("text")} defaultValue={ isRequestClosed ? t("messages:request_closed_message") : "" } disabled={isRequestClosed} fullWidth aria-label={t("messages:chat_input.label")} label={!isRequestClosed ? t("messages:chat_input.label") : ""} id="host-request-message" InputLabelProps={{ style: { transform: isRequestClosed ? "none" : undefined, }, shrink: isRequestClosed ? false : undefined, }} multiline onKeyDown={handleKeyDown} maxRows={6} size="small" sx={{ background: theme.palette.common.white }} /> <FieldButton callback={onSubmit} disabled={isRequestClosed} isLoading={isButtonLoading} isSubmit > {t("global:send")} </FieldButton> </StyledContainer> </form> ); } |