{"size": 1498, "ext": "js", "max_stars_repo_path": "src/store/module/secrets.js", "max_stars_repo_name": "FlowCI/flow-ui-x", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/store/module/secrets.js", "max_issues_repo_name": "FlowCI/flow-ui-x", "max_issues_repo_issues_event_min_datetime": "2021-03-08T19:52:56.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T04:08:36.000Z", "max_forks_repo_path": "src/store/module/secrets.js", "max_forks_repo_name": "gy2006/flow-web-x", "max_forks_repo_forks_event_min_datetime": "2019-03-03T14:09:47.000Z", "max_forks_repo_forks_event_max_datetime": "2019-03-03T14:09:47.000Z"}
{"max_stars_count": null, "max_issues_count": 5, "max_forks_count": 1, "avg_line_length": 17.2183908046, "max_line_length": 61, "alphanum_fraction": 0.523364486}
import http from '../http'

const state = {
  items: [],
  loaded: {
    name: '',
    privateKey: '',
    publicKey: ''
  }
}

const mutations = {
  add (state, credential) {
    state.items.push(credential)
  },

  remove (state, credential) {
    for (let i = 0; i < state.items.length; i++) {
      if (state.items[i].id === credential.id) {
        state.items.splice(i, 1)
        return
      }
    }
  },

  list (state, credentials) {
    state.items = credentials
  },

  loaded (state, credential) {
    state.loaded = credential
  }
}

const actions = {
  list ({commit}) {
    http.get('secrets', (c) => {
      commit('list', c)
    })
  },

  listNameOnly ({commit}, category) {
    http.get('secrets/list/name', (c) => {
      commit('list', c)
    }, {category})
  },

  async createRsa ({commit}, {name, publicKey, privateKey}) {
    await http.post('secrets/rsa', (c) => {
      commit('add', c)
    }, {
      name,
      publicKey,
      privateKey
    })
  },

  async createAuth({commit}, {name, username, password}) {
    await http.post('secrets/auth', (c) => {
      commit('add', c)
    }, {
      name,
      username,
      password
    })
  },

  async delete ({commit}, credential) {
    await http.delete(`secrets/${credential.name}`, (c) => {
      commit('remove', c)
    })
  },

  get ({commit}, name) {
    http.get(`secrets/${name}`, (c) => {
      commit('loaded', c)
    })
  }
}

export const Store = {
  namespaced: true,
  state,
  mutations,
  actions
}
