{"size": 1827, "ext": "js", "max_stars_repo_path": "praas-app/src/store/conduit/edit.js", "max_stars_repo_name": "A1Aadi/praas", "max_stars_repo_stars_event_min_datetime": "2019-01-02T22:06:42.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-13T13:15:00.000Z", "max_issues_repo_path": "dashboard/src/store/conduit/edit.js", "max_issues_repo_name": "conduits-xyz/conduits", "max_issues_repo_issues_event_min_datetime": "2019-01-01T11:30:59.000Z", "max_issues_repo_issues_event_max_datetime": "2020-11-28T11:06:14.000Z", "max_forks_repo_path": "dashboard/src/store/conduit/edit.js", "max_forks_repo_name": "conduits-xyz/conduits", "max_forks_repo_forks_event_min_datetime": "2020-09-03T19:12:52.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-13T18:26:36.000Z"}
{"max_stars_count": 4, "max_issues_count": 147, "max_forks_count": 2, "avg_line_length": 28.1076923077, "max_line_length": 86, "alphanum_fraction": 0.6737821565}
import PraasAPI from 'api/praas';

// This is a create-conduit duck. A duck is a feature state container.

const UPDATE_CONDUIT_REQUEST = 'conduit/UPDATE_CONDUIT_REQUEST';
const UPDATE_CONDUIT_SUCCESS = 'conduit/UPDATE_CONDUIT_SUCCESS';
const UPDATE_CONDUIT_FAILURE = 'conduit/UPDATE_CONDUIT_FAILURE';

// Sync action creators
export const updateConduitSuccess = (conduit) => ({
  type: UPDATE_CONDUIT_SUCCESS, payload: conduit
});

export const updateConduitFailure = (errors) => ({
  type: UPDATE_CONDUIT_FAILURE, payload: errors
});

// Async action creators
export const updateConduit = (conduit) => (dispatch) => {
  dispatch({ type: UPDATE_CONDUIT_REQUEST, payload: conduit });
  console.log('updateConduit: ', conduit.id, Object.keys(conduit));
  return PraasAPI.conduit.update(conduit).then(
    (data) => {
      // console.log('updateConduit, successs: ', payload.conduit.id);
      dispatch(updateConduitSuccess(data.conduit));
      return Promise.resolve(data.conduit);
    },
    (error) => {
      dispatch(updateConduitFailure(error.errors));
      return Promise.reject(error.errors);
    }
  );
};

const initialState = { inflight: false, errors: {} };

export default function create(state = initialState, { type, payload }) {
  switch (type) {
  case UPDATE_CONDUIT_REQUEST:
    return {
      inflight: true,
      errors: {},
      ...payload,
    };
  case UPDATE_CONDUIT_SUCCESS:
    return {
      inflight: false,
      errors: {},
      conduit: { ...state.conduit, ...payload },
    };
  case UPDATE_CONDUIT_FAILURE:
    // console.log('Deal with this:', payload);
    return {
      inflight: false,
      errors: { ...payload },
    };
  default:
    return state;
  };
};

// selector
export const getConduit
  = (state, cid) => state.conduit.list.conduits.find((conduit) => conduit.id === cid);
