Skip to content

Commit

Permalink
Added name field to the email-form widget (#449)
Browse files Browse the repository at this point in the history
* Added name field to the email-form widget

* Responsive design

---------

Co-authored-by: Rajat Saxena <[email protected]>
  • Loading branch information
ravirajput10 and Rajat Saxena authored Aug 29, 2024
1 parent 66a4747 commit 6d466b4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
3 changes: 3 additions & 0 deletions apps/web/graphql/mails/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ import MailRequestStatusModel, {
const { permissions } = constants;

export async function createSubscription(
name: string,
email: string,
ctx: GQLContext,
): Promise<boolean> {
try {
let dbUser: User | null = await UserModel.findOne({
name,
email,
domain: ctx.subdomain._id,
});

if (!dbUser) {
dbUser = await createUser({
domain: ctx.subdomain!,
name: name,
email: email,
lead: constants.leadNewsletter,
});
Expand Down
5 changes: 3 additions & 2 deletions apps/web/graphql/mails/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ const mutations = {
createSubscription: {
type: GraphQLBoolean,
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
email: { type: new GraphQLNonNull(GraphQLString) },
},
resolve: async (
_: any,
{ email }: { email: string },
{ name, email }: { name: string; email: string },
context: GQLContext,
) => createSubscription(email, context),
) => createSubscription(name, email, context),
},
// createMail: {
// type: types.mail,
Expand Down
3 changes: 3 additions & 0 deletions apps/web/graphql/users/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,14 @@ export const recordProgress = async ({

export async function createUser({
domain,
name,
email,
lead,
superAdmin = false,
subscribedToUpdates = true,
}: {
domain: Domain;
name?: string;
email: string;
lead?:
| typeof constants.leadWebsite
Expand All @@ -273,6 +275,7 @@ export async function createUser({
}): Promise<User> {
const newUser: Partial<User> = {
domain: domain._id,
name: name,
email: email,
active: true,
purchases: [],
Expand Down
31 changes: 22 additions & 9 deletions packages/common-widgets/src/email-form/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const Widget = ({
dispatch,
}: WidgetProps) => {
const [email, setEmail] = useState("");
const [name, setName] = useState("");
const justifyContent =
alignment === "center"
? "center"
Expand All @@ -57,7 +58,7 @@ const Widget = ({

const mutation = `
mutation {
response: createSubscription(email: "${email}")
response: createSubscription(name: "${name}" email: "${email}")
}
`;

Expand All @@ -78,6 +79,7 @@ const Widget = ({
),
),
);
setName("");
setEmail("");
} else {
dispatch(
Expand Down Expand Up @@ -114,7 +116,25 @@ const Widget = ({
>
<h2 className="text-4xl mb-4">{title || DEFAULT_TITLE}</h2>
{subtitle && <h3 className="mb-4">{subtitle}</h3>}
<div className="flex gap-2 items-end">
<div
className="flex flex-col md:!flex-row gap-2 w-full"
style={{
justifyContent,
}}
>
<FormField
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Enter your name"
type="text"
required
messages={[
{
match: "valueMissing",
text: "Your name is required",
},
]}
/>
<FormField
value={email}
onChange={(e) => setEmail(e.target.value)}
Expand Down Expand Up @@ -143,13 +163,6 @@ const Widget = ({
{btnText || DEFAULT_BTN_TEXT}
</Button2>
</div>
{/* <FormSubmit
style={{
backgroundColor: btnBackgroundColor,
color: btnForegroundColor,
}}
disabled={state.networkAction}
/> */}
</Form>
</div>
</section>
Expand Down

0 comments on commit 6d466b4

Please sign in to comment.