admin.ts 5.17 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
2
3
import { normalize } from 'path';

// Constants
Maxym Borodenko's avatar
Maxym Borodenko committed
4
import {RECEIVE_FOLDER_DETAILS, RECEIVE_IMAGE_GALLERY, UPDATE_SUBFOLDER_LIST, UPDATE_FILE_LIST, RECEIVE_FILE} from 'repository/constants';
Matija Obreza's avatar
Matija Obreza committed
5
6
7
8
9
10
11
12
13
14
15
16

// Model
import FolderDetails from 'model/repository/FolderDetails';

// Service
import RepositoryService from 'service/genesys/RepositoryService';

// Util
import { log } from 'utilities/debug';
import RepositoryFile from 'model/repository/RepositoryFile';
import RepositoryFolder from 'model/repository/RepositoryFolder';
import ImageGallery from 'model/repository/ImageGallery';
Maxym Borodenko's avatar
Maxym Borodenko committed
17
18
import navigateTo from 'actions/navigation';
import * as _ from 'lodash';
Matija Obreza's avatar
Matija Obreza committed
19
20
21
22
23
24
25
26
27
28
29
30


const receiveFolder = (folder: FolderDetails, error = null) => ({
  type: RECEIVE_FOLDER_DETAILS,
  payload: { folder, error },
});

const receiveGallery = (gallery: ImageGallery, error = null) => ({
  type: RECEIVE_IMAGE_GALLERY,
  payload: { gallery, error },
});

Maxym Borodenko's avatar
Maxym Borodenko committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const receiveFile = (file: RepositoryFile, error = null) => ({
  type: RECEIVE_FILE,
  payload: { file, error },
});

const updateSubfolders = (subFolder: RepositoryFolder) => ({
  type: UPDATE_SUBFOLDER_LIST,
  payload: { subFolder },
});

const updateFiles = (deletedFile: RepositoryFile) => ({
  type: UPDATE_FILE_LIST,
  payload: { deletedFile },
});
Matija Obreza's avatar
Matija Obreza committed
45
46
47

// Get folder details
export const getFolder = (path: string = '/') => (dispatch, getState) => {
48
  return RepositoryService.getFolder(normalize(path))
Matija Obreza's avatar
Matija Obreza committed
49
50
51
52
53
54
55
56
57
58
59
    .then((folder: FolderDetails) => {
      dispatch(receiveFolder(folder));
    })
    .catch((error) => {
      log('Error', error);
    });
};


// Get folder details
export const deleteFolder = (path: string) => (dispatch, getState) => {
60
  return RepositoryService.deleteFolder(normalize(path))
Matija Obreza's avatar
Matija Obreza committed
61
62
63
64
65
66
67
68
69
70
    .then((folder: RepositoryFolder) => {
      console.log(`Folder deleted ${path}`, folder);
      return true;
    })
    .catch((error) => {
      log('Error', error);
      return false;
    });
};

Maxym Borodenko's avatar
Maxym Borodenko committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
export const removeFile = (uuid: string) => (dispatch) => {
  return RepositoryService.removeFile(uuid)
    .then((file: RepositoryFile) => {
      console.log(`File deleted`, file);
      dispatch(updateFiles(file));
      return true;
    })
    .catch((error) => {
      log('Error', error);
      return false;
    });
};

// Create or load folder at specified path
export const ensureFolder = (path: string) => (dispatch, getState) => {
  return RepositoryService.ensureFolder(normalize(path))
    .then((folder: RepositoryFolder) => {
      dispatch(updateSubfolders(folder));
    })
    .catch((error) => {
      log('Error', error);
    });
};

export const updateFolder = (folder: RepositoryFolder)  => (dispatch, getState) => {
  return RepositoryService.updateFolder(folder)
    .then((updated: FolderDetails) => {
      dispatch(receiveFolder(updated));
    })
    .catch((error) => {
      log('Error', error);
    });
};

export const updateFile = (metadata: RepositoryFile)  => (dispatch, getState) => {
  if (_.isEqual({...metadata}, {...getState().repository.admin.file})) {
    dispatch(goToFolder(metadata.folder));
  }
  return RepositoryService.updateFile(metadata)
    .then((updated: RepositoryFile) => {
      dispatch(receiveFile(updated));
      dispatch(goToFolder(updated.folder));
    })
    .catch((error) => {
      log('Error', error);
    });
};

const goToFolder = (folder: RepositoryFolder | string) => (dispatch) => {
  dispatch(navigateTo(`/admin/repository/f${typeof folder === 'object' ? folder.path : folder}/`));
};
Matija Obreza's avatar
Matija Obreza committed
122
123
124

// Upload file
export const uploadFile = (path: string, file: File) => (dispatch, getState) => {
125
  return RepositoryService.uploadFile(normalize(path), file)
Matija Obreza's avatar
Matija Obreza committed
126
127
128
129
130
131
132
133
134
135
136
    .then((file: RepositoryFile) => {
      dispatch(getFolder(file.folder.path));
    })
    .catch((error) => {
      log('Error', error);
    });
};

// Image gallery

export const getGallery = (path: string) => (dispatch, getState) => {
137
  return RepositoryService.getGallery(normalize(path))
Matija Obreza's avatar
Matija Obreza committed
138
139
    .then((gallery: ImageGallery) => {
      dispatch(receiveGallery(gallery));
Maxym Borodenko's avatar
Maxym Borodenko committed
140
141
142
143
144
145
146
147
148
149
150
    })
    .catch((error) => {
      log('Error', error);
    });
};

// Get file
export const getFile = (uuid: string) => (dispatch, getState) => {
  return RepositoryService.getFile(uuid)
    .then((file: RepositoryFile) => {
      dispatch(receiveFile(file));
Matija Obreza's avatar
Matija Obreza committed
151
152
153
154
155
156
157
158
159
160
161
    })
    .catch((error) => {
      log('Error', error);
    });
};

// Get folder details
export const createGallery = (path: string, title: string, description?: string) => (dispatch, getState) => {
  const gallery: ImageGallery = new ImageGallery();
  gallery.title = title;
  gallery.description = description;
162
  return RepositoryService.createGallery(normalize(path), gallery)
Matija Obreza's avatar
Matija Obreza committed
163
164
165
166
167
168
169
170
171
172
173
174
175
    .then((gallery: ImageGallery) => {
      console.log(`Gallery created at ${path}`, gallery);
      dispatch(receiveGallery(null));
      return true;
    })
    .catch((error) => {
      log('Error', error);
      return false;
    });
};

// Get folder details
export const removeGallery = (path: string) => (dispatch, getState) => {
176
  return RepositoryService.removeGallery(normalize(path))
Matija Obreza's avatar
Matija Obreza committed
177
178
179
180
181
182
183
184
185
186
    .then((gallery: ImageGallery) => {
      console.log(`Gallery deleted ${path}`, gallery);
      dispatch(receiveGallery(null));
      return true;
    })
    .catch((error) => {
      log('Error', error);
      return false;
    });
};