Skip to content

Commit

Permalink
Merge branch 'main' into github
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitvidis committed Jul 1, 2024
2 parents 9131ca9 + 1c5e9e0 commit 108fae6
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 157 deletions.
4 changes: 2 additions & 2 deletions admin/src/components/ViewsTable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const TableFooter = () => {
};

return (
<Flex alignItems="flex-end" justifyContent="space-between">
<Flex alignItems="flex-end" justifyContent="space-between" marginTop={4}>
<Flex gap={2}>
<SingleSelect size="S" onChange={handleChangeItemsPerPage} value={itemsPerPage}>
{ITEMS_PER_PAGE.map((option) => (
Expand All @@ -182,7 +182,7 @@ export const TableFooter = () => {
{translate('HomePage.Table.Footer.Pagination.Select')}
</Typography>
</Flex>
<Pagination activePage={fetchParams.currentPage} pageCount={fetchParams.viewsPerPage}>
<Pagination activePage={fetchParams.currentPage} pageCount={viewsPagesCount}>
<PreviousLink
as={NavLink}
to={`?page=${fetchParams.currentPage - 1}&pageSize=${
Expand Down
24 changes: 11 additions & 13 deletions admin/src/hooks/views/useViews.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const useViews = () => {

useEffect(() => {
const query = new URLSearchParams(location.search);
const page = Number(query.get('page'));
const pageSize = Number(query.get('pageSize'));
const page = Number(query.get('page')) === 0 ? 1 : Number(query.get('page'));
const pageSize = Number(query.get('pageSize')) === 0 ? 10 : Number(query.get('pageSize'));

setFetchParams({
currentPage: page,
Expand All @@ -71,12 +71,11 @@ const useViews = () => {
const response = await get(
`${CONST.REQUEST_URLS.GET_USER_VIEWS}?page=${page}&pageSize=${pageSize}&sortBy=createdAt:asc`
);

const { userViewsData, pagination } = response.data;
setViews(userViewsData || []);
setViewsPagination({
count: Number(pagination.count),
totalPages: Number(pagination.totalPages)
count: Number(pagination.total),
totalPages: Number(pagination.pageCount)
});
},
[fetchParams]
Expand All @@ -87,12 +86,11 @@ const useViews = () => {
const response = await get(
`${CONST.REQUEST_URLS.GET_SHARED_VIEWS}?page=${page}&pageSize=${pageSize}&sortBy=createdAt:asc`
);

const { sharedViewsData, pagination } = response.data;
setViews(sharedViewsData || []);
setViewsPagination({
count: Number(pagination.count),
totalPages: Number(pagination.totalPages)
count: Number(pagination.total),
totalPages: Number(pagination.pageCount)
});
},
[fetchParams]
Expand All @@ -103,7 +101,7 @@ const useViews = () => {
const getPrivateViews = async () => {
const { data } = await get(CONST.REQUEST_URLS.GET_PRIVATE_VIEWS);

setPrivateViews(data.privateViewsData);
setPrivateViews(data);
};

const addView = async (viewData) => {
Expand Down Expand Up @@ -131,10 +129,10 @@ const useViews = () => {
await del(`${CONST.REQUEST_URLS.DELETE_VIEW}${id}`);

if (tabsIndex === CONST.TABS_INDEX.userViewsTab) {
getUserViews(fetchParams.page, fetchParams.pageSize);
getUserViews(fetchParams.currentPage, fetchParams.viewsPerPage);
}
if (tabsIndex === CONST.TABS_INDEX.sharedViewsTab) {
getSharedViews(fetchParams.page, fetchParams.pageSize);
getSharedViews(fetchParams.currentPage, fetchParams.viewsPerPage);
}

toggleNotification({
Expand All @@ -154,10 +152,10 @@ const useViews = () => {
await put(`${CONST.REQUEST_URLS.UPDATE_VIEW}${id}`, viewData);

if (tabsIndex === CONST.TABS_INDEX.userViewsTab) {
getUserViews(fetchParams.page, fetchParams.pageSize);
getUserViews(fetchParams.currentPage, fetchParams.viewsPerPage);
}
if (tabsIndex === CONST.TABS_INDEX.sharedViewsTab) {
getSharedViews(fetchParams.page, fetchParams.pageSize);
getSharedViews(fetchParams.currentPage, fetchParams.viewsPerPage);
}

toggleNotification({
Expand Down
1 change: 1 addition & 0 deletions admin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default {
</ViewsProvider>
)
});

if (process.env.STRAPI_ADMIN_FAVORITE_VIEWS_INJECT_TO) {
for (const entry of process.env.STRAPI_ADMIN_FAVORITE_VIEWS_INJECT_TO.split(',')) {
const [pluginName, container, block] = entry.split('.');
Expand Down
93 changes: 80 additions & 13 deletions server/controllers/favorite-views.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,120 @@
'use strict';

const { sanitize } = require('@strapi/utils');

module.exports = ({ strapi }) => ({
async getUserViews(ctx) {
const contentType = strapi.contentType('plugin::favorite-views.saved-view');
const user = ctx.state.user;
const { page = 1, pageSize = 10 } = ctx.query;
const sanitizedQueryParams = await sanitize.contentAPI.query(ctx.query, contentType, {
auth: ctx.state.auth
});
const { page = 1, pageSize = 10 } = sanitizedQueryParams;

ctx.body = await strapi
const { results, pagination } = await strapi
.plugin('favorite-views')
.service('favoriteViews')
.getUserViews({ page, pageSize }, user);

const userViewsData = await sanitize.contentAPI.output(results, contentType, {
auth: user
});

return { userViewsData, pagination };
},

async getSharedViews(ctx) {
const contentType = strapi.contentType('plugin::favorite-views.saved-view');
const user = ctx.state.user;
const { page = 1, pageSize = 10 } = ctx.query;
const sanitizedQueryParams = await sanitize.contentAPI.query(ctx.query, contentType, {
auth: ctx.state.auth
});
const { page = 1, pageSize = 10 } = sanitizedQueryParams;

ctx.body = await strapi
const { results, pagination } = await strapi
.plugin('favorite-views')
.service('favoriteViews')
.getSharedViews({ page, pageSize }, user);

const sharedViewsData = await sanitize.contentAPI.output(results, contentType, {
auth: user
});

return { sharedViewsData, pagination };
},

async getPrivateViews(ctx) {
const contentType = strapi.contentType('plugin::favorite-views.saved-view');
const user = ctx.state.user;

ctx.body = await strapi.plugin('favorite-views').service('favoriteViews').getPrivateViews(user);
const results = await strapi
.plugin('favorite-views')
.service('favoriteViews')
.getPrivateViews(user);

const privateViewsData = await sanitize.contentAPI.output(results, contentType, {
auth: user
});

return privateViewsData;
},

async create(ctx) {
const contentType = strapi.contentType('plugin::favorite-views.saved-view');
const user = ctx.state.user;
const userId = ctx.state.user.id;
const { name, slug, roles, visibility } = ctx.request.body;
const sanitizedRequestBody = await sanitize.contentAPI.input(ctx.request.body, contentType, {
auth: ctx.state.auth
});
const { name, slug, roles, visibility } = sanitizedRequestBody;

ctx.body = await strapi
const result = await strapi
.plugin('favorite-views')
.service('favoriteViews')
.create(name, slug, roles, visibility, userId);

return await sanitize.contentAPI.output(result, contentType, {
auth: user
});
},

async delete(ctx) {
const { id } = ctx.params;
const contentType = strapi.contentType('plugin::favorite-views.saved-view');
const user = ctx.state.user;
const sanitizedQueryParams = await sanitize.contentAPI.query(ctx.params, contentType, {
auth: ctx.state.auth
});
const { id } = sanitizedQueryParams;
const result = await strapi.plugin('favorite-views').service('favoriteViews').delete(id);

ctx.body = await strapi.plugin('favorite-views').service('favoriteViews').delete(id);
return await sanitize.contentAPI.output(result, contentType, {
auth: user
});
},

async update(ctx) {
const { id } = ctx.params;
const userId = ctx.state.user.id;
const { name, roles, visibility } = ctx.request.body;
const contentType = strapi.contentType('plugin::favorite-views.saved-view');
const user = ctx.state.user;
const userId = user.id;
const sanitizedQueryParams = await sanitize.contentAPI.query(ctx.params, contentType, {
auth: ctx.state.auth
});
const { id } = sanitizedQueryParams;
const sanitizedRequestBody = await sanitize.contentAPI.input(ctx.request.body, contentType, {
auth: ctx.state.auth
});
const { name, roles, visibility } = sanitizedRequestBody;

ctx.body = await strapi
const result = await strapi
.plugin('favorite-views')
.service('favoriteViews')
.update(id, name, roles, visibility, userId);

return await sanitize.contentAPI.output(result, contentType, {
auth: user
});
},

async getRoles(ctx) {
ctx.body = await strapi.plugin('favorite-views').service('favoriteViews').getRoles();
}
Expand Down
61 changes: 61 additions & 0 deletions server/routes/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module.exports = {
type: 'admin',
routes: [
{
method: 'GET',
path: '/getUserViews',
handler: 'favoriteViews.getUserViews',
config: {
policies: []
}
},
{
method: 'GET',
path: '/getSharedViews',
handler: 'favoriteViews.getSharedViews',
config: {
policies: []
}
},
{
method: 'GET',
path: '/getPrivateViews',
handler: 'favoriteViews.getPrivateViews',
config: {
policies: []
}
},
{
method: 'POST',
path: '/create',
handler: 'favoriteViews.create',
config: {
policies: []
}
},
{
method: 'DELETE',
path: '/delete/:id',
handler: 'favoriteViews.delete',
config: {
policies: []
}
},
{
method: 'PUT',
path: '/update/:id',
handler: 'favoriteViews.update',
config: {
policies: []
}
},
{
method: 'GET',
path: '/getRoles',
handler: 'favoriteViews.getRoles',
config: {
policies: []
}
}
]
};
61 changes: 3 additions & 58 deletions server/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,3 @@
module.exports = [
{
method: 'GET',
path: '/getUserViews',
handler: 'favoriteViews.getUserViews',
config: {
policies: []
}
},
{
method: 'GET',
path: '/getSharedViews',
handler: 'favoriteViews.getSharedViews',
config: {
policies: []
}
},
{
method: 'GET',
path: '/getPrivateViews',
handler: 'favoriteViews.getPrivateViews',
config: {
policies: []
}
},
{
method: 'POST',
path: '/create',
handler: 'favoriteViews.create',
config: {
policies: []
}
},
{
method: 'DELETE',
path: '/delete/:id',
handler: 'favoriteViews.delete',
config: {
policies: []
}
},
{
method: 'PUT',
path: '/update/:id',
handler: 'favoriteViews.update',
config: {
policies: []
}
},
{
method: 'GET',
path: '/getRoles',
handler: 'favoriteViews.getRoles',
config: {
policies: []
}
}
];
module.exports = {
admin: require('./admin')
};
Loading

0 comments on commit 108fae6

Please sign in to comment.