2020-01-18 02:07:52 +05:30
|
|
|
import { Dispatch, Action as ReduxAction } from 'redux';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { OauthAppResponse } from 'app/services/api/oauth';
|
|
|
|
import oauth from 'app/services/api/oauth';
|
|
|
|
import { User } from 'app/components/user';
|
2020-01-18 02:07:52 +05:30
|
|
|
import { ThunkAction } from 'app/reducers';
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
import { Apps } from './reducer';
|
2018-05-05 12:12:21 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface SetAvailableAction extends ReduxAction {
|
2019-12-07 16:58:52 +05:30
|
|
|
type: 'apps:setAvailable';
|
|
|
|
payload: Array<OauthAppResponse>;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
2018-05-05 12:12:21 +05:30
|
|
|
|
|
|
|
export function setAppsList(apps: Array<OauthAppResponse>): SetAvailableAction {
|
2019-11-27 14:33:32 +05:30
|
|
|
return {
|
|
|
|
type: 'apps:setAvailable',
|
|
|
|
payload: apps,
|
|
|
|
};
|
2018-03-26 00:46:45 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
export function getApp(
|
|
|
|
state: { apps: Apps },
|
|
|
|
clientId: string,
|
2019-12-07 16:58:52 +05:30
|
|
|
): OauthAppResponse | null {
|
2020-05-20 22:05:52 +05:30
|
|
|
return state.apps.available.find((app) => app.clientId === clientId) || null;
|
2018-03-26 00:46:45 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export function fetchApp(clientId: string): ThunkAction<Promise<void>> {
|
2020-05-20 22:05:52 +05:30
|
|
|
return async (dispatch) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
const app = await oauth.getApp(clientId);
|
2018-05-05 12:12:21 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
dispatch(addApp(app));
|
|
|
|
};
|
2018-05-05 12:12:21 +05:30
|
|
|
}
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface AddAppAction extends ReduxAction {
|
|
|
|
type: 'apps:addApp';
|
|
|
|
payload: OauthAppResponse;
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:21 +05:30
|
|
|
function addApp(app: OauthAppResponse): AddAppAction {
|
2019-11-27 14:33:32 +05:30
|
|
|
return {
|
|
|
|
type: 'apps:addApp',
|
|
|
|
payload: app,
|
|
|
|
};
|
2018-03-26 00:46:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export function fetchAvailableApps() {
|
2019-11-27 14:33:32 +05:30
|
|
|
return async (
|
|
|
|
dispatch: Dispatch<any>,
|
|
|
|
getState: () => { user: User },
|
|
|
|
): Promise<void> => {
|
|
|
|
const { id } = getState().user;
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (!id) {
|
|
|
|
dispatch(setAppsList([]));
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const apps = await oauth.getAppsByUser(id);
|
|
|
|
|
|
|
|
dispatch(setAppsList(apps));
|
|
|
|
};
|
2018-03-26 00:46:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteApp(clientId: string) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return async (dispatch: Dispatch<any>): Promise<void> => {
|
|
|
|
await oauth.delete(clientId);
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
dispatch(createDeleteAppAction(clientId));
|
|
|
|
};
|
2018-05-05 12:12:21 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface DeleteAppAction extends ReduxAction {
|
|
|
|
type: 'apps:deleteApp';
|
|
|
|
payload: string;
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:12:21 +05:30
|
|
|
function createDeleteAppAction(clientId: string): DeleteAppAction {
|
2019-11-27 14:33:32 +05:30
|
|
|
return {
|
|
|
|
type: 'apps:deleteApp',
|
|
|
|
payload: clientId,
|
|
|
|
};
|
2018-03-26 00:46:45 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export function resetApp(
|
|
|
|
clientId: string,
|
|
|
|
resetSecret: boolean,
|
|
|
|
): ThunkAction<Promise<void>> {
|
2020-05-20 22:05:52 +05:30
|
|
|
return async (dispatch) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
const { data: app } = await oauth.reset(clientId, resetSecret);
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (resetSecret) {
|
|
|
|
dispatch(addApp(app));
|
|
|
|
}
|
|
|
|
};
|
2018-03-26 00:46:45 +05:30
|
|
|
}
|
2020-01-18 02:07:52 +05:30
|
|
|
|
|
|
|
export type Action = SetAvailableAction | DeleteAppAction | AddAppAction;
|