admin.ts 5.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import {
  ADMIN_APPEND_OAUTH_CLIENTS,
  ADMIN_APPEND_USERS,
  ADMIN_RECEIVE_OAUTH_ACCESS_TOKENS,
  ADMIN_RECEIVE_OAUTH_CLIENT,
  ADMIN_RECEIVE_OAUTH_CLIENT_DETAILS,
  ADMIN_RECEIVE_OAUTH_REFRESH_TOKENS,
  ADMIN_RECEIVE_USER,
  ADMIN_REMOVE_OAUTH_ACCESS_TOKEN,
  ADMIN_REMOVE_OAUTH_CLIENT,
  ADMIN_REMOVE_OAUTH_REFRESH_TOKEN,
Oleksii Savran's avatar
Oleksii Savran committed
12
  ADMIN_TOGGLE_USER_MODAL,
13
} from 'user/constants';
Maxym Borodenko's avatar
Maxym Borodenko committed
14 15 16
import update from 'immutability-helper';

import FilteredPage from 'model/FilteredPage';
17 18 19 20
import { IReducerAction } from 'model/common.model';
import { User } from 'model/user/User';
import OAuthClient from 'model/oauth/OAuthClient';
import ClientDetails from 'model/oauth/ClientDetails';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
21
import ApiCall from 'model/ApiCall';
Maxym Borodenko's avatar
Maxym Borodenko committed
22

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
23
const INITIAL_STATE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
24 25 26 27
  user: ApiCall<User>;
  paged: ApiCall<FilteredPage<User>>;
  oAuthClient: ApiCall<ClientDetails>;
  oAuthPaged: ApiCall<FilteredPage<OAuthClient>>;
Oleksii Savran's avatar
Oleksii Savran committed
28
  modalIsOpen: boolean;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
29
} = {
Maxym Borodenko's avatar
Maxym Borodenko committed
30 31
  user: null,
  paged: null,
32 33
  oAuthClient: null,
  oAuthPaged: null,
Oleksii Savran's avatar
Oleksii Savran committed
34
  modalIsOpen: false,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
35 36
};

Maxym Borodenko's avatar
Maxym Borodenko committed
37
export default function user(state = INITIAL_STATE, action: IReducerAction) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
38 39

  switch (action.type) {
Maxym Borodenko's avatar
Maxym Borodenko committed
40 41

    case ADMIN_RECEIVE_USER: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
42 43
      const {apiCall} = action.payload;
      const receivedIndex = state.paged && state.paged.data && apiCall.data ? state.paged.data.content.findIndex((item) => item.uuid === apiCall.data.uuid) : -1;
Maxym Borodenko's avatar
Maxym Borodenko committed
44 45 46

      if (receivedIndex !== -1) {
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
47
          user: {$set: apiCall},
Maxym Borodenko's avatar
Maxym Borodenko committed
48
          paged: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
49 50 51 52
            data: {
              content: {
                [receivedIndex]: {$set: apiCall.data},
              },
Maxym Borodenko's avatar
Maxym Borodenko committed
53 54 55 56 57
            },
          },
        });
      } else {
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
58
          user: {$set: apiCall},
Maxym Borodenko's avatar
Maxym Borodenko committed
59 60 61
        });
      }
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
62

Maxym Borodenko's avatar
Maxym Borodenko committed
63
    case ADMIN_APPEND_USERS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
64
      const {apiCall: {loading, error, timestamp, data}} = action.payload;
65
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
66 67 68 69 70 71 72 73
        paged: {
          $set: {
            loading,
            error,
            timestamp,
            data: FilteredPage.merge(state.paged && state.paged.data, data),
          },
        },
74 75
      });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
76

77
    case ADMIN_APPEND_OAUTH_CLIENTS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
78 79 80 81 82 83 84 85
      const {apiCall: {loading, error, timestamp, data}} = action.payload;
      return update(state, {
        oAuthPaged: {
          $set: {
            loading,
            error,
            timestamp,
            data: FilteredPage.merge(state.oAuthPaged && state.oAuthPaged.data, data),
86
          },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
87 88
        },
      });
89
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
90

91
    case ADMIN_RECEIVE_OAUTH_CLIENT_DETAILS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
92
      const { apiCall } = action.payload;
93
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
94
        oAuthClient: {$set: apiCall},
95 96 97
      });
    }
    case ADMIN_RECEIVE_OAUTH_CLIENT: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
98
      const { apiCall } = action.payload;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
99
      return state.oAuthClient ? update(state, {
100
        oAuthClient: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
101 102 103
            data: {
              clientDetails: {$set: apiCall.data},
            },
104
          },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
105 106 107 108 109 110 111 112 113
      })
      : update(state, {
        oAuthClient: {
          $set: {
            ...apiCall,
            data: {
              clientDetails: apiCall.data,
            },
          },
114 115 116 117 118
        },
      });
    }

    case ADMIN_REMOVE_OAUTH_CLIENT: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
119 120
      const { apiCall } = action.payload;
      const removeIndex = state.oAuthPaged && state.oAuthPaged.data && apiCall.data && state.oAuthPaged.data.content ? state.oAuthPaged.data.content.findIndex((client) => client.clientId === apiCall.data) : -1;
121 122 123 124 125

      return removeIndex !== -1 ?
        update(state, {
          oAuthClient: { $set: null },
          oAuthPaged: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
126 127 128
            data: {
              content: {$splice: [[removeIndex, 1]]},
            },
129 130 131 132 133 134 135 136 137
          },
        })
        :
        update(state, {
          oAuthClient: { $set: null },
        });
    }

    case ADMIN_REMOVE_OAUTH_ACCESS_TOKEN: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
138 139
      const { apiCall } = action.payload;
      const removeIndex = apiCall.data && state.oAuthClient && state.oAuthClient.data && state.oAuthClient.data.accessTokens ? state.oAuthClient.data.accessTokens.findIndex((token) => token.tokenId === apiCall.data) : -1;
140 141 142 143

      return removeIndex !== -1 ?
        update(state, {
          oAuthClient: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
144
            data: {
145 146 147 148 149 150 151
              accessTokens: { $splice: [[removeIndex, 1]] },
            },
          },
        }) : state;
    }

    case ADMIN_RECEIVE_OAUTH_ACCESS_TOKENS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
152
      const { apiCall } = action.payload;
153 154 155

      return update(state, {
        oAuthClient: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
156 157
          data: {
            accessTokens: { $set: apiCall.data },
158 159 160 161 162 163
          },
        },
      });
    }

    case ADMIN_REMOVE_OAUTH_REFRESH_TOKEN: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
164 165
      const { apiCall } = action.payload;
      const removeIndex = state.oAuthClient && state.oAuthClient.data && state.oAuthClient.data.refreshTokens ? state.oAuthClient.data.refreshTokens.findIndex((token) => token.tokenId === apiCall.data) : -1;
166 167 168 169

      return removeIndex !== -1 ?
        update(state, {
          oAuthClient: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
170
            data: {
171 172 173 174 175 176 177
              refreshTokens: { $splice: [[removeIndex, 1]] },
            },
          },
        }) : state;
    }

    case ADMIN_RECEIVE_OAUTH_REFRESH_TOKENS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
178
      const { apiCall } = action.payload;
179 180 181

      return update(state, {
        oAuthClient: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
182 183
          data: {
            refreshTokens: { $set: apiCall.data },
184 185 186 187
          },
        },
      });
    }
Maxym Borodenko's avatar
Maxym Borodenko committed
188

Oleksii Savran's avatar
Oleksii Savran committed
189 190 191 192 193 194
    case ADMIN_TOGGLE_USER_MODAL: {
      return update(state, {
        modalIsOpen: { $set: action.payload },
      });
    }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
195 196 197 198
    default:
      return state;
  }
}