Refactor API texts handling

This commit is contained in:
Florian Dupret 2021-04-28 19:42:57 +02:00
commit f5f3b5d9b3
30 changed files with 366 additions and 290 deletions

View file

@ -1,56 +1,55 @@
const baseURL = "/api/"; const baseURL = "/api/";
import axios from "axios"; import axios from "axios";
import { store } from "../store"; import { store } from "../store";
import utils from "@/utils";
axios.defaults.headers.common[ axios.defaults.headers.common[
"Authorization" "Authorization"
] = `Bearer ${store.getters.getToken}`; ] = `Bearer ${store.getters.getToken}`;
function handleError(error, getText) {
utils.notify.error(getText(error.response));
return false;
}
function handleResponse(response, getText) {
if(response && getText) {
const successText = getText(response);
utils.notify.success(successText);
}
return response;
}
function defaultErrorText(response) {
return response.statusText;
}
function defaultSuccessText(response) {
return response.statusText;
}
const apiReq = { const apiReq = {
post: async function(url, data) { post: async function(url, data, getErrorText = defaultErrorText, getSuccessText) {
let response = await axios.post(url, data).catch(function(error) { const response = await axios.post(url, data).catch(function(error) { handleError(error, getErrorText) });
if (error.response) { return handleResponse(response, getSuccessText);
return error.response; },
}
}); put: async function(url, data, getErrorText = defaultErrorText, getSuccessText) {
return response; const response = await axios.put(url, data).catch(function(error) { handleError(error, getErrorText) });
return handleResponse(response, getSuccessText);
},
patch: async function(url, data, getErrorText = defaultErrorText, getSuccessText) {
const response = await axios.patch(url, data).catch(function(error) { handleError(error, getErrorText) });
return handleResponse(response, getSuccessText);
}, },
put: async function(url, data) { get: function(url, data, getErrorText = defaultErrorText) {
let response = await axios.put(url, data).catch(function(error) { return axios.get(url, data).catch(function(error) { handleError(error, getErrorText) });
if (error.response) {
return error.response;
} else return;
});
return response;
},
patch: async function(url, data) {
let response = await axios.patch(url, data).catch(function(error) {
if (error.response) {
processResponse(error.response);
return response;
} else return;
});
processResponse(response);
return response;
}, },
get: async function(url, data) { delete: async function(url, data, getErrorText = defaultErrorText, getSuccessText = defaultSuccessText ) {
let response = await axios.get(url, data).catch(function(error) { const response = await axios.delete(url, data).catch( function(error) { handleError(error, getErrorText) } );
if (error.response) { return handleResponse(response, getSuccessText);
return error.response;
} else return;
});
return response;
},
delete: async function(url, data) {
let response = await axios.delete(url, data).catch(function(error) {
if (error.response) {
return error.response;
}
});
return response;
}, },
async download(url) { async download(url) {

View file

@ -1,6 +1,7 @@
import { baseURL } from "./api-utils"; import { baseURL } from "./api-utils";
import { apiReq } from "./api-utils"; import { apiReq } from "./api-utils";
import { store } from "@/store"; import { store } from "@/store";
import i18n from '@/i18n.js';
const backupBase = baseURL + "backups/"; const backupBase = baseURL + "backups/";
@ -40,7 +41,12 @@ export const backupAPI = {
* @param {string} fileName * @param {string} fileName
*/ */
async delete(fileName) { async delete(fileName) {
return await apiReq.delete(backupURLs.deleteBackup(fileName)); return apiReq.delete(
backupURLs.deleteBackup(fileName),
null,
function() { return i18n.t('settings.backup.unable-to-delete-backup'); },
function() { return i18n.t('settings.backup.backup-deleted'); }
);
}, },
/** /**
* Creates a backup on the serve given a set of options * Creates a backup on the serve given a set of options
@ -48,8 +54,12 @@ export const backupAPI = {
* @returns * @returns
*/ */
async create(options) { async create(options) {
let response = apiReq.post(backupURLs.createBackup, options); return apiReq.post(
return response; backupURLs.createBackup,
options,
function() { return i18n.t('settings.backup.error-creating-backup-see-log-file'); },
function(response) { return i18n.t('settings.backup.backup-created-at-response-export_path', {path: response.data.export_path}); }
);
}, },
/** /**
* Downloads a file from the server. I don't actually think this is used? * Downloads a file from the server. I don't actually think this is used?

View file

@ -1,6 +1,7 @@
import { baseURL } from "./api-utils"; import { baseURL } from "./api-utils";
import { apiReq } from "./api-utils"; import { apiReq } from "./api-utils";
import { store } from "@/store"; import { store } from "@/store";
import i18n from '@/i18n.js';
const prefix = baseURL + "categories"; const prefix = baseURL + "categories";
@ -22,26 +23,41 @@ export const categoryAPI = {
return response.data; return response.data;
}, },
async create(name) { async create(name) {
let response = await apiReq.post(categoryURLs.getAll, { name: name }); const response = await apiReq.post(
store.dispatch("requestCategories"); categoryURLs.getAll,
return response.data; { name: name },
function() { return i18n.t('settings.category-creation-failed'); },
function() { return i18n.t('settings.category-created'); }
);
if(response) {
store.dispatch("requestCategories");
return response.data;
}
}, },
async getRecipesInCategory(category) { async getRecipesInCategory(category) {
let response = await apiReq.get(categoryURLs.getCategory(category)); let response = await apiReq.get(categoryURLs.getCategory(category));
return response.data; return response.data;
}, },
async update(name, newName, overrideRequest = false) { async update(name, newName, overrideRequest = false) {
let response = await apiReq.put(categoryURLs.updateCategory(name), { const response = await apiReq.put(
name: newName, categoryURLs.updateCategory(name),
}); { name: newName },
if (!overrideRequest) { function() { return i18n.t('settings.category-update-failed'); },
function() { return i18n.t('settings.category-updated'); }
);
if (response && !overrideRequest) {
store.dispatch("requestCategories"); store.dispatch("requestCategories");
return response.data;
} }
return response.data;
}, },
async delete(category, overrideRequest = false) { async delete(category, overrideRequest = false) {
let response = await apiReq.delete(categoryURLs.deleteCategory(category)); const response = await apiReq.delete(
if (!overrideRequest) { categoryURLs.deleteCategory(category),
null,
function() { return i18n.t('settings.category-deletion-failed'); },
function() { return i18n.t('settings.category-deleted'); }
);
if (response && !overrideRequest) {
store.dispatch("requestCategories"); store.dispatch("requestCategories");
} }
return response; return response;

View file

@ -1,5 +1,6 @@
import { baseURL } from "./api-utils"; import { baseURL } from "./api-utils";
import { apiReq } from "./api-utils"; import { apiReq } from "./api-utils";
import i18n from '@/i18n.js';
const groupPrefix = baseURL + "groups"; const groupPrefix = baseURL + "groups";
const groupsURLs = { const groupsURLs = {
@ -10,25 +11,54 @@ const groupsURLs = {
update: id => `${groupPrefix}/${id}`, update: id => `${groupPrefix}/${id}`,
}; };
function deleteErrorText(response) {
console.log(response.data);
switch(response.data.detail) {
case 'GROUP_WITH_USERS':
return i18n.t('user.cannot-delete-group-with-users');
case 'GROUP_NOT_FOUND':
return i18n.t('user.group-not-found');
case 'DEFAULT_GROUP':
return i18n.t('user.cannot-delete-default-group');
default:
return i18n.t('user.group-deletion-failed');
}
}
export const groupAPI = { export const groupAPI = {
async allGroups() { async allGroups() {
let response = await apiReq.get(groupsURLs.groups); let response = await apiReq.get(groupsURLs.groups);
return response.data; return response.data;
}, },
async create(name) { create(name) {
let response = await apiReq.post(groupsURLs.create, { name: name }); return apiReq.post(
return response; groupsURLs.create,
{ name: name },
function() { return i18n.t('user.user-group-creation-failed'); },
function() { return i18n.t('user.user-group-created'); }
);
}, },
async delete(id) { delete(id) {
let response = await apiReq.delete(groupsURLs.delete(id)); return apiReq.delete(
return response; groupsURLs.delete(id),
null,
deleteErrorText,
function() { return i18n.t('user.group-deleted'); }
);
}, },
async current() { async current() {
let response = await apiReq.get(groupsURLs.current); let response = await apiReq.get(groupsURLs.current);
return response.data; return response.data;
}, },
async update(data) { update(data) {
let response = await apiReq.put(groupsURLs.update(data.id), data); return apiReq.put(
return response; groupsURLs.update(data.id),
data,
function() { return i18n.t('user.error-updating-group'); },
function() { return i18n.t('settings.group-settings-updated'); }
);
}, },
}; };

View file

@ -1,5 +1,6 @@
import { baseURL } from "./api-utils"; import { baseURL } from "./api-utils";
import { apiReq } from "./api-utils"; import { apiReq } from "./api-utils";
import i18n from '@/i18n.js';
const prefix = baseURL + "meal-plans/"; const prefix = baseURL + "meal-plans/";
@ -15,9 +16,13 @@ const mealPlanURLs = {
}; };
export const mealplanAPI = { export const mealplanAPI = {
async create(postBody) { create(postBody) {
let response = await apiReq.post(mealPlanURLs.create, postBody); return apiReq.post(
return response; mealPlanURLs.create,
postBody,
function() { return i18n.t('meal-plan.mealplan-creation-failed')},
function() { return i18n.t('meal-plan.mealplan-created'); }
);
}, },
async all() { async all() {
@ -35,14 +40,21 @@ export const mealplanAPI = {
return response; return response;
}, },
async delete(id) { delete(id) {
let response = await apiReq.delete(mealPlanURLs.delete(id)); return apiReq.delete(mealPlanURLs.delete(id),
return response; null,
function() { return i18n.t('meal-plan.mealplan-deletion-failed'); },
function() { return i18n.t('meal-plan.mealplan-deleted'); }
);
}, },
async update(id, body) { update(id, body) {
let response = await apiReq.put(mealPlanURLs.update(id), body); return apiReq.put(
return response; mealPlanURLs.update(id),
body,
function() { return i18n.t('meal-plan.mealplan-update-failed'); },
function() { return i18n.t('meal-plan.mealplan-updated'); }
);
}, },
async shoppingList(id) { async shoppingList(id) {

View file

@ -1,6 +1,7 @@
import { baseURL } from "./api-utils"; import { baseURL } from "./api-utils";
import { apiReq } from "./api-utils"; import { apiReq } from "./api-utils";
import { store } from "../store"; import { store } from "../store";
import i18n from '@/i18n.js';
const migrationBase = baseURL + "migrations"; const migrationBase = baseURL + "migrations";
@ -17,7 +18,12 @@ export const migrationAPI = {
return response.data; return response.data;
}, },
async delete(folder, file) { async delete(folder, file) {
let response = await apiReq.delete(migrationURLs.delete(folder, file)); const response = await apiReq.delete(
migrationURLs.delete(folder, file),
null,
function() { return i18n.t('general.file-folder-not-found'); },
function() { return i18n.t('migration.migration-data-removed'); }
);
return response; return response;
}, },
async import(folder, file) { async import(folder, file) {

View file

@ -1,6 +1,7 @@
import { baseURL } from "./api-utils"; import { baseURL } from "./api-utils";
import { apiReq } from "./api-utils"; import { apiReq } from "./api-utils";
import { store } from "../store"; import { store } from "../store";
import i18n from '@/i18n.js';
const prefix = baseURL + "recipes/"; const prefix = baseURL + "recipes/";
@ -24,9 +25,12 @@ export const recipeAPI = {
* @returns {string} Recipe Slug * @returns {string} Recipe Slug
*/ */
async createByURL(recipeURL) { async createByURL(recipeURL) {
let response = await apiReq.post(recipeURLs.createByURL, { const response = await apiReq.post(
url: recipeURL, recipeURLs.createByURL,
}); { url: recipeURL },
function() { return i18n.t('recipe.recipe-creation-failed'); },
function() { return i18n.t('recipe.recipe-created'); }
);
store.dispatch("requestRecentRecipes"); store.dispatch("requestRecentRecipes");
return response; return response;
@ -41,7 +45,12 @@ export const recipeAPI = {
}, },
async create(recipeData) { async create(recipeData) {
let response = await apiReq.post(recipeURLs.create, recipeData); const response = await apiReq.post(
recipeURLs.create,
recipeData,
function() { return i18n.t('recipe.recipe-creation-failed'); },
function() { return i18n.t('recipe.recipe-created'); }
);
store.dispatch("requestRecentRecipes"); store.dispatch("requestRecentRecipes");
return response.data; return response.data;
}, },
@ -51,17 +60,31 @@ export const recipeAPI = {
return response.data; return response.data;
}, },
async updateImage(recipeSlug, fileObject) { updateImage(recipeSlug, fileObject, overrideSuccessMsg = false) {
const fd = new FormData(); const formData = new FormData();
fd.append("image", fileObject); formData.append("image", fileObject);
fd.append("extension", fileObject.name.split(".").pop()); formData.append("extension", fileObject.name.split(".").pop());
let response = apiReq.put(recipeURLs.updateImage(recipeSlug), fd);
return response; let successMessage = null;
if(!overrideSuccessMsg) {
successMessage = function() { return overrideSuccessMsg ? null : i18n.t('recipe.recipe-image-updated'); };
}
return apiReq.put(
recipeURLs.updateImage(recipeSlug),
formData,
function() { return i18n.t('general.image-upload-failed'); },
successMessage
);
}, },
async updateImagebyURL(slug, url) { updateImagebyURL(slug, url) {
const response = apiReq.post(recipeURLs.updateImage(slug), { url: url }); return apiReq.post(
return response; recipeURLs.updateImage(slug),
{ url: url },
function() { return i18n.t('general.image-upload-failed'); },
function() { return i18n.t('recipe.recipe-image-updated'); }
);
}, },
async update(data) { async update(data) {
@ -76,8 +99,13 @@ export const recipeAPI = {
return response.data; return response.data;
}, },
async delete(recipeSlug) { delete(recipeSlug) {
return await apiReq.delete(recipeURLs.delete(recipeSlug)); return apiReq.delete(
recipeURLs.delete(recipeSlug),
null,
function() { return i18n.t('recipe.unable-to-delete-recipe'); },
function() { return i18n.t('recipe.recipe-deleted'); }
);
}, },
async allSummary(start = 0, limit = 9999) { async allSummary(start = 0, limit = 9999) {

View file

@ -1,5 +1,6 @@
import { baseURL } from "./api-utils"; import { baseURL } from "./api-utils";
import { apiReq } from "./api-utils"; import { apiReq } from "./api-utils";
import i18n from '@/i18n.js';
const signUpPrefix = baseURL + "users/sign-ups"; const signUpPrefix = baseURL + "users/sign-ups";
@ -20,9 +21,16 @@ export const signupAPI = {
return response.data; return response.data;
}, },
async deleteToken(token) { async deleteToken(token) {
return await apiReq.delete(signUpURLs.deleteToken(token)); return await apiReq.delete(signUpURLs.deleteToken(token),
null,
null,
function() { return i18n.t('user.sign-up-token-deleted'); }
);
}, },
async createUser(token, data) { async createUser(token, data) {
return apiReq.post(signUpURLs.createUser(token), data); return apiReq.post(signUpURLs.createUser(token), data,
function() { return i18n.t('user.you-are-not-allowed-to-create-a-user'); },
function() { return i18n.t('user.user-created'); }
);
}, },
}; };

View file

@ -1,6 +1,7 @@
import { baseURL } from "./api-utils"; import { baseURL } from "./api-utils";
import { apiReq } from "./api-utils"; import { apiReq } from "./api-utils";
import { store } from "@/store"; import { store } from "@/store";
import i18n from '@/i18n.js';
const settingsBase = baseURL + "site-settings"; const settingsBase = baseURL + "site-settings";
@ -19,8 +20,15 @@ export const siteSettingsAPI = {
}, },
async update(body) { async update(body) {
let response = await apiReq.put(settingsURLs.updateSiteSettings, body); const response = await apiReq.put(
store.dispatch("requestSiteSettings"); settingsURLs.updateSiteSettings,
body,
function() { return i18n.t('settings.settings-update-failed'); },
function() { return i18n.t('settings.settings-updated'); }
);
if(response) {
store.dispatch("requestSiteSettings");
}
return response; return response;
}, },
@ -34,20 +42,39 @@ export const siteSettingsAPI = {
return response.data; return response.data;
}, },
async createPage(body) { createPage(body) {
return await apiReq.post(settingsURLs.customPages, body); return apiReq.post(
settingsURLs.customPages,
body,
function() { return i18n.t('page.page-creation-failed'); },
function() { return i18n.t('page.new-page-created'); }
);
}, },
async deletePage(id) { async deletePage(id) {
return await apiReq.delete(settingsURLs.customPage(id)); return await apiReq.delete(
settingsURLs.customPage(id),
null,
function() { return i18n.t('page.page-deletion-failed'); },
function() { return i18n.t('page.page-deleted'); });
}, },
async updatePage(body) { updatePage(body) {
return await apiReq.put(settingsURLs.customPage(body.id), body); return apiReq.put(
settingsURLs.customPage(body.id),
body,
function() { return i18n.t('page.page-update-failed'); },
function() { return i18n.t('page.page-updated'); }
);
}, },
async updateAllPages(allPages) { async updateAllPages(allPages) {
let response = await apiReq.put(settingsURLs.customPages, allPages); let response = await apiReq.put(
settingsURLs.customPages,
allPages,
function() { return i18n.t('page.pages-update-failed'); },
function() { return i18n.t('page.pages-updated'); }
);
return response; return response;
}, },
}; };

View file

@ -1,5 +1,6 @@
import { baseURL } from "./api-utils"; import { baseURL } from "./api-utils";
import { apiReq } from "./api-utils"; import { apiReq } from "./api-utils";
import i18n from '@/i18n.js';
const prefix = baseURL + "themes"; const prefix = baseURL + "themes";
@ -23,18 +24,31 @@ export const themeAPI = {
}, },
async create(postBody) { async create(postBody) {
return await apiReq.post(settingsURLs.createTheme, postBody); return await apiReq.post(
settingsURLs.createTheme,
postBody,
function() { return i18n.t('settings.theme.error-creating-theme-see-log-file'); },
function() { return i18n.t('settings.theme.theme-saved'); });
}, },
async update(themeName, colors) { update(themeName, colors) {
const body = { const body = {
name: themeName, name: themeName,
colors: colors, colors: colors,
}; };
return await apiReq.put(settingsURLs.updateTheme(themeName), body); return apiReq.put(
settingsURLs.updateTheme(themeName),
body,
function() { return i18n.t('settings.theme.error-updating-theme'); },
function() { return i18n.t('settings.theme.theme-updated'); });
}, },
async delete(themeName) { delete(themeName) {
return await apiReq.delete(settingsURLs.deleteTheme(themeName)); return apiReq.delete(
settingsURLs.deleteTheme(themeName),
null,
function() { return i18n.t('settings.theme.error-deleting-theme'); },
function() { return i18n.t('settings.theme.theme-deleted'); }
);
}, },
}; };

View file

@ -1,15 +1,16 @@
import { apiReq } from "./api-utils"; import { apiReq } from "./api-utils";
import i18n from '@/i18n.js';
export const utilsAPI = { export const utilsAPI = {
// import { api } from "@/api"; // import { api } from "@/api";
async uploadFile(url, fileObject) { uploadFile(url, fileObject) {
console.log("API Called"); console.log("API Called");
let response = await apiReq.post(url, fileObject, { return apiReq.post(
headers: { url,
"Content-Type": "multipart/form-data", fileObject,
}, function() { return i18n.t('general.failure-uploading-file'); },
}); function() { return i18n.t('general.file-uploaded'); }
return response; );
}, },
}; };

View file

@ -1,6 +1,7 @@
import { baseURL } from "./api-utils"; import { baseURL } from "./api-utils";
import { apiReq } from "./api-utils"; import { apiReq } from "./api-utils";
import axios from "axios"; import axios from "axios";
import i18n from '@/i18n.js';
const authPrefix = baseURL + "auth"; const authPrefix = baseURL + "auth";
const userPrefix = baseURL + "users"; const userPrefix = baseURL + "users";
@ -17,6 +18,16 @@ const usersURLs = {
resetPassword: id => `${userPrefix}/${id}/reset-password`, resetPassword: id => `${userPrefix}/${id}/reset-password`,
}; };
function deleteErrorText(response) {
console.log(response.data);
switch(response.data.detail) {
case 'SUPER_USER':
return i18n.t('user.error-cannot-delete-super-user');
default:
return i18n.t('user.you-are-not-allowed-to-delete-this-user');
}
}
export const userAPI = { export const userAPI = {
async login(formData) { async login(formData) {
let response = await apiReq.post(authURLs.token, formData, { let response = await apiReq.post(authURLs.token, formData, {
@ -36,8 +47,13 @@ export const userAPI = {
let response = await apiReq.get(usersURLs.users); let response = await apiReq.get(usersURLs.users);
return response.data; return response.data;
}, },
async create(user) { create(user) {
return await apiReq.post(usersURLs.users, user); return apiReq.post(
usersURLs.users,
user,
function() { return i18n.t('user.user-creation-failed'); },
function() { return i18n.t('user.user-created'); }
);
}, },
async self() { async self() {
let response = await apiReq.get(usersURLs.self); let response = await apiReq.get(usersURLs.self);
@ -47,17 +63,37 @@ export const userAPI = {
let response = await apiReq.get(usersURLs.userID(id)); let response = await apiReq.get(usersURLs.userID(id));
return response.data; return response.data;
}, },
async update(user) { update(user) {
return await apiReq.put(usersURLs.userID(user.id), user); return apiReq.put(
usersURLs.userID(user.id),
user,
function() { return i18n.t('user.user-update-failed'); },
function() { return i18n.t('user.user-updated'); }
);
}, },
async changePassword(id, password) { changePassword(id, password) {
let response = await apiReq.put(usersURLs.password(id), password); return apiReq.put(
return response.data; usersURLs.password(id),
password,
function() { return i18n.t('user.existing-password-does-not-match'); },
function() { return i18n.t('user.password-updated'); }
);
}, },
async delete(id) {
return await apiReq.delete(usersURLs.userID(id)); delete(id) {
return apiReq.delete(
usersURLs.userID(id),
null,
deleteErrorText,
function() { return i18n.t('user.user-deleted'); }
);
}, },
async resetPassword(id) { resetPassword(id) {
return await apiReq.put(usersURLs.resetPassword(id)); return apiReq.put(
usersURLs.resetPassword(id),
null,
function() { return i18n.t('user.password-reset-failed'); },
function() { return i18n.t('user.password-has-been-reset-to-the-default-password'); }
);
}, },
}; };

View file

@ -85,7 +85,6 @@
<script> <script>
import { api } from "@/api"; import { api } from "@/api";
import { validators } from "@/mixins/validators"; import { validators } from "@/mixins/validators";
import utils from "@/utils";
export default { export default {
mixins: [validators], mixins: [validators],
data() { data() {
@ -134,11 +133,7 @@ export default {
}; };
if (this.$refs.signUpForm.validate()) { if (this.$refs.signUpForm.validate()) {
let response = await api.signUps.createUser(this.token, userData); if (await api.signUps.createUser(this.token, userData)) {
if (response.status != 200) {
utils.notify.error(this.$t('user.you-are-not-allowed-to-create-a-user'));
} else {
utils.notify.success(this.$t('user.user-created'));
this.$emit("user-created"); this.$emit("user-created");
this.$router.push("/"); this.$router.push("/");
} }

View file

@ -36,11 +36,7 @@ export default {
return utils.getDateAsPythonDate(dateObject); return utils.getDateAsPythonDate(dateObject);
}, },
async update() { async update() {
const response = await api.mealPlans.update(this.mealPlan.uid, this.mealPlan); if (await api.mealPlans.update(this.mealPlan.uid, this.mealPlan)) {
if (response.status != 200) {
utils.notify.error(this.$t('meal-plan.mealplan-update-failed'));
} else {
utils.notify.success(this.$t('meal-plan.mealplan-updated'));
this.$emit("updated"); this.$emit("updated");
} }
}, },

View file

@ -197,16 +197,12 @@ export default {
endDate: this.endDate, endDate: this.endDate,
meals: this.meals, meals: this.meals,
}; };
const response = await api.mealPlans.create(mealBody); if (await api.mealPlans.create(mealBody)) {
if (response.status != 201) { this.$emit(CREATE_EVENT);
utils.notify.error(this.$t('meal-plan.mealplan-creation-failed')); this.meals = [];
} else { this.startDate = null;
utils.notify.success(this.$t('meal-plan.mealplan-created')); this.endDate = null;
} }
this.$emit(CREATE_EVENT);
this.meals = [];
this.startDate = null;
this.endDate = null;
}, },
getImage(image) { getImage(image) {

View file

@ -46,7 +46,6 @@ const REFRESH_EVENT = "refresh";
const UPLOAD_EVENT = "upload"; const UPLOAD_EVENT = "upload";
import TheUploadBtn from "@/components/UI/Buttons/TheUploadBtn"; import TheUploadBtn from "@/components/UI/Buttons/TheUploadBtn";
import { api } from "@/api"; import { api } from "@/api";
import utils from "@/utils";
export default { export default {
components: { components: {
TheUploadBtn, TheUploadBtn,
@ -64,13 +63,9 @@ export default {
}, },
async getImageFromURL() { async getImageFromURL() {
this.loading = true; this.loading = true;
const response = await api.recipes.updateImagebyURL(this.slug, this.url); if (await api.recipes.updateImagebyURL(this.slug, this.url)) {
if (response.status != 200) { this.$emit(REFRESH_EVENT);
utils.notify.error(this.$t('general.image-upload-failed'));
} else {
utils.notify.success(this.$t('general.image-updated'));
} }
if (response) this.$emit(REFRESH_EVENT);
this.loading = false; this.loading = false;
}, },
}, },

View file

@ -16,7 +16,6 @@
<script> <script>
const UPLOAD_EVENT = "uploaded"; const UPLOAD_EVENT = "uploaded";
import { api } from "@/api"; import { api } from "@/api";
import utils from "@/utils";
export default { export default {
props: { props: {
post: { post: {
@ -56,15 +55,10 @@ export default {
let formData = new FormData(); let formData = new FormData();
formData.append(this.fileName, this.file); formData.append(this.fileName, this.file);
const response = await api.utils.uploadFile(this.url, formData); if(await api.utils.uploadFile(this.url, formData)) {
if(response.status != 200) { this.$emit(UPLOAD_EVENT);
utils.notify.error(this.$t('general.failure-uploading-file'));
} else {
utils.notify.success(this.$t('general.file-uploaded'));
} }
this.isSelecting = false; this.isSelecting = false;
this.$emit(UPLOAD_EVENT);
} }
}, },
onButtonClick() { onButtonClick() {

View file

@ -105,17 +105,15 @@ export default {
async createRecipe() { async createRecipe() {
if (this.$refs.urlForm.validate()) { if (this.$refs.urlForm.validate()) {
this.processing = true; this.processing = true;
let response = await api.recipes.createByURL(this.recipeURL); const response = await api.recipes.createByURL(this.recipeURL);
if (response.status !== 201) {
this.error = true;
this.processing = false;
return;
}
this.addRecipe = false;
this.processing = false; this.processing = false;
this.recipeURL = ""; if (response) {
this.$router.push(`/recipe/${response.data}`); this.addRecipe = false;
this.recipeURL = "";
this.$router.push(`/recipe/${response.data}`);
} else {
this.error = true;
}
} }
}, },

View file

@ -130,7 +130,9 @@
"page-updated": "Page updated", "page-updated": "Page updated",
"pages-update-failed": "Pages update failed", "pages-update-failed": "Pages update failed",
"pages-updated": "Pages updated", "pages-updated": "Pages updated",
"recent": "Recent" "recent": "Recent",
"page-deletion-failed": "Page deletion failed",
"page-deleted": "Page deleted"
}, },
"recipe": { "recipe": {
"add-key": "Add Key", "add-key": "Add Key",
@ -172,7 +174,9 @@
"title": "Title", "title": "Title",
"total-time": "Total Time", "total-time": "Total Time",
"unable-to-delete-recipe": "Unable to Delete Recipe", "unable-to-delete-recipe": "Unable to Delete Recipe",
"view-recipe": "View Recipe" "view-recipe": "View Recipe",
"recipe-creation-failed": "Recipe creation failed",
"recipe-created": "Recipe created"
}, },
"search": { "search": {
"category-filter": "Category Filter", "category-filter": "Category Filter",
@ -199,11 +203,12 @@
"full-backup": "Full Backup", "full-backup": "Full Backup",
"import-summary": "Import Summary", "import-summary": "Import Summary",
"partial-backup": "Partial Backup", "partial-backup": "Partial Backup",
"unable-to-delete-backup-see-log-file": "Unable to Delete Backup. See Log File" "unable-to-delete-backup": "Unable to Delete Backup."
}, },
"backup-and-exports": "Backups", "backup-and-exports": "Backups",
"backup-info": "Backups are exported in standard JSON format along with all the images stored on the file system. In your backup folder you'll find a .zip file that contains all of the recipe JSON and images from the database. Additionally, if you selected a markdown file, those will also be stored in the .zip file. To import a backup, it must be located in your backups folder. Automated backups are done each day at 3:00 AM.", "backup-info": "Backups are exported in standard JSON format along with all the images stored on the file system. In your backup folder you'll find a .zip file that contains all of the recipe JSON and images from the database. Additionally, if you selected a markdown file, those will also be stored in the .zip file. To import a backup, it must be located in your backups folder. Automated backups are done each day at 3:00 AM.",
"category-deleted": "Category Deleted", "category-deleted": "Category Deleted",
"category-creation-failed": "Category creation failed",
"category-deletion-failed": "Category deletion failed", "category-deletion-failed": "Category deletion failed",
"change-password": "Change Password", "change-password": "Change Password",
"current": "Version:", "current": "Version:",
@ -275,7 +280,10 @@
"remove-unused": "Remove Unused", "remove-unused": "Remove Unused",
"assign-all": "Assign All", "assign-all": "Assign All",
"bulk-assign": "Bulk Assign" "bulk-assign": "Bulk Assign"
} },
"category-created": "Category created",
"category-update-failed": "Category update failed",
"category-updated": "Category updated"
}, },
"this": {}, "this": {},
"user": { "user": {

View file

@ -40,7 +40,6 @@
<script> <script>
import ImportDialog from "./ImportDialog"; import ImportDialog from "./ImportDialog";
import { api } from "@/api"; import { api } from "@/api";
import utils from "@/utils";
export default { export default {
props: { props: {
backups: Array, backups: Array,
@ -63,22 +62,21 @@ export default {
}, },
async importBackup(data) { async importBackup(data) {
this.$emit("loading"); this.$emit("loading");
let response = await api.backups.import(data.name, data); const response = await api.backups.import(data.name, data);
if(response) {
let importData = response.data;
this.$emit("finished", importData);
} else {
this.$emit("finished");
}
let importData = response.data;
this.$emit("finished", importData);
}, },
async deleteBackup(data) { async deleteBackup(data) {
this.$emit("loading"); this.$emit("loading");
const response = await api.backups.delete(data.name); if (await api.backups.delete(data.name)) {
if (response.status != 200) { this.selectedBackup = null;
utils.notify.error(this.$t('settings.backup.unable-to-delete-backup-see-log-file'));
} else {
utils.notify.success(this.$t('settings.backup.backup-deleted'));
} }
this.selectedBackup = null;
this.backupLoading = false; this.backupLoading = false;
this.$emit("finished"); this.$emit("finished");

View file

@ -44,7 +44,6 @@
<script> <script>
import ImportOptions from "./ImportOptions"; import ImportOptions from "./ImportOptions";
import { api } from "@/api"; import { api } from "@/api";
import utils from "@/utils";
export default { export default {
components: { ImportOptions }, components: { ImportOptions },
data() { data() {
@ -98,14 +97,10 @@ export default {
templates: this.selectedTemplates, templates: this.selectedTemplates,
}; };
const response = await api.backups.create(data); if (await api.backups.create(data)) {
this.loading = false;
if (response.status != 201) {
utils.notify.error(this.$t('settings.backup.error-creating-backup-see-log-file'));
} else {
utils.notify.success(this.$t('settings.backup.backup-created-at-response-export_path', {path: response.data.export_path}));
this.$emit("created"); this.$emit("created");
} }
this.loading = false;
}, },
appendTemplate(templateName) { appendTemplate(templateName) {

View file

@ -57,7 +57,6 @@
const RENDER_EVENT = "update"; const RENDER_EVENT = "update";
import ConfirmationDialog from "@/components/UI/Dialogs/ConfirmationDialog"; import ConfirmationDialog from "@/components/UI/Dialogs/ConfirmationDialog";
import { api } from "@/api"; import { api } from "@/api";
import utils from "@/utils";
export default { export default {
components: { ConfirmationDialog }, components: { ConfirmationDialog },
props: { props: {
@ -92,26 +91,7 @@ export default {
this.$refs.deleteGroupConfirm.open(); this.$refs.deleteGroupConfirm.open();
}, },
async deleteGroup() { async deleteGroup() {
const response = await api.groups.delete(this.group.id); if (await api.groups.delete(this.group.id)) {
if (response.status != 200) {
switch(response.data.detail) {
case 'GROUP_WITH_USERS':
utils.notify.error(this.$t('user.cannot-delete-group-with-users'));
break;
case 'GROUP_NOT_FOUND':
utils.notify.error(this.$t('user.group-not-found'));
break;
case 'DEFAULT_GROUP':
utils.notify.error(this.$t('user.cannot-delete-default-group'));
break;
default:
utils.notify.error(this.$t('user.group-deletion-failed'));
}
} else {
utils.notify.success(this.$t('user.group-deleted'));
this.$emit(RENDER_EVENT); this.$emit(RENDER_EVENT);
} }
}, },

View file

@ -86,7 +86,6 @@
import { validators } from "@/mixins/validators"; import { validators } from "@/mixins/validators";
import { api } from "@/api"; import { api } from "@/api";
import GroupCard from "./GroupCard"; import GroupCard from "./GroupCard";
import utils from "@/utils";
export default { export default {
components: { GroupCard }, components: { GroupCard },
mixins: [validators], mixins: [validators],
@ -105,15 +104,11 @@ export default {
methods: { methods: {
async createGroup() { async createGroup() {
this.groupLoading = true; this.groupLoading = true;
const response = await api.groups.create(this.newGroupName); if (await api.groups.create(this.newGroupName)) {
if (response.status != 201) {
utils.notify.error(this.$t('user.user-group-creation-failed'));
} else {
utils.notify.success(this.$t('user.user-group-created'));
this.groupLoading = false;
this.groupDialog = false; this.groupDialog = false;
this.$store.dispatch("requestAllGroups"); this.$store.dispatch("requestAllGroups");
} }
this.groupLoading = false;
}, },
}, },
}; };

View file

@ -109,7 +109,6 @@
<script> <script>
import ConfirmationDialog from "@/components/UI/Dialogs/ConfirmationDialog"; import ConfirmationDialog from "@/components/UI/Dialogs/ConfirmationDialog";
import { api } from "@/api"; import { api } from "@/api";
import utils from "@/utils";
import { validators } from "@/mixins/validators"; import { validators } from "@/mixins/validators";
export default { export default {
components: { ConfirmationDialog }, components: { ConfirmationDialog },
@ -183,13 +182,9 @@ export default {
}, },
async deleteToken() { async deleteToken() {
const response = await api.signUps.deleteToken(this.activeId); if (await api.signUps.deleteToken(this.activeId)) {
if (response.status != 200) { this.initialize();
utils.notify.error(this.$t('general.not-authorized'));
} else {
utils.notify.success(this.$t('user.sign-up-token-deleted'));
} }
this.initialize();
}, },
editItem(item) { editItem(item) {

View file

@ -146,7 +146,6 @@
<script> <script>
import ConfirmationDialog from "@/components/UI/Dialogs/ConfirmationDialog"; import ConfirmationDialog from "@/components/UI/Dialogs/ConfirmationDialog";
import { api } from "@/api"; import { api } from "@/api";
import utils from "@/utils";
import { validators } from "@/mixins/validators"; import { validators } from "@/mixins/validators";
export default { export default {
components: { ConfirmationDialog }, components: { ConfirmationDialog },
@ -224,18 +223,7 @@ export default {
}, },
async deleteUser() { async deleteUser() {
const response = await api.users.delete(this.activeId); if (await api.users.delete(this.activeId)) {
if (response.status != 200) {
switch(response.data.detail) {
case 'SUPER_USER':
utils.notify.error(this.$t('user.error-cannot-delete-super-user'));
break;
default:
utils.notify.error(this.$t('user.you-are-not-allowed-to-delete-this-user'));
}
} else {
utils.notify.success(this.$t('user.user-deleted'));
this.initialize(); this.initialize();
} }
}, },
@ -283,31 +271,18 @@ export default {
} }
await this.initialize(); await this.initialize();
}, },
async resetPassword() { resetPassword() {
const response = await api.users.resetPassword(this.editedItem.id); api.users.resetPassword(this.editedItem.id);
if (response.status != 200) {
utils.notify.error(this.$t('user.password-reset-failed'));
} else {
utils.notify.success(this.$t('user.password-has-been-reset-to-the-default-password'));
}
}, },
async createUser() { async createUser() {
const response = await api.users.create(this.editedItem); if(await api.users.create(this.editedItem)) {
if(response.status!=201) {
utils.notify.error(this.$t('user.user-creation-failed'));
} else {
utils.notify.success(this.$t('user.user-created'));
this.close(); this.close();
} }
}, },
async updateUser() { async updateUser() {
const response = await api.users.update(this.editedItem); if(await api.users.update(this.editedItem)) {
if(response.status!=200) {
utils.notify.error(this.$t('user.user-update-failed'));
} else {
utils.notify.success(this.$t('user.user-updated'));
this.close(); this.close();
} }
} }

View file

@ -84,7 +84,6 @@
import { api } from "@/api"; import { api } from "@/api";
import TimePickerDialog from "@/components/FormHelpers/TimePickerDialog"; import TimePickerDialog from "@/components/FormHelpers/TimePickerDialog";
import CategoryTagSelector from "@/components/FormHelpers/CategoryTagSelector"; import CategoryTagSelector from "@/components/FormHelpers/CategoryTagSelector";
import utils from "@/utils";
export default { export default {
components: { components: {
TimePickerDialog, TimePickerDialog,
@ -136,11 +135,7 @@ export default {
this.groupSettings.webhookUrls.splice(index, 1); this.groupSettings.webhookUrls.splice(index, 1);
}, },
async saveGroupSettings() { async saveGroupSettings() {
const response = await api.groups.update(this.groupSettings); if (await api.groups.update(this.groupSettings)) {
if (response.status != 200) {
utils.notify.error(this.$t('user.error-updating-group'));
} else {
utils.notify.success(this.$t('settings.group-settings-updated'));
await this.$store.dispatch("requestCurrentGroup"); await this.$store.dispatch("requestCurrentGroup");
this.getSiteSettings(); this.getSiteSettings();
} }

View file

@ -69,7 +69,6 @@
import TheUploadBtn from "@/components/UI/Buttons/TheUploadBtn"; import TheUploadBtn from "@/components/UI/Buttons/TheUploadBtn";
import { api } from "@/api"; import { api } from "@/api";
import MigrationDialog from "./MigrationDialog"; import MigrationDialog from "./MigrationDialog";
import utils from "@/utils";
export default { export default {
props: { props: {
folder: String, folder: String,
@ -88,11 +87,7 @@ export default {
}, },
methods: { methods: {
async deleteMigration(file_name) { async deleteMigration(file_name) {
const response = await api.migrations.delete(this.folder, file_name); if (await api.migrations.delete(this.folder, file_name)) {
if (response.status != 200) {
utils.notify.error(this.$t('general.file-folder-not-found'));
} else {
utils.notify.success(this.$t('migration.migration-data-removed'));
this.$emit("refresh"); this.$emit("refresh");
} }
}, },

View file

@ -147,7 +147,6 @@
// import AvatarPicker from '@/components/AvatarPicker' // import AvatarPicker from '@/components/AvatarPicker'
import TheUploadBtn from "@/components/UI/Buttons/TheUploadBtn"; import TheUploadBtn from "@/components/UI/Buttons/TheUploadBtn";
import { api } from "@/api"; import { api } from "@/api";
import utils from "@/utils";
import { validators } from "@/mixins/validators"; import { validators } from "@/mixins/validators";
import { initials } from "@/mixins/initials"; import { initials } from "@/mixins/initials";
export default { export default {
@ -203,10 +202,7 @@ export default {
async updateUser() { async updateUser() {
this.loading = true; this.loading = true;
const response = await api.users.update(this.user); const response = await api.users.update(this.user);
if(response.status != 200) { if(response) {
utils.notify.error(this.$t('user.user-update-failed'));
} else {
utils.notify.success(this.$t('user.user-updated'));
this.$store.commit("setToken", response.data.access_token); this.$store.commit("setToken", response.data.access_token);
this.refreshProfile(); this.refreshProfile();
this.loading = false; this.loading = false;
@ -221,11 +217,7 @@ export default {
}; };
if (this.$refs.passChange.validate()) { if (this.$refs.passChange.validate()) {
const response = await api.users.changePassword(this.user.id, data); if (await api.users.changePassword(this.user.id, data)) {
if (response.status != 200) {
utils.notify.error(this.$t('user.existing-password-does-not-match'));
} else {
utils.notify.success(this.$t('user.password-updated'));
this.$emit("refresh"); this.$emit("refresh");
} }
} }

View file

@ -44,7 +44,6 @@
<script> <script>
const NEW_PAGE_EVENT = "refresh-page"; const NEW_PAGE_EVENT = "refresh-page";
import { api } from "@/api"; import { api } from "@/api";
import utils from "@/utils";
import CategoryTagSelector from "@/components/FormHelpers/CategoryTagSelector"; import CategoryTagSelector from "@/components/FormHelpers/CategoryTagSelector";
export default { export default {
components: { components: {
@ -83,21 +82,14 @@ export default {
this.$refs.categoryFormSelector.setInit(this.page.categories); this.$refs.categoryFormSelector.setInit(this.page.categories);
}, },
async submitForm() { async submitForm() {
let response, sucessMessage, errorMessage; let response;
if (this.create) { if (this.create) {
response = await api.siteSettings.createPage(this.page); response = await api.siteSettings.createPage(this.page);
sucessMessage = this.$t('page.new-page-created');
errorMessage = this.$t('page.page-creation-failed');
} else { } else {
response = await api.siteSettings.updatePage(this.page); response = await api.siteSettings.updatePage(this.page);
sucessMessage = this.$t('page.page-updated');
errorMessage = this.$t('page.page-update-failed');
} }
if (response.status != 200) { if (response) {
utils.notify.error(errorMessage);
} else {
utils.notify.success(sucessMessage);
this.pageDialog = false; this.pageDialog = false;
this.page.categories = []; this.page.categories = [];
this.$emit(NEW_PAGE_EVENT); this.$emit(NEW_PAGE_EVENT);

View file

@ -65,7 +65,6 @@
import draggable from "vuedraggable"; import draggable from "vuedraggable";
import CreatePageDialog from "./CreatePageDialog"; import CreatePageDialog from "./CreatePageDialog";
import { api } from "@/api"; import { api } from "@/api";
import utils from "@/utils";
export default { export default {
components: { components: {
draggable, draggable,
@ -110,11 +109,7 @@ export default {
element.position = index; element.position = index;
}); });
const response = await api.siteSettings.updateAllPages(this.customPages); if (await api.siteSettings.updateAllPages(this.customPages)) {
if (response.status != 200) {
utils.notify.error(this.$t('page.pages-update-failed'));
} else {
utils.notify.success(this.$t('page.pages-updated'));
this.getPages(); this.getPages();
} }