badguardhome/client/src/reducers/dhcp.js
Ainar Garipov 5104b79cf6 Pull request: client: add reset leases btn
Updates #1691.

Squashed commit of the following:

commit 2c48fb956aba28eae47071c9f7f4d579dde12955
Merge: 38f5191b 7547d3a4
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Jun 17 14:28:23 2021 +0300

    Merge branch 'master' into 1691-dhcp-reset-form

commit 38f5191bcd62eb53e4663fccdfc2a60247881931
Author: Ildar Kamalov <ik@adguard.com>
Date:   Thu Jun 17 13:14:59 2021 +0300

    client: handle dhcp leases reset

commit a97df17028ca640fd32b4d9762aa54fb381df7e5
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Jun 16 17:12:10 2021 +0300

    client: add reset leases btn
2021-06-17 14:32:33 +03:00

208 lines
5.9 KiB
JavaScript

import { handleActions } from 'redux-actions';
import * as actions from '../actions';
import { enrichWithConcatenatedIpAddresses } from '../helpers/helpers';
const dhcp = handleActions(
{
[actions.getDhcpStatusRequest]: (state) => ({
...state,
processing: true,
}),
[actions.getDhcpStatusFailure]: (state) => ({
...state,
processing: false,
}),
[actions.getDhcpStatusSuccess]: (state, { payload }) => {
const { static_leases: staticLeases, ...values } = payload;
const newState = {
...state,
staticLeases,
processing: false,
...values,
};
return newState;
},
[actions.getDhcpInterfacesRequest]: (state) => ({
...state,
processingInterfaces: true,
}),
[actions.getDhcpInterfacesFailure]: (state) => ({
...state,
processingInterfaces: false,
}),
[actions.getDhcpInterfacesSuccess]: (state, { payload }) => {
const newState = {
...state,
interfaces: enrichWithConcatenatedIpAddresses(payload),
processingInterfaces: false,
};
return newState;
},
[actions.findActiveDhcpRequest]: (state) => ({
...state,
processingStatus: true,
}),
[actions.findActiveDhcpFailure]: (state) => ({
...state,
processingStatus: false,
}),
[actions.findActiveDhcpSuccess]: (state, { payload }) => {
const newState = {
...state,
check: payload,
processingStatus: false,
};
return newState;
},
[actions.toggleDhcpRequest]: (state) => ({
...state,
processingDhcp: true,
}),
[actions.toggleDhcpFailure]: (state) => ({
...state,
processingDhcp: false,
}),
[actions.toggleDhcpSuccess]: (state) => {
const { enabled } = state;
const newState = {
...state,
enabled: !enabled,
check: null,
processingDhcp: false,
};
return newState;
},
[actions.setDhcpConfigRequest]: (state) => ({
...state,
processingConfig: true,
}),
[actions.setDhcpConfigFailure]: (state) => ({
...state,
processingConfig: false,
}),
[actions.setDhcpConfigSuccess]: (state, { payload }) => {
const { v4, v6 } = state;
const newConfigV4 = { ...v4, ...payload.v4 };
const newConfigV6 = { ...v6, ...payload.v6 };
const newState = {
...state,
v4: newConfigV4,
v6: newConfigV6,
interface_name: payload.interface_name,
processingConfig: false,
};
return newState;
},
[actions.resetDhcpRequest]: (state) => ({
...state,
processingReset: true,
}),
[actions.resetDhcpFailure]: (state) => ({
...state,
processingReset: false,
}),
[actions.resetDhcpSuccess]: (state) => ({
...state,
processingReset: false,
enabled: false,
v4: {},
v6: {},
interface_name: '',
}),
[actions.resetDhcpLeasesSuccess]: (state) => ({
...state,
leases: [],
staticLeases: [],
}),
[actions.toggleLeaseModal]: (state) => {
const newState = {
...state,
isModalOpen: !state.isModalOpen,
};
return newState;
},
[actions.addStaticLeaseRequest]: (state) => ({
...state,
processingAdding: true,
}),
[actions.addStaticLeaseFailure]: (state) => ({
...state,
processingAdding: false,
}),
[actions.addStaticLeaseSuccess]: (state, { payload }) => {
const { ip, mac, hostname } = payload;
const newLease = {
ip,
mac,
hostname: hostname || '',
};
const leases = [...state.staticLeases, newLease];
const newState = {
...state,
staticLeases: leases,
processingAdding: false,
};
return newState;
},
[actions.removeStaticLeaseRequest]: (state) => ({
...state,
processingDeleting: true,
}),
[actions.removeStaticLeaseFailure]: (state) => ({
...state,
processingDeleting: false,
}),
[actions.removeStaticLeaseSuccess]: (state, { payload }) => {
const leaseToRemove = payload.ip;
const leases = state.staticLeases.filter((item) => item.ip !== leaseToRemove);
const newState = {
...state,
staticLeases: leases,
processingDeleting: false,
};
return newState;
},
},
{
processing: true,
processingStatus: false,
processingInterfaces: false,
processingDhcp: false,
processingConfig: false,
processingAdding: false,
processingDeleting: false,
enabled: false,
interface_name: '',
check: null,
v4: {
gateway_ip: '',
subnet_mask: '',
range_start: '',
range_end: '',
lease_duration: 0,
},
v6: {
range_start: '',
lease_duration: 0,
},
leases: [],
staticLeases: [],
isModalOpen: false,
dhcp_available: false,
},
);
export default dhcp;