Skip to content

Commit

Permalink
component ProjectItem (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
nighca authored Oct 10, 2024
1 parent 68f71f8 commit 4882f5f
Show file tree
Hide file tree
Showing 18 changed files with 549 additions and 769 deletions.
90 changes: 82 additions & 8 deletions spx-gui/src/apis/project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FileCollection, ByPage, PaginationParams } from './common'
import { client, IsPublic } from './common'
import { ApiException, ApiExceptionCode } from './common/exception'

export { IsPublic }

Expand All @@ -22,16 +23,61 @@ export type ProjectData = {
files: FileCollection
/** Project version */
version: number
/** Full name of the project release from which the project is remixed */
remixedFrom: string
/** Brief description of the project */
description: string
/** Instructions on how to interact with the project */
instructions: string
/** URL of the project's thumbnail image */
thumbnail: string
/** Number of times the project has been viewed */
viewCount: number
/** Number of likes the project has received */
likeCount: number
/** Number of releases associated with the project */
releaseCount: number
/** Number of remixes associated with the project */
remixCount: number
/** Create time */
cTime: string
/** Update time */
uTime: string
}

// TODO: remove me
function __adaptProjectData(p: unknown): ProjectData {
function rand(decimals: number) {
return Math.floor(Math.pow(10, Math.random() * decimals))
}
const project = p as Omit<
ProjectData,
| 'remixedFrom'
| 'description'
| 'instructions'
| 'thumbnail'
| 'viewCount'
| 'likeCount'
| 'releaseCount'
| 'remixCount'
>
return {
...project,
remixedFrom: `${project.owner}/${project.name}/0`,
description: `This is description of ${project.name}`,
instructions: `This is instructions of ${project.name}`,
thumbnail: 'https://github.com/user-attachments/assets/d5032bf5-dbb0-4a17-a46d-8d41ce14f595',
viewCount: rand(7),
likeCount: rand(6),
releaseCount: rand(2),
remixCount: rand(2)
}
}

export type AddProjectParams = Pick<ProjectData, 'name' | 'isPublic' | 'files'>

export function addProject(params: AddProjectParams, signal?: AbortSignal) {
return client.post('/project', params, { signal }) as Promise<ProjectData>
export async function addProject(params: AddProjectParams, signal?: AbortSignal) {
return __adaptProjectData(await client.post('/project', params, { signal }))
}

export type UpdateProjectParams = Pick<ProjectData, 'isPublic' | 'files'>
Expand All @@ -40,13 +86,13 @@ function encode(owner: string, name: string) {
return `${encodeURIComponent(owner)}/${encodeURIComponent(name)}`
}

export function updateProject(
export async function updateProject(
owner: string,
name: string,
params: UpdateProjectParams,
signal?: AbortSignal
) {
return client.put(`/project/${encode(owner, name)}`, params, { signal }) as Promise<ProjectData>
return __adaptProjectData(await client.put(`/project/${encode(owner, name)}`, params, { signal }))
}

export function deleteProject(owner: string, name: string) {
Expand All @@ -61,10 +107,38 @@ export type ListProjectParams = PaginationParams & {
/** `owner: ownerAll` indicates that we want to list project of all users */
export const ownerAll = '*'

export function listProject(params?: ListProjectParams) {
return client.get('/projects/list', params) as Promise<ByPage<ProjectData>>
export async function listProject(params?: ListProjectParams) {
const { total, data } = await (client.get('/projects/list', params) as Promise<
ByPage<ProjectData>
>)
return {
total,
data: data.map(__adaptProjectData)
}
}

export async function getProject(owner: string, name: string) {
return __adaptProjectData(await client.get(`/project/${encode(owner, name)}`))
}

export function getProject(owner: string, name: string) {
return client.get(`/project/${encode(owner, name)}`) as Promise<ProjectData>
/**
* Check if given project liked by current logged-in user.
* If not logged in, `false` will be returned.
*/
export async function isLiking(owner: string, name: string) {
// TOOD: remove me
if (process.env.NODE_ENV === 'development') return Math.random() > 0.5
try {
await client.get(`/project/liking/${encode(owner, name)}`)
return true
} catch (e) {
if (e instanceof ApiException) {
// Not liked. TODO: reconfirm value of `code` here
if (e.code === ApiExceptionCode.errorNotFound) return false
// Not logged in.
if (e.code === ApiExceptionCode.errorUnauthorized) return false
throw e
}
throw e
}
}
33 changes: 33 additions & 0 deletions spx-gui/src/apis/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { client } from './common'

export type User = {
/** Unique identifier */
id: string
/** Unique username of the user */
username: string
/** Brief bio or description of the user */
description: string
/** Name to display, TODO: from Casdoor? */
displayName: string
/** Avatar URL, TODO: from Casdoor? */
avatar: string
/** Create time */
cTime: string
/** Update time */
uTime: string
}

export async function getUser(name: string): Promise<User> {
// TOOD: remove me
if (process.env.NODE_ENV === 'development')
return {
id: '1',
username: name,
description: 'This is description of ' + name,
displayName: name,
avatar: 'https://avatars.githubusercontent.com/u/1492263?v=4',
cTime: '2021-08-07T07:00:00Z',
uTime: '2021-08-07T07:00:00Z'
}
return client.get(`/user/${encodeURIComponent(name)}`) as Promise<User>
}
Loading

0 comments on commit 4882f5f

Please sign in to comment.