PluginProbe
Extendify / 0.2.0
Extendify v0.2.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.2.0, at src/middleware/NeedsRegistrationModal.js

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