Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust projectApi #344

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ function removeMember(item: ProjectAccessDetail) {
function roleUpdate(item: ProjectAccessDetail, value: ProjectRoleName) {
projectStore.updateProjectAccess({
projectId: projectStore.project.id,
data: { role: value, user_id: item.id }
userId: item.id,
data: { role: value }
})
}

Expand Down
35 changes: 32 additions & 3 deletions web-app/packages/lib/src/modules/project/projectApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import {
PushProjectChangesParams,
PushProjectChangesResponse,
SaveProjectSettings,
UpdateProjectAccessParams,
ProjectVersion,
ProjectAccessDetail,
ProjectAccess,
ProjectVersionFileChange
ProjectVersionFileChange,
UpdateProjectPayload,
UpdatePublicFlagParams
} from '@/modules/project/types'

export const ProjectApi = {
Expand Down Expand Up @@ -175,14 +176,42 @@ export const ProjectApi = {

async updateProjectAccess(
id: string,
data: UpdateProjectAccessParams,
userId: number,
data: UpdateProjectPayload,
withRetry?: boolean
): Promise<AxiosResponse<ProjectAccess>> {
return ProjectModule.httpService.patch(
`/v2/projects/${id}/collaborators/${userId}`,
data,
{
...(withRetry ? getDefaultRetryOptions() : {})
}
)
},

async updatePublicFlag(
id: string,
data: UpdatePublicFlagParams,
withRetry?: boolean
): Promise<AxiosResponse<ProjectAccess>> {
return ProjectModule.httpService.patch(`/app/project/${id}/access`, data, {
...(withRetry ? getDefaultRetryOptions() : {})
})
},

async removeProjectAccess(
id: string,
userId: number,
withRetry?: boolean
): Promise<AxiosResponse<ProjectAccess>> {
return ProjectModule.httpService.delete(
`/v2/projects/${id}/collaborators/${userId}`,
{
...(withRetry ? getDefaultRetryOptions() : {})
}
)
},

async pushProjectChanges(
url: string,
data: PushProjectChangesParams
Expand Down
41 changes: 33 additions & 8 deletions web-app/packages/lib/src/modules/project/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
SaveProjectSettings,
ErrorCodes,
ProjectAccessDetail,
UpdateProjectAccessParams,

Check warning on line 44 in web-app/packages/lib/src/modules/project/store.ts

View workflow job for this annotation

GitHub Actions / JavaScript code convention check

'UpdateProjectAccessParams' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 44 in web-app/packages/lib/src/modules/project/store.ts

View workflow job for this annotation

GitHub Actions / JavaScript code convention check

'UpdateProjectAccessParams' is defined but never used. Allowed unused vars must match /^_/u
ProjectVersionFileChange,
ProjectVersionListItem
ProjectVersionListItem,
UpdateProjectPayload,
UpdatePublicFlagParams
} from '@/modules/project/types'
import { useUserStore } from '@/modules/user/store'

Expand Down Expand Up @@ -758,10 +760,10 @@
const notificationStore = useNotificationStore()
this.accessLoading = true
try {
const response = await ProjectApi.updateProjectAccess(this.project.id, {
user_id: item.id,
role: 'none'
})
const response = await ProjectApi.removeProjectAccess(
this.project.id,
item.id
)
this.access = this.access.filter((access) => access.id !== item.id)
this.project.access = response.data
} catch {
Expand All @@ -774,24 +776,26 @@
},

/**
* Updates the access for a user on the given project. `project.access` contains also public attribute. This attribute can be changed also.
* Updates the access for a user on the given project.
*
* @param payload - Object containing the project ID and access update details.
* @returns Promise resolving when the API call completes.
*/
async updateProjectAccess(payload: {
projectId: string
data: UpdateProjectAccessParams
userId: number
data: UpdateProjectPayload
}) {
const notificationStore = useNotificationStore()
this.accessLoading = true
try {
const response = await ProjectApi.updateProjectAccess(
payload.projectId,
payload.userId,
payload.data
)
this.access = this.access.map((access) => {
if (access.id === payload.data.user_id) {
if (access.id === payload.userId) {
access.project_permission = payload.data.role
}
return access
Expand All @@ -806,6 +810,27 @@
}
},

async updatePublicFlag(payload: {
projectId: string
data: UpdatePublicFlagParams
}) {
const notificationStore = useNotificationStore()
this.accessLoading = true
try {
const response = await ProjectApi.updatePublicFlag(
payload.projectId,
payload.data
)
this.project.access = response.data
} catch {
notificationStore.error({
text: `Failed to update public flag`
})
} finally {
this.accessLoading = false
}
},

async getVersionChangeset(payload: {
workspace: string
projectName: string
Expand Down
8 changes: 5 additions & 3 deletions web-app/packages/lib/src/modules/project/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,11 @@ export type EnhancedProjectDetail = ProjectDetail & {
path: string
}

export interface UpdateProjectAccessParams {
user_id?: number
role?: ProjectRoleName
export interface UpdateProjectPayload {
role: ProjectRoleName
}

export interface UpdatePublicFlagParams {
public?: boolean
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ export default defineComponent({
}
},
methods: {
...mapActions(useProjectStore, ['deleteProject', 'updateProjectAccess']),
...mapActions(useProjectStore, ['deleteProject', 'updatePublicFlag']),
...mapActions(useDialogStore, { showDialog: 'show' }),
togglePublicPrivate() {
this.updateProjectAccess({
this.updatePublicFlag({
projectId: this.project.id,
data: {
public: !this.project.access.public
Expand Down
Loading