Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Resource Context #5456

Merged
merged 10 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions packages/ra-core/src/actions/resourcesActions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
export const REGISTER_RESOURCE = 'RA/REGISTER_RESOURCE';
import { ResourceDefinition } from '../types';

export interface ResourceDefinition {
readonly name: string;
readonly options?: any;
readonly hasList?: boolean;
readonly hasEdit?: boolean;
readonly hasShow?: boolean;
readonly hasCreate?: boolean;
readonly icon?: any;
}
export const REGISTER_RESOURCE = 'RA/REGISTER_RESOURCE';

export interface RegisterResourceAction {
readonly type: typeof REGISTER_RESOURCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../../sideEffect';
import { Record } from '../../types';
import { OnFailure, OnSuccess } from '../saveModifiers';
import { useResourceContext } from '../../core';

/**
* Prepare a set of callbacks for a delete button guarded by confirmation dialog
Expand Down Expand Up @@ -67,15 +68,18 @@ import { OnFailure, OnSuccess } from '../saveModifiers';
* );
* };
*/
const useDeleteWithConfirmController = ({
resource,
record,
redirect: redirectTo,
basePath,
onClick,
onSuccess,
onFailure,
}: UseDeleteWithConfirmControllerParams): UseDeleteWithConfirmControllerReturn => {
const useDeleteWithConfirmController = (
props: UseDeleteWithConfirmControllerParams
): UseDeleteWithConfirmControllerReturn => {
const {
record,
redirect: redirectTo,
basePath,
onClick,
onSuccess,
onFailure,
} = props;
const { resource } = useResourceContext(props);
const [open, setOpen] = useState(false);
const notify = useNotify();
const redirect = useRedirect();
Expand Down Expand Up @@ -137,7 +141,8 @@ export interface UseDeleteWithConfirmControllerParams {
basePath?: string;
record?: Record;
redirect?: RedirectionSideEffect;
resource: string;
// @deprecated. This hook get the resource from the context
resource?: string;
onClick?: ReactEventHandler<any>;
onSuccess?: OnSuccess;
onFailure?: OnFailure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../../sideEffect';
import { Record } from '../../types';
import { OnFailure, OnSuccess } from '../saveModifiers';
import { useResourceContext } from '../../core';

/**
* Prepare callback for a Delete button with undo support
Expand Down Expand Up @@ -47,15 +48,18 @@ import { OnFailure, OnSuccess } from '../saveModifiers';
* );
* };
*/
const useDeleteWithUndoController = ({
resource,
record,
basePath,
redirect: redirectTo = 'list',
onClick,
onSuccess,
onFailure,
}: UseDeleteWithUndoControllerParams): UseDeleteWithUndoControllerReturn => {
const useDeleteWithUndoController = (
props: UseDeleteWithUndoControllerParams
): UseDeleteWithUndoControllerReturn => {
const {
record,
basePath,
redirect: redirectTo = 'list',
onClick,
onSuccess,
onFailure,
} = props;
const { resource } = useResourceContext(props);
const notify = useNotify();
const redirect = useRedirect();
const refresh = useRefresh();
Expand Down Expand Up @@ -107,7 +111,8 @@ export interface UseDeleteWithUndoControllerParams {
basePath?: string;
record?: Record;
redirect?: RedirectionSideEffect;
resource: string;
// @deprecated. This hook get the resource from the context
resource?: string;
onClick?: ReactEventHandler<any>;
onSuccess?: OnSuccess;
onFailure?: OnFailure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useTranslate } from '../../i18n';
import useVersion from '../useVersion';
import { CRUD_CREATE } from '../../actions';
import { Record } from '../../types';
import { useResourceContext } from '../../core';

export interface CreateProps<RecordType extends Omit<Record, 'id'> = Record> {
basePath?: string;
Expand Down Expand Up @@ -101,16 +102,16 @@ export const useCreateController = <
useCheckMinimumRequiredProps('Create', ['basePath', 'resource'], props);
const {
basePath,
resource,
record = {},
hasShow,
hasEdit,
hasShow,
record = {},
successMessage,
onSuccess,
onFailure,
transform,
} = props;

const { resource } = useResourceContext(props);
const location = useLocation();
const translate = useTranslate();
const notify = useNotify();
Expand Down
3 changes: 2 additions & 1 deletion packages/ra-core/src/controller/details/useEditController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
SetTransformData,
useSaveModifiers,
} from '../saveModifiers';
import { useResourceContext } from '../../core';

export interface EditProps {
basePath?: string;
Expand Down Expand Up @@ -98,13 +99,13 @@ export const useEditController = <RecordType extends Record = Record>(
hasList,
hasShow,
id,
resource,
successMessage,
undoable = true,
onSuccess,
onFailure,
transform,
} = props;
const { resource } = useResourceContext(props);
const translate = useTranslate();
const notify = useNotify();
const redirect = useRedirect();
Expand Down
12 changes: 3 additions & 9 deletions packages/ra-core/src/controller/details/useShowController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useGetOne } from '../../dataProvider';
import { useTranslate } from '../../i18n';
import { useNotify, useRedirect, useRefresh } from '../../sideEffect';
import { CRUD_GET_ONE } from '../../actions';
import { useResourceContext } from '../../core';

export interface ShowProps {
basePath?: string;
Expand Down Expand Up @@ -57,15 +58,8 @@ export const useShowController = <RecordType extends Record = Record>(
props: ShowProps
): ShowControllerProps<RecordType> => {
useCheckMinimumRequiredProps('Show', ['basePath', 'resource'], props);
const {
basePath,
hasCreate,
hasEdit,
hasList,
hasShow,
id,
resource,
} = props;
const { basePath, hasCreate, hasEdit, hasList, hasShow, id } = props;
const { resource } = useResourceContext(props);
const translate = useTranslate();
const notify = useNotify();
const redirect = useRedirect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useNotify } from '../../sideEffect';
import usePaginationState from '../usePaginationState';
import useSelectionState from '../useSelectionState';
import useSortState from '../useSortState';
import { useResourceContext } from '../../core';

interface Option {
basePath: string;
Expand Down Expand Up @@ -49,17 +50,20 @@ const defaultSort = { field: null, order: null };
*
* @returns {ReferenceArrayProps} The reference props
*/
const useReferenceArrayFieldController = ({
basePath,
filter = defaultFilter,
page: initialPage = 1,
perPage: initialPerPage = 1000,
record,
reference,
resource,
sort: initialSort = defaultSort,
source,
}: Option): ListControllerProps => {
const useReferenceArrayFieldController = (
props: Option
): ListControllerProps => {
const {
basePath,
filter = defaultFilter,
page: initialPage = 1,
perPage: initialPerPage = 1000,
record,
reference,
sort: initialSort = defaultSort,
source,
} = props;
const { resource } = useResourceContext(props);
const notify = useNotify();
const ids = get(record, source) || [];
const { data, error, loading, loaded } = useGetMany(reference, ids, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ListControllerProps } from '../useListController';
import usePaginationState from '../usePaginationState';
import useSelectionState from '../useSelectionState';
import useSortState from '../useSortState';
import { useResourceContext } from '../../core';

interface Options {
basePath: string;
Expand Down Expand Up @@ -65,18 +66,21 @@ const defaultFilter = {};
*
* @returns {ReferenceManyProps} The reference many props
*/
const useReferenceManyFieldController = ({
resource,
reference,
record,
target,
filter = defaultFilter,
source,
basePath,
page: initialPage,
perPage: initialPerPage,
sort: initialSort = { field: 'id', order: 'DESC' },
}: Options): ListControllerProps => {
const useReferenceManyFieldController = (
props: Options
): ListControllerProps => {
const {
reference,
record,
target,
filter = defaultFilter,
source,
basePath,
page: initialPage,
perPage: initialPerPage,
sort: initialSort = { field: 'id', order: 'DESC' },
} = props;
const { resource } = useResourceContext(props);
const notify = useNotify();

// pagination logic
Expand Down
24 changes: 14 additions & 10 deletions packages/ra-core/src/controller/input/useGetMatchingReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback } from 'react';
import { useSelector, useDispatch } from 'react-redux';

import { crudGetMatchingAccumulate } from '../../actions/accumulateActions';
import { useResourceContext } from '../../core';
import {
getPossibleReferences,
getPossibleReferenceValues,
Expand Down Expand Up @@ -35,16 +36,19 @@ interface UseMatchingReferencesProps {
const defaultReferenceSource = (resource: string, source: string) =>
`${resource}@${source}`;

export default ({
reference,
referenceSource = defaultReferenceSource,
resource,
source,
filter,
pagination,
sort,
id,
}: UseMatchingReferencesOption): UseMatchingReferencesProps => {
export default (
props: UseMatchingReferencesOption
): UseMatchingReferencesProps => {
const {
reference,
referenceSource = defaultReferenceSource,
source,
filter,
pagination,
sort,
id,
} = props;
const { resource } = useResourceContext(props);
const dispatch = useDispatch();

useDeepCompareEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { FieldInputProps } from 'react-final-form';
import useGetMatching from '../../dataProvider/useGetMatching';
import { useTranslate } from '../../i18n';
import { getStatusForArrayInput as getDataStatus } from './referenceDataStatus';
import { useResourceContext } from '../../core';

/**
* Prepare data for the ReferenceArrayInput components
Expand All @@ -36,17 +37,20 @@ import { getStatusForArrayInput as getDataStatus } from './referenceDataStatus';
*
* @return {Object} controllerProps Fetched data and callbacks for the ReferenceArrayInput components
*/
const useReferenceArrayInputController = ({
filter: defaultFilter,
filterToQuery = defaultFilterToQuery,
input,
perPage = 25,
sort: defaultSort = { field: 'id', order: 'DESC' },
options,
reference,
resource,
source,
}: Option): ReferenceArrayInputProps => {
const useReferenceArrayInputController = (
props: Option
): ReferenceArrayInputProps => {
const {
filter: defaultFilter,
filterToQuery = defaultFilterToQuery,
input,
perPage = 25,
sort: defaultSort = { field: 'id', order: 'DESC' },
options,
reference,
source,
} = props;
const { resource } = useResourceContext(props);
const translate = useTranslate();

// We store the current input value in a ref so that we are able to fetch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import usePaginationState from '../usePaginationState';
import { useSortState } from '..';
import useFilterState from '../useFilterState';
import useSelectionState from '../useSelectionState';
import { useResourceContext } from '../../core';

const defaultReferenceSource = (resource: string, source: string) =>
`${resource}@${source}`;
Expand Down Expand Up @@ -59,20 +60,23 @@ const defaultFilter = {};
* filterToQuery: searchText => ({ title: searchText })
* });
*/
export const useReferenceInputController = ({
basePath,
input,
page: initialPage = 1,
perPage: initialPerPage = 25,
filter = defaultFilter,
reference,
filterToQuery,
// @deprecated
referenceSource = defaultReferenceSource,
resource,
sort: sortOverride,
source,
}: Option): ReferenceInputValue => {
export const useReferenceInputController = (
props: Option
): ReferenceInputValue => {
const {
basePath,
input,
page: initialPage = 1,
perPage: initialPerPage = 25,
filter = defaultFilter,
reference,
filterToQuery,
// @deprecated
referenceSource = defaultReferenceSource,
sort: sortOverride,
source,
} = props;
const { resource } = useResourceContext(props);
const translate = useTranslate();

// pagination logic
Expand Down
Loading