391 lines
18 KiB
JavaScript
391 lines
18 KiB
JavaScript
'use strict';
|
||
|
||
var qs = require('qs');
|
||
var collections = require('../constants/collections.js');
|
||
var api = require('./api.js');
|
||
|
||
const documentApi = api.contentManagerApi.injectEndpoints({
|
||
overrideExisting: true,
|
||
endpoints: (builder)=>({
|
||
autoCloneDocument: builder.mutation({
|
||
query: ({ model, sourceId, query })=>({
|
||
url: `/content-manager/collection-types/${model}/auto-clone/${sourceId}`,
|
||
method: 'POST',
|
||
config: {
|
||
params: query
|
||
}
|
||
}),
|
||
invalidatesTags: (_result, error, { model })=>{
|
||
if (error) {
|
||
return [];
|
||
}
|
||
return [
|
||
{
|
||
type: 'Document',
|
||
id: `${model}_LIST`
|
||
},
|
||
'RecentDocumentList'
|
||
];
|
||
}
|
||
}),
|
||
cloneDocument: builder.mutation({
|
||
query: ({ model, sourceId, data, params })=>({
|
||
url: `/content-manager/collection-types/${model}/clone/${sourceId}`,
|
||
method: 'POST',
|
||
data,
|
||
config: {
|
||
params
|
||
}
|
||
}),
|
||
invalidatesTags: (_result, _error, { model })=>[
|
||
{
|
||
type: 'Document',
|
||
id: `${model}_LIST`
|
||
},
|
||
{
|
||
type: 'UidAvailability',
|
||
id: model
|
||
},
|
||
'RecentDocumentList'
|
||
]
|
||
}),
|
||
/**
|
||
* Creates a new collection-type document. This should ONLY be used for collection-types.
|
||
* single-types should always be using `updateDocument` since they always exist.
|
||
*/ createDocument: builder.mutation({
|
||
query: ({ model, data, params })=>({
|
||
url: `/content-manager/collection-types/${model}`,
|
||
method: 'POST',
|
||
data,
|
||
config: {
|
||
params
|
||
}
|
||
}),
|
||
invalidatesTags: (result, _error, { model })=>[
|
||
{
|
||
type: 'Document',
|
||
id: `${model}_LIST`
|
||
},
|
||
'Relations',
|
||
{
|
||
type: 'UidAvailability',
|
||
id: model
|
||
},
|
||
'RecentDocumentList'
|
||
],
|
||
transformResponse: (response, meta, arg)=>{
|
||
/**
|
||
* TODO v6
|
||
* Adapt plugin:users-permissions.user to return the same response
|
||
* shape as all other requests. The error is returned as expected.
|
||
*/ if (!('data' in response) && arg.model === 'plugin::users-permissions.user') {
|
||
return {
|
||
data: response,
|
||
meta: {
|
||
availableStatus: [],
|
||
availableLocales: []
|
||
}
|
||
};
|
||
}
|
||
return response;
|
||
}
|
||
}),
|
||
deleteDocument: builder.mutation({
|
||
query: ({ collectionType, model, documentId, params })=>({
|
||
url: `/content-manager/${collectionType}/${model}${collectionType !== collections.SINGLE_TYPES && documentId ? `/${documentId}` : ''}`,
|
||
method: 'DELETE',
|
||
config: {
|
||
params
|
||
}
|
||
}),
|
||
invalidatesTags: (_result, _error, { collectionType, model })=>[
|
||
{
|
||
type: 'Document',
|
||
id: collectionType !== collections.SINGLE_TYPES ? `${model}_LIST` : model
|
||
},
|
||
'RecentDocumentList'
|
||
]
|
||
}),
|
||
deleteManyDocuments: builder.mutation({
|
||
query: ({ model, params, ...body })=>({
|
||
url: `/content-manager/collection-types/${model}/actions/bulkDelete`,
|
||
method: 'POST',
|
||
data: body,
|
||
config: {
|
||
params
|
||
}
|
||
}),
|
||
invalidatesTags: (_res, _error, { model })=>[
|
||
{
|
||
type: 'Document',
|
||
id: `${model}_LIST`
|
||
},
|
||
'RecentDocumentList'
|
||
]
|
||
}),
|
||
discardDocument: builder.mutation({
|
||
query: ({ collectionType, model, documentId, params })=>({
|
||
url: documentId ? `/content-manager/${collectionType}/${model}/${documentId}/actions/discard` : `/content-manager/${collectionType}/${model}/actions/discard`,
|
||
method: 'POST',
|
||
config: {
|
||
params
|
||
}
|
||
}),
|
||
invalidatesTags: (_result, _error, { collectionType, model, documentId })=>{
|
||
return [
|
||
{
|
||
type: 'Document',
|
||
id: collectionType !== collections.SINGLE_TYPES ? `${model}_${documentId}` : model
|
||
},
|
||
{
|
||
type: 'Document',
|
||
id: `${model}_LIST`
|
||
},
|
||
'Relations',
|
||
{
|
||
type: 'UidAvailability',
|
||
id: model
|
||
},
|
||
'RecentDocumentList'
|
||
];
|
||
}
|
||
}),
|
||
/**
|
||
* Gets all documents of a collection type or single type.
|
||
* By passing different params you can get different results e.g. only published documents or 'es' documents.
|
||
*/ getAllDocuments: builder.query({
|
||
query: ({ model, params })=>({
|
||
url: `/content-manager/collection-types/${model}`,
|
||
method: 'GET',
|
||
config: {
|
||
params: qs.stringify(params, {
|
||
encode: true
|
||
})
|
||
}
|
||
}),
|
||
providesTags: (result, _error, arg)=>{
|
||
return [
|
||
{
|
||
type: 'Document',
|
||
id: `ALL_LIST`
|
||
},
|
||
{
|
||
type: 'Document',
|
||
id: `${arg.model}_LIST`
|
||
},
|
||
...result?.results.map(({ documentId })=>({
|
||
type: 'Document',
|
||
id: `${arg.model}_${documentId}`
|
||
})) ?? []
|
||
];
|
||
}
|
||
}),
|
||
getDraftRelationCount: builder.query({
|
||
query: ({ collectionType, model, documentId, params })=>({
|
||
url: documentId ? `/content-manager/${collectionType}/${model}/${documentId}/actions/countDraftRelations` : `/content-manager/${collectionType}/${model}/actions/countDraftRelations`,
|
||
method: 'GET',
|
||
config: {
|
||
params
|
||
}
|
||
})
|
||
}),
|
||
getDocument: builder.query({
|
||
// @ts-expect-error – TODO: fix ts error where data unknown doesn't work with response via an assertion?
|
||
queryFn: async ({ collectionType, model, documentId, params }, _api, _extraOpts, baseQuery)=>{
|
||
const res = await baseQuery({
|
||
url: `/content-manager/${collectionType}/${model}${documentId ? `/${documentId}` : ''}`,
|
||
method: 'GET',
|
||
config: {
|
||
params
|
||
}
|
||
});
|
||
/**
|
||
* To stop the query from locking itself in multiple retries, we intercept the error here and manage correctly.
|
||
* This is because single-types don't have a list view and fetching them with the route `/single-types/:model`
|
||
* never returns a list, just a single document but this won't exist if you've not made one before.
|
||
*/ if (res.error && res.error.name === 'NotFoundError' && collectionType === collections.SINGLE_TYPES) {
|
||
return {
|
||
data: {
|
||
document: undefined
|
||
},
|
||
error: undefined
|
||
};
|
||
}
|
||
return res;
|
||
},
|
||
providesTags: (result, _error, { collectionType, model, documentId })=>{
|
||
return [
|
||
// we prefer the result's id because we don't fetch single-types with an ID.
|
||
{
|
||
type: 'Document',
|
||
id: collectionType !== collections.SINGLE_TYPES ? `${model}_${result && 'documentId' in result ? result.documentId : documentId}` : model
|
||
},
|
||
// Make it easy to invalidate all individual documents queries for a model
|
||
{
|
||
type: 'Document',
|
||
id: `${model}_ALL_ITEMS`
|
||
}
|
||
];
|
||
}
|
||
}),
|
||
getManyDraftRelationCount: builder.query({
|
||
query: ({ model, ...params })=>({
|
||
url: `/content-manager/collection-types/${model}/actions/countManyEntriesDraftRelations`,
|
||
method: 'GET',
|
||
config: {
|
||
params
|
||
}
|
||
}),
|
||
transformResponse: (response)=>response.data
|
||
}),
|
||
/**
|
||
* This endpoint will either create or update documents at the same time as publishing.
|
||
*/ publishDocument: builder.mutation({
|
||
query: ({ collectionType, model, documentId, params, data })=>({
|
||
url: documentId ? `/content-manager/${collectionType}/${model}/${documentId}/actions/publish` : `/content-manager/${collectionType}/${model}/actions/publish`,
|
||
method: 'POST',
|
||
data,
|
||
config: {
|
||
params
|
||
}
|
||
}),
|
||
invalidatesTags: (_result, _error, { collectionType, model, documentId })=>{
|
||
return [
|
||
{
|
||
type: 'Document',
|
||
id: collectionType !== collections.SINGLE_TYPES ? `${model}_${documentId}` : model
|
||
},
|
||
{
|
||
type: 'Document',
|
||
id: `${model}_LIST`
|
||
},
|
||
'Relations',
|
||
'RecentDocumentList'
|
||
];
|
||
}
|
||
}),
|
||
publishManyDocuments: builder.mutation({
|
||
query: ({ model, params, ...body })=>({
|
||
url: `/content-manager/collection-types/${model}/actions/bulkPublish`,
|
||
method: 'POST',
|
||
data: body,
|
||
config: {
|
||
params
|
||
}
|
||
}),
|
||
invalidatesTags: (_res, _error, { model, documentIds })=>documentIds.map((id)=>({
|
||
type: 'Document',
|
||
id: `${model}_${id}`
|
||
}))
|
||
}),
|
||
updateDocument: builder.mutation({
|
||
query: ({ collectionType, model, documentId, data, params })=>({
|
||
url: `/content-manager/${collectionType}/${model}${documentId ? `/${documentId}` : ''}`,
|
||
method: 'PUT',
|
||
data,
|
||
config: {
|
||
params
|
||
}
|
||
}),
|
||
invalidatesTags: (_result, _error, { collectionType, model, documentId })=>{
|
||
return [
|
||
{
|
||
type: 'Document',
|
||
id: collectionType !== collections.SINGLE_TYPES ? `${model}_${documentId}` : model
|
||
},
|
||
'Relations',
|
||
{
|
||
type: 'UidAvailability',
|
||
id: model
|
||
},
|
||
'RecentDocumentList',
|
||
'RecentDocumentList'
|
||
];
|
||
},
|
||
async onQueryStarted ({ data, ...patch }, { dispatch, queryFulfilled }) {
|
||
// Optimistically update the cache with the new data
|
||
const patchResult = dispatch(documentApi.util.updateQueryData('getDocument', patch, (draft)=>{
|
||
Object.assign(draft.data, data);
|
||
}));
|
||
try {
|
||
await queryFulfilled;
|
||
} catch {
|
||
// Rollback the optimistic update if there's an error
|
||
patchResult.undo();
|
||
}
|
||
},
|
||
transformResponse: (response, meta, arg)=>{
|
||
/**
|
||
* TODO v6
|
||
* Adapt plugin:users-permissions.user to return the same response
|
||
* shape as all other requests. The error is returned as expected.
|
||
*/ if (!('data' in response) && arg.model === 'plugin::users-permissions.user') {
|
||
return {
|
||
data: response,
|
||
meta: {
|
||
availableStatus: [],
|
||
availableLocales: []
|
||
}
|
||
};
|
||
}
|
||
return response;
|
||
}
|
||
}),
|
||
unpublishDocument: builder.mutation({
|
||
query: ({ collectionType, model, documentId, params, data })=>({
|
||
url: documentId ? `/content-manager/${collectionType}/${model}/${documentId}/actions/unpublish` : `/content-manager/${collectionType}/${model}/actions/unpublish`,
|
||
method: 'POST',
|
||
data,
|
||
config: {
|
||
params
|
||
}
|
||
}),
|
||
invalidatesTags: (_result, _error, { collectionType, model, documentId })=>{
|
||
return [
|
||
{
|
||
type: 'Document',
|
||
id: collectionType !== collections.SINGLE_TYPES ? `${model}_${documentId}` : model
|
||
},
|
||
'RecentDocumentList'
|
||
];
|
||
}
|
||
}),
|
||
unpublishManyDocuments: builder.mutation({
|
||
query: ({ model, params, ...body })=>({
|
||
url: `/content-manager/collection-types/${model}/actions/bulkUnpublish`,
|
||
method: 'POST',
|
||
data: body,
|
||
config: {
|
||
params
|
||
}
|
||
}),
|
||
invalidatesTags: (_res, _error, { model, documentIds })=>[
|
||
...documentIds.map((id)=>({
|
||
type: 'Document',
|
||
id: `${model}_${id}`
|
||
})),
|
||
'RecentDocumentList'
|
||
]
|
||
})
|
||
})
|
||
});
|
||
const { useAutoCloneDocumentMutation, useCloneDocumentMutation, useCreateDocumentMutation, useDeleteDocumentMutation, useDeleteManyDocumentsMutation, useDiscardDocumentMutation, useGetAllDocumentsQuery, useLazyGetDocumentQuery, useGetDocumentQuery, useLazyGetDraftRelationCountQuery, useGetManyDraftRelationCountQuery, usePublishDocumentMutation, usePublishManyDocumentsMutation, useUpdateDocumentMutation, useUnpublishDocumentMutation, useUnpublishManyDocumentsMutation } = documentApi;
|
||
|
||
exports.useAutoCloneDocumentMutation = useAutoCloneDocumentMutation;
|
||
exports.useCloneDocumentMutation = useCloneDocumentMutation;
|
||
exports.useCreateDocumentMutation = useCreateDocumentMutation;
|
||
exports.useDeleteDocumentMutation = useDeleteDocumentMutation;
|
||
exports.useDeleteManyDocumentsMutation = useDeleteManyDocumentsMutation;
|
||
exports.useDiscardDocumentMutation = useDiscardDocumentMutation;
|
||
exports.useGetAllDocumentsQuery = useGetAllDocumentsQuery;
|
||
exports.useGetDocumentQuery = useGetDocumentQuery;
|
||
exports.useGetDraftRelationCountQuery = useLazyGetDraftRelationCountQuery;
|
||
exports.useGetManyDraftRelationCountQuery = useGetManyDraftRelationCountQuery;
|
||
exports.useLazyGetDocumentQuery = useLazyGetDocumentQuery;
|
||
exports.usePublishDocumentMutation = usePublishDocumentMutation;
|
||
exports.usePublishManyDocumentsMutation = usePublishManyDocumentsMutation;
|
||
exports.useUnpublishDocumentMutation = useUnpublishDocumentMutation;
|
||
exports.useUnpublishManyDocumentsMutation = useUnpublishManyDocumentsMutation;
|
||
exports.useUpdateDocumentMutation = useUpdateDocumentMutation;
|
||
//# sourceMappingURL=documents.js.map
|