|
| 1 | +import React, { useState, useCallback } from 'react'; |
| 2 | +import B4aModal from 'components/B4aModal/B4aModal.react'; |
| 3 | +import Field from 'components/Field/Field.react'; |
| 4 | +import Label from 'components/Label/Label.react'; |
| 5 | +import FileInput from 'components/FileInput/FileInput.react'; |
| 6 | +import Dropdown from 'components/Dropdown/Dropdown.react'; |
| 7 | +import Option from 'components/Dropdown/Option.react'; |
| 8 | +import RadioButton from 'components/RadioButton/RadioButton.react'; |
| 9 | +import FormNote from 'components/FormNote/FormNote.react'; |
| 10 | +import styles from './PushiOSSettings.scss'; |
| 11 | + |
| 12 | +function validate(fields) { |
| 13 | + const errors = {}; |
| 14 | + |
| 15 | + if (!fields.file) { |
| 16 | + errors.file = 'APNs certificate file is required.'; |
| 17 | + } else if (!fields.file.name.endsWith('.p12')) { |
| 18 | + errors.file = 'File must have a .p12 extension.'; |
| 19 | + } |
| 20 | + |
| 21 | + if (!fields.deviceType) { |
| 22 | + errors.deviceType = 'Device type is required.'; |
| 23 | + } |
| 24 | + |
| 25 | + return errors; |
| 26 | +} |
| 27 | + |
| 28 | +const P12CertificateModal = ({ deviceTypes, onSave, onClose }) => { |
| 29 | + const [file, setFile] = useState(null); |
| 30 | + const [deviceType, setDeviceType] = useState('ios'); |
| 31 | + const [production, setProduction] = useState(true); |
| 32 | + const [errors, setErrors] = useState({}); |
| 33 | + const [saving, setSaving] = useState(false); |
| 34 | + const [serverError, setServerError] = useState(null); |
| 35 | + |
| 36 | + const handleSubmit = useCallback(async () => { |
| 37 | + const fields = { file, deviceType }; |
| 38 | + const validationErrors = validate(fields); |
| 39 | + setErrors(validationErrors); |
| 40 | + |
| 41 | + if (Object.keys(validationErrors).length > 0) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + setSaving(true); |
| 46 | + setServerError(null); |
| 47 | + try { |
| 48 | + await onSave(file, deviceType, production); |
| 49 | + setSaving(false); |
| 50 | + onClose(); |
| 51 | + } catch (err) { |
| 52 | + const msg = typeof err === 'string' ? err |
| 53 | + : (err && err.error) || (err && err.message) || 'Failed to save certificate.'; |
| 54 | + setServerError(msg); |
| 55 | + setSaving(false); |
| 56 | + } |
| 57 | + }, [file, deviceType, production, onSave]); |
| 58 | + |
| 59 | + const firstError = Object.values(errors).find(Boolean); |
| 60 | + |
| 61 | + return ( |
| 62 | + <B4aModal |
| 63 | + type={B4aModal.Types.DEFAULT} |
| 64 | + title="APNs Certificate" |
| 65 | + width={700} |
| 66 | + confirmText={saving ? 'Saving\u2026' : 'Save'} |
| 67 | + cancelText="Cancel" |
| 68 | + onConfirm={handleSubmit} |
| 69 | + onCancel={onClose} |
| 70 | + disabled={saving} |
| 71 | + progress={saving} |
| 72 | + canCancel={!saving} |
| 73 | + > |
| 74 | + <Field |
| 75 | + label={<Label text="APNs Certificate File" description="Upload your .p12 certificate file" />} |
| 76 | + input={ |
| 77 | + <div className={styles.fileInputField}> |
| 78 | + <FileInput |
| 79 | + onChange={f => { setFile(f); setErrors(prev => ({ ...prev, file: undefined })); setServerError(null); }} |
| 80 | + accept=".p12,application/x-pkcs12" |
| 81 | + value={file ? { name: file.name } : undefined} |
| 82 | + /> |
| 83 | + </div> |
| 84 | + } |
| 85 | + /> |
| 86 | + <Field |
| 87 | + label={<Label text="Device Type" />} |
| 88 | + input={ |
| 89 | + <Dropdown |
| 90 | + value={deviceType} |
| 91 | + onChange={value => { setDeviceType(value); setErrors(prev => ({ ...prev, deviceType: undefined })); }} |
| 92 | + placeHolder="Select device type" |
| 93 | + dark={false} |
| 94 | + fixed={true} |
| 95 | + > |
| 96 | + {deviceTypes.map(type => ( |
| 97 | + <Option key={type} value={type}>{type}</Option> |
| 98 | + ))} |
| 99 | + </Dropdown> |
| 100 | + } |
| 101 | + /> |
| 102 | + <Field |
| 103 | + label={<Label text="Certificate type" description="Development or production" />} |
| 104 | + input={ |
| 105 | + <div className={styles.radiobuttonWrapper}> |
| 106 | + <label htmlFor="p12_development" className={styles.radioOption}> |
| 107 | + <RadioButton |
| 108 | + dark={true} |
| 109 | + id="p12_development" |
| 110 | + name="p12Production" |
| 111 | + checked={!production} |
| 112 | + onChange={() => setProduction(false)} |
| 113 | + />Development |
| 114 | + </label> |
| 115 | + <label htmlFor="p12_production" className={styles.radioOption}> |
| 116 | + <RadioButton |
| 117 | + dark={true} |
| 118 | + id="p12_production" |
| 119 | + name="p12Production" |
| 120 | + checked={production} |
| 121 | + onChange={() => setProduction(true)} |
| 122 | + />Production |
| 123 | + </label> |
| 124 | + </div> |
| 125 | + } |
| 126 | + /> |
| 127 | + <FormNote show={!!firstError} color="red"> |
| 128 | + {firstError} |
| 129 | + </FormNote> |
| 130 | + <FormNote show={!!serverError && !firstError} color="red"> |
| 131 | + {serverError} |
| 132 | + </FormNote> |
| 133 | + </B4aModal> |
| 134 | + ); |
| 135 | +}; |
| 136 | + |
| 137 | +export default P12CertificateModal; |
0 commit comments