PluginProbe
Extendify / 0.3.0
Extendify v0.3.0
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / middleware / NeedsRegistrationModal.js

NeedsRegistrationModal.js in Extendify 0.3.0, at src/middleware/NeedsRegistrationModal.js

141 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __, sprintf } from '@wordpress/i18n'
2 import { Button } from '@wordpress/components'
3 import { useUserStore } from '../state/User'
4 import { useState, useRef } from '@wordpress/element'
5 import { User as UserApi } from '../api/User'
6 import { useGlobalStore } from '../state/GlobalState'
7 import { Modal } from '../components/modals/Modal'
8 import { brandMark } from '../components/icons'
9 import { Icon } from '@wordpress/icons'
10 import Primary from '../components/buttons/Primary'
11 import { safeHTML } from '@wordpress/dom'
12
13 export default function NeedsRegistrationModal({ finished, resetMiddleware }) {
14 const [email, setEmail] = useState('')
15 const remainingImports = useUserStore((state) => state.remainingImports)
16 const emailRef = useRef(null)
17 const removeAllModals = useGlobalStore((state) => state.removeAllModals)
18 const registerAndContinue = async (event) => {
19 event.preventDefault()
20 useUserStore.setState({
21 registration: { email },
22 freebieImports: Number(useUserStore.getState().freebieImports) + 10,
23 })
24 await UserApi.registerMailingList(email)
25 finished()
26 }
27
28 const optOut = () => {
29 useUserStore.setState({
30 registration: { optedOut: true },
31 })
32 finished()
33 }
34
35 return (
36 <Modal
37 isOpen={true}
38 onClose={() => {
39 removeAllModals()
40 resetMiddleware()
41 }}
42 ref={emailRef}>
43 <div className="p-10 space-y-4 text-extendify-black">
44 <Icon icon={brandMark} size={42} className="-ml-2 -mt-2" />
45 <h3 className="text-xl md:leading-3">
46 {remainingImports() == 1
47 ? __('This is your last import', 'extendify')
48 : sprintf(
49 __('You now have %s imports left', 'extendify'),
50 remainingImports(),
51 )}
52 </h3>
53 <p
54 className="max-w-md text-sm"
55 dangerouslySetInnerHTML={{
56 __html: safeHTML(
57 sprintf(
58 // Translators: 1. and 2. are <strong> tags
59 __(
60 "Subscribe and %1$swe'll send you 10 more%2$s. Plus you'll get updates and special offers from us fine folks at Extendify.",
61 'extendify',
62 ),
63 '<strong>',
64 '</strong>',
65 ),
66 ),
67 }}></p>
68 <form
69 onSubmit={registerAndContinue}
70 className="flex space-x-2 py-2 items-stretch">
71 <div className="relative w-full max-w-xs">
72 <label
73 htmlFor="extendify-email-register"
74 className="sr-only">
75 {__('Email', 'extendify')}
76 </label>
77 <input
78 ref={emailRef}
79 id="extendify-email-register"
80 name="extendify-email-register"
81 required
82 onChange={(event) => setEmail(event.target.value)}
83 type="email"
84 className="text-sm min-h-0 p-2 border-2 border-gray-900 rounded-md w-full"
85 placeholder={__(
86 'Enter your email address',
87 'extendify',
88 )}
89 />
90 </div>
91 <Primary type="submit" className="px-4 rounded-md my-0">
92 {__('Submit', 'extendify')}
93 </Primary>
94 </form>
95 <Button
96 isLink
97 className="text-extendify-gray text-sm my-0"
98 onClick={optOut}>
99 {__('No thanks — finish importing', 'extendify')}
100 </Button>
101 </div>
102 </Modal>
103 )
104 }
105
106 const pass = () => {
107 const userState = useUserStore.getState()
108 const remainingImports = userState?.remainingImports()
109
110 // On the last import, show the modal if they opted out
111 if (remainingImports === 1 && userState?.registration?.optedOut) {
112 return false
113 }
114 return (
115 userState?.registration?.email?.length || // Already registered
116 userState?.apiKey?.length || // Already a member
117 userState?.registration?.optedOut || // Already opted out
118 userState?.imports === 0 // Hasn't imported yet
119 )
120 }
121
122 export function check() {
123 const pushModal = useGlobalStore.getState().pushModal
124
125 return {
126 id: 'NeedsRegistrationModal',
127 pass: pass(),
128 allow() {},
129 deny() {
130 return new Promise((resolve, reject) => {
131 pushModal(
132 <NeedsRegistrationModal
133 finished={resolve}
134 resetMiddleware={reject}
135 />,
136 )
137 })
138 },
139 }
140 }
141