Skip to content

Commit

Permalink
Lint cleanup, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jakdan99 committed Nov 1, 2024
1 parent ea34335 commit 2a58ec0
Show file tree
Hide file tree
Showing 16 changed files with 609 additions and 629 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@babel/plugin-transform-react-jsx-self": "^7.24.7",
"@babel/plugin-transform-react-jsx-source": "^7.24.7",
"@carp-dk/authentication-react": "^1.0.1",
"@carp-dk/client": "1.4.0",
"@carp-dk/client": "1.5.0",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@js-joda/core": "^5.6.2",
Expand Down
700 changes: 323 additions & 377 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions src/components/CarpAccordion/styles.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
Accordion,
AccordionDetails,
AccordionSummary,
Typography,
} from "@mui/material";
import { Accordion, AccordionSummary, Typography } from "@mui/material";
import { styled } from "@Utils/theme";

export const StyledAccordion = styled(Accordion)(({ expanded }) => ({
Expand Down
322 changes: 164 additions & 158 deletions src/components/SendReminderModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,73 @@
import {Modal, TextField, Typography} from "@mui/material";
import {useFormik} from "formik";
import {useEffect} from "react";
import { Modal, TextField, Typography } from "@mui/material";
import { useFormik } from "formik";
import { useEffect } from "react";
import * as yup from "yup";
import { GenericEmailRequest } from "@carp-dk/client/models/Email";
import { usePostEmailSendGeneric } from "@Utils/queries/participants";
import {
CancelButton, TypographyVariant,
Content,
DoneButton,
HorizontalInputContainer, HorizontalInputContainerWithAutoHeight,
ModalActions,
ModalBox,
Title,
VerticalInputContainer,
CancelButton,
TypographyVariant,
Content,
DoneButton,
HorizontalInputContainer,
HorizontalInputContainerWithAutoHeight,
ModalActions,
ModalBox,
Title,
VerticalInputContainer,
} from "./styles";
import * as yup from "yup";
import {GenericEmailRequest} from "@carp-dk/client/models/Email";
import {usePostEmailSendGeneric} from "@Utils/queries/participants";

type Props = {
open: boolean,
onClose: () => void,
to: string,
initials: string
researcherEmail: string,
researcherName: string,
studyName: string
open: boolean;
onClose: () => void;
to: string;
initials: string;
researcherEmail: string;
researcherName: string;
studyName: string;
};

const SendReminderModal = ({open, onClose, to, initials, researcherEmail, researcherName, studyName}: Props) => {
const postEmailSendGeneric = usePostEmailSendGeneric();

const validationSchema = yup.object({
message: yup.string().required("Message (email content) is required"),
subject: yup.string().required("Subject is required"),
cc: yup.array().transform(function (value, originalValue) {
if (this.isType(value) && value !== null) {
return value;
}
return originalValue ? originalValue.split(/[\s,]+/) : [];
})
.of(yup.string().email(({value}) => `${value} is not a valid email`)),
});

const reminderFormik = useFormik({
initialValues: {
cc: researcherEmail,
message: `Dear ${initials},
const validationSchema = yup.object({
message: yup.string().required("Message (email content) is required"),
subject: yup.string().required("Subject is required"),
cc: yup
.array()
.transform(function (value, originalValue) {
if (this.isType(value) && value !== null) {
return value;
}
return originalValue ? originalValue.split(/[\s,]+/) : [];
})
.of(yup.string().email(({ value }) => `${value} is not a valid email`)),
});

const SendReminderModal = ({
open,
onClose,
to,
initials,
researcherEmail,
researcherName,
studyName,
}: Props) => {
const postEmailSendGeneric = usePostEmailSendGeneric();

const convertTextareaInputToHtml = (str: string) => {
const urlRegex = /(https?:\/\/[^\s]+)/g;
const urlsWrappedIntoAnchorTags = str.replace(
urlRegex,
(url) => `<a href="${url}">${url}</a>`,
);
const withAddedBr = urlsWrappedIntoAnchorTags.replace(/\n/g, "<br>");
const wrappedInPre = `<pre style="white-space: pre-wrap;">${withAddedBr}</pre>`;

return wrappedInPre;
};

const reminderFormik = useFormik({
initialValues: {
cc: researcherEmail,
message: `Dear ${initials},
You have recently been invited to participate in the "${studyName}" study.
Expand Down Expand Up @@ -72,124 +96,106 @@ Hvis du har spørgsmål, så er du velkommen til at skrive til mig på denne mai
Med venlig hilsen
${researcherName}`,
subject: studyName,
},
onSubmit: (values) => {
let genericEmailRequest: GenericEmailRequest = {
recipient: to,
subject: values.subject,
message: convertTextareaInputToHtml(values.message),
cc: values.cc.split(/[\s,]+/).filter(Boolean),
}
postEmailSendGeneric.mutate(genericEmailRequest)
},
validationSchema
});

function convertTextareaInputToHtml(str: string): string {
const urlRegex = /(https?:\/\/[^\s]+)/g;
const urlsWrappedIntoAnchorTags = str.replace(urlRegex, url => `<a href="${url}">${url}</a>`);
let withAddedBr = urlsWrappedIntoAnchorTags.replace(/\n/g, "<br>");
let wrappedInPre = `<pre style="white-space: pre-wrap;">${withAddedBr}</pre>`;

return wrappedInPre;
}

useEffect(() => {
return () => {
reminderFormik.resetForm();
};
}, [open]);

useEffect(
() => {
onClose();
},
[
postEmailSendGeneric.isSuccess
],
);

return (
<Modal
open={open}
aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description"
onClose={onClose}
>
<ModalBox sx={{boxShadow: 24}}>
<Title variant="h2">Send a reminder</Title>
<Content>
<HorizontalInputContainer>
<Typography variant="h5" width="56px">
To:
</Typography>
<Typography variant="h5">{to}</Typography>
</HorizontalInputContainer>
<HorizontalInputContainerWithAutoHeight>
<TypographyVariant variant="h5">
CC:
</TypographyVariant>
<TextField
type="text"
fullWidth
size="small"
name="cc"
placeholder={"e.g. \"alice@gmail.com, bob@gmail.com\""}
helperText={reminderFormik.errors.cc}
error={!!reminderFormik.errors.cc}
value={reminderFormik.values.cc}
onChange={reminderFormik.handleChange}
/>
</HorizontalInputContainerWithAutoHeight>
<HorizontalInputContainerWithAutoHeight>
<TypographyVariant variant="h5">
Subject:
</TypographyVariant>
<TextField
type="text"
fullWidth
size="small"
name="subject"
helperText={reminderFormik.errors.subject}
error={!!reminderFormik.errors.subject}
value={reminderFormik.values.subject}
onChange={reminderFormik.handleChange}
/>
</HorizontalInputContainerWithAutoHeight>
<VerticalInputContainer>
<Typography variant="h5">Message:</Typography>
<TextField
type="text"
name="message"
fullWidth
multiline
rows={9}
helperText={reminderFormik.errors.message}
error={!!reminderFormik.errors.message}
value={reminderFormik.values.message}
onChange={reminderFormik.handleChange}
/>
</VerticalInputContainer>
</Content>
<ModalActions>
<CancelButton variant="text" onClick={onClose}>
Cancel
</CancelButton>
<DoneButton
disabled={postEmailSendGeneric.isPending}
variant="contained"
sx={{elevation: 0}}
onClick={() => {
reminderFormik.handleSubmit();
}}
>
Send
</DoneButton>
</ModalActions>
</ModalBox>
</Modal>
);
subject: studyName,
},
onSubmit: (values) => {
const genericEmailRequest: GenericEmailRequest = {
recipient: to,
subject: values.subject,
message: convertTextareaInputToHtml(values.message),
cc: values.cc.split(/[\s,]+/).filter(Boolean),
};
postEmailSendGeneric.mutate(genericEmailRequest);
},
validationSchema,
});

useEffect(() => {
return () => {
reminderFormik.resetForm();
};
}, [open]);

useEffect(() => {
onClose();
}, [postEmailSendGeneric.isSuccess]);

return (
<Modal
open={open}
aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description"
onClose={onClose}
>
<ModalBox sx={{ boxShadow: 24 }}>
<Title variant="h2">Send a reminder</Title>
<Content>
<HorizontalInputContainer>
<Typography variant="h5" width="56px">
To:
</Typography>
<Typography variant="h5">{to}</Typography>
</HorizontalInputContainer>
<HorizontalInputContainerWithAutoHeight>
<TypographyVariant variant="h5">CC:</TypographyVariant>
<TextField
type="text"
fullWidth
size="small"
name="cc"
placeholder='e.g. "alice@gmail.com, bob@gmail.com"'
helperText={reminderFormik.errors.cc}
error={!!reminderFormik.errors.cc}
value={reminderFormik.values.cc}
onChange={reminderFormik.handleChange}
/>
</HorizontalInputContainerWithAutoHeight>
<HorizontalInputContainerWithAutoHeight>
<TypographyVariant variant="h5">Subject:</TypographyVariant>
<TextField
type="text"
fullWidth
size="small"
name="subject"
helperText={reminderFormik.errors.subject}
error={!!reminderFormik.errors.subject}
value={reminderFormik.values.subject}
onChange={reminderFormik.handleChange}
/>
</HorizontalInputContainerWithAutoHeight>
<VerticalInputContainer>
<Typography variant="h5">Message:</Typography>
<TextField
type="text"
name="message"
fullWidth
multiline
rows={9}
helperText={reminderFormik.errors.message}
error={!!reminderFormik.errors.message}
value={reminderFormik.values.message}
onChange={reminderFormik.handleChange}
/>
</VerticalInputContainer>
</Content>
<ModalActions>
<CancelButton variant="text" onClick={onClose}>
Cancel
</CancelButton>
<DoneButton
disabled={postEmailSendGeneric.isPending}
variant="contained"
sx={{ elevation: 0 }}
onClick={() => {
reminderFormik.handleSubmit();
}}
>
Send
</DoneButton>
</ModalActions>
</ModalBox>
</Modal>
);
};

export default SendReminderModal;
6 changes: 4 additions & 2 deletions src/components/SendReminderModal/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ export const HorizontalInputContainer = styled("div")({
alignItems: "center",
});

export const HorizontalInputContainerWithAutoHeight = styled(HorizontalInputContainer)({
height: "auto",
export const HorizontalInputContainerWithAutoHeight = styled(
HorizontalInputContainer,
)({
height: "auto",
});

export const VerticalInputContainer = styled("div")({
Expand Down
Loading

0 comments on commit 2a58ec0

Please sign in to comment.