0.0.1 • Published 3 years ago

vuex-crud-toolkit v0.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

Vuex CRUD toolkit

Install

npm install --save vuex-crud-toolkit

or

yarn add vuex-crud-toolkit

Features

  • Dashboard with filter, pagination, trash
  • Create page
  • Update page
  • Detail page
  • Confirm remove: move item to trash
  • Confirm restore
  • Confirm purge: purge item
  • Confirm empty trash

How to use

// app.store.js
import { buildCRUDStore } from "vuex-crud-toolkit";

const APP_NAME = "Posts";

const apis = {
  getList: async (params) => {... ; return items};
  count: async (params) => {...; return { count}};
  getOne: async (id) => { ...; return data};
  create: async (data) => {...; return data};
  update: async (id, data) => {...; return data};
  remove: async (id) => {...};

  trashGetList: async (params) => {...; return items};
  trashCount: async (params) => {...; return {count}};
  trashRestore: async (id) => {...};
  trashPurge: async (id) => {...};
  trashEmpty: async () => {...};
}

const store = buildCRUDStore({
  appName: APP_NAME,
  apis,
  getEntityId: (item) => item._id,
  dashboardConfig: {
    defaultFilter: {
      q: "",
    },
    defaultLimit: 5,
    defaultPage: 1,
  },
  createConfig: {
    redirectOnSuccess: "UPDATE", // 'DASHBOARD' | 'DETAIL' | 'UPDATE' | null
    reloadDashboardOnSuccess: true,
    showNotificationOnSuccess: true,
  },
  updateConfig: {
    redirectOnSuccess: "DASHBOARD", // 'DASHBOARD' | 'DETAIL' | null
    reloadDashboardOnSuccess: true,
    showNotificationOnSuccess: true,
  },
});

export default store;

Apis params

NameParamsResponseDescription
getListanyany[]Get list items
countany{count: number}Count items
getOnestring | numberanyGet item data by id
createanyanyCreate new item
updateid: string | number, data: anyanyUpdate item by id
removestring | numberanyRemove item to trash
trashGetListanyany[]Get list trash items
trashCountany{count: number}Count trash items
trashRestorestring | numberanyRestore item by id
trashPurgestring | numberanyPurge item by id
trashEmptystring |numberanyEmpty trash data

Modules

This store will contain modules:

  • Notification: success and error notification control
  • Dashboard: list item with search, filter, pagination control
  • DetailPage: detail page control
  • CreatePage: create page control
  • UpdatePage: update page control
  • ConfirmRemove: confirm remove control
  • ConfirmRestore: confirm restore control
  • ConfirmPurge: confirm purge control
  • ConfirmTrashEmpty: confirm trash empty control

State

type AppState = {
  Notification: {
    success: {
      visible: boolean; // default False: success notification visible control
      message: string | null; // default null: success notification message
    };
    error: {
      visible: boolean; //default False: error notification visible control
      message: string | null; // default null: success notification message
    };
  };
  Dashboard: {
    requestId: number; // last request id, increment, avoid response is not last response
    loading: boolean; // request loading
    error: null | { message: string }; // request error
    success: boolean; // === true if request success
    items: array; // list data
    limit: number; // pagination limit
    page: number; // pagination current page
    total: number; // total items found
    filter: any; // request filter, can update by user via UI
    trashMode: boolean; // === true if go to trash. Then .items is trash items
    status: {
      normalTotal: number; // count total item (available)
      trashTotal: number; // count total item in trash (deleted)
    };
  };
  DetailPage: {
    visible: boolean; // === true if you show detail page. Used to control show detail page
    requestId: number; // last request id
    id: null | number | string; // item id
    loading: boolean; // fetch item data loading
    error: null | { message: string }; // fetch item data error
    success: boolean; // fetch item data success
    inputData: null | any; // a part of data you can show before fetchedData crawled
    fetchedData: null | any; // data fetched
  };
  CreatePage: {
    visible: boolean; // === true if you show create page. Used to control show create page
    loading: boolean; // create requesting
    error: null | { message: string }; // create request error
    success: boolean; // create request success
    submittedData: null | any; // submitted data from UI form. Example: when you click button submit
    resultData: null | any; // response of create request
  };
  UpdatePage: {
    visible: boolean; // === true if you show update page. Used to control show update page

    id: null | string | number; // id of updated item
    inputData: null | any; // a part of data you can show before fetchedData crawled

    fetchLoading: boolean; // item data fetching
    fetchError: null | { message: string };
    fetchSuccess: boolean;
    fetchedData: null | any; // response after fetch success

    updateLoading: boolean; // update requesting
    updateError: null | { message: string };
    updateSuccess: boolean;
    submittedData: null | any; // submitted data from UI form. Example: when you click button submit
    resultData: null | any; // response after create success
  };
  ConfirmRemove: {
    visible: boolean; // === true if you show confirm remove dialog. Used to control show confirm remove dialog
    id: null | string | number; // id of removed item
    inputData: null | any; // a part of data you can show before fetchedData crawled
    loading: boolean; // remove requesting
    error: null | { message: string }; // remove error
    success: boolean; // remove success
  };
  ConfirmRestore: {
    visible: boolean; // === true if you show confirm restore dialog. Used to control show confirm restore dialog
    id: null | string | number; // a part of data you can show in UI
    inputData: null | any;
    loading: boolean;
    error: null | { message: string };
    success: boolean;
  };
  ConfirmPurge: {
    visible: boolean; //  === true if you show confirm purge dialog. Used to control show confirm purge dialog
    id: null | string | number;
    inputData: null | any;
    loading: boolean;
    error: null | { message: string };
    success: boolean;
  };
  ConfirmTrashEmpty: {
    visible: boolean; //  === true if you show confirm trash empty dialog. Used to control show confirm trash empty dialog
    loading: boolean;
    error: null | { message: string };
    success: boolean;
  };
};

Module

Dashboard

State

export interface DashboardState {
  requestId: number;
  loading: boolean;
  error: null | { message: string };
  success: boolean;
  items: any[];
  limit: number;
  page: number;
  total: number;
  filter: any;
  trashMode: boolean;
  status: {
    normalTotal: number;
    trashTotal: number;
  };
}

Actions

ActionParamsDescripiton
/Dashboard/fetchFetch dashboard data
/Dashboard/updateTrashModebooleanUpdate Trash Mode, true is trash and false is normal
/Dashboard/updateFilterobjectUpdate filter
/Dashboard/updatePagenumberUpdate page
/Dashboard/updateLimitnumberUpdate page size

Notification

State

export interface NotificationState {
  success: {
    visible: boolean;
    message: null | string;
  };
  error: {
    visible: boolean;
    message: null | string;
  };
}

Actions

ActionParamsDescripiton
/Notification/showSuccessstringShow success notification
/Notification/hideSuccessHide success Notification
/Notification/showErrorstringShow error notification
/Notification/hideErrorHide error notification

Detail page

State

export interface DetailState {
  visible: boolean;
  requestId: number;
  id: null | string | number;
  loading: boolean;
  error: null | { message: string };
  success: boolean;
  inputData: null | any;
  fetchedData: null | any;
}

Actions

ActionParamsDescripiton
/DetailPage/open{id: string, data: any}Open detail page
/DetailPage/closeClose detail page
/DetailPage/fetchFetch detail data

Create page

State

export interface CreateState {
  visible: boolean;
  loading: boolean;
  error: null | { message: string };
  success: boolean;
  submittedData: null | any;
  resultData: null | any;
}

Actions

ActionParamsDescripiton
/CreatePage/openOpen create page
/CreatePage/closeClose create page
/CreatePage/createanySend create request

Update page

State

export interface UpdateState {
  visible: boolean;

  id: null | string | number;
  inputData: null | any;

  fetchLoading: boolean;
  fetchError: null | { message: string };
  fetchSuccess: boolean;
  fetchedData: null | any;

  updateLoading: boolean;
  updateError: null | { message: string };
  updateSuccess: boolean;
  submittedData: null | any;
  resultData: null | any;
}

Actions

ActionParamsDescripiton
/UpdatePage/open{id: string, data: any}Open update page
/UpdatePage/closeClose update page
/UpdatePage/fetchSend fetch request
/UpdatePage/updateanySend update request

Confirm remove

State

export interface RemoveState {
  visible: boolean;
  id: null | string | number;
  inputData: null | any;
  loading: boolean;
  error: null | { message: string };
  success: boolean;
}

Actions

ActionParamsDescripiton
/ConfirmRemove/open{id: string, data: any}Open confirm remove dialog
/ConfirmRemove/closeClose confirm remove dialog
/ConfirmRemove/removeSend remove request

Confirm restore

State

export interface RestoreState {
  visible: boolean;
  id: null | string | number;
  inputData: null | any;
  loading: boolean;
  error: null | { message: string };
  success: boolean;
}

Actions

ActionParamsDescripiton
/ConfirmRestore/open{id: string, data: any}Open confirm restore dialog
/ConfirmRestore/closeClose confirm restore dialog
/ConfirmRestore/restoreSend restore request

Confirm purge

State

export interface PurgeState {
  visible: boolean;
  id: null | string | number;
  inputData: null | any;
  loading: boolean;
  error: null | { message: string };
  success: boolean;
}

Actions

ActionParamsDescripiton
/ConfirmPurge/open{id: string, data: any}Open confirm purge dialog
/ConfirmPurge/closeClose confirm purge dialog
/ConfirmPurge/restoreSend purge request

Confirm empty trash

State

export interface TrashEmptyState {
  visible: boolean;
  loading: boolean;
  error: null | { message: string };
  success: boolean;
}

Actions

ActionParamsDescripiton
/ConfirmTrashEmpty/openOpen confirm empty trash dialog
/ConfirmTrashEmpty/closeClose confirm empty trsh dialog
/ConfirmTrashEmpty/emptySend empty request

Change log

1.0.0

First release