Step 7 - gunjandatta/sp-dashboard-vue GitHub Wiki

Store (./src/store.ts)

Imports & Initialization

import Vue from "vue";
import Vuex from "vuex";
import { ContextInfo, List, SPTypes } from "gd-sprest-bs";
import Strings from "./strings";

// Use Vuex
Vue.use(Vuex);

/**
 * Item
 * Sample interface for the SharePoint data
 */
interface IItem {
    Id: number;
    ItemType: string;
    Status: string;
    Title: string;
}

State & Methods

  • State
    • displayFormUrl The display form url
    • editFormUrl The edit form url
    • newFormUrl The new form url
    • filterText The selected filter
    • items The list items
    • rows The table rows
    • searchText The search text
  • Mutations
    • All state values will have an associated method for setting the value
    • Must be synchronous
  • Actions
    • Any asynchronous methods will be defined here_
    • We will load the SharePoint form urls here
    • We will load the SharePoint list data here
  • Getters
    • Similar to computed, we will create a method to return the rows and apply the filter if defined
/**
 * Store
 */
export default new Vuex.Store({
    // Application State
    state: {
        displayFormUrl: null,
        editFormUrl: null,
        newFormUrl: null,
        filterText: "",
        items: null,
        rows: null,
        searchText: ""
    },

    // Synchronous Methods
    mutations: {
        setItems(state, items) {
            // Update the items
            state.items = items;
        },

        setFilter(state, value) {
            // Update the filter text
            state.filterText = value;
        },

        setSearch(state, value) {
            // Update the search text
            state.searchText = value;
        },

        setUrls(state, value) {
            // Update the urls
            state.displayFormUrl = value.displayFormUrl;
            state.editFormUrl = value.editFormUrl;
            state.newFormUrl = value.newFormUrl;
        }
    },

    // Asynchronous Methods
    actions: {
        loadFormUrls(store) {
            let formUrls = { displayFormUrl: "", editFormUrl: "", newFormUrl: "" };

            // See if the SP environment exists
            if (ContextInfo.existsFl) {
                // Load the forms
                List(Strings.Lists.Main).Forms().execute(forms => {
                    // Parse the forms
                    for (let i = 0; i < forms.results.length; i++) {
                        let form = forms.results[i];

                        // Save the url, based on the type
                        switch (form.FormType) {
                            // Display
                            case SPTypes.PageType.DisplayForm:
                                formUrls.displayFormUrl = form.ServerRelativeUrl;
                                break;

                            // Edit
                            case SPTypes.PageType.EditForm:
                                formUrls.editFormUrl = form.ServerRelativeUrl;
                                break;

                            // New
                            case SPTypes.PageType.NewForm:
                                formUrls.newFormUrl = form.ServerRelativeUrl;
                                break;
                        }
                    }

                    // Default the form urls
                    store.commit("setUrls", formUrls);
                });
            } else {
                // Default the form urls
                store.commit("setUrls", formUrls);
            }
        },

        loadItems(store) {
            // See if the SP environment exists
            if (ContextInfo.existsFl) {
                // Load the list items
                List(Strings.Lists.Main).Items().query({
                    OrderBy: ["Title", "Status"]
                }).execute(
                    // Success
                    items => {
                        // Update the state
                        store.commit("setItems", items.results);
                    }

                    // Error
                    // TODO
                );
            } else {
                // Set test data
                store.commit("setItems", [
                    { Id: 1, ItemType: "Type 1", Title: "Item 1", Status: "Draft" },
                    { Id: 2, ItemType: "Type 1", Title: "Item 2", Status: "Submitted" },
                    { Id: 3, ItemType: "Type 2", Title: "Item 3", Status: "Approved" },
                    { Id: 4, ItemType: "Type 2", Title: "Item 4", Status: "Rejected" },
                    { Id: 5, ItemType: "Type 3", Title: "Item 5", Status: "Draft" }
                ]);
            }
        }
    },

    // Getters
    getters: {
        getRows(state): Array<IItem> {
            let items = [];

            // See if a filter exists
            if (state.filterText) {
                // Parse the items
                for (let i = 0; i < state.items.length; i++) {
                    let item = state.items[i];

                    if (item.Status == state.filterText) {
                        // Add the item
                        items.push(item);
                    }
                }
            } else {
                items = state.items;
            }

            // Return the table rows
            return items;
        }
    }
});

Main Source (./src/index.ts)

Now that we have defined the store, we must apply it similar to the router.

import Vue from "vue";
import App from "./app.vue";
import { Configuration } from "./cfg";
import router from "./router";
import store from "./store";
import Strings from "./strings";

// Create the global variable for this solution
window[Strings.GlobalVariable] = {
    Configuration
}

// Render the app
new Vue({
    router,
    store,
    el: "#" + Strings.AppElementId,
    render: h => h(App)
});
⚠️ **GitHub.com Fallback** ⚠️