HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux wordpress-ubuntu-s-2vcpu-4gb-fra1-01 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/shoetique/wp-content/plugins/woocommerce-stock-manager/admin/assets/src/store/image.js
import {
  IMAGE_FETCHING,
  IMAGE_SUCCESS,
  IMAGE_ERROR,
  IMAGE_INVALIDATE,
  IMAGES_FETCHING,
  IMAGES_SUCCESS,
  IMAGES_ERROR,
} from './actionTypes';

const initialState = () => ({
  isFetching: false,
  didInvalidate: false,
});

export const IMAGE_REDUCER_NAME = 'image';

export const imageReducer = (state = initialState(), action) => {
  const {
    type,
    data,
    error,
    ids,
    id,
  } = action;

  switch (type) {
    case IMAGE_FETCHING:
      return {
        ...state,
        [id]: {
          ...(state[id] || null),
          isFetching: true,
          didInvalidate: false,
        },
      };

    case IMAGE_SUCCESS:
      return {
        ...state,
        [id]: {
          ...data,
          isFetching: false,
          didInvalidate: false,
          lastUpdated: new Date(),
        },
      };

    case IMAGE_ERROR:
      return {
        ...state,
        [id]: {
          ...(state[id] || null),
          error,
          isFetching: false,
          didInvalidate: false,
        },
      };

    case IMAGE_INVALIDATE:
      return {
        ...state,
        [id]: {
          ...(state[id] || null),
          didInvalidate: true,
        },
      };

    case IMAGES_FETCHING: {
      const nextState = {
        ...state,
        isFetching: true,
        didInvalidate: false,
      };

      ids.forEach((id) => {
        nextState[id] = {
          ...(nextState[id] || null),
          isFetching: true,
          didInvalidate: true,
        };
      });

      return nextState;
    }

    case IMAGES_SUCCESS: {
      const nextState = {
        ...state,
        isFetching: false,
        didInvalidate: false,
        lastUpdated: new Date(),
      };

      data.forEach((image) => {
        nextState[image.id] = {
          ...image,
          isFetching: false,
          didInvalidate: false,
          lastUpdated: new Date(),
        };
      });

      return nextState;
    }

    case IMAGES_ERROR: {
      const nextState = {
        ...state,
        isFetching: false,
        didInvalidate: false,
        error,
      };

      ids.forEach((id) => {
        nextState[id] = {
          ...(nextState[id] || null),
          isFetching: false,
          didInvalidate: false,
          error,
        };
      });

      return nextState;
    }

    default:
      return state;
  }
};

export const getImage = (state, { id }) => state[IMAGE_REDUCER_NAME][id];

export const getMissingImages = (state, { ids = [] }) => ids.filter((id) => (
  !state[IMAGE_REDUCER_NAME][id]
));

export const fetchImage = (id) => ({
  types: {
    requestTypes: [IMAGE_FETCHING],
    successTypes: [IMAGE_SUCCESS],
    failureTypes: [IMAGE_ERROR],
  },
  endpoint: `wp/v2/media/${id}`,
  id,
  shouldCallAPI: (state) => {
    const image = state[IMAGE_REDUCER_NAME][id];

    if (!image) {
      return true;
    }

    if (image.isFetching) {
      return false;
    }

    return image.didInvalidate || !image.lastUpdated;
  },
});

export const fetchImages = (ids = []) => ({
  types: {
    requestTypes: [IMAGES_FETCHING],
    successTypes: [IMAGES_SUCCESS],
    failureTypes: [IMAGES_ERROR],
  },
  endpoint: 'wp/v2/media',
  query: { include: ids.join() },
  ids,
  shouldCallAPI: (state) => {
    const images = state[IMAGE_REDUCER_NAME];

    if (images.isFetching) {
      return false;
    }

    return ids.map((id) => !!images[id]).includes(false);
  },
});