refactor: clean up comments and streamline payload handling in user detail form

This commit is contained in:
2025-12-23 20:10:33 +07:00
parent 016c77a586
commit 797f6c2383
2 changed files with 6 additions and 21 deletions

View File

@@ -267,9 +267,6 @@ const ListContact = memo(function ListContact(props) {
} }
} }
// Backend doesn't support is_active filter or order parameter
// Contact hanya supports: criteria, name, code, limit, page
const queryParams = new URLSearchParams(); const queryParams = new URLSearchParams();
Object.entries(searchParams).forEach(([key, value]) => { Object.entries(searchParams).forEach(([key, value]) => {
if (value !== '' && value !== null && value !== undefined) { if (value !== '' && value !== null && value !== undefined) {
@@ -309,11 +306,10 @@ const ListContact = memo(function ListContact(props) {
// Listen for saved contact data // Listen for saved contact data
useEffect(() => { useEffect(() => {
if (props.lastSavedContact) { if (props.lastSavedContact) {
fetchContacts(); // Refetch all contacts when data is saved fetchContacts();
} }
}, [props.lastSavedContact]); }, [props.lastSavedContact]);
// Get contacts (already filtered by backend)
const getFilteredContacts = () => { const getFilteredContacts = () => {
return filteredContacts; return filteredContacts;
}; };
@@ -326,7 +322,7 @@ const ListContact = memo(function ListContact(props) {
const showAddModal = () => { const showAddModal = () => {
props.setSelectedData(null); props.setSelectedData(null);
props.setActionMode('add'); props.setActionMode('add');
// Pass the current active tab to determine contact type
props.setContactType?.(activeTab); props.setContactType?.(activeTab);
}; };

View File

@@ -220,32 +220,24 @@ const DetailUser = (props) => {
// For update mode: only send email if it has changed // For update mode: only send email if it has changed
if (FormData.user_id) { if (FormData.user_id) {
// Only include email if it has changed from original
if (FormData.user_email !== originalEmail) { if (FormData.user_email !== originalEmail) {
payload.user_email = FormData.user_email; payload.user_email = FormData.user_email;
} }
// Add is_active for update mode
payload.is_active = FormData.is_active; payload.is_active = FormData.is_active;
} else { } else {
// For create mode: always send email
payload.user_email = FormData.user_email; payload.user_email = FormData.user_email;
} }
// Only add role_id if it exists (backend requires number >= 1, no null)
if (FormData.role_id) { if (FormData.role_id) {
payload.role_id = FormData.role_id; payload.role_id = FormData.role_id;
} }
// Add password and name for new user (create mode) // Add password and name for new user (create mode)
if (!FormData.user_id) { if (!FormData.user_id) {
payload.user_name = FormData.user_name; // Username only for create payload.user_name = FormData.user_name;
payload.user_password = FormData.password; // Backend expects 'user_password' payload.user_password = FormData.password;
// Don't send confirmPassword, is_sa for create
} }
// For update mode:
// - Don't send 'user_name' (username is immutable)
// - is_active is now sent for update mode
// - Only send email if it has changed
try { try {
console.log('Payload being sent:', payload); console.log('Payload being sent:', payload);
@@ -261,7 +253,6 @@ const DetailUser = (props) => {
// Check if response is successful // Check if response is successful
if (response && (response.statusCode === 200 || response.statusCode === 201)) { if (response && (response.statusCode === 200 || response.statusCode === 201)) {
// If in edit mode and newPassword is provided, change password
if (FormData.user_id && FormData.newPassword) { if (FormData.user_id && FormData.newPassword) {
try { try {
const passwordResponse = await changePassword( const passwordResponse = await changePassword(
@@ -1146,9 +1137,7 @@ const DetailUser = (props) => {
))} ))}
</Select> </Select>
{errors.role_id && ( {errors.role_id && (
<Text style={{ color: 'red', fontSize: '12px' }}> <Text style={{ color: 'red', fontSize: '12px' }}>{errors.role_id}</Text>
{errors.role_id}
</Text>
)} )}
</div> </div>
</div> </div>