feat: add conflict handling for duplicate configuration names in save and update processes

This commit is contained in:
Sam Chau
2025-01-10 03:06:51 +10:30
parent afad8f320c
commit cd05e07a0e
4 changed files with 107 additions and 33 deletions

View File

@@ -33,7 +33,18 @@ export const pingService = async (url, apiKey, type) => {
export const saveArrConfig = async config => {
try {
const response = await axios.post(`/api/arr/config`, config);
const response = await axios.post(`/api/arr/config`, config, {
validateStatus: status => {
return (status >= 200 && status < 300) || status === 409;
}
});
if (response.status === 409) {
return {
success: false,
error: 'Configuration with this name already exists'
};
}
return response.data;
} catch (error) {
console.error('Error saving arr config:', error);
@@ -41,24 +52,35 @@ export const saveArrConfig = async config => {
}
};
export const updateArrConfig = async (id, config) => {
try {
const response = await axios.put(`/api/arr/config/${id}`, config, {
validateStatus: status => {
return (status >= 200 && status < 300) || status === 409;
}
});
if (response.status === 409) {
return {
success: false,
error: 'Configuration with this name already exists'
};
}
return response.data;
} catch (error) {
console.error('Error updating arr config:', error);
throw error;
}
};
export const getArrConfigs = async () => {
try {
const response = await axios.get(`/api/arr/config`);
console.log('Raw axios response:', response);
console.log('Response data:', response.data);
return response.data; // This is correct - don't change this
} catch (error) {
console.error('Error fetching arr configs:', error);
throw error;
}
};
export const updateArrConfig = async (id, config) => {
try {
const response = await axios.put(`/api/arr/config/${id}`, config);
return response.data;
} catch (error) {
console.error('Error updating arr config:', error);
console.error('Error fetching arr configs:', error);
throw error;
}
};

View File

@@ -211,19 +211,24 @@ export const useArrModal = ({isOpen, onSubmit, editingArr}) => {
? await updateArrConfig(editingArr.id, configToSave)
: await saveArrConfig(configToSave);
if (result.success) {
Alert.success(
`Configuration ${
editingArr ? 'updated' : 'saved'
} successfully`
);
// Handle the name conflict error specifically
if (!result.success) {
Alert.error(result.error);
setSaveConfirm(false);
return;
}
// If it's not a manual sync method, show the sync confirmation
if (formData.sync_method !== 'manual') {
setShowSyncConfirm(true);
} else {
onSubmit();
}
Alert.success(
`Configuration ${
editingArr ? 'updated' : 'saved'
} successfully`
);
// If it's not a manual sync method, show the sync confirmation
if (formData.sync_method !== 'manual') {
setShowSyncConfirm(true);
} else {
onSubmit();
}
} catch (error) {
Alert.error('Failed to save configuration');