Skip to content

Instantly share code, notes, and snippets.

@xamedow
Created January 14, 2020 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xamedow/bd56d843fb130aa3cab0945bb830bf8b to your computer and use it in GitHub Desktop.
Save xamedow/bd56d843fb130aa3cab0945bb830bf8b to your computer and use it in GitHub Desktop.
Documents sagas
const createDocumentToAPI = (appID: string) => {
utils.api.setBaseURL(`${BASE_API_URL}:${DOCUMENTS_PORT}`);
const headers = {
...defaultHeaders,
'Content-Type': 'multipart/form-data;'
};
const formData = new FormData();
formData.append('files', '');
return utils.api.post(
`${PATHS.APPLICATIONS.DRAFTS}/${appID}/documents`,
formData,
{
headers
}
);
};
function* create(action: AnyAction): any {
try {
const cnum = yield select(selectors.selectCNUM);
const response = yield call(createDocumentToAPI, action.payload, cnum);
if (!response.ok) {
const message =
response.status === HTTP_STATUS_CONFLICT
? response.data
: LABELS.GENERIC_ERROR;
throw new TypeError(message);
}
yield put(actions.documents.create.finish(response.data));
} catch (e) {
yield put(actions.documents.create.fail(e.message));
}
}
const uploadDocumentsToAPI = (
{ files, appID }: { files: File[]; appID: string },
cnum: string,
docID: string
) => {
utils.api.setBaseURL(`${BASE_API_URL}:${DOCUMENTS_PORT}`);
const formData = new FormData();
if (files && files.length > 5) {
throw new RangeError(LABELS.ERRORS.FILE_COUNT);
}
files.forEach((file: File) => {
if (!file.size || file.size > MAX_FILE_SIZE) {
throw new RangeError(LABELS.ERRORS.FILE_SIZE);
}
if (!(file.name && utils.file.checkFileExtension(file.name))) {
throw new TypeError(
LABELS.ERRORS.FILE_TYPE(utils.file.getFileExt(file.name))
);
}
formData.append('files', file);
});
const headers = {
...defaultHeaders,
'X-CNUM': cnum,
'Content-Type': 'multipart/form-data;'
};
return utils.api.put(
`${PATHS.APPLICATIONS.DRAFTS}/${appID}/documents/${docID}`,
formData,
{
headers,
params: { documentName: LABELS.DOCUMENTS.NAME }
}
);
};
function* upload(action: AnyAction): any {
try {
const cnum = yield select(selectors.selectCNUM);
const docID = yield select(selectors.selectDocID);
if (!docID) {
throw new TypeError(LABELS.ERRORS.GENERIC_ERROR);
}
const response = yield call(
uploadDocumentsToAPI,
action.payload,
cnum,
docID
);
if (!response.ok) {
const message =
response.status === HTTP_STATUS_CONFLICT
? response.data
: LABELS.ERRORS.GENERIC_ERROR;
throw new TypeError(message);
}
yield put(actions.documents.upload.finish(response.data));
yield put(actions.documents.fetch.start({ appID: action.payload.appID }));
} catch (e) {
yield put(actions.documents.upload.fail(e.message));
}
}
const fetchDocumentsFromAPI = ({ appID }: { appID: string }) => {
utils.api.setBaseURL(`${BASE_API_URL}:${DOCUMENTS_PORT}`);
return utils.api.get(
`${PATHS.APPLICATIONS.DRAFTS}/${appID}/documents`,
{},
{
headers: defaultHeaders
}
);
};
function* fetch(action: AnyAction) {
try {
let cnum = yield select(selectors.selectCNUM);
if (!cnum) {
// Handling the case when application has been opened directly and user hasn't been fetched yet
yield take('user/fetched');
cnum = yield select(selectors.selectCNUM);
}
const resp = yield call(fetchDocumentsFromAPI, {
...action.payload,
cnum
});
if (!(resp && resp.ok)) {
throw new TypeError(LABELS.ERRORS.FILE_LIST_ERROR);
}
yield put(actions.documents.fetch.finish(resp.data));
const docID = yield select(selectors.selectDocID);
if (!docID) {
yield put(actions.documents.create.start(action.payload.appID));
}
} catch (e) {
yield put(actions.documents.fetch.fail(e.message));
}
}
type LoadDocumentsFromAPI = ({
cnum,
appID,
documentData,
onDownloadProgress
}: {
cnum: string;
appID: string;
documentData: any;
onDownloadProgress: any;
}) => Promise<any>;
const loadDocumentsFromAPI: LoadDocumentsFromAPI = ({
appID,
documentData,
onDownloadProgress
}) => {
utils.api.setBaseURL(`${BASE_API_URL}:${DOCUMENTS_PORT}`);
return utils.api.get(
`${PATHS.APPLICATIONS.DRAFTS}/${appID}/documents/${documentData.parent}/files/${documentData.id}`,
{ fileName: documentData.fileName },
{
responseType: 'blob',
headers: defaultHeaders,
onDownloadProgress
}
);
};
function* download(action: AnyAction) {
const { documentData, onDownloadProgress } = action.payload;
try {
const cnum = yield select(selectors.selectCNUM);
const appID = yield select(selectors.selectAppID);
const resp = yield call(loadDocumentsFromAPI, {
documentData,
cnum,
appID,
onDownloadProgress
});
if (!(resp && resp.ok)) {
throw new TypeError('Ошибка сервера при получении списка файлов');
}
FileSaver.saveAs(resp.data, documentData.fileName);
yield put(actions.documents.download.finish());
} catch (e) {
yield put(actions.documents.download.fail(e.message));
}
}
type DeleteDocumentFromAPI = ({
cnum,
appID,
documentData
}: {
appID: string;
documentData: any;
}) => Promise<any>;
const deleteDocumentFromAPI: DeleteDocumentFromAPI = ({
appID,
documentData
}) => {
utils.api.setBaseURL(`${BASE_API_URL}:${DOCUMENTS_PORT}`);
return utils.api.delete(
`${PATHS.APPLICATIONS.DRAFTS}/${appID}/documents/${documentData.parent}/files/${documentData.id}`,
{},
{
headers: defaultHeaders
}
);
};
function* deleteDocument(action: AnyAction) {
try {
const cnum = yield select(selectors.selectCNUM);
const appID = yield select(selectors.selectAppID);
const resp = yield call(deleteDocumentFromAPI, {
documentData: action.payload,
cnum,
appID
});
if (!(resp && resp.ok)) {
throw new TypeError(LABELS.ERRORS.DELETE_FILE_ERROR);
}
yield put(actions.documents.delete.finish());
yield put(actions.documents.fetch.start({ appID }));
} catch (e) {
yield put(actions.documents.delete.fail(e.message));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment