1- import { collections , scenes } from "../firebase.js" ;
2-
31import * as types from "../constants/ActionTypes" ;
42
3+ export const collectRef = "/apiv1/collections" ;
4+
55export function asyncCollections ( id ) {
66 // fetch user's collections
77 return ( dispatch ) => {
88 if ( id ) {
99 let userCollections = [ ] ;
10- collections . where ( "uid" , "==" , id ) . get ( ) . then ( snap => {
11- snap . forEach ( doc => {
12- let dat = doc . data ( ) ;
13- userCollections . push ( {
14- id : doc . id ,
15- collectionID : dat . collectionID ,
16- data : dat
10+ fetch ( collectRef , { headers : { "x-access-token" : id } } ) . then ( ( data ) => {
11+ data . json ( ) . then ( ( data ) => {
12+ data . forEach ( ( doc ) => {
13+ userCollections . push ( doc ) ;
1714 } ) ;
1815 } ) ;
16+ dispatch ( syncCollections ( userCollections ) ) ;
1917 } ) ;
20- dispatch ( syncCollections ( userCollections ) ) ;
2118 }
2219 } ;
2320}
@@ -26,33 +23,40 @@ export function syncCollections(payload) {
2623 return { type : types . SYNC_CLASSES , payload : payload } ;
2724}
2825
29- export function asyncCollection ( collectionID ) {
26+ export function asyncCollection ( collectionID , uid ) {
3027 // fetch projects in collection
3128 return ( dispatch ) => {
3229 if ( collectionID ) {
3330 let collectionProjects = [ ] ;
3431 let projectOptions = [ ] ;
35- scenes . where ( "settings.collectionID" , "==" , collectionID ) . get ( ) . then ( snap => {
36- snap . forEach ( doc => {
37- let dat = doc . data ( ) ;
38- collectionProjects . push ( {
39- id : doc . id ,
40- name : dat . name ,
41- collectionID : dat . collectionID ,
42- data : dat
43- } ) ;
32+ fetch ( `${ collectRef } /collectionID/${ collectionID } ` , { headers : { "x-access-token" : uid } } )
33+ . then ( ( resp ) => {
34+ switch ( resp . status ) {
35+ case 200 :
36+ resp . json ( ) . then ( ( data ) => {
37+ data . forEach ( ( doc ) => {
38+ collectionProjects . push ( doc ) ;
39+ } ) ;
40+ collectionProjects . map ( ( proj ) => {
41+ return projectOptions . push ( {
42+ value : proj . _id ,
43+ label : proj . name
44+ } ) ;
45+ } ) ;
46+
47+ dispatch ( syncCollection ( projectOptions ) ) ;
48+ } ) ;
49+ break ;
50+ case 401 :
51+ window . alert ( "Error: You are not logged in as the owner of this collection" ) ;
52+ break ;
53+ case 404 :
54+ window . location . assign ( "/error-404" ) ;
55+ break ;
56+ default :
57+ window . alert ( `Error fetching collection scenes: ${ resp . statusText } ` ) ;
58+ }
4459 } ) ;
45- } ) . then ( ( ) => {
46- collectionProjects . map ( ( proj ) =>
47- projectOptions . push ( {
48- value : proj . id ,
49- label : proj . name
50- } )
51- ) ;
52- } ) . then ( ( ) => {
53- dispatch ( syncCollection ( projectOptions ) ) ;
54- } ) ;
55-
5660 }
5761 } ;
5862}
@@ -61,15 +65,45 @@ export function syncCollection(payload) {
6165 return { type : types . SYNC_CLASS , payload : payload } ;
6266}
6367
64- export function deleteCollection ( id , name = null ) {
65- if ( window . confirm ( `Are you sure you want to delete collection "${ name !== null ? name : id } "?` ) ) {
68+ export function deleteCollection ( id , name = null , uid ) {
69+ return ( dispatch ) => {
70+ name = ( name ? name : id ) ;
71+ if ( window . confirm ( `Are you sure you want to delete collection "${ name } "?` ) ) {
6672
67- // Delete Document
68- collections . doc ( id ) . delete ( )
69- . catch ( ( error ) => {
70- console . error ( "Error removing collection: " , error ) ;
73+ // Delete Document
74+ fetch ( `${ collectRef } /collectionID/${ name } ` , { method : "DELETE" , headers : { "x-access-token" : uid } } ) . then ( ( resp ) => {
75+ if ( resp . status !== 204 ) {
76+ console . error ( `Error deleting collection ${ name } : ${ resp . statusText } ` ) ;
77+ return ;
78+ }
79+ dispatch ( { type : types . DELETE_CLASS , id : id } ) ;
7180 } ) ;
72- return { type : types . DELETE_CLASS , id : id } ;
81+ }
82+ } ;
83+ }
84+ /**
85+ *
86+ * @param {string } name The name of the collection to be created
87+ * @param {* } uid A JWT token to authenticate with the backend
88+ */
89+ export async function createCollection ( name , uid ) {
90+ name = name . toLowerCase ( ) . trim ( ) ;
91+
92+ let resp = await fetch ( `${ collectRef } /` , {
93+ method : "POST" ,
94+ body : JSON . stringify ( { collectID : name } ) ,
95+ headers :{ "Content-Type" : "application/json" , "x-access-token" : uid }
96+ } ) ;
97+ if ( resp . status === 409 ) {
98+ window . alert ( "Error: A collection already exists with that collection name." ) ;
99+ return false ;
100+ } else if ( resp . status !== 201 ) {
101+ window . alert ( `Error creating collection: ${ resp . statusText } ` ) ;
102+ return false ;
103+ } else {
104+ asyncCollections ( uid ) ;
105+ window . alert ( "Collection added!" ) ;
106+ return true ;
73107 }
74108}
75109
@@ -79,5 +113,7 @@ export default {
79113 asyncCollections,
80114 deleteCollection,
81115 syncCollection,
82- syncCollections
116+ syncCollections,
117+ createCollection,
118+ collectRef
83119} ;
0 commit comments