From 829bf7c3902e83fbf86c5ecbda8c4f6672a407e9 Mon Sep 17 00:00:00 2001 From: HichamELBSI Date: Wed, 1 Apr 2020 15:58:48 +0200 Subject: [PATCH] Custom timestamps Signed-off-by: HichamELBSI --- .../controllers/ContentTypes.js | 1 - .../FiltersPicker/FiltersCard/index.js | 35 +++++++++++++++---- .../FiltersPicker/FiltersCard/reducer.js | 29 ++++++++++++--- .../FiltersCard/tests/reducer.test.js | 13 +++++++ 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/packages/strapi-plugin-content-manager/controllers/ContentTypes.js b/packages/strapi-plugin-content-manager/controllers/ContentTypes.js index 7a70faff81..4a2eaf795d 100644 --- a/packages/strapi-plugin-content-manager/controllers/ContentTypes.js +++ b/packages/strapi-plugin-content-manager/controllers/ContentTypes.js @@ -22,7 +22,6 @@ module.exports = { const contentTypes = Object.keys(strapi.contentTypes) .filter(uid => { if (uid.startsWith('strapi::')) return false; - if (uid === 'plugins::upload.file') return false; if (kind && _.get(strapi.contentTypes[uid], 'kind', 'collectionType') !== kind) { return false; diff --git a/packages/strapi-plugin-upload/admin/src/components/FiltersPicker/FiltersCard/index.js b/packages/strapi-plugin-upload/admin/src/components/FiltersPicker/FiltersCard/index.js index 1325cf46a3..dc842e8c58 100644 --- a/packages/strapi-plugin-upload/admin/src/components/FiltersPicker/FiltersCard/index.js +++ b/packages/strapi-plugin-upload/admin/src/components/FiltersPicker/FiltersCard/index.js @@ -1,24 +1,23 @@ -import React, { useReducer } from 'react'; +import React, { useReducer, useEffect } from 'react'; import PropTypes from 'prop-types'; import { FormattedMessage } from 'react-intl'; +import { useIsMounted } from '@buffetjs/hooks'; import { Select } from '@buffetjs/core'; -import { getFilterType } from 'strapi-helper-plugin'; +import { getFilterType, request } from 'strapi-helper-plugin'; import { getTrad } from '../../../utils'; import reducer, { initialState } from './reducer'; -import filtersForm from './utils/filtersForm'; - import Wrapper from './Wrapper'; import InputWrapper from './InputWrapper'; import FilterButton from './FilterButton'; import FilterInput from './FilterInput'; const FiltersCard = ({ onChange, filters }) => { + const isMounted = useIsMounted(); const [state, dispatch] = useReducer(reducer, initialState); - - const { name, filter, value } = state.toJS(); + const { name, filter, filtersForm, value } = state.toJS(); const type = filtersForm[name].type; const filtersOptions = getFilterType(type); @@ -36,7 +35,7 @@ const FiltersCard = ({ onChange, filters }) => { }; const addFilter = () => { - onChange({ target: { value: state.toJS() } }); + onChange({ target: { value: { name, filter, value } } }); dispatch({ type: 'RESET_FORM', @@ -61,6 +60,28 @@ const FiltersCard = ({ onChange, filters }) => { }); }; + const fetchTimestampNames = async () => { + try { + const result = await request('/content-manager/content-types/plugins::upload.file', { + method: 'GET', + }); + + dispatch({ + type: 'HANDLE_CUSTOM_TIMESTAMPS', + timestamps: result.data.contentType.schema.options.timestamps, + }); + } catch (err) { + if (isMounted) { + strapi.notification.error('notification.error'); + } + } + }; + + useEffect(() => { + fetchTimestampNames(); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + return ( diff --git a/packages/strapi-plugin-upload/admin/src/components/FiltersPicker/FiltersCard/reducer.js b/packages/strapi-plugin-upload/admin/src/components/FiltersPicker/FiltersCard/reducer.js index 5f9eea5cd9..b768705d2d 100644 --- a/packages/strapi-plugin-upload/admin/src/components/FiltersPicker/FiltersCard/reducer.js +++ b/packages/strapi-plugin-upload/admin/src/components/FiltersPicker/FiltersCard/reducer.js @@ -1,12 +1,13 @@ import { fromJS } from 'immutable'; import moment from 'moment'; -import filters from './utils/filtersForm'; +import filtersForm from './utils/filtersForm'; const initialState = fromJS({ name: 'created_at', filter: '=', value: moment(), + filtersForm, }); function reducer(state, action) { @@ -17,14 +18,34 @@ function reducer(state, action) { if (name === 'name') { return state .update(name, () => value) - .update('filter', () => filters[value].defaultFilter) - .update('value', () => defaultValue || filters[value].defaultValue); + .update('filter', () => state.getIn(['filtersForm', value, 'defaultFilter'])) + .update( + 'value', + () => defaultValue || state.getIn(['filtersForm', value, 'defaultValue']) + ); } return state.update(name, () => value); } + case 'HANDLE_CUSTOM_TIMESTAMPS': { + return state.set('name', action.timestamps[0]).updateIn(['filtersForm'], filtersFormMap => + filtersFormMap + .set(action.timestamps[1], filtersFormMap.get('updated_at')) + .set(action.timestamps[0], filtersFormMap.get('created_at')) + .delete('created_at') + .delete('updated_at') + ); + } case 'RESET_FORM': - return initialState; + return initialState + .set( + 'name', + state + .get('filtersForm') + .keySeq() + .first() + ) + .update('filtersForm', () => state.get('filtersForm')); default: return state; } diff --git a/packages/strapi-plugin-upload/admin/src/components/FiltersPicker/FiltersCard/tests/reducer.test.js b/packages/strapi-plugin-upload/admin/src/components/FiltersPicker/FiltersCard/tests/reducer.test.js index e8f860d895..584801ad80 100644 --- a/packages/strapi-plugin-upload/admin/src/components/FiltersPicker/FiltersCard/tests/reducer.test.js +++ b/packages/strapi-plugin-upload/admin/src/components/FiltersPicker/FiltersCard/tests/reducer.test.js @@ -30,6 +30,19 @@ describe('Upload | components | FiltersCard | reducer', () => { expect(actual).toEqual(expected); }); + it('should return the updated filters form with custom timestamps', () => { + const state = initialState; + + const action = { + type: 'HANDLE_CUSTOM_TIMESTAMPS', + timestamps: ['createdAtCustom', 'updatedAtCustom'], + }; + + const actualFiltersFormKeys = Object.keys(reducer(state, action).toJS().filtersForm); + const expectedFiltersFormKeys = ['createdAtCustom', 'updatedAtCustom', 'size', 'mime']; + + expect(actualFiltersFormKeys).toEqual(expectedFiltersFormKeys); + }); it('should return the initialState on reset', () => { const state = initialState.set('filter', '>');