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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 101x 1x 101x 12x 101x 101x 101x 101x 101x 101x 101x 101x 101x 3x 3x 101x 101x 101x 101x 101x 1x 101x 101x 69x 1x 1x | import { ButtonProps, styled } from "@mui/material";
import { UseMutationResult } from "@tanstack/react-query";
import Button 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 { referenceTypeRoute, routeToLeaveReference } from "routes";
import { theme } from "theme";
import FieldButton from "./FieldButton";
import HostRequestGuideLinks from "./HostRequestGuideLinks";
import HostRequestRespondButtons from "./HostRequestRespondButtons";
interface MessageFormData {
text: string;
}
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)<ButtonProps>({
display: "flex",
flexShrink: 0,
marginInlineStart: theme.spacing(1),
height: theme.spacing(5),
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, isPending } = sendMutation;
const { mutate: handleRespond, isPending: isResponseLoading } =
respondMutation;
const { register, handleSubmit, reset, watch } = useForm<MessageFormData>();
const messageText = watch("text", "");
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 = isPending || isResponseLoading;
const isPast = hostRequest.toDate < new Date().toISOString().split("T")[0];
const isRequestClosed =
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) => {
if (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 && (
<StyledButton color="primary" component={Link} href={referenceRoute}>
{t("messages:write_reference_button_text")}
</StyledButton>
)}
</StyledButtonContainer>
<StyledContainer>
<TextField
{...register("text")}
value={
isRequestClosed ? t("messages:request_closed_message") : undefined
}
disabled={isRequestClosed}
fullWidth
aria-label={t("messages:chat_input.label")}
label={!isRequestClosed ? t("messages:chat_input.label") : ""}
id="host-request-message"
slotProps={{
inputLabel: {
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 || !messageText.trim()}
isLoading={isButtonLoading}
isSubmit
>
{t("global:send")}
</FieldButton>
</StyledContainer>
</form>
);
}
|