import { v4 as uuid } from 'uuid' ; import { emptyProjectData } from '../dashboard/store' ; import { Project , ProjectData , ProjectMeta } from '../dashboard/store/types' ; import { createBoilerplateState } from './boilerplate' ; import starterData from './starter-data' ; const localStorageKey = 'kanban_dashboard_projects' ; export interface ServiceClient { getUserProjectsMeta ( ) : Promise < ProjectMeta [ ] > ; createProject ( title , description , boilderplate ) : Promise < string > ; getProject ( id ) : Promise < Project > ; updateProject ( id , project ) : Promise < void > ; deleteProject ( id ) : Promise < void > ; } export class LocalStorageServiceClient implements ServiceClient { async getUserProjectsMeta ( ) { const projects = await this . getProjects ( ) ; return Object . values ( projects ) . map ( ( project ) => project . meta ) ; } async createProject ( title , description , boilerplate ) { const id = uuid ( ) ; const data = boilerplate ? createBoilerplateState ( ) : emptyProjectData ; const project = { meta : { id , title , description , ts : new Date ( ) } , data } ; await this . putProject ( id , project ) ; return id ; } async getProject ( id ) { const projects = await this . getProjects ( ) ; return projects [ id ] ; } async updateProject ( id , project ) { const existing = await this . getProject ( id ) ; if ( ! existing ) { throw new Error ( `project ${ id } does not exist` ) ; } return this . putProject ( id , project ) ; } async updateProjectData ( id , projectData ) { const project = await this . getProject ( id ) ; if ( ! project ) { throw new Error ( `project ${ id } does not exist` ) ; } project . data = projectData ; return this . putProject ( id , project ) ; } async deleteProject ( id ) { const projects = await this . getProjects ( ) ; delete projects [ id ] ; const persistable = JSON . stringify ( projects ) ; localStorage . setItem ( localStorageKey , persistable ) ; } private async getProjects ( ) { let persistable = localStorage . getItem ( localStorageKey ) ; return persistable ? JSON . parse ( persistable ) : starterData ; } private async putProject ( id , project ) { const projects = await this . getProjects ( ) ; projects [ id ] = project ; const persistable = JSON . stringify ( projects ) ; localStorage . setItem ( localStorageKey , persistable ) ; } }