Client - add/delete an OAuth app (WIP)

This commit is contained in:
Sam
2022-05-28 17:14:52 +02:00
parent 25decef696
commit 7e45923b25
15 changed files with 479 additions and 11 deletions

View File

@ -1,13 +1,69 @@
import { ActionContext, ActionTree } from 'vuex'
import authApi from '@/api/authApi'
import router from '@/router'
import { OAUTH2_STORE, ROOT_STORE } from '@/store/constants'
import { IOAuth2Actions, IOAuth2State } from '@/store/modules/oauth2/types'
import { IRootState } from '@/store/modules/root/types'
import { IOauth2ClientsPayload } from '@/types/oauth'
import { IOauth2ClientsPayload, IOAuth2ClientPayload } from '@/types/oauth'
import { handleError } from '@/utils'
export const actions: ActionTree<IOAuth2State, IRootState> & IOAuth2Actions = {
[OAUTH2_STORE.ACTIONS.CREATE_CLIENT](
context: ActionContext<IOAuth2State, IRootState>,
payload: IOAuth2ClientPayload
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
authApi
.post('oauth/apps', payload)
.then((res) => {
if (res.data.status === 'created') {
context
.dispatch(OAUTH2_STORE.ACTIONS.GET_CLIENTS)
.then(() => router.push('/profile/apps'))
} else {
handleError(context, null)
}
})
.catch((error) => handleError(context, error))
},
[OAUTH2_STORE.ACTIONS.DELETE_CLIENT](
context: ActionContext<IOAuth2State, IRootState>,
id: number
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
authApi
.delete(`oauth/apps/${id}`)
.then((res) => {
if (res.status === 204) {
context
.dispatch(OAUTH2_STORE.ACTIONS.GET_CLIENTS)
.then(() => router.push('/profile/apps'))
} else {
handleError(context, null)
}
})
.catch((error) => handleError(context, error))
},
[OAUTH2_STORE.ACTIONS.GET_CLIENT](
context: ActionContext<IOAuth2State, IRootState>,
id: string
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
authApi
.get(`oauth/apps/${id}`)
.then((res) => {
if (res.data.status === 'success') {
context.commit(
OAUTH2_STORE.MUTATIONS.SET_CLIENT,
res.data.data.client
)
} else {
handleError(context, null)
}
})
.catch((error) => handleError(context, error))
},
[OAUTH2_STORE.ACTIONS.GET_CLIENTS](
context: ActionContext<IOAuth2State, IRootState>,
payload: IOauth2ClientsPayload

View File

@ -1,13 +1,19 @@
export enum OAuth2Actions {
CREATE_CLIENT = 'CREATE_CLIENT',
DELETE_CLIENT = 'DELETE_CLIENT',
GET_CLIENTS = 'GET_CLIENTS',
GET_CLIENT = 'GET_CLIENT',
}
export enum OAuth2Getters {
CLIENT = 'CLIENT',
CLIENTS = 'CLIENTS',
CLIENTS_PAGINATION = 'CLIENTS_PAGINATION',
}
export enum OAuth2Mutations {
EMPTY_CLIENT = 'EMPTY_CLIENT',
SET_CLIENT = 'SET_CLIENT',
SET_CLIENTS = 'SET_CLIENTS',
SET_CLIENTS_PAGINATION = 'SET_CLIENTS_PAGINATION',
}

View File

@ -5,6 +5,7 @@ import { IOAuth2Getters, IOAuth2State } from '@/store/modules/oauth2/types'
import { IRootState } from '@/store/modules/root/types'
export const getters: GetterTree<IOAuth2State, IRootState> & IOAuth2Getters = {
[OAUTH2_STORE.GETTERS.CLIENT]: (state: IOAuth2State) => state.client,
[OAUTH2_STORE.GETTERS.CLIENTS]: (state: IOAuth2State) => state.clients,
[OAUTH2_STORE.GETTERS.CLIENTS_PAGINATION]: (state: IOAuth2State) =>
state.pagination,

View File

@ -6,6 +6,15 @@ import { IPagination } from '@/types/api'
import { IOAuth2Client } from '@/types/oauth'
export const mutations: MutationTree<IOAuth2State> & TOAuth2Mutations = {
[OAUTH2_STORE.MUTATIONS.SET_CLIENT](
state: IOAuth2State,
client: IOAuth2Client
) {
state.client = client
},
[OAUTH2_STORE.MUTATIONS.EMPTY_CLIENT](state: IOAuth2State) {
state.client = <IOAuth2Client>{}
},
[OAUTH2_STORE.MUTATIONS.SET_CLIENTS](
state: IOAuth2State,
clients: IOAuth2Client[]

View File

@ -1,7 +1,9 @@
import { IOAuth2State } from '@/store/modules/oauth2/types'
import { IPagination } from '@/types/api'
import { IOAuth2Client } from '@/types/oauth'
export const oAuth2State: IOAuth2State = {
client: <IOAuth2Client>{},
clients: [],
pagination: <IPagination>{},
}

View File

@ -8,14 +8,31 @@ import {
import { OAUTH2_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/types'
import { IPagination } from '@/types/api'
import { IOAuth2Client, IOauth2ClientsPayload } from '@/types/oauth'
import {
IOAuth2Client,
IOAuth2ClientPayload,
IOauth2ClientsPayload,
} from '@/types/oauth'
export interface IOAuth2State {
client: IOAuth2Client
clients: IOAuth2Client[]
pagination: IPagination
}
export interface IOAuth2Actions {
[OAUTH2_STORE.ACTIONS.CREATE_CLIENT](
context: ActionContext<IOAuth2State, IRootState>,
payload: IOAuth2ClientPayload
): void
[OAUTH2_STORE.ACTIONS.DELETE_CLIENT](
context: ActionContext<IOAuth2State, IRootState>,
id: number
): void
[OAUTH2_STORE.ACTIONS.GET_CLIENT](
context: ActionContext<IOAuth2State, IRootState>,
id: string
): void
[OAUTH2_STORE.ACTIONS.GET_CLIENTS](
context: ActionContext<IOAuth2State, IRootState>,
payload: IOauth2ClientsPayload
@ -23,11 +40,14 @@ export interface IOAuth2Actions {
}
export interface IOAuth2Getters {
[OAUTH2_STORE.GETTERS.CLIENT](state: IOAuth2State): IOAuth2Client
[OAUTH2_STORE.GETTERS.CLIENTS](state: IOAuth2State): IOAuth2Client[]
[OAUTH2_STORE.GETTERS.CLIENTS_PAGINATION](state: IOAuth2State): IPagination
}
export type TOAuth2Mutations<S = IOAuth2State> = {
[OAUTH2_STORE.MUTATIONS.EMPTY_CLIENT](state: S): void
[OAUTH2_STORE.MUTATIONS.SET_CLIENT](state: S, client: IOAuth2Client): void
[OAUTH2_STORE.MUTATIONS.SET_CLIENTS](state: S, clients: IOAuth2Client[]): void
[OAUTH2_STORE.MUTATIONS.SET_CLIENTS_PAGINATION](
state: S,