Merge pull request #5679 from strapi/media-lib/custom-timestamps

Custom timestamps
This commit is contained in:
cyril lopez 2020-04-01 16:30:13 +02:00 committed by GitHub
commit 351fee255b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 12 deletions

View File

@ -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;

View File

@ -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 (
<Wrapper>
<InputWrapper>

View File

@ -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;
}

View File

@ -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', '>');