From 4bd429aebe49af644bc10dc75e98f9681d9edf5c Mon Sep 17 00:00:00 2001 From: Adrian Smijulj Date: Sun, 21 Oct 2018 20:11:38 +0200 Subject: [PATCH] feat: Implement multi select functionality in "withDataList" HOC (#244) --- .../src/components/withCrud.js | 11 +- .../src/views/ApiTokens/ApiTokensDataList.js | 4 +- .../views/Components/GroupsAutoComplete.js | 42 +- .../src/views/Components/RolesAutoComplete.js | 42 +- .../src/views/Groups/GroupsDataList.js | 4 +- .../src/views/Roles/RolesDataList.js | 4 +- .../src/views/Users/UsersDataList.js | 8 +- .../views/Categories/CategoriesDataList.js | 4 +- .../src/admin/views/Pages/Pages.js | 9 +- .../src/admin/views/Pages/PagesDataList.js | 5 - .../bkp/actions/crud/generateCreateQuery.js | 23 - .../bkp/actions/crud/generateDeleteQuery.js | 23 - .../bkp/actions/crud/generateListQuery.js | 42 - .../bkp/actions/crud/generateOneQuery.js | 27 - .../bkp/actions/crud/generateUpdateQuery.js | 23 - packages/webiny-app/bkp/actions/crud/index.js | 6 - .../bkp/actions/dataList.actions.js | 138 -- .../webiny-app/bkp/actions/form.actions.js | 143 -- .../webiny-app/bkp/actions/graphql.actions.js | 79 - packages/webiny-app/bkp/actions/index.js | 7 - .../webiny-app/bkp/actions/router.actions.js | 14 - .../bkp/actions/security.actions.js | 59 - .../webiny-app/bkp/actions/type.actions.js | 172 --- packages/webiny-app/bkp/components/Webiny.js | 56 - packages/webiny-app/bkp/components/index.js | 12 - .../webiny-app/bkp/components/withConfig.js | 19 - .../webiny-app/bkp/components/withDataList.js | 163 -- .../withDataListUtils/getPropName.js | 11 - .../bkp/components/withDataListUtils/index.js | 5 - .../prepareLoadListParams.js | 33 - .../prepareNewLoadListParams.js | 32 - .../redirectToRouteWithQueryParams.js | 25 - .../bkp/components/withDataListUtils/types.js | 40 - .../bkp/components/withFileUpload.js | 75 - .../withFileUpload/localStoragePlugin.js | 28 - .../bkp/components/withFileUpload/types.js | 15 - .../webinyCloudStoragePlugin.js | 45 - .../webiny-app/bkp/components/withForm.js | 78 - .../components/withFormUtils/getPropName.js | 10 - .../bkp/components/withFormUtils/index.js | 3 - .../withFormUtils/prepareLoadFormParams.js | 14 - .../bkp/components/withFormUtils/types.js | 8 - .../webiny-app/bkp/components/withGraphQL.js | 20 - .../bkp/components/withNewDataList.js | 157 -- .../webiny-app/bkp/components/withRouter.js | 38 - .../webiny-app/bkp/components/withSecurity.js | 20 - packages/webiny-app/bkp/graphql/Client.js | 96 -- packages/webiny-app/bkp/graphql/Error.js | 23 - packages/webiny-app/bkp/i18n/index.js | 2 - packages/webiny-app/bkp/index.js | 5 - packages/webiny-app/bkp/plugins/index.js | 28 - .../webiny-app/bkp/redux/createMiddleware.js | 18 - .../webiny-app/bkp/redux/createRootReducer.js | 83 -- packages/webiny-app/bkp/redux/index.js | 108 -- packages/webiny-app/bkp/router/Link.js | 92 -- packages/webiny-app/bkp/router/index.js | 12 - .../bkp/router/middleware/authentication.js | 40 - .../webiny-app/bkp/router/middleware/redux.js | 11 - packages/webiny-app/bkp/security/Security.js | 26 - .../webiny-app/bkp/security/SecurityError.js | 19 - .../webiny-app/bkp/security/Security_bkp.js | 167 --- .../webiny-app/bkp/security/createSecurity.js | 118 -- packages/webiny-app/bkp/security/index.js | 1 - packages/webiny-app/bkp/utils/document.js | 64 - packages/webiny-app/bkp/utils/i18n.js | 7 - .../src/components/withAppConfig.js | 6 +- .../webiny-app/src/components/withDataList.js | 87 +- .../withDataListUtils/getPropName.js | 2 +- .../src/components/withDataListUtils/types.js | 1 - packages/webiny-ui/index.js | 2 - packages/webiny-ui/package.json | 2 +- .../src/AutoComplete/AutoComplete.js | 101 +- packages/webiny-ui/src/Input/Input.js | 4 + packages/webiny-ui/src/Menu/Menu.js | 3 +- packages/webiny-ui/src/index.js | 18 - yarn.lock | 1316 ++++------------- 76 files changed, 461 insertions(+), 3797 deletions(-) delete mode 100644 packages/webiny-app/bkp/actions/crud/generateCreateQuery.js delete mode 100644 packages/webiny-app/bkp/actions/crud/generateDeleteQuery.js delete mode 100644 packages/webiny-app/bkp/actions/crud/generateListQuery.js delete mode 100644 packages/webiny-app/bkp/actions/crud/generateOneQuery.js delete mode 100644 packages/webiny-app/bkp/actions/crud/generateUpdateQuery.js delete mode 100644 packages/webiny-app/bkp/actions/crud/index.js delete mode 100644 packages/webiny-app/bkp/actions/dataList.actions.js delete mode 100644 packages/webiny-app/bkp/actions/form.actions.js delete mode 100644 packages/webiny-app/bkp/actions/graphql.actions.js delete mode 100644 packages/webiny-app/bkp/actions/index.js delete mode 100644 packages/webiny-app/bkp/actions/router.actions.js delete mode 100644 packages/webiny-app/bkp/actions/security.actions.js delete mode 100644 packages/webiny-app/bkp/actions/type.actions.js delete mode 100644 packages/webiny-app/bkp/components/Webiny.js delete mode 100644 packages/webiny-app/bkp/components/index.js delete mode 100644 packages/webiny-app/bkp/components/withConfig.js delete mode 100644 packages/webiny-app/bkp/components/withDataList.js delete mode 100644 packages/webiny-app/bkp/components/withDataListUtils/getPropName.js delete mode 100644 packages/webiny-app/bkp/components/withDataListUtils/index.js delete mode 100644 packages/webiny-app/bkp/components/withDataListUtils/prepareLoadListParams.js delete mode 100644 packages/webiny-app/bkp/components/withDataListUtils/prepareNewLoadListParams.js delete mode 100644 packages/webiny-app/bkp/components/withDataListUtils/redirectToRouteWithQueryParams.js delete mode 100644 packages/webiny-app/bkp/components/withDataListUtils/types.js delete mode 100644 packages/webiny-app/bkp/components/withFileUpload.js delete mode 100644 packages/webiny-app/bkp/components/withFileUpload/localStoragePlugin.js delete mode 100644 packages/webiny-app/bkp/components/withFileUpload/types.js delete mode 100644 packages/webiny-app/bkp/components/withFileUpload/webinyCloudStoragePlugin.js delete mode 100644 packages/webiny-app/bkp/components/withForm.js delete mode 100644 packages/webiny-app/bkp/components/withFormUtils/getPropName.js delete mode 100644 packages/webiny-app/bkp/components/withFormUtils/index.js delete mode 100644 packages/webiny-app/bkp/components/withFormUtils/prepareLoadFormParams.js delete mode 100644 packages/webiny-app/bkp/components/withFormUtils/types.js delete mode 100644 packages/webiny-app/bkp/components/withGraphQL.js delete mode 100644 packages/webiny-app/bkp/components/withNewDataList.js delete mode 100644 packages/webiny-app/bkp/components/withRouter.js delete mode 100644 packages/webiny-app/bkp/components/withSecurity.js delete mode 100644 packages/webiny-app/bkp/graphql/Client.js delete mode 100644 packages/webiny-app/bkp/graphql/Error.js delete mode 100644 packages/webiny-app/bkp/i18n/index.js delete mode 100644 packages/webiny-app/bkp/index.js delete mode 100644 packages/webiny-app/bkp/plugins/index.js delete mode 100644 packages/webiny-app/bkp/redux/createMiddleware.js delete mode 100644 packages/webiny-app/bkp/redux/createRootReducer.js delete mode 100644 packages/webiny-app/bkp/redux/index.js delete mode 100644 packages/webiny-app/bkp/router/Link.js delete mode 100644 packages/webiny-app/bkp/router/index.js delete mode 100644 packages/webiny-app/bkp/router/middleware/authentication.js delete mode 100644 packages/webiny-app/bkp/router/middleware/redux.js delete mode 100644 packages/webiny-app/bkp/security/Security.js delete mode 100644 packages/webiny-app/bkp/security/SecurityError.js delete mode 100644 packages/webiny-app/bkp/security/Security_bkp.js delete mode 100644 packages/webiny-app/bkp/security/createSecurity.js delete mode 100644 packages/webiny-app/bkp/security/index.js delete mode 100644 packages/webiny-app/bkp/utils/document.js delete mode 100644 packages/webiny-app/bkp/utils/i18n.js delete mode 100644 packages/webiny-ui/index.js delete mode 100644 packages/webiny-ui/src/index.js diff --git a/packages/webiny-app-admin/src/components/withCrud.js b/packages/webiny-app-admin/src/components/withCrud.js index aab5851f892..2763844f83d 100644 --- a/packages/webiny-app-admin/src/components/withCrud.js +++ b/packages/webiny-app-admin/src/components/withCrud.js @@ -2,7 +2,6 @@ import * as React from "react"; import { setDisplayName, compose, withProps, mapProps, withHandlers, withState } from "recompose"; import { graphql } from "react-apollo"; -import { omit } from "lodash"; import { withDataList, withRouter, type WithRouterProps } from "webiny-app/components"; import { withSnackbar, @@ -15,8 +14,6 @@ export type WithCrudBaseProps = WithRouterProps & WithSnackbarProps & WithDialog export type WithCrudListProps = WithCrudBaseProps & { dataList: Object, - data: Array, - meta: ?Object, deleteRecord: (item: Object) => Promise }; @@ -139,7 +136,8 @@ export const withCrud = ({ list, form }: Object): Function => { withDataList({ name: "dataList", query: list.get.query, - variables: list.get.variables + variables: list.get.variables, + response: list.get.response }), // Delete mutation list.delete && withDeleteHandler(list.delete), @@ -172,10 +170,10 @@ export const withCrud = ({ list, form }: Object): Function => { showSnackbar, showDialog, listProps: { - dataList: omit(dataList, ["data"]), + dataList, router, showSnackbar, - ...list.get.response(dataList.data), + showDialog, deleteRecord }, formProps: { @@ -184,6 +182,7 @@ export const withCrud = ({ list, form }: Object): Function => { onSubmit: saveRecord, router, showSnackbar, + showDialog, error: formError } }; diff --git a/packages/webiny-app-admin/src/views/ApiTokens/ApiTokensDataList.js b/packages/webiny-app-admin/src/views/ApiTokens/ApiTokensDataList.js index 84dd9f628c9..5303cbea179 100644 --- a/packages/webiny-app-admin/src/views/ApiTokens/ApiTokensDataList.js +++ b/packages/webiny-app-admin/src/views/ApiTokens/ApiTokensDataList.js @@ -15,12 +15,10 @@ import { DeleteIcon } from "webiny-ui/List/DataList/icons"; const t = i18n.namespace("Security.ApiTokensDataList"); -const ApiTokensDataList = ({ deleteRecord, dataList, data, meta, router }: WithCrudListProps) => { +const ApiTokensDataList = ({ deleteRecord, dataList, router }: WithCrudListProps) => { return ( { - const { groupsList, ...rest } = props; +const GroupsAutoComplete = (props: Object) => ( + + {({ query, renderOptions }) => ( + + {({ data }) => renderOptions(get(data, "security.groups.data", []))} + + )} + +); - return ( - { - query && groupsList.setSearch({ query, fields: ["name", "description"] }); - }, 250)} - /> - ); -}; - -export default compose( - withDataList({ - name: "groupsList", - query: groupsAutoComplete, - variables: { sort: { savedOn: -1 } } - }) -)(GroupsAutoComplete); +export default GroupsAutoComplete; diff --git a/packages/webiny-app-admin/src/views/Components/RolesAutoComplete.js b/packages/webiny-app-admin/src/views/Components/RolesAutoComplete.js index 12e785cc325..b1cdf2eb34f 100644 --- a/packages/webiny-app-admin/src/views/Components/RolesAutoComplete.js +++ b/packages/webiny-app-admin/src/views/Components/RolesAutoComplete.js @@ -1,30 +1,24 @@ // @flow import * as React from "react"; -import { withDataList } from "webiny-app/components"; import { AutoComplete } from "webiny-ui/AutoComplete"; import { rolesAutoComplete } from "./graphql"; -import { compose } from "recompose"; -import { get, debounce } from "lodash"; +import { get } from "lodash"; +import { Query } from "react-apollo"; -const RolesAutoComplete = props => { - const { rolesList, ...rest } = props; +const RolesAutoComplete = (props: Object) => ( + + {({ query, renderOptions }) => ( + + {({ data }) => renderOptions(get(data, "security.roles.data", []))} + + )} + +); - return ( - { - query && rolesList.setSearch({ query, fields: ["name", "description"] }); - }, 250)} - /> - ); -}; - -export default compose( - withDataList({ - name: "rolesList", - query: rolesAutoComplete, - variables: { sort: { savedOn: -1 } } - }) -)(RolesAutoComplete); +export default RolesAutoComplete; diff --git a/packages/webiny-app-admin/src/views/Groups/GroupsDataList.js b/packages/webiny-app-admin/src/views/Groups/GroupsDataList.js index c73f34764d6..e7bb827ab96 100644 --- a/packages/webiny-app-admin/src/views/Groups/GroupsDataList.js +++ b/packages/webiny-app-admin/src/views/Groups/GroupsDataList.js @@ -19,12 +19,10 @@ import { Checkbox } from "webiny-ui/Checkbox"; const t = i18n.namespace("Security.GroupsDataList"); -const GroupsDataList = ({ dataList, router, data, meta, deleteRecord }: WithCrudListProps) => { +const GroupsDataList = ({ dataList, router, deleteRecord }: WithCrudListProps) => { return ( { +const RolesDataList = ({ dataList, router, deleteRecord }: WithCrudListProps) => { return ( { +const UsersDataList = ({ dataList, router, security, deleteRecord }: Props) => { return ( diff --git a/packages/webiny-app-cms/src/admin/views/Categories/CategoriesDataList.js b/packages/webiny-app-cms/src/admin/views/Categories/CategoriesDataList.js index 2eaf4ec4ca9..bf5d505c186 100644 --- a/packages/webiny-app-cms/src/admin/views/Categories/CategoriesDataList.js +++ b/packages/webiny-app-cms/src/admin/views/Categories/CategoriesDataList.js @@ -16,12 +16,10 @@ import { const t = i18n.namespace("Cms.CategoriesDataList"); -const CategoriesDataList = ({ data, dataList, meta, router, deleteRecord }: WithCrudListProps) => { +const CategoriesDataList = ({ dataList, router, deleteRecord }: WithCrudListProps) => { return ( Promise, @@ -45,10 +46,10 @@ class Pages extends React.Component { /> - + - + @@ -62,8 +63,10 @@ export default compose( withRouter(), graphql(createPage, { name: "createMutation" }), withDataList({ - name: "dataList", query: loadPages, + response: data => { + return get(data, "cms.pages", {}); + }, variables: { sort: { savedOn: -1 } } diff --git a/packages/webiny-app-cms/src/admin/views/Pages/PagesDataList.js b/packages/webiny-app-cms/src/admin/views/Pages/PagesDataList.js index a320b99108b..a8dedc2a798 100644 --- a/packages/webiny-app-cms/src/admin/views/Pages/PagesDataList.js +++ b/packages/webiny-app-cms/src/admin/views/Pages/PagesDataList.js @@ -1,6 +1,5 @@ // @flow import * as React from "react"; -import { get } from "dot-prop-immutable"; import TimeAgo from "timeago-react"; import { withRouter } from "webiny-app/components"; import { i18n } from "webiny-app/i18n"; @@ -18,13 +17,9 @@ const t = i18n.namespace("Cms.PagesDataList"); const PagesDataList = props => { const { dataList, router } = props; - const { data, meta } = get(dataList, "data.cms.pages") || { data: [], meta: {} }; - return ( { - let query = JSON.stringify(_.set({}, params.type, " { {fields} }")) - .replace(/"/g, " ") - .replace(/:/g, " ") - .replace("{fields}", `create(data: $data) { ${params.fields} }`); - - query = `query createType($data: JSON!) ${query}`; - - return gql` - ${query} - `; -}; - -export default generateCreateQuery; diff --git a/packages/webiny-app/bkp/actions/crud/generateDeleteQuery.js b/packages/webiny-app/bkp/actions/crud/generateDeleteQuery.js deleted file mode 100644 index 28861b8aa86..00000000000 --- a/packages/webiny-app/bkp/actions/crud/generateDeleteQuery.js +++ /dev/null @@ -1,23 +0,0 @@ -// @flow -import gql from "graphql-tag"; -import _ from "lodash"; - -type DeleteParams = { - type: string, - fields: string -}; - -const generateListQuery = (params: DeleteParams) => { - let query = JSON.stringify(_.set({}, params.type, " { {fields} }")) - .replace(/"/g, " ") - .replace(/:/g, " ") - .replace("{fields}", `delete(id: $id)`); - - query = `query deleteType ($id: String!) ${query}`; - - return gql` - ${query} - `; -}; - -export default generateListQuery; diff --git a/packages/webiny-app/bkp/actions/crud/generateListQuery.js b/packages/webiny-app/bkp/actions/crud/generateListQuery.js deleted file mode 100644 index 17126988d77..00000000000 --- a/packages/webiny-app/bkp/actions/crud/generateListQuery.js +++ /dev/null @@ -1,42 +0,0 @@ -// @flow -import gql from "graphql-tag"; -import _ from "lodash"; - -type ListQueryParams = { - type: string, - fields: string -}; - -const generateListQuery = (params: ListQueryParams) => { - let query = ` - list(where: $where, sort: $sort, page: $page, perPage: $perPage, search: $search) { - data { - ${params.fields} - } - meta { - count - totalCount - from - to - page - totalPages - perPage - nextPage - previousPage - } - } - `; - - query = JSON.stringify(_.set({}, params.type, " { {fields} }")) - .replace(/"/g, " ") - .replace(/:/g, " ") - .replace("{fields}", query); - - query = `query typeList($where: JSON, $sort: JSON, $page: Int, $perPage: Int, $search: SearchInput) ${query}`; - - return gql` - ${query} - `; -}; - -export default generateListQuery; diff --git a/packages/webiny-app/bkp/actions/crud/generateOneQuery.js b/packages/webiny-app/bkp/actions/crud/generateOneQuery.js deleted file mode 100644 index 45ababc391f..00000000000 --- a/packages/webiny-app/bkp/actions/crud/generateOneQuery.js +++ /dev/null @@ -1,27 +0,0 @@ -// @flow -import gql from "graphql-tag"; -import _ from "lodash"; - -type FindOneParams = { - type: string, - fields: string -}; -const generateListQuery = (params: FindOneParams) => { - let query = ` - one(id: $id) { - ${params.fields} - } - `; - - query = JSON.stringify(_.set({}, params.type, " { {fields} }")) - .replace(/"/g, " ") - .replace(/:/g, " ") - .replace("{fields}", query); - - query = `query one($id: String!) ${query}`; - return gql` - ${query} - `; -}; - -export default generateListQuery; diff --git a/packages/webiny-app/bkp/actions/crud/generateUpdateQuery.js b/packages/webiny-app/bkp/actions/crud/generateUpdateQuery.js deleted file mode 100644 index 33dd4304bf8..00000000000 --- a/packages/webiny-app/bkp/actions/crud/generateUpdateQuery.js +++ /dev/null @@ -1,23 +0,0 @@ -// @flow -import gql from "graphql-tag"; -import _ from "lodash"; - -type UpdateParams = { - type: string, - fields: string -}; - -const generateUpdateQuery = (params: UpdateParams) => { - let query = JSON.stringify(_.set({}, params.type, " { {fields} }")) - .replace(/"/g, " ") - .replace(/:/g, " ") - .replace("{fields}", `update(id: $id, data: $data) { ${params.fields} }`); - - query = `query updateType($id: String!, $data: JSON!) ${query}`; - - return gql` - ${query} - `; -}; - -export default generateUpdateQuery; diff --git a/packages/webiny-app/bkp/actions/crud/index.js b/packages/webiny-app/bkp/actions/crud/index.js deleted file mode 100644 index 77419e87b87..00000000000 --- a/packages/webiny-app/bkp/actions/crud/index.js +++ /dev/null @@ -1,6 +0,0 @@ -// @flow -export { default as generateCreateQuery } from "./generateCreateQuery"; -export { default as generateDeleteQuery } from "./generateDeleteQuery"; -export { default as generateListQuery } from "./generateListQuery"; -export { default as generateOneQuery } from "./generateOneQuery"; -export { default as generateUpdateQuery } from "./generateUpdateQuery"; diff --git a/packages/webiny-app/bkp/actions/dataList.actions.js b/packages/webiny-app/bkp/actions/dataList.actions.js deleted file mode 100644 index 5f1cb7e4636..00000000000 --- a/packages/webiny-app/bkp/actions/dataList.actions.js +++ /dev/null @@ -1,138 +0,0 @@ -// @flow -import { createAction, addReducer, addMiddleware } from "./../redux"; -import { typeList } from "."; -import type { StatePath } from "webiny-app/types"; - -const PREFIX = "[LIST]"; - -export const LIST_SET_LOADING = `${PREFIX} Set Loading`; -export const LIST_LOAD = `${PREFIX} Load`; -export const LIST_REFRESH = `${PREFIX} Refresh`; -export const LIST_LOAD_SUCCESS = `${PREFIX} List Success`; -export const LIST_LOAD_ERROR = `${PREFIX} List Error`; -export const LIST_MULTISELECT = `${PREFIX} Multi Select`; - -export const listSlice: StatePath = action => { - return "lists." + action.payload.name; -}; - -export const setDataListLoading = createAction(LIST_SET_LOADING); - -addReducer([LIST_SET_LOADING], listSlice, (state, action) => { - return { ...state, loading: action.payload.loading }; -}); - -export const loadDataList = createAction(LIST_LOAD); - -addMiddleware([LIST_LOAD], ({ store, action, next }) => { - next(action); - - const { name, type, fields, page, perPage, sort, search, where } = action.payload; - store.dispatch(setDataListLoading({ name, loading: true })); - - store.dispatch( - typeList({ - type, - fields, - page, - perPage, - sort, - search, - where, - onSuccess: data => { - store.dispatch(setDataListLoading({ name, loading: false })); - store.dispatch( - loadDataListSuccess({ - data, - params: { - type, - fields, - page, - perPage, - sort, - search, - where - }, - name - }) - ); - }, - onError: error => { - store.dispatch(setDataListLoading({ name, loading: false })); - store.dispatch(loadDataListError({ error, name })); - } - }) - ); -}); - -export const loadDataListSuccess = createAction(LIST_LOAD_SUCCESS); - -addReducer([LIST_LOAD_SUCCESS], "lists", (state, action) => { - const { data, params, name } = action.payload; - return { - ...state, - [name]: { - params, - data, - error: null - } - }; -}); - -export const loadDataListError = createAction(LIST_LOAD_ERROR); - -addReducer([LIST_LOAD_ERROR], "lists", (state, action) => { - const { error, name } = action.payload; - return { - ...state, - [name]: { - error, - data: null - } - }; -}); - -export const refreshDataList = createAction(LIST_REFRESH); - -addMiddleware([LIST_REFRESH], ({ store, action, next }) => { - next(action); - - const { name } = action.payload; - const { lists } = store.getState(); - if (!lists || !lists[name]) { - return; - } - - store.dispatch(loadDataList({ name, ...lists[name].params })); -}); - -export const multiSelect = createAction(LIST_MULTISELECT); - -addReducer([LIST_MULTISELECT], listSlice, (state, action) => { - let { item: items, value } = action.payload; - if (!Array.isArray(items)) { - items = [items]; - } - - let multiSelectedItems = []; - if (Array.isArray(state.multiSelectedItems)) { - multiSelectedItems = [...state.multiSelectedItems]; - } - - items.forEach(item => { - if (value === undefined) { - multiSelectedItems.includes(item) - ? multiSelectedItems.splice(multiSelectedItems.indexOf(item), 1) - : multiSelectedItems.push(item); - } else { - if (value === true) { - !multiSelectedItems.includes(item) && multiSelectedItems.push(item); - } else { - multiSelectedItems.includes(item) && - multiSelectedItems.splice(multiSelectedItems.indexOf(item), 1); - } - } - }); - - return { ...state, multiSelectedItems }; -}); diff --git a/packages/webiny-app/bkp/actions/form.actions.js b/packages/webiny-app/bkp/actions/form.actions.js deleted file mode 100644 index e1e874a300e..00000000000 --- a/packages/webiny-app/bkp/actions/form.actions.js +++ /dev/null @@ -1,143 +0,0 @@ -// @flow -import { createAction, addReducer, addMiddleware } from "./../redux"; -import { typeOne, typeSave } from "."; -import type { StatePath } from "webiny-app/types"; - -const PREFIX = "[FORM]"; - -export const FORM_SET_LOADING = `${PREFIX} Set Loading`; -export const FORM_RESET = `${PREFIX} Reset`; -export const FORM_EMPTY = `${PREFIX} Empty`; -export const FORM_LOAD = `${PREFIX} Load`; -export const FORM_EMPTY_DATA = `${PREFIX} Empty Data`; -export const FORM_SET_DATA = `${PREFIX} Set Data`; -export const FORM_LOAD_SUCCESS = `${PREFIX} Form Load Success`; -export const FORM_LOAD_ERROR = `${PREFIX} Form Load Error`; -export const FORM_SUBMIT = `${PREFIX} Submit`; -export const FORM_SUBMIT_SUCCESS = `${PREFIX} Form Submit Success`; -export const FORM_SUBMIT_ERROR = `${PREFIX} Form Submit Error`; - -export const formSlice: StatePath = (action): string => { - return "forms." + action.payload.name; -}; - -export const setFormLoading = createAction(FORM_SET_LOADING); - -addReducer([FORM_SET_LOADING], formSlice, (state = {}, action) => { - const { loading } = action.payload; - state.loading = loading; - return state; -}); - -/** - * Ideally this would just set the initial state of the form. That means, if a record was loaded (eg. "policies/{id}"), - * hitting reset would just revert any changes. Otherwise, all data would be emptied. - */ -export const resetForm = createAction(FORM_RESET); -export const emptyForm = createAction(FORM_EMPTY); - -addReducer([FORM_RESET, FORM_EMPTY], formSlice, () => { - return {}; -}); - -export const loadForm = createAction(FORM_LOAD); - -addMiddleware([FORM_LOAD], ({ store, action, next }) => { - next(action); - const { name, type, fields, id, onSuccess, onError } = action.payload; - store.dispatch(setFormLoading({ name, loading: true })); - - store.dispatch( - typeOne({ - type, - fields, - variables: { name, type, fields, id }, - onSuccess: data => { - store.dispatch(setFormLoading({ name, loading: false })); - store.dispatch(loadFormSuccess({ data, name })); - typeof onSuccess === "function" && onSuccess({ data, name }); - }, - onError: error => { - store.dispatch(setFormLoading({ name, loading: false })); - store.dispatch(loadFormError({ error, name })); - typeof onError === "function" && onError({ error, name }); - } - }) - ); -}); - -export const emptyFormData = createAction(FORM_EMPTY_DATA); - -addReducer([FORM_EMPTY_DATA], formSlice, state => { - return { ...state, data: null }; -}); - -export const setFormData = createAction(FORM_SET_DATA); - -addReducer([FORM_SET_DATA], formSlice, (state, action) => { - return { ...state, data: action.payload.data }; -}); - -export const loadFormSuccess = createAction(FORM_LOAD_SUCCESS); - -addReducer([FORM_LOAD_SUCCESS], formSlice, (state, action) => { - const { data } = action.payload; - return { - data, - error: null - }; -}); - -export const loadFormError = createAction(FORM_LOAD_ERROR); - -addReducer([FORM_LOAD_ERROR], formSlice, (state, action) => { - const { error } = action.payload; - return { - data: null, - error - }; -}); - -export const submitForm = createAction(FORM_SUBMIT); - -addMiddleware([FORM_SUBMIT], ({ store, action, next }) => { - next(action); - const { name, onSuccess, onError } = action.payload; - store.dispatch(setFormLoading({ name, loading: true })); - - store.dispatch( - typeSave({ - ...action.payload, - onSuccess: data => { - store.dispatch(submitFormSuccess({ data, name })); - typeof onSuccess === "function" && onSuccess(data); - store.dispatch(setFormLoading({ name, loading: false })); - }, - onError: error => { - store.dispatch(submitFormError({ error, name })); - typeof onError === "function" && onError(error); - store.dispatch(setFormLoading({ name, loading: false })); - } - }) - ); -}); - -export const submitFormSuccess = createAction(FORM_SUBMIT_SUCCESS); - -addReducer([FORM_SUBMIT_SUCCESS], formSlice, (state, action) => { - const { data } = action.payload; - return { - data, - error: null - }; -}); - -export const submitFormError = createAction(FORM_SUBMIT_ERROR); - -addReducer([FORM_SUBMIT_ERROR], formSlice, (state, action) => { - const { error } = action.payload; - return { - error, - data: null - }; -}); diff --git a/packages/webiny-app/bkp/actions/graphql.actions.js b/packages/webiny-app/bkp/actions/graphql.actions.js deleted file mode 100644 index 5e7aad90f1c..00000000000 --- a/packages/webiny-app/bkp/actions/graphql.actions.js +++ /dev/null @@ -1,79 +0,0 @@ -// @flow -import { createAction, addReducer, addMiddleware } from "./../redux"; -import { app } from "webiny-app"; -import GraphQLError from "./../graphql/Error"; - -const generateApi = (promise, dataKey) => { - return promise - .then(({ data }) => { - console.log(data); - return data[dataKey]; - }) - .catch(error => { - throw GraphQLError.from(error); - }); -}; - -const PREFIX = "[GRAPHQL]"; - -export const GRAPHQL_MUTATION = `${PREFIX} Mutation`; -export const GRAPHQL_QUERY = `${PREFIX} Query`; -export const GRAPHQL_START = `${PREFIX} Start`; -export const GRAPHQL_FINISH = `${PREFIX} Finish`; -export const GRAPHQL_SUCCESS = `${PREFIX} Success`; -export const GRAPHQL_ERROR = `${PREFIX} Error`; - -export const graphqlSuccess = createAction(GRAPHQL_SUCCESS); -export const graphqlError = createAction(GRAPHQL_ERROR); - -export const graphqlStart = createAction(GRAPHQL_START); - -addReducer([GRAPHQL_START], "graphql.activeOperationsCount", (state = 0) => { - return Number(state) + 1; -}); - -export const graphqlFinish = createAction(GRAPHQL_FINISH); - -addReducer([GRAPHQL_FINISH], "graphql.activeOperationsCount", (state = 0) => { - return Number(state) - 1; -}); - -export const graphqlQuery = createAction(GRAPHQL_QUERY); - -addMiddleware([GRAPHQL_QUERY], async ({ store, action, next }) => { - next(action); - - store.dispatch(graphqlStart()); - const { query, variables, onSuccess } = action.payload; - const response = await app.graphql.query({ query, variables }); - - store.dispatch(graphqlFinish()); - store.dispatch(graphqlSuccess(response)); - if (onSuccess) { - onSuccess(response); - } -}); - -export const graphqlMutation = createAction(GRAPHQL_MUTATION); - -addMiddleware([GRAPHQL_MUTATION], ({ store, action, next }) => { - next(action); - - store.dispatch(graphqlStart()); - const { mutation, variables, methodName, onSuccess, onError } = action.payload; - generateApi(app.graphql.mutate({ mutation, variables }), methodName) - .then(data => { - store.dispatch(graphqlFinish()); - store.dispatch(graphqlSuccess(data)); - if (onSuccess) { - onSuccess({ data }); - } - }) - .catch(error => { - store.dispatch(graphqlFinish()); - store.dispatch(graphqlError(error)); - if (onError) { - onError({ error }); - } - }); -}); diff --git a/packages/webiny-app/bkp/actions/index.js b/packages/webiny-app/bkp/actions/index.js deleted file mode 100644 index 071908a4b9c..00000000000 --- a/packages/webiny-app/bkp/actions/index.js +++ /dev/null @@ -1,7 +0,0 @@ -// @flow -export * from "./graphql.actions"; -//export * from "./security.actions"; -export * from "./type.actions"; -export * from "./dataList.actions"; -export * from "./form.actions"; -export * from "./router.actions"; diff --git a/packages/webiny-app/bkp/actions/router.actions.js b/packages/webiny-app/bkp/actions/router.actions.js deleted file mode 100644 index 12ae8301a3e..00000000000 --- a/packages/webiny-app/bkp/actions/router.actions.js +++ /dev/null @@ -1,14 +0,0 @@ -// @flow -import { createAction, addReducer } from "./../redux"; - -const PREFIX = "[ROUTER]"; - -export const ROUTER_ROUTE_CHANGED = `${PREFIX} Route changed`; - -const routeChanged = createAction(ROUTER_ROUTE_CHANGED); - -addReducer([ROUTER_ROUTE_CHANGED], "ui.route", (state, action) => { - return action.payload; -}); - -export { routeChanged }; diff --git a/packages/webiny-app/bkp/actions/security.actions.js b/packages/webiny-app/bkp/actions/security.actions.js deleted file mode 100644 index 3821257858a..00000000000 --- a/packages/webiny-app/bkp/actions/security.actions.js +++ /dev/null @@ -1,59 +0,0 @@ -// @flow -import { createAction, addReducer, addMiddleware } from "./../redux"; -import { app } from "./.."; -const SECURITY = "[SECURITY]"; - -export const AUTH = `${SECURITY} Authenticate`; -export const AUTH_SUCCESS = `${SECURITY} Authentication successful`; -export const AUTH_ERROR = `${SECURITY} Authentication failed`; -export const LOGOUT = `${SECURITY} Logout`; - -export const authenticationSuccess = createAction(AUTH_SUCCESS); - -addReducer([AUTH_SUCCESS], "security.authentication", (state, action) => { - return { - error: null, - user: action.payload, - inProgress: false - }; -}); - -export const authenticationError = createAction(AUTH_ERROR); - -addReducer([AUTH_ERROR], "security.authentication", (state, action) => { - return { - error: action.payload, - user: null, - inProgress: false - }; -}); - -export const authenticate = createAction(AUTH); - -addReducer([AUTH], "security.authentication.inProgress", () => true); -addMiddleware([AUTH], async ({ store, action, next }) => { - next(action); - - const security = app.security; - try { - const { identity, strategy, ...authenticationPayload } = action.payload; - const result = await security.login(identity, strategy, authenticationPayload); - - if (result.token) { - store.dispatch(authenticationSuccess(result)); - } else { - throw Error("Token not received."); - } - } catch (e) { - store.dispatch(authenticationError(e)); - } -}); - -export const logout = createAction(LOGOUT); - -addReducer([LOGOUT], "security.authentication.user", () => null); -addMiddleware([LOGOUT], async ({ action, next }) => { - next(action); - - await app.security.logout(); -}); diff --git a/packages/webiny-app/bkp/actions/type.actions.js b/packages/webiny-app/bkp/actions/type.actions.js deleted file mode 100644 index d1fca284a83..00000000000 --- a/packages/webiny-app/bkp/actions/type.actions.js +++ /dev/null @@ -1,172 +0,0 @@ -// @flow -import { createAction, addMiddleware } from "./../redux"; -import { graphqlQuery } from "."; -import { - generateListQuery, - generateOneQuery, - generateUpdateQuery, - generateCreateQuery, - generateDeleteQuery -} from "./crud"; - -import _ from "lodash"; - -const PREFIX = "[CRUD]"; - -export const TYPE_LIST = `${PREFIX} List`; -export const TYPE_ONE = `${PREFIX} Find`; -export const TYPE_SAVE = `${PREFIX} Save`; -export const TYPE_CREATE = `${PREFIX} Create`; -export const TYPE_UPDATE = `${PREFIX} Update`; -export const TYPE_DELETE = `${PREFIX} Delete`; - -const typeList = createAction(TYPE_LIST); -addMiddleware([TYPE_LIST], ({ store, action, next }) => { - next(action); - - const { type, fields, page, perPage, search, sort, where, onSuccess, onError } = action.payload; - - const query = generateListQuery({ type, fields }); - - store.dispatch( - graphqlQuery({ - query, - variables: { page, perPage, sort, where, search }, - onSuccess: response => { - const data = _.get(response, ["data", type, "list"].join(".")); - if (typeof onSuccess === "function") { - onSuccess(data); - } - }, - onError: error => { - if (typeof onError === "function") { - onError({ ...error }); - } - } - }) - ); -}); - -const typeOne = createAction(TYPE_ONE); -addMiddleware([TYPE_ONE], ({ store, action, next }) => { - next(action); - - const { type, fields, variables, onSuccess, onError } = action.payload; - const query = generateOneQuery({ type, fields }); - store.dispatch( - graphqlQuery({ - query, - variables, - onSuccess: response => { - const data = _.get(response, ["data", type, "one"].join(".")); - if (typeof onSuccess === "function") { - onSuccess(data); - } - }, - onError: error => { - if (typeof onError === "function") { - onError({ ...error }); - } - } - }) - ); -}); - -export type TypeSave = (payload: { - type: string, - fields: string, - data: Object, - onSuccess?: Function, - onError?: Function, -}) => Object; - -const typeSave: TypeSave = createAction(TYPE_SAVE); - -addMiddleware([TYPE_SAVE], ({ store, action, next }) => { - next(action); - if (action.payload.data.id) { - store.dispatch(typeUpdate(action.payload)); - } else { - store.dispatch(typeCreate(action.payload)); - } -}); - -export type TypeCreate = TypeSave; - -const typeCreate: TypeCreate = createAction(TYPE_CREATE); -addMiddleware([TYPE_CREATE], ({ store, action, next }) => { - next(action); - const { type, fields, onSuccess, onError, data } = action.payload; - const query = generateCreateQuery({ type, fields }); - - store.dispatch( - graphqlQuery({ - query, - variables: { data, id: data.id }, - onSuccess: response => { - const data = _.get(response, ["data", type, "create"].join(".")); - if (typeof onSuccess === "function") { - onSuccess(data); - } - }, - onError: error => { - if (typeof onError === "function") { - onError({ ...error }); - } - } - }) - ); -}); - -export type TypeUpdate = TypeSave; - -const typeUpdate: TypeUpdate = createAction(TYPE_UPDATE); -addMiddleware([TYPE_UPDATE], ({ store, action, next }) => { - next(action); - const { type, fields, onSuccess, onError, data } = action.payload; - const query = generateUpdateQuery({ type, fields }); - - store.dispatch( - graphqlQuery({ - query, - variables: { data, id: data.id }, - onSuccess: response => { - const data = _.get(response, ["data", type, "update"].join(".")); - if (typeof onSuccess === "function") { - onSuccess(data); - } - }, - onError: error => { - if (typeof onError === "function") { - onError({ ...error }); - } - } - }) - ); -}); - -const typeDelete = createAction(TYPE_DELETE); -addMiddleware([TYPE_DELETE], ({ store, action, next }) => { - next(action); - const { type, fields, onSuccess, onError, id } = action.payload; - const query = generateDeleteQuery({ type, fields }); - - store.dispatch( - graphqlQuery({ - query, - variables: { id }, - onSuccess: data => { - if (typeof onSuccess === "function") { - onSuccess({ ...data.data }); - } - }, - onError: error => { - if (typeof onError === "function") { - onError({ ...error }); - } - } - }) - ); -}); - -export { typeList, typeOne, typeCreate, typeUpdate, typeSave, typeDelete }; diff --git a/packages/webiny-app/bkp/components/Webiny.js b/packages/webiny-app/bkp/components/Webiny.js deleted file mode 100644 index 7a5fa74a4a3..00000000000 --- a/packages/webiny-app/bkp/components/Webiny.js +++ /dev/null @@ -1,56 +0,0 @@ -// @flow -import React from "react"; -import { ApolloProvider } from "react-apollo"; -import { Provider as ReduxProvider } from "react-redux"; -import type ApolloClient from "apollo-client"; -import { redux } from "../redux"; -import { router } from "../router"; -import createSecurity from "../security/createSecurity"; -import { SecurityProvider } from "../security"; -import { getPlugins } from "../plugins"; -import type { Security } from "webiny-app/types"; - -type WebinyProps = { config: Object, children: Function }; - -const { Provider, Consumer } = React.createContext(); - -export const ConfigProvider = ({ config, children }: Object) => { - return {children}; -}; - -export const ConfigConsumer = ({ children }: Object) => ( - {config => React.cloneElement(children, { config })} -); - -export const app: { graphql: ApolloClient } = { - graphql: null -}; - -export let apolloClient: ApolloClient; -export let security: Security; - -const Webiny = ({ config, children }: WebinyProps) => { - // Temporary monkey patch - app.graphql = config.apolloClient; - apolloClient = config.apolloClient; - security = createSecurity(config.security); - // Setup router - router.configure(config.router); - getPlugins("route").forEach((pl: Object) => { - router.addRoute(pl.route); - }); - - const store = redux.initStore(); - - return ( - - - - {children({ router, config })} - - - - ); -}; - -export default Webiny; diff --git a/packages/webiny-app/bkp/components/index.js b/packages/webiny-app/bkp/components/index.js deleted file mode 100644 index cb805bde19b..00000000000 --- a/packages/webiny-app/bkp/components/index.js +++ /dev/null @@ -1,12 +0,0 @@ -// @flow -export { default as withForm } from "./withForm"; -export { default as withGraphQL } from "./withGraphQL"; -export * from "./withRouter"; - -export { withDataList } from "./withDataList"; -export { withNewDataList } from "./withNewDataList"; -export { withFileUpload } from "./withFileUpload"; -export { withSecurity } from "./withSecurity"; -export { withConfig } from "./withConfig"; -export type { WithSecurityProps } from "./withSecurity"; -export type { WithDataListProps, SearchParams, WithDataListParams } from "./withDataList"; diff --git a/packages/webiny-app/bkp/components/withConfig.js b/packages/webiny-app/bkp/components/withConfig.js deleted file mode 100644 index 94a71657e2b..00000000000 --- a/packages/webiny-app/bkp/components/withConfig.js +++ /dev/null @@ -1,19 +0,0 @@ -// @flow -import * as React from "react"; -import { ConfigConsumer } from "./Webiny"; - -export type WithConfigProps = { - config: Object -}; - -export const withConfig = (): Function => { - return (Component: typeof React.Component) => { - return props => { - return ( - - - - ); - }; - }; -}; diff --git a/packages/webiny-app/bkp/components/withDataList.js b/packages/webiny-app/bkp/components/withDataList.js deleted file mode 100644 index 7e2fd962eb1..00000000000 --- a/packages/webiny-app/bkp/components/withDataList.js +++ /dev/null @@ -1,163 +0,0 @@ -// @flow -import * as React from "react"; -import { connect } from "react-redux"; -import { compose, lifecycle } from "recompose"; -import { loadDataList, typeDelete, multiSelect } from "./../actions"; -import _ from "lodash"; - -import type { - WithDataListParams, - SearchParams, - WithDataListProps -} from "./withDataListUtils/types"; - -import { - getPropName, - prepareLoadListParams, - redirectToRouteWithQueryParams -} from "./withDataListUtils"; - -export type { WithDataListProps, SearchParams, WithDataListParams }; - -// For returning the same instance in connect function (returnProps). -const emptyArray = []; -const emptyObject = {}; - -export const withDataList = (withDataListParams: WithDataListParams): Function => { - const propName = getPropName(withDataListParams); - - return (BaseComponent: typeof React.Component) => { - return compose( - connect( - state => ({ - dataListState: _.get(state, `lists.${withDataListParams.name}`) - }), - { loadList: loadDataList, typeDelete, multiSelect }, - (stateProps, dispatchProps, ownProps) => { - const returnProps = Object.assign({}, ownProps); - const { router } = ownProps; - - const { dataListState } = stateProps; - - const dataListProps: WithDataListProps = { - ...dataListState, - meta: _.get(dataListState, "data.meta", emptyObject), - data: _.get(dataListState, "data.data", emptyArray), - init(): void { - if (dataListProps.__loadParams.load === false) { - return; - } - this.refresh(); - }, - refresh(params): void { - if (!params) { - dispatchProps.loadList(dataListProps.__loadParams); - return; - } - - if (router) { - redirectToRouteWithQueryParams(params, router); - } else { - dispatchProps.loadList(params); - } - }, - delete(id, options = {}): void { - const { type, fields, name } = withDataListParams; - dispatchProps.typeDelete({ - type, - fields, - name, - id, - onSuccess: options.onSuccess || dataListProps.refresh - }); - }, - setPerPage(perPage: number): void { - const preparedParams = { - ...dataListProps.__loadParams, - perPage: perPage - }; - this.refresh(preparedParams); - }, - setPage(page: number): void { - const preparedParams = { ...dataListProps.__loadParams, page: page }; - this.refresh(preparedParams); - }, - setSearch(search: SearchParams): void { - const preparedParams = { ...dataListProps.__loadParams, search }; - this.refresh(preparedParams); - }, - setWhere(where: Object): void { - const preparedParams = { ...dataListProps.__loadParams, where }; - this.refresh(preparedParams); - }, - setSorters(sort: Object): void { - const preparedParams = { ...dataListProps.__loadParams, sort }; - this.refresh(preparedParams); - }, - multiSelect(item, value): void { - dispatchProps.multiSelect({ ...withDataListParams, item, value }); - }, - multiSelectAll(value: ?Object): void { - const { data } = returnProps[propName]; - if (Array.isArray(data)) { - dispatchProps.multiSelect({ - ...withDataListParams, - item: data, - value - }); - } else { - dispatchProps.multiSelect({ - ...withDataListParams, - item: [], - value - }); - } - }, - isMultiSelected(item): boolean { - if (!Array.isArray(returnProps[propName].multiSelectedItems)) { - return false; - } - - return returnProps[propName].multiSelectedItems.includes(item); - }, - isAllMultiSelected(): boolean { - return ( - returnProps[propName].getMultiSelected().length === - returnProps[propName].data.length - ); - }, - isNoneMultiSelected(): boolean { - return returnProps[propName].getMultiSelected().length === 0; - }, - getMultiSelected(): Array { - return returnProps[propName].multiSelectedItems || []; - }, - __loadParams: prepareLoadListParams(withDataListParams, ownProps.router) - }; - - returnProps[propName] = dataListProps; - - return returnProps; - }, - { - areStatePropsEqual: (next, previous) => { - return _.isEqual(previous, next); - } - } - ), - lifecycle({ - componentDidMount() { - this.props[propName].init(); - }, - componentDidUpdate(prevProps) { - const params = { - prev: prevProps[propName].__loadParams, - next: this.props[propName].__loadParams - }; - - !_.isEqual(params.prev, params.next) && this.props[propName].init(); - } - }) - )(BaseComponent); - }; -}; diff --git a/packages/webiny-app/bkp/components/withDataListUtils/getPropName.js b/packages/webiny-app/bkp/components/withDataListUtils/getPropName.js deleted file mode 100644 index e4f03b3ebd6..00000000000 --- a/packages/webiny-app/bkp/components/withDataListUtils/getPropName.js +++ /dev/null @@ -1,11 +0,0 @@ -// @flow -import type { WithDataListParams } from "./types"; - -/** - * All list data is passed into child components via specific prop. Be default, "name" parameter will be - * used to determine its name. Alternatively, "prop" parameter can be used to specify a different name. - * @param withDataListParams - * @returns {*} - */ -export default (withDataListParams: WithDataListParams): string => - withDataListParams.prop || withDataListParams.name; diff --git a/packages/webiny-app/bkp/components/withDataListUtils/index.js b/packages/webiny-app/bkp/components/withDataListUtils/index.js deleted file mode 100644 index 9659d19f3f0..00000000000 --- a/packages/webiny-app/bkp/components/withDataListUtils/index.js +++ /dev/null @@ -1,5 +0,0 @@ -// @flow -export { default as getPropName } from "./getPropName"; -export { default as prepareLoadListParams } from "./prepareLoadListParams"; -export { default as prepareNewLoadListParams } from "./prepareNewLoadListParams"; -export { default as redirectToRouteWithQueryParams } from "./redirectToRouteWithQueryParams"; diff --git a/packages/webiny-app/bkp/components/withDataListUtils/prepareLoadListParams.js b/packages/webiny-app/bkp/components/withDataListUtils/prepareLoadListParams.js deleted file mode 100644 index 0a8c9b7d1b2..00000000000 --- a/packages/webiny-app/bkp/components/withDataListUtils/prepareLoadListParams.js +++ /dev/null @@ -1,33 +0,0 @@ -// @flow -import type { WithDataListParams } from "./types"; -import typeof { router as Router } from "webiny-app/router"; - -export default (withDataListParams: WithDataListParams, router: ?Router) => { - const paramsClone = Object.assign({}, withDataListParams); - if (router) { - const { page, perPage } = router.match.query; - if (page) { - paramsClone.page = page; - } - - if (perPage) { - paramsClone.perPage = perPage; - } - - ["sort", "where", "search"].forEach(key => { - if (!router) { - return; - } - - if (typeof router.match.query[key] === "string") { - try { - paramsClone[key] = JSON.parse(router.match.query[key]); - } catch (e) { - // Do nothing. - } - } - }); - } - - return paramsClone; -}; diff --git a/packages/webiny-app/bkp/components/withDataListUtils/prepareNewLoadListParams.js b/packages/webiny-app/bkp/components/withDataListUtils/prepareNewLoadListParams.js deleted file mode 100644 index 218d161c467..00000000000 --- a/packages/webiny-app/bkp/components/withDataListUtils/prepareNewLoadListParams.js +++ /dev/null @@ -1,32 +0,0 @@ -// @flow -import typeof { router as Router } from "webiny-app/router"; - -export default (router: ?Router) => { - const params = {}; - if (router) { - const { page, perPage } = router.match.query; - if (page) { - params.page = page; - } - - if (perPage) { - params.perPage = perPage; - } - - ["sort", "where", "search"].forEach(key => { - if (!router) { - return; - } - - if (typeof router.match.query[key] === "string") { - try { - params[key] = JSON.parse(router.match.query[key]); - } catch (e) { - // Do nothing. - } - } - }); - } - - return params; -}; diff --git a/packages/webiny-app/bkp/components/withDataListUtils/redirectToRouteWithQueryParams.js b/packages/webiny-app/bkp/components/withDataListUtils/redirectToRouteWithQueryParams.js deleted file mode 100644 index 0b379182bab..00000000000 --- a/packages/webiny-app/bkp/components/withDataListUtils/redirectToRouteWithQueryParams.js +++ /dev/null @@ -1,25 +0,0 @@ -// @flow -import typeof { router as Router } from "webiny-app/router"; - -export default (params: Object, router: Router) => { - const paramsClone = Object.assign({}, params); - - ["sort", "search", "where"].forEach(key => { - if (typeof paramsClone[key] === "object") { - paramsClone[key] = JSON.stringify(paramsClone[key]); - } - }); - - const { perPage, page, where, search, sort } = paramsClone; - - router.goToRoute({ - merge: true, - params: { - perPage, - page, - where, - search, - sort - } - }); -}; diff --git a/packages/webiny-app/bkp/components/withDataListUtils/types.js b/packages/webiny-app/bkp/components/withDataListUtils/types.js deleted file mode 100644 index bb6cf12bf5e..00000000000 --- a/packages/webiny-app/bkp/components/withDataListUtils/types.js +++ /dev/null @@ -1,40 +0,0 @@ -// @flow -export type WithDataListParams = { - load?: boolean, - prop?: string, - name: string, - type: string, - fields: string, - page?: number, - perPage?: number, - sort?: Object, - where?: Object, - search?: Object -}; - -export type SearchParams = { - query: string, - operator?: "and" | "or", - fields?: string -}; - -export type WithDataListProps = { - data: Array, - meta: Object, - init: () => void, - refresh: (params: ?Object) => void, - delete: (id: string, options: Object) => void, - setPerPage: (perPage: number) => void, - setPage: (page: number) => void, - setSearch: (search: SearchParams) => void, - setWhere: (where: Object) => void, - setSorters: (sort: Object) => void, - multiSelect: (item: Object, value: boolean) => void, - multiSelectAll: (value: boolean) => void, - - isMultiSelected: (item: Object) => boolean, - isAllMultiSelected: () => boolean, - isNoneMultiSelected: () => boolean, - getMultiSelected: () => Array, - __loadParams: WithDataListParams -}; diff --git a/packages/webiny-app/bkp/components/withFileUpload.js b/packages/webiny-app/bkp/components/withFileUpload.js deleted file mode 100644 index 9d930ccf878..00000000000 --- a/packages/webiny-app/bkp/components/withFileUpload.js +++ /dev/null @@ -1,75 +0,0 @@ -// @flow -import * as React from "react"; -import { compose, withProps } from "recompose"; -import type { FileBrowserFile } from "webiny-ui/FileBrowser"; -import { withConfig } from "webiny-app/components"; -import invariant from "invariant"; - -type WithFileUploadOptions = { - multiple?: boolean -}; - -const mustUpload = (file: FileBrowserFile) => { - if (!file) { - return false; - } - - const src: string = (file.src: any); - return src.startsWith("data:"); -}; - -export const withFileUpload = (options: WithFileUploadOptions = {}): Function => { - return (BaseComponent: typeof React.Component) => { - return compose( - withConfig(), - withProps(props => { - return { - ...props, - onChange: async file => { - const { onChange, config } = props; - - const withFileUploadPlugin = config.withFileUploadPlugin; - invariant( - withFileUploadPlugin, - `Plugin not defined for "withFileUpload component. - Make sure set the plugin in app's config ("app.config.components.withFileUploadPlugin").` - ); - - onChange && (await onChange(file)); - - if (options.multiple) { - Array.isArray(file) && - file.forEach((current, index) => { - if (mustUpload(current)) { - withFileUploadPlugin - .upload(current) - .then(async uploadedFile => { - file[index] = uploadedFile; - onChange && (await onChange(file)); - }); - } - }); - return; - } - - invariant( - !Array.isArray(file), - `Selected two or more files instead of one. Did you forget to set "multiple" option to true ("withFileUpload({multiple: true})")?` - ); - - if (mustUpload(file)) { - // Send file to server and get its path. - try { - withFileUploadPlugin.upload(file).then(async uploadedFile => { - onChange && (await onChange(uploadedFile)); - }); - } catch (e) { - console.warn(e); - } - } - } - }; - }) - )(BaseComponent); - }; -}; diff --git a/packages/webiny-app/bkp/components/withFileUpload/localStoragePlugin.js b/packages/webiny-app/bkp/components/withFileUpload/localStoragePlugin.js deleted file mode 100644 index 14b3413597e..00000000000 --- a/packages/webiny-app/bkp/components/withFileUpload/localStoragePlugin.js +++ /dev/null @@ -1,28 +0,0 @@ -// @flow -import type { FileBrowserFile } from "webiny-ui/FileBrowser"; - -/** - * A simple local storage plugin, used with "withFileUpload" HOC. - * When using this, make sure server side is also configured in the same way. - * Can be used for development purposes only. - */ - -type LocalStoragePluginConfig = { - uri: string -}; - -export default (config: LocalStoragePluginConfig) => { - return { - upload: async (file: FileBrowserFile) => { - return new Promise(resolve => { - const xhr = new window.XMLHttpRequest(); - xhr.open("POST", config.uri, true); - xhr.setRequestHeader("Content-Type", "application/json"); - xhr.send(JSON.stringify(file)); - xhr.onload = function() { - resolve(JSON.parse(this.responseText)); - }; - }); - } - }; -}; diff --git a/packages/webiny-app/bkp/components/withFileUpload/types.js b/packages/webiny-app/bkp/components/withFileUpload/types.js deleted file mode 100644 index c75c8eb59a6..00000000000 --- a/packages/webiny-app/bkp/components/withFileUpload/types.js +++ /dev/null @@ -1,15 +0,0 @@ -// @flow -import type { Plugin } from "webiny-app/types"; -import type { FileBrowserFile } from "webiny-ui/FileBrowser"; - -export type WithFileUploadPlugin = Plugin & { - upload: (file: FileBrowserFile) => Promise -}; - -export type FileUploadSuccess = FileBrowserFile & { - // Nothing for now, probably won't be anything here. -}; - -export type FileUploadError = { - // TODO - still no unified error messaging on the API side. -}; diff --git a/packages/webiny-app/bkp/components/withFileUpload/webinyCloudStoragePlugin.js b/packages/webiny-app/bkp/components/withFileUpload/webinyCloudStoragePlugin.js deleted file mode 100644 index 9f852eadcd6..00000000000 --- a/packages/webiny-app/bkp/components/withFileUpload/webinyCloudStoragePlugin.js +++ /dev/null @@ -1,45 +0,0 @@ -// @flow -import { dispatch } from "webiny-app/redux"; -import { typeSave } from "webiny-app/actions"; -import type { FileBrowserFile } from "webiny-ui/FileBrowser"; - -/** - * TODO: when we get to webiny cloud development. - * A simple S3 storage plugin, used with "withFileUpload" HOC. - * When using this, make sure server side is also configured in the same way. - * Can be used for production. - * - * How it works? - * First it sends file data to the server, which then returns a signed URL, to be used for actual upload to S3. - * Once the upload is complete, another request will be sent to the server, to confirm that the file was uploaded. - */ - -type WebinyCloudStoragePluginConfig = { - siteToken: string // Just a sample config param, remove this if needed. -}; - -export default (config: WebinyCloudStoragePluginConfig) => { - return { - upload: async (file: FileBrowserFile) => { - return new Promise((resolve, reject) => { - dispatch( - typeSave({ - fields: "name src type size", - type: "Files", - data: file, - onSuccess: (response: FileBrowserFile) => { - // TODO: upload to S3 with a signed URL. - // TODO: tell the server that the file upload is done or report errors if any. - resolve(response); - }, - onError: errors => { - console.log(config.siteToken); // Just a sample, remove this if needed. - // Failed to receive S3 signed upload URL? - reject(errors); - } - }) - ); - }); - } - }; -}; diff --git a/packages/webiny-app/bkp/components/withForm.js b/packages/webiny-app/bkp/components/withForm.js deleted file mode 100644 index e34614a9dfe..00000000000 --- a/packages/webiny-app/bkp/components/withForm.js +++ /dev/null @@ -1,78 +0,0 @@ -// @flow -import * as React from "react"; -import { connect } from "react-redux"; -import { compose, lifecycle } from "recompose"; -import { loadForm, emptyForm, submitForm } from "./../actions"; -import _ from "lodash"; -import type { WithFormParams } from "./withFormUtils/types"; -import { getPropName, prepareLoadFormParams } from "./withFormUtils"; - -export default (withFormParams: WithFormParams): Function => { - const propName = getPropName(withFormParams); - - return (BaseComponent: typeof React.Component) => { - return compose( - connect( - state => ({ - formState: _.get(state, `forms.${withFormParams.name}`) - }), - { loadForm, submitForm, emptyForm }, - (stateProps, dispatchProps, ownProps) => { - const returnProps = Object.assign({}, ownProps); - - const { formState } = stateProps; - const formProps = { - ...formState, - submit({ data, onSuccess, onError }) { - dispatchProps.submitForm({ - ...withFormParams, - data, - onSuccess, - onError - }); - }, - load() { - // Do not load if received "id" is the same as in form data. - // Possibly add "force" flag if this check needs to be skipped. - const id = { - store: formProps.data && formProps.data.id, - props: formProps.__loadParams.id - }; - - id.store !== id.props && - dispatchProps.loadForm({ ...formProps.__loadParams }); - }, - empty() { - dispatchProps.emptyForm({ ...formProps.__loadParams }); - }, - init() { - formProps.__loadParams.id ? formProps.load() : formProps.empty(); - }, - __loadParams: prepareLoadFormParams(withFormParams, ownProps.route) - }; - returnProps[propName] = formProps; - - return returnProps; - }, - { - areStatePropsEqual: (next, previous) => { - return _.isEqual(previous, next); - } - } - ), - lifecycle({ - componentDidUpdate(prevProps) { - const params = { - prev: prevProps[propName].__loadParams, - next: this.props[propName].__loadParams - }; - - !_.isEqual(params.prev, params.next) && this.props[propName].init(); - }, - componentDidMount() { - this.props[propName].init(); - } - }) - )(BaseComponent); - }; -}; diff --git a/packages/webiny-app/bkp/components/withFormUtils/getPropName.js b/packages/webiny-app/bkp/components/withFormUtils/getPropName.js deleted file mode 100644 index 9a41a382b5f..00000000000 --- a/packages/webiny-app/bkp/components/withFormUtils/getPropName.js +++ /dev/null @@ -1,10 +0,0 @@ -// @flow -import type { WithFormParams } from "./types"; - -/** - * All list data is passed into child components via specific prop. Be default, "name" parameter will be - * used to determine its name. Alternatively, "prop" parameter can be used to specify a different name. - * @param params - * @returns {*} - */ -export default (params: WithFormParams): string => params.prop || params.name; diff --git a/packages/webiny-app/bkp/components/withFormUtils/index.js b/packages/webiny-app/bkp/components/withFormUtils/index.js deleted file mode 100644 index c60224e219d..00000000000 --- a/packages/webiny-app/bkp/components/withFormUtils/index.js +++ /dev/null @@ -1,3 +0,0 @@ -// @flow -export { default as getPropName } from "./getPropName"; -export { default as prepareLoadFormParams } from "./prepareLoadFormParams"; diff --git a/packages/webiny-app/bkp/components/withFormUtils/prepareLoadFormParams.js b/packages/webiny-app/bkp/components/withFormUtils/prepareLoadFormParams.js deleted file mode 100644 index c93c4f870e2..00000000000 --- a/packages/webiny-app/bkp/components/withFormUtils/prepareLoadFormParams.js +++ /dev/null @@ -1,14 +0,0 @@ -// @flow -import type { WithFormParams } from "./types"; - -export default (withDataFormParams: WithFormParams, route: Object) => { - const paramsClone = Object.assign({}, withDataFormParams); - if (route) { - let id = route.match.params.id || route.match.query.id; - if (id) { - paramsClone.id = id; - } - } - - return paramsClone; -}; diff --git a/packages/webiny-app/bkp/components/withFormUtils/types.js b/packages/webiny-app/bkp/components/withFormUtils/types.js deleted file mode 100644 index 86e42c78d63..00000000000 --- a/packages/webiny-app/bkp/components/withFormUtils/types.js +++ /dev/null @@ -1,8 +0,0 @@ -// @flow -export type WithFormParams = { - name: string, - type: string, - fields: string, - id?: string, - prop?: string -}; diff --git a/packages/webiny-app/bkp/components/withGraphQL.js b/packages/webiny-app/bkp/components/withGraphQL.js deleted file mode 100644 index 828c34d9a76..00000000000 --- a/packages/webiny-app/bkp/components/withGraphQL.js +++ /dev/null @@ -1,20 +0,0 @@ -// @flow -import * as React from "react"; -import { bindActionCreators } from "redux"; -import { connect } from "react-redux"; -import { graphqlMutation, graphqlQuery } from "./../actions"; - -const query = params => graphqlQuery(params); -const mutation = params => graphqlMutation(params); - -export default () => { - return (BaseComponent: typeof React.Component) => { - const Component = connect( - null, - dispatch => ({ graphql: bindActionCreators({ query, mutation }, dispatch) }) - )(BaseComponent); - - Component.displayName = "withGraphQL"; - return Component; - }; -}; diff --git a/packages/webiny-app/bkp/components/withNewDataList.js b/packages/webiny-app/bkp/components/withNewDataList.js deleted file mode 100644 index a32dcad0a01..00000000000 --- a/packages/webiny-app/bkp/components/withNewDataList.js +++ /dev/null @@ -1,157 +0,0 @@ -// @flow -import * as React from "react"; -import { connect } from "react-redux"; -import { compose, lifecycle, withProps } from "recompose"; -import { graphql } from "react-apollo"; -import _ from "lodash"; - -import type { - WithDataListParams, - SearchParams, - WithDataListProps -} from "./withDataListUtils/types"; - -import { - getPropName, - prepareNewLoadListParams, - redirectToRouteWithQueryParams -} from "./withDataListUtils"; - -export type { WithDataListProps, SearchParams, WithDataListParams }; - -// For returning the same instance in connect function (returnProps). -const emptyArray = []; -const emptyObject = {}; - -export const withNewDataList = (withDataListParams: Object): Function => { - const propName = getPropName(withDataListParams); - - return (BaseComponent: typeof React.Component) => { - return compose( - graphql(withDataListParams.query, { - name: "queryData", - options: props => { - return { - variables: { - ...withDataListParams.variables, - ...prepareNewLoadListParams(props.router) - } - }; - } - }), - withProps(props => { - const propName = withDataListParams.name; - const returnProps = Object.assign({}, props); - const { router, queryData } = props; - - const dataListProps: Object = { - data: queryData, - init(): void { - this.refresh(); - }, - refresh(params): void { - if (!params) { - queryData.refetch(dataListProps.__loadParams); - return; - } - - if (router) { - redirectToRouteWithQueryParams(params, router); - } else { - queryData.refetch(params); - } - }, - delete(id, options = {}): void { - const { type, fields, name } = withDataListParams; - // TODO: - /*dispatchProps.typeDelete({ - type, - fields, - name, - id, - onSuccess: options.onSuccess || dataListProps.refresh - });*/ - }, - setPerPage(perPage: number): void { - const preparedParams = { - ...dataListProps.__loadParams, - perPage - }; - this.refresh(preparedParams); - }, - setPage(page: number): void { - const preparedParams = { ...dataListProps.__loadParams, page: page }; - this.refresh(preparedParams); - }, - setSearch(search: SearchParams): void { - const preparedParams = { ...dataListProps.__loadParams, search }; - this.refresh(preparedParams); - }, - setWhere(where: Object): void { - const preparedParams = { ...dataListProps.__loadParams, where }; - this.refresh(preparedParams); - }, - setSorters(sort: Object): void { - const preparedParams = { ...dataListProps.__loadParams, sort }; - this.refresh(preparedParams); - }, - multiSelect(item, value): void { - // TODO: - //dispatchProps.multiSelect({ ...withDataListParams, item, value }); - }, - multiSelectAll(value: ?Object): void { - // TODO: - /*const { data } = returnProps[propName]; - if (Array.isArray(data)) { - dispatchProps.multiSelect({ - ...withDataListParams, - item: data, - value - }); - } else { - dispatchProps.multiSelect({ - ...withDataListParams, - item: [], - value - }); - }*/ - }, - isMultiSelected(item): boolean { - if (!Array.isArray(returnProps[propName].multiSelectedItems)) { - return false; - } - - return returnProps[propName].multiSelectedItems.includes(item); - }, - isAllMultiSelected(): boolean { - return ( - returnProps[propName].getMultiSelected().length === - returnProps[propName].data.length - ); - }, - isNoneMultiSelected(): boolean { - return returnProps[propName].getMultiSelected().length === 0; - }, - getMultiSelected(): Array { - return returnProps[propName].multiSelectedItems || []; - }, - __loadParams: prepareNewLoadListParams(props.router) - }; - - returnProps[propName] = dataListProps; - - return returnProps; - }), - lifecycle({ - componentDidUpdate(prevProps) { - const params = { - prev: prevProps[propName].__loadParams, - next: this.props[propName].__loadParams - }; - - !_.isEqual(params.prev, params.next) && this.props[propName].init(); - } - }) - )(BaseComponent); - }; -}; diff --git a/packages/webiny-app/bkp/components/withRouter.js b/packages/webiny-app/bkp/components/withRouter.js deleted file mode 100644 index 4142e73f69b..00000000000 --- a/packages/webiny-app/bkp/components/withRouter.js +++ /dev/null @@ -1,38 +0,0 @@ -// @flow -import * as React from "react"; -import { router } from "webiny-app/router"; - -import { connect } from "react-redux"; -import { compose } from "recompose"; -import _ from "lodash"; - -const emptyObject = {}; - -export type WithRouterProps = { - router: typeof router -}; - -export const withRouter = (): Function => { - return (BaseComponent: typeof React.Component) => { - return compose( - connect( - state => ({ - route: _.get(state, `ui.route`, emptyObject) - }), - null, - (stateProps, dispatchProps, ownProps) => { - return { - ...ownProps, - ...stateProps, - router - }; - }, - { - areStatePropsEqual: (next, previous) => { - return _.isEqual(previous.route, next.route); - } - } - ) - )(BaseComponent); - }; -}; diff --git a/packages/webiny-app/bkp/components/withSecurity.js b/packages/webiny-app/bkp/components/withSecurity.js deleted file mode 100644 index 645a1d641e1..00000000000 --- a/packages/webiny-app/bkp/components/withSecurity.js +++ /dev/null @@ -1,20 +0,0 @@ -// @flow -import * as React from "react"; -import { SecurityConsumer } from "../security"; -import type { Security } from "../../types"; - -export type WithSecurityProps = { - security: Security -}; - -export const withSecurity = (): Function => { - return (Component: typeof React.Component) => { - return props => { - return ( - - - - ); - }; - }; -}; diff --git a/packages/webiny-app/bkp/graphql/Client.js b/packages/webiny-app/bkp/graphql/Client.js deleted file mode 100644 index db82b7bc690..00000000000 --- a/packages/webiny-app/bkp/graphql/Client.js +++ /dev/null @@ -1,96 +0,0 @@ -// @flow -import _ from "lodash"; -import ApolloClient, { type ApolloClientOptions, ApolloError } from "apollo-client"; -import { ApolloLink } from "apollo-link"; -import { HttpLink } from "apollo-link-http"; -import { setContext } from "apollo-link-context"; -import { InMemoryCache } from "apollo-cache-inmemory"; -import GraphQLError from "./Error"; - -type GraphQLQueryParams = { - query: Object, - variables?: Object -}; - -type GraphQLMutateParams = { - mutation: Object, - variables?: Object -}; - -type GraphQLResponse = Promise; - -function createApolloClient() { - const { uri, ...config } = this.config; - - config.link = ApolloLink.from([ - setContext((req, context) => { - const newContext = { ...context }; - this.interceptors.forEach(cb => { - _.merge(newContext, cb(req, newContext)); - }); - return newContext; - }), - new HttpLink({ uri }) - ]); - - if (!config.cache) { - config.cache = new InMemoryCache(); - } - - return new ApolloClient(config); -} - -class Client { - apolloClient: ApolloClient; - config: Object; - interceptors: Array; - constructor() { - this.config = { - errorPolicy: "all" - }; - this.interceptors = []; - this.apolloClient = null; - - return new Proxy(this, { - get: (instance, key) => { - if (key in instance) { - return Reflect.get(instance, key); - } - - return instance.apolloClient[key]; - } - }); - } - - query(params: GraphQLQueryParams): GraphQLResponse { - return this.apolloClient.query(params); - } - - mutate(params: GraphQLMutateParams): GraphQLResponse { - return this.apolloClient.mutate(params); - } - - addRequestInterceptor(cb: Function) { - this.interceptors.push(cb); - } - - toError(error: ApolloError) { - return GraphQLError.from(error); - } - - /** - * Set ApolloClient instance. - * Use when you want to entirely customize how your GraphQL client works. - * @param client - */ - setClient(client: ApolloClient) { - this.apolloClient = client; - } - - setConfig(config: ApolloClientOptions) { - this.config = { ...this.config, ...config }; - this.apolloClient = createApolloClient.call(this); - } -} - -export default Client; diff --git a/packages/webiny-app/bkp/graphql/Error.js b/packages/webiny-app/bkp/graphql/Error.js deleted file mode 100644 index d48750553b0..00000000000 --- a/packages/webiny-app/bkp/graphql/Error.js +++ /dev/null @@ -1,23 +0,0 @@ -// @flow -import type { ApolloError } from "apollo-client"; - -class GraphQLError extends Error { - code: string; - data: ?Object; - apolloError: Error; - - constructor(message: ?string, code: string, data: Object, apolloError: Error) { - super(message); - this.name = "GraphQLError"; - this.code = code; - this.data = data; - this.apolloError = apolloError; - } - - static from(error: ApolloError) { - const { message, code, data } = error.graphQLErrors[0]; - return new GraphQLError(message, code, data, error); - } -} - -export default GraphQLError; diff --git a/packages/webiny-app/bkp/i18n/index.js b/packages/webiny-app/bkp/i18n/index.js deleted file mode 100644 index 88cbad94e82..00000000000 --- a/packages/webiny-app/bkp/i18n/index.js +++ /dev/null @@ -1,2 +0,0 @@ -// @flow -export { default as i18n } from "./../utils/i18n"; diff --git a/packages/webiny-app/bkp/index.js b/packages/webiny-app/bkp/index.js deleted file mode 100644 index dfce6e2ccdc..00000000000 --- a/packages/webiny-app/bkp/index.js +++ /dev/null @@ -1,5 +0,0 @@ -// @flow -import "@babel/polyfill"; -export { default as Webiny, app, apolloClient, security } from "./components/Webiny"; -export { Router, router } from "./router"; -export { SecurityProvider, SecurityConsumer } from "./security"; diff --git a/packages/webiny-app/bkp/plugins/index.js b/packages/webiny-app/bkp/plugins/index.js deleted file mode 100644 index b4cfbb13159..00000000000 --- a/packages/webiny-app/bkp/plugins/index.js +++ /dev/null @@ -1,28 +0,0 @@ -// @flow -const plugins = {}; - -export type Plugin = { - name: string, - type: string, - target?: Array, - ...Object -}; - -export const addPlugin = (...args: Array): void => { - args.forEach(pl => { - plugins[pl.name] = pl; - }); -}; - -export const getPlugins = (type: string): Array => { - const values: Array = (Object.values(plugins): any); - return values.filter((plugin: Plugin) => (type ? plugin.type === type : true)); -}; - -export const getPlugin = (name: string): ?Plugin => { - return plugins[name]; -}; - -export const removePlugin = (name: string): void => { - delete plugins[name]; -}; diff --git a/packages/webiny-app/bkp/redux/createMiddleware.js b/packages/webiny-app/bkp/redux/createMiddleware.js deleted file mode 100644 index 6b3ef8f133a..00000000000 --- a/packages/webiny-app/bkp/redux/createMiddleware.js +++ /dev/null @@ -1,18 +0,0 @@ -// @flow -import type { Redux, MiddlewareFunction, Store, Action } from "webiny-app/types"; - -const wrapMiddleware = (types: Array, middleware: MiddlewareFunction): Function => { - return (store: Store) => (next: Function) => (action: Action) => { - if (types.includes(action.type)) { - middleware({ store, next, action }); - } else { - next(action); - } - }; -}; - -export default (redux: Redux): Array => { - return redux.middleware.map(mw => { - return wrapMiddleware(mw.actions, mw.middleware); - }); -}; \ No newline at end of file diff --git a/packages/webiny-app/bkp/redux/createRootReducer.js b/packages/webiny-app/bkp/redux/createRootReducer.js deleted file mode 100644 index bbc7068511a..00000000000 --- a/packages/webiny-app/bkp/redux/createRootReducer.js +++ /dev/null @@ -1,83 +0,0 @@ -// @flow -import { clone } from "lodash"; -import { get, set } from "dot-prop-immutable"; -import type { Redux, Reducer, State, Action, StatePath } from "webiny-app/types"; - -type ReducerCollection = Array<{ statePath: StatePath, reducer: Reducer, actions: Array }>; - -const findHigherOrderReducer = ( - reducers: ReducerCollection, - statePath: StatePath, - action: Action -) => { - for (let i = 0; i < reducers.length; i++) { - const hor = reducers[i]; - if (hor.actions.includes(action.type)) { - if (!hor.statePath) { - // If HOR does not have a slice defined it means it will process the entire app state - return { statePath: null, reducer: hor.reducer }; - } - - // Normalize statePath using current action - const horStatePath = resolveStatePath(hor.statePath, action); - - if ( - statePath && - horStatePath && - (statePath.startsWith(horStatePath + ".") || statePath === horStatePath) - ) { - return { statePath: horStatePath, reducer: hor.reducer }; - } - } - } - - return null; -}; - -const runReducer = (appState, action, extra, reducer, statePath) => { - // Get a slice of state for this reducer - const stateSlice = statePath ? get(appState, statePath) : appState; - // Run reducer and update app state - const newState = reducer(stateSlice, action, extra); - return statePath ? set(appState, statePath, newState) : newState; -}; - -// Resolve statePath to string -const resolveStatePath = (statePath: StatePath, action: Action): null | string => { - if (typeof statePath === "function") { - return statePath(action); - } - - return statePath; -}; - -export default (INIT_STATE: Object = {}, redux: Redux): Reducer => { - return (state: State = INIT_STATE, action: Action) => { - let newState = clone(state); - redux.reducers.forEach(({ statePath, reducer, actions }) => { - if (!actions.includes(action.type)) { - return; - } - - statePath = resolveStatePath(statePath, action); - - // If requested statePath triggers a higher order reducer - delegate processing to that HOR - const horDef = findHigherOrderReducer(redux.higherOrderReducers, statePath, action); - - if (horDef) { - // Run HOR and pass the original {slice, reducer} that triggered this HOR - newState = runReducer( - newState, - action, - { statePath, reducer }, - horDef.reducer, - horDef.statePath - ); - } else { - newState = runReducer(newState, action, {}, reducer, statePath); - } - }); - - return newState; - }; -}; diff --git a/packages/webiny-app/bkp/redux/index.js b/packages/webiny-app/bkp/redux/index.js deleted file mode 100644 index cf4463462a5..00000000000 --- a/packages/webiny-app/bkp/redux/index.js +++ /dev/null @@ -1,108 +0,0 @@ -// @flow -import { applyMiddleware, createStore, compose } from "redux"; -import _ from "lodash"; -import invariant from "invariant"; -import createRootReducer from "./createRootReducer"; -import createMiddleware from "./createMiddleware"; -import type { - MiddlewareFunction, - ReducerFactory, - Reducer, - Action, - ActionCreator, - ActionOptions, - StatePath, - Store -} from "webiny-app/types"; - -export class Redux { - store: Store; - middleware: Array<{ actions: Array, middleware: MiddlewareFunction }>; - reducers: Array<{ statePath: StatePath, reducer: Reducer, actions: Array }>; - higherOrderReducers: Array<{ - statePath: StatePath, - reducer: Reducer, - actions: Array - }>; - - constructor() { - this.reducers = []; - this.higherOrderReducers = []; - this.middleware = []; - } - - createAction(type: string, options?: ActionOptions = {}): ActionCreator { - return (payload?: Object = {}, meta: Object = {}) => { - return { - type, - payload, - meta: { log: options.hasOwnProperty("log") ? options.log : true, ...meta } - }; - }; - } - - addReducer(actions: Array, statePath: StatePath, reducer: Reducer) { - invariant(reducer, "Must pass a valid reducer function!"); - - this.reducers.push({ actions, statePath, reducer }); - } - - addHigherOrderReducer(actions: Array, statePath: StatePath, factory: ReducerFactory) { - this.higherOrderReducers.push({ actions, statePath, reducer: factory() }); - } - - addMiddleware(actions: Array, middleware: MiddlewareFunction) { - this.middleware.push({ actions, middleware }); - } - - initStore(INIT_STATE: Object = {}, middleware: Array = []) { - // dev tool - const reduxDevTools = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__; - const composeEnhancers = - (reduxDevTools && - reduxDevTools({ - predicate: (state, action) => _.get(action, "meta.log", true) - })) || - compose; - - this.store = createStore( - createRootReducer(INIT_STATE, this), - composeEnhancers(applyMiddleware(...middleware, ...createMiddleware(this))) - ); - - this.store.dispatch({ type: "INIT" }); - - return this.store; - } -} - -const redux = new Redux(); - -export { redux }; - -export const createAction = (type: string, options?: ActionOptions) => { - return redux.createAction(type, options); -}; - -export const addReducer = (actions: Array, statePath: StatePath, reducer: Reducer) => { - redux.addReducer(actions, statePath, reducer); -}; - -export const addHigherOrderReducer = ( - actions: Array, - statePath: StatePath, - factory: ReducerFactory -) => { - redux.addHigherOrderReducer(actions, statePath, factory); -}; - -export const addMiddleware = (actions: Array, middleware: MiddlewareFunction) => { - redux.addMiddleware(actions, middleware); -}; - -export const dispatch = (action: Action) => { - return redux.store.dispatch(action); -}; - -export const selectUi = (state: Object) => state.ui || {}; -export const selectApp = (state: Object) => state.app || {}; diff --git a/packages/webiny-app/bkp/router/Link.js b/packages/webiny-app/bkp/router/Link.js deleted file mode 100644 index 2d92eea8b2c..00000000000 --- a/packages/webiny-app/bkp/router/Link.js +++ /dev/null @@ -1,92 +0,0 @@ -// @flow -import * as React from "react"; -import each from "lodash/each"; -import { router } from "."; - -type Props = { - children: React.Node, - disabled?: boolean, - url?: ?string, - title?: ?string, - route?: ?string, - params?: Object, - className?: ?string, - tabIndex?: ?number, - onClick?: ?Function -}; - -class Link extends React.Component { - static defaultProps = { - disabled: false, - url: null, - title: null, - route: null, - params: {}, - className: null, - tabIndex: null, - onClick: null - }; - - allowedProps = [ - "className", - "style", - "target", - "href", - "onClick", - "title", - "tabIndex", - "disabled" - ]; - - getLinkProps = (): Object => { - const props: Props & { href?: ?string } = { ...this.props }; - - props.href = null; - - if (!props.disabled) { - if (props.url) { - const url: string = props.url; - // Let's ensure we have at least http:// specified - for cases where users just type www... - if (!/^(f|ht)tps?:\/\//i.test(url) && !url.startsWith("/")) { - props.url = "http://" + url; - } - props.href = props.url; - } else if (props.route) { - let route = props.route; - if (typeof route === "string") { - route = route === "current" ? router.route : router.getRoute(route); - } - - props.href = null; - if (!route) { - props.href = null; - } else { - props.href = router.createHref(route.name, props.params); - if (props.href.startsWith("//")) { - // $FlowFixMe - props.href = props.href.substring(1); // Get everything after first character (after first slash) - } - } - } - } - - const finalProps = {}; - each(props, (value, prop) => { - if (this.allowedProps.includes(prop) || prop.startsWith("data-")) { - finalProps[prop] = value; - } - }); - - if (props.onClick) { - props.onClick = e => props.onClick && props.onClick({ event: e }); - } - - return finalProps; - }; - - render() { - return {this.props.children}; - } -} - -export default Link; diff --git a/packages/webiny-app/bkp/router/index.js b/packages/webiny-app/bkp/router/index.js deleted file mode 100644 index bcadc96e2d8..00000000000 --- a/packages/webiny-app/bkp/router/index.js +++ /dev/null @@ -1,12 +0,0 @@ -// @flow -import { Router } from "webiny-react-router"; -export const router = new Router(); -export { default as authenticationMiddleware } from "./middleware/authentication"; -export { default as reduxMiddleware } from "./middleware/redux"; -export { default as Link } from "./Link"; - -export { - RouterComponent as Router, - renderMiddleware, - resolveMiddleware -} from "webiny-react-router"; diff --git a/packages/webiny-app/bkp/router/middleware/authentication.js b/packages/webiny-app/bkp/router/middleware/authentication.js deleted file mode 100644 index a918be2fdd2..00000000000 --- a/packages/webiny-app/bkp/router/middleware/authentication.js +++ /dev/null @@ -1,40 +0,0 @@ -// @flow -import debug from "debug"; -import { security } from "webiny-app"; - -const log = debug("webiny-app-security"); - -/** - * Authentication middleware factory. - * @param options - * @returns {Function} Request middleware function. - */ -export default (options: { onNotAuthenticated: Function }) => { - /** - * Authentication middleware. - * Attempts to authenticate the user using `authentication` service. - * If successful, `identity` instance is set on the `route` object. - * If not successful, `onNotAuthenticated` callback is called to decide what to do. - * - * @param params - * @param next - * @param finish - * @return {Promise} - */ - return async (params: Object, next: Function, finish: Function) => { - try { - params.route.identity = await security.authenticate(); - } catch (e) { - if (e.name === "AuthenticationError") { - log(`${e.name}: ${e.message}`); - } else { - console.error(e); - } - - if (typeof options.onNotAuthenticated === "function") { - return await options.onNotAuthenticated(params, next, finish); - } - } - next(); - }; -}; diff --git a/packages/webiny-app/bkp/router/middleware/redux.js b/packages/webiny-app/bkp/router/middleware/redux.js deleted file mode 100644 index 859bf7b1b25..00000000000 --- a/packages/webiny-app/bkp/router/middleware/redux.js +++ /dev/null @@ -1,11 +0,0 @@ -// @flow -import { dispatch } from "webiny-app/redux"; -import { routeChanged } from "webiny-app/actions"; - -export default () => { - return async (params: Object, next: Function) => { - const { route, match } = params; - dispatch(routeChanged({ route, match })); - next(); - }; -}; diff --git a/packages/webiny-app/bkp/security/Security.js b/packages/webiny-app/bkp/security/Security.js deleted file mode 100644 index 60894eb91f6..00000000000 --- a/packages/webiny-app/bkp/security/Security.js +++ /dev/null @@ -1,26 +0,0 @@ -import React from "react"; -import { isEqual } from "lodash"; -const { Provider, Consumer } = React.createContext(); - -export const SecurityProvider = ({ security, children }) => { - return {children}; -}; - -export const SecurityConsumer = ({ children }) => { - return ( - - {security => {children}} - - ); -}; - -class EnsureChange extends React.Component { - shouldComponentUpdate(props) { - return !isEqual(props.security.identity, this.props.security.identity); - } - - render() { - console.log("RENDERING SECURITY CONSUMER"); - return React.cloneElement(this.props.children, { security: this.props.security }); - } -} diff --git a/packages/webiny-app/bkp/security/SecurityError.js b/packages/webiny-app/bkp/security/SecurityError.js deleted file mode 100644 index d42eb2333e0..00000000000 --- a/packages/webiny-app/bkp/security/SecurityError.js +++ /dev/null @@ -1,19 +0,0 @@ -// @flow -class SecurityError extends Error { - code: string; - data: Object; - - constructor(message: string, code: string, data: Object = {}) { - super(); - this.name = "AuthenticationError"; - this.message = message; - this.data = data; - this.code = code; - } - - toString() { - return this.message; - } -} - -export default SecurityError; diff --git a/packages/webiny-app/bkp/security/Security_bkp.js b/packages/webiny-app/bkp/security/Security_bkp.js deleted file mode 100644 index 6e739ba388b..00000000000 --- a/packages/webiny-app/bkp/security/Security_bkp.js +++ /dev/null @@ -1,167 +0,0 @@ -// @flow -import cookies from "js-cookie"; -import _ from "lodash"; -import debugFactory from "debug"; -import invariant from "invariant"; -import { app } from "webiny-app"; -import gql from "graphql-tag"; -import type { AuthenticationServiceConfig } from "../../types"; -import SecurityError from "./SecurityError"; - -const debug = debugFactory("webiny-app-security"); - -function getToken() { - return cookies.get(this.config.cookie); -} - -class Security_bkp { - identity: null | Object; - callbacks: { [event: string]: Array }; - config: AuthenticationServiceConfig; - - configure(config: Object) { - const defaultConfig = { - header: "Authorization", - cookie: "webiny-token", - me: () => { - const fields = config.identities.map(({ identity, fields }) => { - return `... on ${identity} { - ${fields} - }`; - }); - - return gql` - { - Me { - get { - ${fields.join("\\n")} - } - } - }`; - }, - onLogout: () => { - // Override to do something - } - }; - this.config = { ...defaultConfig, ...config }; - this.callbacks = { - onIdentity: [] - }; - - app.graphql.addRequestInterceptor(() => { - const token = getToken.call(this); - if (token) { - return { - headers: { - [this.config.header]: "Bearer " + token - } - }; - } - - return {}; - }); - } - - async login(identity: string, strategy: string, payload: Object): Promise<{}> { - try { - const identityConfig = _.find(this.config.identities, { identity }); - invariant( - identityConfig, - `Identity "${identity}" not found in authentication service!` - ); - - const strategyConfig = _.find(identityConfig.authenticate, { strategy }); - invariant( - strategyConfig, - `Strategy "${strategy}" not found in authentication service!` - ); - - // Attempt to login - const { data, errors } = await app.graphql.query({ - query: strategyConfig.query, - variables: payload - }); - - const me = _.get(data, "Security.Users.authenticate"); - - if (errors) { - const { message, code, data } = errors[0]; - return Promise.reject(new SecurityError(message, code, data)); - } - - // Set token cookie - const expires = new Date(me.expiresOn * 1000); - cookies.set(this.config.cookie, me.token, { path: "/", expires }); - - this.identity = me.identity; - const { id, email } = this.identity; - debug(`Loaded user %o with id %o`, email, id); - this.callbacks.onIdentity.map(cb => cb(this.identity)); - - return Promise.resolve({ token: me.token, identity: this.identity }); - } catch (e) { - return Promise.reject(e); - } - } - - /** - * Authenticate user (if possible). - * @returns {Promise} Identity data. - */ - async authenticate(): Promise { - if (this.identity) { - return Promise.resolve(this.identity); - } - - const token = getToken.call(this); - if (!token) { - return Promise.reject(new SecurityError("Identity token is not set!", "TOKEN_NOT_SET")); - } - - const { errors, data } = await app.graphql.query({ - query: this.config.me() - }); - - if (errors) { - const { message, code, data } = errors[0]; - return Promise.reject(new SecurityError(message, code, data)); - } - - this.identity = data.Me.get; - const { id, email } = this.identity; - debug(`Loaded user %o with id %o`, email, id); - this.callbacks.onIdentity.map(cb => cb(this.identity)); - - return Promise.resolve(this.identity); - } - - /** - * Refresh user data by fetching fresh data via API. - * - * @returns {Promise} - */ - refresh(): Promise { - this.identity = null; - return this.authenticate(); - } - - async logout(): Promise { - this.identity = null; - cookies.remove(this.config.cookie, { path: "/" }); - this.callbacks.onIdentity.map(cb => cb(null)); - this.config.onLogout && (await this.config.onLogout()); - return Promise.resolve(); - } - - /** - * Add callback for when `identity` data is changed. - * @param callback - * @returns {Function} A function to remove the callback. - */ - onIdentity(callback: Function): Function { - const length = this.callbacks.onIdentity.push(callback); - return () => this.callbacks.onIdentity.splice(length - 1, 1); - } -} - -export default Security_bkp; diff --git a/packages/webiny-app/bkp/security/createSecurity.js b/packages/webiny-app/bkp/security/createSecurity.js deleted file mode 100644 index 4d38df544e3..00000000000 --- a/packages/webiny-app/bkp/security/createSecurity.js +++ /dev/null @@ -1,118 +0,0 @@ -import invariant from "invariant"; -import _ from "lodash"; -import SecurityError from "./SecurityError"; -import cookies from "js-cookie"; -import { apolloClient } from "webiny-app"; - -function getToken(config) { - return cookies.get(config.cookie); -} - -export default (config) => { - const callbacks = { - onIdentity: [] - }; - - const security = { - identity: null, - async login(identity: string, strategy: string, payload: Object): Promise<{}> { - try { - const identityConfig = _.find(config.identities, { identity }); - invariant( - identityConfig, - `Identity "${identity}" not found in authentication service!` - ); - - const strategyConfig = _.find(identityConfig.authenticate, { strategy }); - invariant( - strategyConfig, - `Strategy "${strategy}" not found in authentication service!` - ); - - // Attempt to login - const { data, errors } = await apolloClient.query({ - query: strategyConfig.query, - variables: payload - }); - - const me = _.get(data, "Security.Users.authenticate"); - - if (errors) { - const { message, code, data } = errors[0]; - return Promise.reject(new SecurityError(message, code, data)); - } - - // Set token cookie - const expires = new Date(me.expiresOn * 1000); - cookies.set(config.cookie, me.token, { path: "/", expires }); - - this.identity = me.identity; - callbacks.onIdentity.map(cb => cb(this.identity)); - - return Promise.resolve({ token: me.token, identity: this.identity }); - } catch (e) { - return Promise.reject(e); - } - }, - - /** - * Authenticate user (if possible). - * @returns {Promise} Identity data. - */ - async authenticate(): Promise { - if (this.identity) { - return Promise.resolve(this.identity); - } - - const token = getToken(config); - if (!token) { - return Promise.reject( - new SecurityError("Identity token is not set!", "TOKEN_NOT_SET") - ); - } - - const { errors, data } = await apolloClient.query({ - query: config.me() - }); - - if (errors) { - const { message, code, data } = errors[0]; - return Promise.reject(new SecurityError(message, code, data)); - } - - this.identity = data.Me.get; - callbacks.onIdentity.map(cb => cb(this.identity)); - - return Promise.resolve(this.identity); - }, - /** - * Refresh user data by fetching fresh data via API. - * - * @returns {Promise} - */ - refresh(): Promise { - this.identity = null; - return this.authenticate(); - }, - - async logout(): Promise { - this.identity = null; - cookies.remove(config.cookie, { path: "/" }); - callbacks.onIdentity.map(cb => cb(null)); - config.onLogout && (await config.onLogout()); - return Promise.resolve(); - }, - - /** - * Add callback for when `identity` data is changed. - * @param callback - * @returns {Function} A function to remove the callback. - */ - onIdentity(callback: Function): Function { - const length = callbacks.onIdentity.push(callback); - return () => callbacks.onIdentity.splice(length - 1, 1); - } - }; - - return security; -}; diff --git a/packages/webiny-app/bkp/security/index.js b/packages/webiny-app/bkp/security/index.js deleted file mode 100644 index d5c29ce7e8f..00000000000 --- a/packages/webiny-app/bkp/security/index.js +++ /dev/null @@ -1 +0,0 @@ -export { SecurityConsumer, SecurityProvider } from "./Security"; diff --git a/packages/webiny-app/bkp/utils/document.js b/packages/webiny-app/bkp/utils/document.js deleted file mode 100644 index b0950d7b655..00000000000 --- a/packages/webiny-app/bkp/utils/document.js +++ /dev/null @@ -1,64 +0,0 @@ -import _ from 'lodash'; - -class DocumentUtils { - loadScript(url) { - return new Promise(resolve => { - // Is it already inserted, possibly with server-side rendering? - if (document.querySelectorAll(`script[src="${url}"]`).length > 0) { - return; - } - const s = document.createElement('script'); - s.type = 'text/javascript'; - s.src = url; - s.async = true; - s.onload = resolve; - document.body.appendChild(s); - }); - } - - loadStylesheet(url) { - return new Promise(resolve => { - // Is it already inserted, possibly with server-side rendering? - if (document.querySelectorAll(`link[rel="stylesheet"][href="${url}"]`).length > 0) { - return; - } - - const s = document.createElement('link'); - s.rel = 'stylesheet'; - s.href = url; - s.onload = resolve; - document.head.appendChild(s); - }); - } - - setMeta(attributes) { - let updatedExisting = false; - _.each(['name', 'property'], name => { - if (_.has(attributes, name)) { - // Fetch existing element - const element = document.querySelector(`meta[${name}="${attributes[name]}"]`); - if (element) { - // If exists, update with new attributes - _.each(attributes, (value, key) => element.setAttribute(key, value)); - updatedExisting = true; - return false; - } - } - }); - - if (updatedExisting) { - return; - } - - // Create new element - const element = document.createElement('meta'); - _.each(attributes, (value, key) => element.setAttribute(key, value)); - document.head.appendChild(element); - } - - setTitle(title) { - document.title = title; - } -} - -export default DocumentUtils; \ No newline at end of file diff --git a/packages/webiny-app/bkp/utils/i18n.js b/packages/webiny-app/bkp/utils/i18n.js deleted file mode 100644 index cfc39e00c63..00000000000 --- a/packages/webiny-app/bkp/utils/i18n.js +++ /dev/null @@ -1,7 +0,0 @@ -// @flow -import { I18n, defaultProcessor } from "webiny-i18n"; -import reactProcessor from "webiny-i18n-react"; - -const i18n = new I18n(); -i18n.registerProcessors([defaultProcessor, reactProcessor]); -export default i18n; diff --git a/packages/webiny-app/src/components/withAppConfig.js b/packages/webiny-app/src/components/withAppConfig.js index 3df2245e2c1..bf61bdd37a1 100644 --- a/packages/webiny-app/src/components/withAppConfig.js +++ b/packages/webiny-app/src/components/withAppConfig.js @@ -1,10 +1,10 @@ // @flow -import React from "react"; +import * as React from "react"; import { AppConfigContextConsumer } from "webiny-app/config"; export function withAppConfig() { - return function decorator(Component) { - return props => { + return function decorator(Component: React.ComponentType<*>) { + return function withAppConfig(props: Object) { return ( diff --git a/packages/webiny-app/src/components/withDataList.js b/packages/webiny-app/src/components/withDataList.js index d3f2b2c6366..2528cd6bb01 100644 --- a/packages/webiny-app/src/components/withDataList.js +++ b/packages/webiny-app/src/components/withDataList.js @@ -1,6 +1,6 @@ // @flow import * as React from "react"; -import { lifecycle, withProps, setDisplayName } from "recompose"; +import { lifecycle, withProps, withState, setDisplayName } from "recompose"; import { compose, graphql } from "react-apollo"; import _ from "lodash"; @@ -20,7 +20,6 @@ export type { WithDataListProps, SearchParams, WithDataListParams }; export const withDataList = (withDataListParams: Object): Function => { const propName = getPropName(withDataListParams); - return (BaseComponent: typeof React.Component) => { return compose( setDisplayName("withDataList"), @@ -35,17 +34,20 @@ export const withDataList = (withDataListParams: Object): Function => { }; } }), + withState("multiSelectedItems", "multiSelect", []), withProps(props => { - const propName = withDataListParams.name; const returnProps = Object.assign({}, props); const { router, queryData } = props; const dataListProps: Object = { - data: queryData, + ...withDataListParams.response(queryData), init(): void { this.refresh(); }, refresh(params): void { + // Refresh multi select first. + props.multiSelect([]); + if (!params) { queryData.refetch(dataListProps.__loadParams); return; @@ -80,46 +82,63 @@ export const withDataList = (withDataListParams: Object): Function => { const preparedParams = { ...dataListProps.__loadParams, sort }; this.refresh(preparedParams); }, - multiSelect(item, value): void { - // TODO: - //dispatchProps.multiSelect({ ...withDataListParams, item, value }); - }, - multiSelectAll(value: ?Object): void { - // TODO: - /*const { data } = returnProps[propName]; - if (Array.isArray(data)) { - dispatchProps.multiSelect({ - ...withDataListParams, - item: data, - value - }); - } else { - dispatchProps.multiSelect({ - ...withDataListParams, - item: [], - value - }); - }*/ + multiSelect(items, value): void { + if (!Array.isArray(items)) { + items = [items]; + } + + let multiSelectedItems = [...props.multiSelectedItems]; + + items.forEach(item => { + if (value === undefined) { + multiSelectedItems.includes(item) + ? multiSelectedItems.splice(multiSelectedItems.indexOf(item), 1) + : multiSelectedItems.push(item); + } else { + if (value === true) { + !multiSelectedItems.includes(item) && + multiSelectedItems.push(item); + } else { + multiSelectedItems.includes(item) && + multiSelectedItems.splice( + multiSelectedItems.indexOf(item), + 1 + ); + } + } + }); + + props.multiSelect(multiSelectedItems); }, isMultiSelected(item): boolean { - if (!Array.isArray(returnProps[propName].multiSelectedItems)) { + if (!Array.isArray(props.multiSelectedItems)) { return false; } - return returnProps[propName].multiSelectedItems.includes(item); - }, - isAllMultiSelected(): boolean { - return ( - returnProps[propName].getMultiSelected().length === - returnProps[propName].data.length - ); + return props.multiSelectedItems.includes(item); }, isNoneMultiSelected(): boolean { - return returnProps[propName].getMultiSelected().length === 0; + return props.multiSelectedItems.length === 0; }, getMultiSelected(): Array { - return returnProps[propName].multiSelectedItems || []; + return props.multiSelectedItems; + }, + multiSelectAll(value: boolean): void { + const { data } = dataListProps; + if (Array.isArray(data)) { + dataListProps.multiSelect(data, value); + } else { + dataListProps.multiSelect([], value); + } }, + isAllMultiSelected(): boolean { + const { data } = dataListProps; + + return ( + Array.isArray(data) && props.multiSelectedItems.length === data.length + ); + }, + __loadParams: prepareLoadListParams(props.router) }; diff --git a/packages/webiny-app/src/components/withDataListUtils/getPropName.js b/packages/webiny-app/src/components/withDataListUtils/getPropName.js index e4f03b3ebd6..d4e9bcbf548 100644 --- a/packages/webiny-app/src/components/withDataListUtils/getPropName.js +++ b/packages/webiny-app/src/components/withDataListUtils/getPropName.js @@ -8,4 +8,4 @@ import type { WithDataListParams } from "./types"; * @returns {*} */ export default (withDataListParams: WithDataListParams): string => - withDataListParams.prop || withDataListParams.name; + withDataListParams.name || "dataList"; diff --git a/packages/webiny-app/src/components/withDataListUtils/types.js b/packages/webiny-app/src/components/withDataListUtils/types.js index bb6cf12bf5e..bbd6f0b4572 100644 --- a/packages/webiny-app/src/components/withDataListUtils/types.js +++ b/packages/webiny-app/src/components/withDataListUtils/types.js @@ -1,7 +1,6 @@ // @flow export type WithDataListParams = { load?: boolean, - prop?: string, name: string, type: string, fields: string, diff --git a/packages/webiny-ui/index.js b/packages/webiny-ui/index.js deleted file mode 100644 index 990a77ff938..00000000000 --- a/packages/webiny-ui/index.js +++ /dev/null @@ -1,2 +0,0 @@ -// @flow -export * from "./src"; diff --git a/packages/webiny-ui/package.json b/packages/webiny-ui/package.json index 340d941d454..b8caa1aacd0 100644 --- a/packages/webiny-ui/package.json +++ b/packages/webiny-ui/package.json @@ -43,8 +43,8 @@ "emotion": "^9.2.6", "fabric": "~1.6.7", "install": "^0.12.1", + "keycode": "^2.2.0", "material-components-web": "^0.39.1", - "npm": "^6.4.1", "nprogress": "^0.2.0", "nuka-carousel": "^4.3.9", "rc-tooltip": "^3.7.2", diff --git a/packages/webiny-ui/src/AutoComplete/AutoComplete.js b/packages/webiny-ui/src/AutoComplete/AutoComplete.js index 4bacdaffd16..49bfb0e7f40 100644 --- a/packages/webiny-ui/src/AutoComplete/AutoComplete.js +++ b/packages/webiny-ui/src/AutoComplete/AutoComplete.js @@ -9,8 +9,15 @@ import { css } from "emotion"; import classNames from "classnames"; import { Elevation } from "webiny-ui/Elevation"; import { Typography } from "webiny-ui/Typography"; +import keycode from "keycode"; type Option = any; +type Options = Array