add doResetThumbnailStatus and doUploadThumbnail actions

This commit is contained in:
Travis Eden 2018-04-02 09:38:06 -04:00 committed by Sean Yesmunt
parent e32a597c38
commit 9a658d41a8

View file

@ -6,6 +6,7 @@ import {
doNotify,
MODALS,
selectMyChannelClaims,
STATUSES
} from 'lbry-redux';
import { selectPendingPublishes } from 'redux/selectors/publish';
import type {
@ -13,6 +14,8 @@ import type {
UpdatePublishFormAction,
PublishParams,
} from 'redux/reducers/publish';
import fs from 'fs';
import path from 'path';
type Action = UpdatePublishFormAction | { type: ACTIONS.CLEAR_PUBLISH };
type PromiseAction = Promise<Action>;
@ -30,15 +33,78 @@ export const doUpdatePublishForm = (publishFormValue: UpdatePublishFormData) =>
data: { ...publishFormValue },
});
export const doPrepareEdit = (claim: any, uri: string) => (dispatch: Dispatch) => {
const {
name,
amount,
channel_name: channelName,
value: {
stream: { metadata },
},
} = claim;
export const doResetThumbnailStatus = () => (dispatch: Dispatch): Action =>
fetch('https://spee.ch/api/channel/availability/@testing')
.then(() =>
dispatch({
type: ACTIONS.UPDATE_PUBLISH_FORM,
data: { thumbnailStatus: STATUSES.READY },
})
)
.catch(() =>
dispatch({
type: ACTIONS.UPDATE_PUBLISH_FORM,
data: { thumbnailStatus: STATUSES.DOWN },
})
);
export const doUploadThumbnail = (filePath: string, nsfw: boolean) => (dispatch: Dispatch) => {
const thumbnail = fs.readFileSync(filePath);
const fileExt = path.extname(filePath);
const makeid = () => {
let text = '';
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < 24; i += 1) text += possible.charAt(Math.floor(Math.random() * 62));
return text;
};
const uploadError = (error: string = '') =>
dispatch(
batchActions(
{
type: ACTIONS.UPDATE_PUBLISH_FORM,
data: { thumbnailStatus: STATUSES.DOWN },
},
doAlertError(error)
)
);
dispatch({
type: ACTIONS.UPDATE_PUBLISH_FORM,
data: { thumbnailStatus: STATUSES.IN_PROGRESS },
});
const data = new FormData();
const name = makeid();
const blob = new Blob([thumbnail], { type: `image/${fileExt.slice(1)}` });
data.append('name', name);
data.append('file', blob);
data.append('nsfw', nsfw.toString());
return fetch('https://spee.ch/api/claim/publish', {
method: 'POST',
body: data,
})
.then(response => response.json())
.then(
json =>
json.success
? dispatch({
type: ACTIONS.UPDATE_PUBLISH_FORM,
data: {
thumbnailStatus: STATUSES.COMPLETE,
thumbnailUrl: `${json.data.url}${fileExt}`,
},
})
: uploadError()
)
.catch(() => uploadError());
};
export const doPrepareEdit = (claim: any) => (dispatch: Dispatch) => {
const { name, amount, channel_name: channelName, value: { stream: { metadata } } } = claim;
const {
author,
description,