50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import type { Project } from "~/types/api";
|
|
|
|
export default function useProject()
|
|
{
|
|
const project = useCookie('project');
|
|
|
|
const id = useState<number>("projectId", () => parseInt(project.value ?? '0'));
|
|
const name = useState<string>("projectName", undefined);
|
|
const owner = useState<number>("projectOwner", undefined);
|
|
const home = useState<string | null>("projectHomepage", () => null);
|
|
const summary = useState<string | null>("projectSummary", () => null);
|
|
|
|
return {
|
|
id, name, owner, home, summary, get, set
|
|
};
|
|
}
|
|
|
|
async function get(): Promise<boolean> {
|
|
const id = useState<number>("projectId");
|
|
|
|
if (!id.value)
|
|
return false;
|
|
|
|
try {
|
|
const result = await $fetch(`/api/project/${id.value}`) as Project;
|
|
|
|
const name = useState<string>("projectName");
|
|
const owner = useState<number>("projectOwner");
|
|
const home = useState<string | null>("projectHomepage");
|
|
const summary = useState<string | null>("projectSummary");
|
|
|
|
name.value = result.name;
|
|
owner.value = result.owner;
|
|
home.value = result.home;
|
|
summary.value = result.summary;
|
|
|
|
return true;
|
|
} catch(e) {
|
|
return false;
|
|
}
|
|
}
|
|
async function set(id: number): Promise<boolean> {
|
|
const _id = useState<number>("projectId");
|
|
|
|
_id.value = id;
|
|
const project = useCookie('project');
|
|
project.value = id.toString();
|
|
|
|
return await get();
|
|
} |