1.3.57 • Published 1 year ago

@veeqo/custom-views v1.3.57

Weekly downloads
7
License
ISC
Repository
github
Last release
1 year ago

Custom Views

Implement Views from veeqo-components library

Installation

Using npm:

npm install @veeqo/components
npm install @veeqo/custom-views

Using yarn:

yarn add @veeqo/components
yarn add @veeqo/custom-views

Step by step Custom Views implementing to your project:

Step 1: Go to your root file where you call ReactDOM render

Step 2: Create fixedViews array

It's fixed views so the user can't delete them

Example of fixed views:

const fixedViews = [
  {
    id: 'default1',
    key: 'default1',
    label: 'First default',
    type: 'fixed',
  },
  {
    id: 'default1',
    key: 'default2',
    label: 'Second default',
    type: 'fixed',
  },
];

Also, you can provide some additional info:

  • duplicatable. This prop control is Duplicate item visible in options. By default it's true
  • filters. This prop can be any type you want, this is what you will get at some points when you need to change the saved filters

Example of fixed view with those props:

{
  id: 'default',
  key: 'default',
  label: 'Not duplicatable default',
  type: 'fixed',
  duplicatable: false,
  filters: {
    metric: 'units_sold',
    groupBy: 'by_variants',
  },
},

Step 3: Create apiHandlers object

Example of apiHandlers:

const apiHandlers = {
  fetch: (customViewModelType) => new Promise((resolve) => (
    fetchViews(customViewModelType)
      .then((result) => resolve({ data: result })))),
  post: (view) => new Promise((resolve) => (
    saveView(view, store.dispatch)
      .then(((result) => {
        resolve({
          data: {
            id: result.body.data.id,
          },
        });
      }))
  )),
  patch: (view) => {
    saveView(view);
    setFiltersToView(view.key, view.attributes.filters);
  },
  delete: (viewId) => deleteView(viewId),
};

This object should have 4 functions:

  • fetch. Used when the store started initialization. Should be a Promise. Arguments:

    1) customViewModelType, string. Name of your page, for example: inventory or sales
  • persist. Used when view saved and its type was draft so you need to send this new view to server. Should be a Promise and resolve with id that you will get from the server. Arguments:

    1) view, object. View with all attributes. Example:
{
  type: 'custom_view',
  key: 'l6PJGo-UKXeGJNFBFzCde',
  user: {
    userId: 'XOBDXR15v0mkOoNaYp24K',
  },
  attributes: {
    title: 'Some Draft Tab',
    type: 'sales',
    filters: {
      metric: 'units_sold',
      groupBy: 'by_variants',
    },
    editable: true,
    shared: true,
  },
}
  • patch. Same as persist, but view already has saved type so you don't need to return Promise and resolve id, but you still should send changed view to server. Arguments are the same as in persist

  • delete. Used when saved view deleted. Arguments:

    1) viewId, string

Step 4: Create CustomViewsProviderFactory

This factory will create a store and pass the first data to it. Here you should use variables that you create in step 3 and step 4

Example:

const CustomViewsContextProvider = CustomViewsProviderFactory({
  customViewModelType: getLocation(),
  apiHandlers,
  fixedViews,
});

Step 5: Add provider to your ReactDOM render

Example:

ReactDOM.render(
  <Provider store={store}>
    <CustomViewsContextProvider>
      <App />
    </CustomViewsContextProvider>
  </Provider>,
  root,
);

Step 6: Create CustomViews component

Example:

import React from 'react';

import { CustomViews } from '@veeqo/custom-views';

const CustomViewsComponent = (props) => (
  <CustomViews {...props} />
);

export default CustomViewsComponent;

Step 7: Pass props to component

Props required for initialization

Those props you should get from user XHR, until you don't, pass null. You can't change those values inside store after initialization

NameTypeDescription
customViewPositionsstring[] | nullInitialization doesn't start until you don't pass an array
defaultViewIdstring | nullInitialization doesn't start until you don't pass a string

Props required for proper work

NameTypeDescriptionExample
filtersanyThis prop can be any type you want, this is what you will get at some points when you need to change the saved filters
includedFilters{ label: string, text: string }[]Used to show filter values in edit/save dropdown{ label: 'filter name', text: 'filter values' }
hasUnsavedChangesbooleanUsed to understand should we should show unsaved changes pill. You need just to check the equality of your current filters and filters inside the current view

Optional props

NameTypeDescriptionExample
useranyAdds user info to view attributes
isLoadingboolean
pageNamestringAdds page name to success notifications'sales'
currentViewstringYou should pass view key. Used to the manual set current view
isSortingDropdownEnabledboolean

Handlers

You should use those handlers to keep your store actual

NameTypeDescription
handleChangeActiveView(key: string) => void
handleDiscardUnsavedChanges() => void
handleCreateDraftView(key: string) => voidUsed when draft view created from plus button in controls
handleCreateDraftViewWithCurrentFilters(key: string) => voidUsed when draft view created from unsaved changes pill
handleDuplicateView(key: string, newViewKey: string) => void
handleCustomViewPositionsChange(viewPositions: string[], customViewModelType: string) => void
handleDefaultViewChange(id: string, customViewModelType: string) => void
handleSaveView(key: string, filters: any) => voidUsed when draft view saved
handleSaveToCurrentView(key: string, filters: any) => void
1.3.57

1 year ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3-beta-2

3 years ago

1.3.3-beta-1

3 years ago

1.3.3-beta-3

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.0-peer-deps

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.50

3 years ago

1.1.49

3 years ago

1.1.48

3 years ago

1.1.47

3 years ago

1.1.45

3 years ago

1.1.46

3 years ago

1.1.41

3 years ago

1.1.44

3 years ago

1.1.42

3 years ago

1.1.40

3 years ago

1.1.39

3 years ago

1.1.38

3 years ago

1.1.37

3 years ago

1.1.36

3 years ago

1.1.34

3 years ago

1.1.35

3 years ago

1.1.33

3 years ago

1.1.32

3 years ago

1.1.30

3 years ago

1.1.31

3 years ago

1.1.29

3 years ago

1.1.28

3 years ago

1.1.27

3 years ago

1.1.26

3 years ago

1.1.24

3 years ago

1.1.23

3 years ago

1.1.22

3 years ago

1.1.21

3 years ago

1.1.19

3 years ago

1.1.20

3 years ago

1.1.18

3 years ago

1.1.17

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago