src/components/view-items.js
// This is called every time something is updated in the store.
This is called every time something is updated in the store.
This is called every time something is updated in the store .
stateChanged(state) {
    if (state.app.user && (state.items.items.length === 0)) {
      store.dispatch(getItems());
      store.dispatch(getItemTypes());
    }
    if (state.items.items.length > 0) {
      this._items = state.items.items.map(item => {
        item.owner = item.owner.split('@')[0];
        const date = new Date(parseInt(item.lastupdate));
        item.lastupdateString = date.toLocaleString();
        return item;
      });
      this._items.sort((a, b) => a.id.localeCompare(b.id));
      const [latestItem] = this._items.slice(-1);
      const lastIdSplit = latestItem.id.split('-');
      const currentYear = (new Date()).getFullYear();
      let newId = 'AWGS-' + currentYear + '-';
      if (lastIdSplit[1] == currentYear) {
        newId += (parseInt(lastIdSplit[2]) + 1).toString().padStart(3, '0');
      } else {
        newId += '001';
      }
      this._nextId = newId;
      this._resetForm();
    }
    if (state.items.itemtypes.length > 0) {
      this._itemtypes = state.items.itemtypes;
    }
  }
stateChanged ( state ) { if ( state . app . user && ( state . items . items . length === 0 ) ) { store . dispatch ( getItems ( ) ) ; store . dispatch ( getItemTypes ( ) ) ; } if ( state . items . items . length > 0 ) { this . _items = state . items . items . map ( item => { item . owner = item . owner . split ( '@' ) [ 0 ] ; const date = new Date ( parseInt ( item . lastupdate ) ) ; item . lastupdateString = date . toLocaleString ( ) ; return item ; } ) ; this . _items . sort ( ( a , b ) => a . id . localeCompare ( b . id ) ) ; const [ latestItem ] = this . _items . slice ( - 1 ) ; const lastIdSplit = latestItem . id . split ( '-' ) ; const currentYear = ( new Date ( ) ) . getFullYear ( ) ; let newId = 'AWGS-' + currentYear + '-' ; if ( lastIdSplit [ 1 ] == currentYear ) { newId += ( parseInt ( lastIdSplit [ 2 ] ) + 1 ) . toString ( ) . padStart ( 3 , '0' ) ; } else { newId += '001' ; } this . _nextId = newId ; this . _resetForm ( ) ; } if ( state . items . itemtypes . length > 0 ) { this . _itemtypes = state . items . itemtypes ; } }
This is called every time something is updated in the store.
This is called every time something is updated in the store .
//TODO: use wait-for-redux or something similar for automatically dispatching once we're signed in // strip owner name // format date // sort array because we cannot rely on how the server returns the results // happy new year! // in case we just sent an item to the server reset the form //TODO: only reset after saving item