[FEAT] Auth. and user projects
This commit is contained in:
130
src/stores/useProjectStore.ts
Normal file
130
src/stores/useProjectStore.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
import { useAuthStore } from './useAuthStore';
|
||||
|
||||
export interface Project {
|
||||
id: string;
|
||||
name: string;
|
||||
data: any; // Store the JSON export of the project here
|
||||
created: string;
|
||||
updated: string;
|
||||
}
|
||||
|
||||
export const useProjectStore = defineStore('project', () => {
|
||||
const authStore = useAuthStore();
|
||||
const projects = ref<Project[]>([]);
|
||||
const currentProject = ref<Project | null>(null);
|
||||
const isLoading = ref(false);
|
||||
|
||||
async function fetchProjects() {
|
||||
if (!authStore.user) return;
|
||||
isLoading.value = true;
|
||||
try {
|
||||
const records = await authStore.pb.collection('projects').getList(1, 50, {
|
||||
sort: '-updated',
|
||||
});
|
||||
projects.value = records.items.map((r: any) => ({
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
data: r.data,
|
||||
created: r.created,
|
||||
updated: r.updated,
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch projects:', error);
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function createProject(name: string, data: any) {
|
||||
if (!authStore.user) return;
|
||||
isLoading.value = true;
|
||||
try {
|
||||
const record = await authStore.pb.collection('projects').create({
|
||||
name,
|
||||
data,
|
||||
user: authStore.user.id,
|
||||
});
|
||||
currentProject.value = {
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
data: record.data,
|
||||
created: record.created,
|
||||
updated: record.updated,
|
||||
};
|
||||
await fetchProjects();
|
||||
} catch (error) {
|
||||
console.error('Failed to create project:', error);
|
||||
throw error;
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function updateProject(id: string, data: any) {
|
||||
if (!authStore.user) return;
|
||||
isLoading.value = true;
|
||||
try {
|
||||
const record = await authStore.pb.collection('projects').update(id, {
|
||||
data,
|
||||
});
|
||||
currentProject.value = { ...currentProject.value!, data: record.data, updated: record.updated };
|
||||
await fetchProjects();
|
||||
} catch (error) {
|
||||
console.error('Failed to update project:', error);
|
||||
throw error;
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadProject(id: string) {
|
||||
if (!authStore.user) return;
|
||||
isLoading.value = true;
|
||||
try {
|
||||
const record = await authStore.pb.collection('projects').getOne(id);
|
||||
currentProject.value = {
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
data: record.data,
|
||||
created: record.created,
|
||||
updated: record.updated
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Failed to load project", error);
|
||||
throw error;
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteProject(id: string) {
|
||||
if (!authStore.user) return;
|
||||
if (!confirm('Are you sure you want to delete this project?')) return;
|
||||
|
||||
isLoading.value = true;
|
||||
try {
|
||||
await authStore.pb.collection('projects').delete(id);
|
||||
if (currentProject.value?.id === id) {
|
||||
currentProject.value = null;
|
||||
}
|
||||
await fetchProjects();
|
||||
} catch (error) {
|
||||
console.error('Failed to delete project:', error);
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
projects,
|
||||
currentProject,
|
||||
isLoading,
|
||||
fetchProjects,
|
||||
createProject,
|
||||
updateProject,
|
||||
loadProject,
|
||||
deleteProject
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user