Skip to content

Commit

Permalink
Fixed feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
DigneshGujarathi committed Dec 14, 2020
1 parent 5e1a75c commit 5b712ff
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 49 deletions.
75 changes: 29 additions & 46 deletions src/containers/Form/FormLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { SEARCH_QUERY } from '../../graphql/queries/Search';
import { USER_LANGUAGES } from '../../graphql/queries/Organization';
import { ReactComponent as DeleteIcon } from '../../assets/images/icons/Delete/White.svg';
import { ReactComponent as BackIcon } from '../../assets/images/icons/Back.svg';
import { CREATE_MEDIA_MESSAGE } from '../../graphql/mutations/Chat';

export interface FormLayoutProps {
match: any;
Expand Down Expand Up @@ -52,6 +51,7 @@ export interface FormLayoutProps {
getLanguageId?: any;
backLinkButton?: any;
isAttachment?: boolean;
getMediaId?: any;
}

export const FormLayout: React.SFC<FormLayoutProps> = ({
Expand Down Expand Up @@ -88,6 +88,7 @@ export const FormLayout: React.SFC<FormLayoutProps> = ({
getLanguageId,
backLinkButton,
isAttachment = false,
getMediaId,
}: FormLayoutProps) => {
const client = useApolloClient();
const [showDialog, setShowDialog] = useState(false);
Expand Down Expand Up @@ -182,9 +183,6 @@ export const FormLayout: React.SFC<FormLayoutProps> = ({
},
});

// create media for attachment
const [createMediaMessage] = useMutation(CREATE_MEDIA_MESSAGE);

const [createItem] = useMutation(createItemQuery, {
onCompleted: (data) => {
const itemCreated = `create${camelCaseItem}`;
Expand Down Expand Up @@ -222,36 +220,21 @@ export const FormLayout: React.SFC<FormLayoutProps> = ({
return null;
}

const getMediaId = (payload: any) => {
createMediaMessage({
variables: {
input: {
caption: payload.body,
sourceUrl: payload.attachmentURL,
url: payload.attachmentURL,
const performTask = (payload: any) => {
if (itemId) {
updateItem({
variables: {
id: itemId,
input: payload,
},
},
}).then((data) => {
if (data) {
const payloadCopy = payload;
delete payloadCopy.attachmentURL;
payloadCopy.messageMediaId = parseInt(data.data.createMessageMedia.messageMedia.id, 10);
if (itemId) {
updateItem({
variables: {
id: itemId,
input: payloadCopy,
},
});
} else {
createItem({
variables: {
input: payloadCopy,
},
});
}
}
});
});
} else {
createItem({
variables: {
input: payload,
},
});
}
};

const saveHandler = ({ languageId: languageIdValue, ...itemData }: any) => {
Expand Down Expand Up @@ -288,20 +271,20 @@ export const FormLayout: React.SFC<FormLayoutProps> = ({
});
// for template create media for attachment
if (isAttachment && payload.type !== 'TEXT' && payload.type) {
getMediaId(payload);
} else if (itemId) {
updateItem({
variables: {
id: itemId,
input: payload,
},
});
getMediaId(payload)
.then((data: any) => {
if (data) {
const payloadCopy = payload;
delete payloadCopy.attachmentURL;
payloadCopy.messageMediaId = parseInt(data.data.createMessageMedia.messageMedia.id, 10);
performTask(payloadCopy);
}
})
.catch((e: any) => {
setErrorMessage(client, e);
});
} else {
createItem({
variables: {
input: payload,
},
});
performTask(payload);
}
};

Expand Down
24 changes: 21 additions & 3 deletions src/containers/Template/Form/Template.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect } from 'react';
import * as Yup from 'yup';
import { useLazyQuery, useQuery } from '@apollo/client';
import { useLazyQuery, useMutation, useQuery } from '@apollo/client';
import { EditorState } from 'draft-js';

import { Input } from '../../../components/UI/Form/Input/Input';
Expand All @@ -16,6 +16,7 @@ import {
import { MEDIA_MESSAGE_TYPES } from '../../../common/constants';
import { USER_LANGUAGES } from '../../../graphql/queries/Organization';
import { AutoComplete } from '../../../components/UI/Form/AutoComplete/AutoComplete';
import { CREATE_MEDIA_MESSAGE } from '../../../graphql/mutations/Chat';

const FormSchema = Yup.object().shape({
label: Yup.string().required('Title is required.').max(50, 'Title is length too long.'),
Expand Down Expand Up @@ -106,6 +107,9 @@ const Template: React.SFC<TemplateProps> = (props) => {

const [getSessionTemplate, { data: template }] = useLazyQuery<any>(GET_TEMPLATE);

// create media for attachment
const [createMediaMessage] = useMutation(CREATE_MEDIA_MESSAGE);

useEffect(() => {
if (Object.prototype.hasOwnProperty.call(match.params, 'id') && match.params.id) {
getSessionTemplate({ variables: { id: match.params.id } });
Expand Down Expand Up @@ -253,8 +257,7 @@ const Template: React.SFC<TemplateProps> = (props) => {
payloadCopy.type = payloadCopy.type.id || 'TEXT';

delete payloadCopy.language;

if (payloadCopy.type.id === 'TEXT') {
if (payloadCopy.type === 'TEXT') {
delete payloadCopy.attachmentURL;
}
} else {
Expand Down Expand Up @@ -298,6 +301,20 @@ const Template: React.SFC<TemplateProps> = (props) => {
return payloadCopy;
};

// create media for attachment
const getMediaId = async (payload: any) => {
const data = await createMediaMessage({
variables: {
input: {
caption: payload.body,
sourceUrl: payload.attachmentURL,
url: payload.attachmentURL,
},
},
});
return data;
};

return (
<FormLayout
{...queries}
Expand All @@ -316,6 +333,7 @@ const Template: React.SFC<TemplateProps> = (props) => {
getLanguageId={getLanguageId}
languageSupport={false}
isAttachment
getMediaId={getMediaId}
/>
);
};
Expand Down

0 comments on commit 5b712ff

Please sign in to comment.