|
| 1 | +import reflex as rx |
| 2 | + |
| 3 | +from admin_panel.states.customer_state import CustomerState |
| 4 | + |
| 5 | + |
| 6 | +def customer_edit_form() -> rx.Component: |
| 7 | + return rx.el.dialog( |
| 8 | + rx.el.div( |
| 9 | + rx.el.h2( |
| 10 | + rx.cond( |
| 11 | + CustomerState.form_customer_id == 0, |
| 12 | + "Add New User", |
| 13 | + "Edit User", |
| 14 | + ), |
| 15 | + class_name="text-lg sm:text-xl font-semibold mb-4 sm:mb-6 text-gray-800", |
| 16 | + ), |
| 17 | + rx.el.form( |
| 18 | + rx.el.input( |
| 19 | + type="hidden", |
| 20 | + name="customer_id", |
| 21 | + default_value=CustomerState.form_customer_id.to_string(), |
| 22 | + key=rx.cond( |
| 23 | + CustomerState.form_customer_id == 0, |
| 24 | + "customer_id-new-form", |
| 25 | + CustomerState.form_customer_id.to_string() + "_cust_id_form", |
| 26 | + ), |
| 27 | + ), |
| 28 | + rx.el.div( |
| 29 | + rx.el.label( |
| 30 | + "First Name:", |
| 31 | + html_for="form_first_name_input", |
| 32 | + class_name="block text-xs sm:text-sm font-medium text-gray-700 mb-1", |
| 33 | + ), |
| 34 | + rx.el.input( |
| 35 | + id="form_first_name_input", |
| 36 | + name="first_name", |
| 37 | + class_name="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 text-xs sm:text-sm", |
| 38 | + placeholder="Enter first name", |
| 39 | + default_value=CustomerState.form_first_name, |
| 40 | + key=rx.cond( |
| 41 | + CustomerState.form_customer_id == 0, |
| 42 | + "form_first_name-new-form", |
| 43 | + CustomerState.form_customer_id.to_string() + "_fn_form", |
| 44 | + ), |
| 45 | + ), |
| 46 | + class_name="mb-3 sm:mb-4", |
| 47 | + ), |
| 48 | + rx.el.div( |
| 49 | + rx.el.label( |
| 50 | + "Last Name:", |
| 51 | + html_for="form_last_name_input", |
| 52 | + class_name="block text-xs sm:text-sm font-medium text-gray-700 mb-1", |
| 53 | + ), |
| 54 | + rx.el.input( |
| 55 | + id="form_last_name_input", |
| 56 | + name="last_name", |
| 57 | + class_name="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 text-xs sm:text-sm", |
| 58 | + placeholder="Enter last name", |
| 59 | + default_value=CustomerState.form_last_name, |
| 60 | + key=rx.cond( |
| 61 | + CustomerState.form_customer_id == 0, |
| 62 | + "form_last_name-new-form", |
| 63 | + CustomerState.form_customer_id.to_string() + "_ln_form", |
| 64 | + ), |
| 65 | + ), |
| 66 | + class_name="mb-3 sm:mb-4", |
| 67 | + ), |
| 68 | + rx.el.div( |
| 69 | + rx.el.label( |
| 70 | + "Email:", |
| 71 | + html_for="form_email_input", |
| 72 | + class_name="block text-xs sm:text-sm font-medium text-gray-700 mb-1", |
| 73 | + ), |
| 74 | + rx.el.input( |
| 75 | + id="form_email_input", |
| 76 | + name="email", |
| 77 | + type="email", |
| 78 | + class_name="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 text-xs sm:text-sm", |
| 79 | + placeholder="Enter email", |
| 80 | + default_value=CustomerState.form_email, |
| 81 | + key=rx.cond( |
| 82 | + CustomerState.form_customer_id == 0, |
| 83 | + "form_email-new-form", |
| 84 | + CustomerState.form_customer_id.to_string() + "_email_form", |
| 85 | + ), |
| 86 | + ), |
| 87 | + class_name="mb-3 sm:mb-4", |
| 88 | + ), |
| 89 | + rx.el.div( |
| 90 | + rx.el.label( |
| 91 | + "Tag:", |
| 92 | + html_for="form_tags_input", |
| 93 | + class_name="block text-xs sm:text-sm font-medium text-gray-700 mb-1", |
| 94 | + ), |
| 95 | + rx.el.input( |
| 96 | + id="form_tags_input", |
| 97 | + name="tags", |
| 98 | + class_name="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 text-xs sm:text-sm", |
| 99 | + placeholder="Enter a single tag", |
| 100 | + default_value=CustomerState.form_tags, |
| 101 | + key=rx.cond( |
| 102 | + CustomerState.form_customer_id == 0, |
| 103 | + "form_tags-new-form", |
| 104 | + CustomerState.form_customer_id.to_string() + "_tags_form", |
| 105 | + ), |
| 106 | + ), |
| 107 | + class_name="mb-3 sm:mb-4", |
| 108 | + ), |
| 109 | + rx.el.div( |
| 110 | + rx.el.label( |
| 111 | + "Role:", |
| 112 | + html_for="form_role_select", |
| 113 | + class_name="block text-xs sm:text-sm font-medium text-gray-700 mb-1", |
| 114 | + ), |
| 115 | + rx.el.select( |
| 116 | + rx.foreach( |
| 117 | + CustomerState.available_roles, |
| 118 | + lambda role: rx.el.option(role, value=role), |
| 119 | + ), |
| 120 | + id="form_role_select", |
| 121 | + name="role", |
| 122 | + value=CustomerState.form_role, |
| 123 | + on_change=CustomerState.set_form_role, |
| 124 | + class_name="mt-1 block w-full pl-3 pr-10 py-2 text-base bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 text-xs sm:text-sm", |
| 125 | + key=rx.cond( |
| 126 | + CustomerState.form_customer_id == 0, |
| 127 | + "form_role-new-form", |
| 128 | + CustomerState.form_customer_id.to_string() + "_role_form", |
| 129 | + ), |
| 130 | + ), |
| 131 | + class_name="mb-3 sm:mb-4", |
| 132 | + ), |
| 133 | + rx.el.div( |
| 134 | + rx.el.label( |
| 135 | + "Status:", |
| 136 | + html_for="form_status_select", |
| 137 | + class_name="block text-xs sm:text-sm font-medium text-gray-700 mb-1", |
| 138 | + ), |
| 139 | + rx.el.select( |
| 140 | + rx.foreach( |
| 141 | + CustomerState.customer_statuses, |
| 142 | + lambda status: rx.el.option(status, value=status), |
| 143 | + ), |
| 144 | + id="form_status_select", |
| 145 | + name="status", |
| 146 | + value=CustomerState.form_status, |
| 147 | + on_change=CustomerState.set_form_status, |
| 148 | + class_name="mt-1 block w-full pl-3 pr-10 py-2 text-base bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 text-xs sm:text-sm", |
| 149 | + key=rx.cond( |
| 150 | + CustomerState.form_customer_id == 0, |
| 151 | + "form_status-new-form", |
| 152 | + CustomerState.form_customer_id.to_string() + "_status_form", |
| 153 | + ), |
| 154 | + ), |
| 155 | + class_name="mb-4 sm:mb-6", |
| 156 | + ), |
| 157 | + rx.el.div( |
| 158 | + rx.el.button( |
| 159 | + "Cancel", |
| 160 | + type="button", |
| 161 | + on_click=CustomerState.toggle_edit_dialog, |
| 162 | + class_name="mr-2 sm:mr-3 px-3 py-2 sm:px-4 text-xs sm:text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-md shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500", |
| 163 | + ), |
| 164 | + rx.el.button( |
| 165 | + rx.cond( |
| 166 | + CustomerState.form_customer_id == 0, |
| 167 | + "Add User", |
| 168 | + "Save Changes", |
| 169 | + ), |
| 170 | + type="submit", |
| 171 | + class_name="px-3 py-2 sm:px-4 text-xs sm:text-sm font-medium text-white bg-blue-600 border border-transparent rounded-md shadow-sm hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500", |
| 172 | + ), |
| 173 | + class_name="flex justify-end pt-1 sm:pt-2", |
| 174 | + ), |
| 175 | + on_submit=CustomerState.handle_edit_customer, |
| 176 | + reset_on_submit=False, |
| 177 | + class_name="space-y-3 sm:space-y-4", |
| 178 | + ), |
| 179 | + class_name="bg-white p-4 sm:p-6 rounded-lg shadow-xl w-full max-w-md mx-auto", |
| 180 | + ), |
| 181 | + open=CustomerState.show_edit_dialog, |
| 182 | + data_state=rx.cond(CustomerState.show_edit_dialog, "open", "closed"), |
| 183 | + class_name="fixed inset-0 z-50 overflow-y-auto bg-black bg-opacity-50 data-[state=open]:flex items-center justify-center p-2 sm:p-4 h-screen w-screen", |
| 184 | + ) |
0 commit comments