UNPKG

burdy

Version:

Open Source Headless Platform

286 lines (285 loc) 12.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PostsContextProvider = exports.usePosts = void 0; const react_async_hook_1 = require("react-async-hook"); const react_1 = __importStar(require("react")); const axios_1 = __importDefault(require("axios")); const react_2 = require("@fluentui/react"); const hooks_1 = require("../../../helpers/hooks"); const PostsContext = (0, react_1.createContext)({}); const PostsContextProvider = ({ defaultAdditionalData = {}, children }) => { const [selectedPosts, setSelectedPosts] = (0, react_1.useState)([]); const [additionalData, setAdditionalData] = (0, react_1.useState)(defaultAdditionalData !== null && defaultAdditionalData !== void 0 ? defaultAdditionalData : {}); const loadingContent = (0, react_1.useRef)({}); const setLoadingContent = (id, val) => { if (val) { loadingContent.current[id] = val; } else { delete loadingContent.current[id]; } }; const [stateData, setStateDataImpl] = (0, react_1.useState)({}); const setStateData = (key, value) => { setStateDataImpl(Object.assign(Object.assign({}, (stateData || {})), { [key]: value })); }; const [params, setParams] = (0, react_1.useState)({}); const [post, setPost] = (0, react_1.useState)(null); const postsState = (0, hooks_1.useModelState)([], (a, b) => { if (!(a === null || a === void 0 ? void 0 : a.updatedAt) || !(b === null || b === void 0 ? void 0 : b.updatedAt)) return 0; return (new Date(b.updatedAt)).getTime() - (new Date(a.updatedAt)).getTime(); }); const selection = (0, react_1.useMemo)(() => new react_2.Selection({ onSelectionChanged: () => { setSelectedPosts(selection.getSelection()); }, getKey: (post) => post.id, }), []); const getPosts = (0, react_async_hook_1.useAsyncCallback)((params) => __awaiter(void 0, void 0, void 0, function* () { try { postsState.setArrayState([]); const response = yield axios_1.default.get('/api/posts', { params: Object.assign(Object.assign({}, additionalData), params), }); postsState.setArrayState(response.data); return response.data; } catch (e) { throw e.response.data; } })); const getPost = (0, react_async_hook_1.useAsyncCallback)((id, params) => __awaiter(void 0, void 0, void 0, function* () { try { const response = yield axios_1.default.get(`/api/posts/${id}`, { params, }); setPost(response.data); return response.data; } catch (e) { throw e.response.data; } })); const getBySlug = (0, react_async_hook_1.useAsyncCallback)((slugPath) => __awaiter(void 0, void 0, void 0, function* () { try { const response = yield axios_1.default.get(`/api/posts/one`, { params: { slugPath, }, }); setPost(response.data); return response.data; } catch (e) { throw e.response.data; } })); const getOneContentType = (0, react_async_hook_1.useAsyncCallback)((id) => __awaiter(void 0, void 0, void 0, function* () { try { const response = yield axios_1.default.get(`/api/content-types/${id}`); return response.data; } catch (e) { throw e.response.data; } })); const createPost = (0, react_async_hook_1.useAsyncCallback)((data) => __awaiter(void 0, void 0, void 0, function* () { try { const response = yield axios_1.default.post('/api/posts', Object.assign(Object.assign({}, additionalData), data)); postsState.create([response.data]); return response.data; } catch (e) { throw e.response.data; } })); const updatePost = (0, react_async_hook_1.useAsyncCallback)((id, data) => __awaiter(void 0, void 0, void 0, function* () { try { const response = yield axios_1.default.put(`/api/posts/${id}`, data); postsState.update([response.data]); setPost(response.data); return response.data; } catch (e) { throw e.response.data; } })); const publishPosts = (0, react_async_hook_1.useAsyncCallback)((data) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h; try { const response = yield axios_1.default.put(`/api/posts/publish`, data); // @ts-ignore setPost(Object.assign(Object.assign({}, (post || {})), { status: (_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.status, publishedAt: (_d = (_c = response.data) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.publishedAt, publishedFrom: (_f = (_e = response.data) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.publishedFrom, publishedUntil: (_h = (_g = response.data) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.publishedUntil })); return response.data; } catch (e) { throw e.response.data; } })); const copyPosts = (0, react_async_hook_1.useAsyncCallback)((id, data) => __awaiter(void 0, void 0, void 0, function* () { try { const response = yield axios_1.default.post(`/api/posts/${id}/copy`, Object.assign(Object.assign({}, additionalData), data)); postsState.create(response.data); return response.data; } catch (e) { throw e.response.data; } })); const updatePostContent = (0, react_async_hook_1.useAsyncCallback)((id, data) => __awaiter(void 0, void 0, void 0, function* () { try { const response = yield axios_1.default.put(`/api/posts/${id}/content`, Object.assign(Object.assign({}, additionalData), data)); setPost(response.data); return response.data; } catch (e) { throw e.response.data; } })); const deletePosts = (0, react_async_hook_1.useAsyncCallback)((ids) => __awaiter(void 0, void 0, void 0, function* () { var _j; try { yield axios_1.default.delete('/api/posts', { data: ids, }); postsState.delete(ids); return ids; } catch (e) { throw (_j = e.response) === null || _j === void 0 ? void 0 : _j.data; } })); const compilePost = (0, react_async_hook_1.useAsyncCallback)((data) => __awaiter(void 0, void 0, void 0, function* () { try { const response = yield axios_1.default.post('/api/posts/compile', data); return response.data; } catch (e) { throw e.response.data; } })); const getPreviewData = (0, react_async_hook_1.useAsyncCallback)((id, versionId) => __awaiter(void 0, void 0, void 0, function* () { try { const response = yield axios_1.default.get(`/api/posts/preview/${id}`, { params: { versionId } }); return response.data; } catch (e) { throw e.response.data; } })); const getVersions = (0, react_async_hook_1.useAsyncCallback)((postId, params) => __awaiter(void 0, void 0, void 0, function* () { try { const response = yield axios_1.default.get(`/api/posts/${postId}/versions`, { params, }); return response.data; } catch (e) { throw e.response.data; } })); const getVersionsCount = (0, react_async_hook_1.useAsyncCallback)((postId) => __awaiter(void 0, void 0, void 0, function* () { try { const response = yield axios_1.default.get(`/api/posts/${postId}/versions`, { params: { count: true, }, }); return response.data; } catch (e) { throw e.response.data; } })); const deleteVersions = (0, react_async_hook_1.useAsyncCallback)((postId, ids) => __awaiter(void 0, void 0, void 0, function* () { try { yield axios_1.default.delete(`/api/posts/${postId}/versions`, { data: ids, }); return ids; } catch (e) { throw e.response.data; } })); const restoreVersion = (0, react_async_hook_1.useAsyncCallback)((postId, id) => __awaiter(void 0, void 0, void 0, function* () { try { const response = yield axios_1.default.post(`/api/posts/${postId}/versions/${id}`); return response.data; } catch (e) { throw e.response.data; } })); return (react_1.default.createElement(PostsContext.Provider, { value: { selection, selectedPosts, getPosts, getPost, getBySlug, createPost, updatePost, updatePostContent, deletePosts, copyPosts, publishPosts, getOneContentType, compilePost, getPreviewData, getVersions, getVersionsCount, deleteVersions, restoreVersion, post, setPost, posts: postsState.arrayState, postsState, additionalData, setAdditionalData, params, setParams, stateData, setStateData, loadingContent: Object.values(loadingContent === null || loadingContent === void 0 ? void 0 : loadingContent.current).some((key) => { var _a; return (_a = loadingContent === null || loadingContent === void 0 ? void 0 : loadingContent.current) === null || _a === void 0 ? void 0 : _a[key]; }), setLoadingContent } }, children)); }; exports.PostsContextProvider = PostsContextProvider; const usePosts = () => (0, react_1.useContext)(PostsContext); exports.usePosts = usePosts;