leumas-universal-crud-react
Version:
Leumas Universal CRUD to a dynamic Endpoint, Setup your own Dynamic Endpoint and Use Leumas API to send to your MONGO clusters
388 lines (334 loc) • 12.3 kB
JSX
import {endpoints} from "../../ConnectBackends/endpoints"
export const createItem = async (model, endpointKey, data, token) => {
console.log("endpoint key : " , endpointKey)
console.log(data.model)
try {
const api = endpoints[endpointKey](token);
const response = await api.post(`/create`, data);
return response.data;
} catch (err) {
console.error("Error creating the item:", err);
console.log(err)
throw err;
}
};
export const createItemsBulk = async (model, endpointKey, bulkData, token) => {
console.log("endpoint key:", endpointKey);
console.log("Model:", model);
// Ensure bulkData is an array
if (!Array.isArray(bulkData)) {
throw new Error("bulkData must be an array.");
}
try {
const api = endpoints[endpointKey](token);
// Use the `/create-bulk` endpoint for bulk creation
const response = await api.post(`/create-bulk`, { data: bulkData });
return response.data;
} catch (err) {
console.error("Error creating items in bulk:", err);
console.log(err.response?.data || err.message); // Log the server's response message if it exists
throw err;
}
};
export const editItem = async (model, id, data, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.put(`/edit/${model}/${id}`, data);
return response.data;
} catch (err) {
console.error("Error editing the item:", err);
throw err;
}
};
export const editCoverPhoto = async (blockId, coverPhotoUrl, token , ownerID) => {
try {
const dataToSubmit = {
model:"Block",
owner: ownerID,
endpoint:"LeumasAPI",
data : {
coverPhoto: coverPhotoUrl
}
}
const response = await editItem('Block', blockId, dataToSubmit, 'LeumasAPI', token);
return response; // Returns the response from the editItem function
} catch (error) {
console.error('Error editing cover photo:', error);
throw error;
}
};
export const getAllItems = async (model, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.post('/all', { model });
return response.data;
} catch (err) {
console.error("Error fetching all items:", err);
throw err;
}
};
export const getAllOwnerRelated = async (owner, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.post(`/all/owner/related/${owner}`, { token });
return response.data;
} catch (err) {
console.error("Error fetching all items:", err);
throw err;
}
};
export const getAllOwnerCreated = async (owner, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.post(`/all/owner/created/${owner}`, { token });
return response.data;
} catch (err) {
console.error("Error fetching all items:", err);
throw err;
}
};
export const getAllPublicItems = async (model, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.post('/all/public', { model });
console.log(response)
return response.data;
} catch (err) {
console.error("Error fetching all items:", err);
throw err;
}
};
export const getItemById = async (model, id, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.get(`/${model}/${id}`);
return response.data;
} catch (err) {
console.error("Error fetching the item by ID:", err);
throw err;
}
};
export const getRelatedApp = async ( id, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.get(`/leumasapp/${id}`);
return response.data;
} catch (err) {
console.error("Error fetching the item by ID:", err);
throw err;
}
};
export const getItemsByOwner = async (model, id, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.get(`/${model}/owner/${id}`);
return response.data;
} catch (err) {
console.error("Error fetching the item by ID:", err);
throw err;
}
};
export const getVirtualIdsByOwner = async ( id, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.get(`/id/owner/${id}`);
return response.data;
} catch (err) {
console.error("Error fetching the item by ID:", err);
throw err;
}
};
export const deleteItem = async (model, id, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.delete(`/${model}/${id}`);
return response.data;
} catch (err) {
console.error("Error deleting the item:", err);
throw err;
}
};
export const togglePublishStatus = async (model, endpointKey, id, token) => {
try {
const api = endpoints[endpointKey](token); // Ensure endpoints[endpointKey] is a function
const response = await api.post(`/${model}/${id}/toggle`);
return response.data;
} catch (err) {
console.error("Error toggling the item's status:", err);
throw err;
}
};
// Helper to filter items based on criteria
export const filterItems = async (model, criteria, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.post(`/${model}/items/filter`, criteria);
console.log("Response : " , response.data)
return response.data;
} catch (err) {
console.error("Error filtering items:", err);
throw err;
}
};
// Helper to search items
export const searchItems = async (model, query, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.get(`/${model}/items/search`, { params: { query } });
return response.data;
} catch (err) {
console.error("Error searching for items:", err);
throw err;
}
};
// Helper to bulk edit items
export const bulkEditItems = async (model, items, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.put(`/${model}/items/bulk-edit`, { items });
return response.data;
} catch (err) {
console.error("Error in bulk edit of items:", err);
throw err;
}
};
// Helper to export items
export const exportItems = async (model, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.get(`/${model}/items/export`);
return response.data;
} catch (err) {
console.error("Error exporting items:", err);
throw err;
}
};
// Helper to get item history log
export const getItemHistoryLog = async (model, itemId, endpointKey, token, userId) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.get(`/history/${model}/${itemId}?userId=${userId}`);
return response.data;
} catch (err) {
console.error("Error getting item history log:", err);
throw err;
}
};
// Helper to undo last action on an item
export const undoLastAction = async (model, itemId, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.post(`/${model}/items/${itemId}/undo`);
return response.data;
} catch (err) {
console.error("Error undoing last action:", err);
throw err;
}
};
// Helper to redo last action on an item
export const redoLastAction = async (model, itemId, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.post(`/${model}/items/${itemId}/redo`);
return response.data;
} catch (err) {
console.error("Error redoing last action:", err);
throw err;
}
};
// Helper to set user permissions for an item
export const setUserPermissions = async (model, itemId, permissions, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.post(`/${model}/items/${itemId}/permissions`, permissions);
return response.data;
} catch (err) {
console.error("Error setting user permissions:", err);
throw err;
}
};
// Helper to send notifications
export const sendNotifications = async (model, notificationData, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.post(`/${model}/notifications`, notificationData);
return response.data;
} catch (err) {
console.error("Error sending notifications:", err);
throw err;
}
};
// Helper to toggle item as favorite
export const toggleFavoriteItem = async (model, itemId, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.post(`/${model}/items/${itemId}/favorite`);
return response.data;
} catch (err) {
console.error("Error toggling item as favorite:", err);
throw err;
}
};
// Helper to process batch tasks
export const processBatchTasks = async (model, batchTasks, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.post(`/${model}/batch/process`, batchTasks);
return response.data;
} catch (err) {
console.error("Error processing batch tasks:", err);
throw err;
}
};
// Helper to suggest related items
export const suggestRelatedItems = async (model, itemId, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.get(`/${model}/items/${itemId}/related`);
return response.data;
} catch (err) {
console.error("Error suggesting related items:", err);
throw err;
}
};
//Dynamic Schema Universal Helpers
export const getItemFromDynamicSchema = async (model, itemId, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.get(`/dynamic-schema/${model}/${itemId}`);
return response.data;
} catch (err) {
console.error("Error suggesting related items:", err);
throw err;
}
};
export const createItemForDynamicSchema = async (model, data, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.create(`/dynamic-schema/${model}` , {data});
return response.data;
} catch (err) {
console.error("Error suggesting related items:", err);
throw err;
}
};
export const editItemFromDynamicSchema = async (model, itemId, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.put(`/dynamic-schema/${model}/${itemId}`);
return response.data;
} catch (err) {
console.error("Error suggesting related items:", err);
throw err;
}
};
export const deleteItemFromDynamicSchema = async (model, itemId, endpointKey, token) => {
try {
const api = endpoints[endpointKey](token);
const response = await api.delete(`/dynamic-schema/${model}/${itemId}`);
return response.data;
} catch (err) {
console.error("Error suggesting related items:", err);
throw err;
}
};