-
Notifications
You must be signed in to change notification settings - Fork 10
/
SponsorSubmit.jsx
81 lines (73 loc) · 2.48 KB
/
SponsorSubmit.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import React from 'react';
import { Components, getApolloClient, Utils, useCurrentUser } from 'meteor/vulcan:core';
import Posts from '../../modules/posts/collection';
import gql from 'graphql-tag';
import SponsorPreview from './SponsorPreview';
import { useHistory, useParams, Link } from 'react-router-dom';
import PageLayout from '../common/PageLayout';
const convertForPreview = (d) => {
const { body, url, categoriesIds, credit } = d;
if (credit) d.credit = credit;
if (body) d.htmlBody = body;
if (url) d.domain = Utils.getDomain(url);
if (categoriesIds) {
d.categories =
d.categoriesIds &&
d.categoriesIds.map((c) => {
const client = getApolloClient();
const result = client.readFragment({
id: `Category:${c}`, // `id` is any id that could be returned by `dataIdFromObject`.
fragment: gql`
fragment myCategory on Category {
_id
name
}
`,
});
return result;
});
}
return d;
};
const FormLayout = ({ FormComponents, formProps, errorProps, repeatErrors, submitProps, children, commonProps }) => (
<FormComponents.FormElement {...formProps}>
<SponsorPreview document={convertForPreview(commonProps.document)} showEdit={false} />
<FormComponents.FormErrors {...errorProps} />
{children}
{repeatErrors && <FormComponents.FormErrors {...errorProps} />}
<FormComponents.FormSubmit {...submitProps} />
</FormComponents.FormElement>
);
const SponsorSubmit = () => {
const history = useHistory();
const { code } = useParams();
const { currentUser } = useCurrentUser();
const prefilledProps = { isSponsored: true };
const fields = ['url', 'title', 'credit', 'body', 'categoriesIds', 'scheduledAt'];
if (code) {
fields.push('discountCode');
prefilledProps.discountCode = code;
}
return (
<PageLayout name="sponsor-submit" title="Submit a Sponsored Link" description="Reach over 40,000 designers">
{currentUser ? (
<Components.SmartForm
prefilledProps={prefilledProps}
collection={Posts}
fields={fields}
components={{
FormLayout,
}}
successCallback={(document) => {
history.push(`/sponsor/checkout/${document._id}`);
}}
/>
) : (
<p>
Please <Link to="/log-in?redirect=/sponsor/submit">log in or sign up</Link> first.
</p>
)}
</PageLayout>
);
};
export default SponsorSubmit;