Pārlūkot izejas kodu

add customer on newsletter

master
azri 1 nedēļu atpakaļ
vecāks
revīzija
54868dc12d

+ 1
- 0
src/App.js Parādīt failu

@@ -15,6 +15,7 @@ import { ThemeProvider } from '@mui/material';
15 15
 import { fetchCart, createCart } from './redux/slices/cartSlice';
16 16
 import { useSelector, useDispatch } from 'react-redux';
17 17
 import Collection from './pages/Collection';
18
+import customerService from './services/CustomerService';
18 19
 
19 20
 const isEmptyObject = (obj) => Object.keys(obj).length === 0 && obj.constructor === Object;
20 21
 

+ 41
- 2
src/components/Footer/Footer.jsx Parādīt failu

@@ -1,9 +1,13 @@
1
-import React from 'react';
1
+import { useState } from 'react';
2 2
 import { Box, Typography, TextField, Button, InputAdornment } from '@mui/material';
3 3
 import logoSrc from "../../assets/images/amberlogofooter.png";
4 4
 import Grid from '@mui/material/Grid2';
5
+import customerService from '../../services/CustomerService';
5 6
 
6 7
 const Footer = () => {
8
+
9
+  const [email, setEmail] = useState("")
10
+
7 11
   return (
8 12
     <Box
9 13
       sx={{
@@ -100,11 +104,46 @@ const Footer = () => {
100 104
             <TextField
101 105
               variant="outlined"
102 106
               placeholder="your email address"
107
+              value={email}
108
+              type='email'
109
+              onChange={(e) => { setEmail(e.target.value) }}
103 110
               sx={{ p: 0, mb: 2 }}
104 111
               InputProps={{
105 112
                 startAdornment: (
106 113
                   <InputAdornment position="end">
107
-                    <Button variant='contained' sx={{position:"absolute", right:0, height:"100%" }}>
114
+                    <Button 
115
+                      variant='contained' 
116
+                      sx={{ position: "absolute", right: 0, height: "100%" }}
117
+                      onClick={() => {
118
+
119
+                        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
120
+  
121
+                        if (!email || email.trim() === "") {
122
+                          alert("Email is required");
123
+                          return;
124
+                        }
125
+  
126
+                        if (!emailRegex.test(email)) {
127
+                          alert("Invalid Email");
128
+                          return;
129
+                        }
130
+  
131
+                        if(customerService.createCustomer({
132
+                          "email": email,
133
+                          "firstName": email,
134
+                          "lastName": "customer",
135
+                          "password": "securePassword123"
136
+                        })){
137
+  
138
+                          alert(`SUCCESS ADD: ${email}`)
139
+                          setEmail('')
140
+  
141
+                        }else{
142
+                          alert("ALREADY ADDED");
143
+                        }
144
+  
145
+                      }}
146
+                    >
108 147
                       Subscribe
109 148
                     </Button>
110 149
                   </InputAdornment>

+ 38
- 2
src/components/NewsLetter/NewsLetter.jsx Parādīt failu

@@ -1,8 +1,12 @@
1
-import React from 'react';
1
+import { useState } from 'react';
2 2
 import bgImage from "../../assets/images/newsletterwallpaper.jpg"
3 3
 import { Box, Button, TextField, InputAdornment, Typography } from '@mui/material';
4
+import customerService from '../../services/CustomerService';
4 5
 
5 6
 const NewsLetter = () => {
7
+
8
+  const [email, setEmail] = useState("")
9
+
6 10
   return (
7 11
     <Box
8 12
       sx={{
@@ -11,7 +15,7 @@ const NewsLetter = () => {
11 15
         height: 0,
12 16
         paddingTop: '600px', // This sets the height based on top padding
13 17
         backgroundImage: `url(${bgImage})`,
14
-        backgroundSize:"cover",
18
+        backgroundSize: "cover",
15 19
         backgroundPosition: 'center center',
16 20
         overflow: 'hidden',
17 21
         display: "flex",
@@ -52,6 +56,9 @@ const NewsLetter = () => {
52 56
             label="Enter your email address"
53 57
             variant="outlined"
54 58
             fullWidth
59
+            value={email}
60
+            type='email'
61
+            onChange={(e) => { setEmail(e.target.value) }}
55 62
             sx={{
56 63
               backgroundColor: "white",
57 64
               my: 4,
@@ -76,6 +83,35 @@ const NewsLetter = () => {
76 83
                       position: "absolute",
77 84
                       right: 0
78 85
                     }}
86
+                    onClick={() => {
87
+
88
+                      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
89
+
90
+                      if (!email || email.trim() === "") {
91
+                        alert("Email is required");
92
+                        return;
93
+                      }
94
+
95
+                      if (!emailRegex.test(email)) {
96
+                        alert("Invalid Email");
97
+                        return;
98
+                      }
99
+
100
+                      if(customerService.createCustomer({
101
+                        "email": email,
102
+                        "firstName": email,
103
+                        "lastName": "customer",
104
+                        "password": "securePassword123"
105
+                      })){
106
+
107
+                        alert(`SUCCESS ADD: ${email}`)
108
+                        setEmail('')
109
+
110
+                      }else{
111
+                        alert("ALREADY ADDED");
112
+                      }
113
+
114
+                    }}
79 115
                   >
80 116
                     Subscribe
81 117
                   </Button>

+ 45
- 0
src/services/CustomerService.js Parādīt failu

@@ -0,0 +1,45 @@
1
+import { createStorefrontApiClient } from '@shopify/storefront-api-client';
2
+import { REACT_APP_ACCESS_TOKEN, REACT_APP_SHOP_NAME } from '../utils/httpCommon';
3
+
4
+const client = createStorefrontApiClient({
5
+  storeDomain: `https://${REACT_APP_SHOP_NAME}/api/2024-10/graphql.json`,
6
+  apiVersion: '2024-10',
7
+  publicAccessToken: REACT_APP_ACCESS_TOKEN,
8
+});
9
+
10
+// Create a cart
11
+const createCustomer = async (input) => {
12
+  const query = `
13
+    mutation CreateCustomer($input: CustomerCreateInput!) {
14
+      customerCreate(input: $input) {
15
+        customer {
16
+          id
17
+          email
18
+          firstName
19
+          lastName
20
+        }
21
+        userErrors {
22
+          field
23
+          message
24
+        }
25
+      }
26
+    }
27
+  `;
28
+
29
+  const {customerCreate} = await client.request(query,{
30
+    variables: {
31
+      input,
32
+    }
33
+  });
34
+
35
+  if(!customerCreate) return 0
36
+  if(customerCreate?.userErrors?.length == 0) return 1
37
+
38
+};
39
+
40
+// Export services
41
+const customerService = {
42
+  createCustomer
43
+};
44
+
45
+export default customerService;

Notiek ielāde…
Atcelt
Saglabāt