yapi/client/reducer/modules/interface.js

105 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-08-11 18:57:58 +08:00
import axios from 'axios'
2017-08-07 19:34:34 +08:00
// Actions
const FETCH_INTERFACE_DATA = 'yapi/interface/FETCH_INTERFACE_DATA';
2017-08-11 18:57:58 +08:00
const FETCH_INTERFACE_LIST = 'yapi/interface/FETCH_INTERFACE_LIST';
const CHANGE_INTERFACE_ID = 'yapi/interface/CHANGE_INTERFACE_ID';
const ADD_INTERFACE_DATA = 'yapi/interface/ADD_INTERFACE_DATA';
const DELETE_INTERFACE_DATA = 'yapi/interface/DELETE_INTERFACE_DATA';
2017-08-11 18:57:58 +08:00
// const SAVE_INTERFACE_PROJECT_ID = 'yapi/interface/SAVE_INTERFACE_PROJECT_ID';
// const GET_INTERFACE_GROUP_LIST = 'yapi/interface/GET_INTERFACE_GROUP_LIST';
2017-08-07 15:40:21 +08:00
2017-08-07 19:34:34 +08:00
// Reducer
2017-08-07 15:40:21 +08:00
const initialState = {
interfaceId: 0,
2017-08-11 18:57:58 +08:00
curdata: {},
list: []
2017-08-07 15:40:21 +08:00
}
export default (state = initialState, action) => {
switch (action.type) {
case DELETE_INTERFACE_DATA:
return (() => {
let newlist = state.list.filter(data=> data._id !== action.payload), newid, curdata;
if (state.interfaceId === action.payload && state.list.length > 0) {
newid = state.list[0]._id
curdata = state.list[0]
} else if (state.list.length == 0) {
newid = 0;
curdata = {}
}
return {
...state,
curdata: curdata,
interfaceId: newid,
list: newlist
}
})()
case ADD_INTERFACE_DATA:
return {
...state,
curdata: action.payload,
list: [].concat(state.list, action.payload),
interfaceId: action.payload._id
}
case CHANGE_INTERFACE_ID:
return {
...state,
interfaceId: action.payload
}
2017-08-07 15:40:21 +08:00
case FETCH_INTERFACE_DATA:
return {
...state,
2017-08-11 18:57:58 +08:00
curdata: action.payload.data
2017-08-07 15:40:21 +08:00
}
2017-08-11 18:57:58 +08:00
case FETCH_INTERFACE_LIST:
2017-08-07 15:40:21 +08:00
return {
...state,
2017-08-11 18:57:58 +08:00
list: action.payload.data
}
2017-08-07 15:40:21 +08:00
default:
return state
}
}
2017-08-07 19:34:34 +08:00
export function changeInterfaceId(id) {
return {
type: CHANGE_INTERFACE_ID,
payload: id
}
}
export async function deleteInterfaceData(id) {
await axios.post('/api/interface/del', { id: id })
return {
type: DELETE_INTERFACE_DATA,
payload: id
}
}
2017-08-07 19:34:34 +08:00
// Action Creators
export async function fetchInterfaceData(interfaceId) {
let result = await axios.get('/api/interface/get?id=' + interfaceId);
return {
type: FETCH_INTERFACE_DATA,
payload: result.data
2017-08-07 19:34:34 +08:00
}
}
export async function fetchInterfaceList(projectId) {
let result = await axios.get('/api/interface/list?project_id=' + projectId);
return {
type: FETCH_INTERFACE_LIST,
payload: result.data
2017-08-07 19:34:34 +08:00
}
}
export async function addInterfaceData(data) {
return {
type: ADD_INTERFACE_DATA,
payload: data
}
}