storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/browser/app/js/objects/reducer.js (about)

     1  /*
     2   * MinIO Cloud Storage (C) 2018 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import * as actionsObjects from "./actions"
    18  import { SORT_ORDER_ASC } from "../constants"
    19  
    20  const removeObject = (list, objectToRemove, lookup) => {
    21    const idx = list.findIndex(object => lookup(object) === objectToRemove)
    22    if (idx == -1) {
    23      return list
    24    }
    25    return [...list.slice(0, idx), ...list.slice(idx + 1)]
    26  }
    27  
    28  export default (
    29    state = {
    30      list: [],
    31      filter: "",
    32      listLoading: false,
    33      sortBy: "",
    34      sortOrder: SORT_ORDER_ASC,
    35      currentPrefix: "",
    36      prefixWritable: false,
    37      shareObject: {
    38        show: false,
    39        object: "",
    40        url: ""
    41      },
    42      checkedList: []
    43    },
    44    action
    45  ) => {
    46    switch (action.type) {
    47      case actionsObjects.SET_LIST:
    48        return {
    49          ...state,
    50          list: action.objects
    51        }
    52      case actionsObjects.RESET_LIST:
    53        return {
    54          ...state,
    55          list: []
    56        }
    57      case actionsObjects.SET_FILTER:
    58        return {
    59          ...state,
    60          filter: action.filter
    61        }
    62      case actionsObjects.SET_LIST_LOADING:
    63        return {
    64          ...state,
    65          listLoading: action.listLoading
    66        }
    67      case actionsObjects.REMOVE:
    68        return {
    69          ...state,
    70          list: removeObject(state.list, action.object, object => object.name)
    71        }
    72      case actionsObjects.SET_SORT_BY:
    73        return {
    74          ...state,
    75          sortBy: action.sortBy
    76        }
    77      case actionsObjects.SET_SORT_ORDER:
    78        return {
    79          ...state,
    80          sortOrder: action.sortOrder
    81        }
    82      case actionsObjects.SET_CURRENT_PREFIX:
    83        return {
    84          ...state,
    85          currentPrefix: action.prefix
    86        }
    87      case actionsObjects.SET_PREFIX_WRITABLE:
    88        return {
    89          ...state,
    90          prefixWritable: action.prefixWritable
    91        }
    92      case actionsObjects.SET_SHARE_OBJECT:
    93        return {
    94          ...state,
    95          shareObject: {
    96            show: action.show,
    97            object: action.object,
    98            url: action.url,
    99            showExpiryDate: action.showExpiryDate
   100          }
   101        }
   102      case actionsObjects.CHECKED_LIST_ADD:
   103        return {
   104          ...state,
   105          checkedList: [...state.checkedList, action.object]
   106        }
   107      case actionsObjects.CHECKED_LIST_REMOVE:
   108        return {
   109          ...state,
   110          checkedList: removeObject(
   111            state.checkedList,
   112            action.object,
   113            object => object
   114          )
   115        }
   116      case actionsObjects.CHECKED_LIST_RESET:
   117        return {
   118          ...state,
   119          checkedList: []
   120        }
   121      default:
   122        return state
   123    }
   124  }