Skip to content

Commit

Permalink
Merge pull request #1 from CaptainChemist/staging
Browse files Browse the repository at this point in the history
Release V2.0
  • Loading branch information
CaptainChemist authored Jun 23, 2020
2 parents 19f7971 + 4910bf1 commit f211a82
Show file tree
Hide file tree
Showing 69 changed files with 15,403 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["next/babel"],
"plugins": [["styled-components", { "ssr": true }]]
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.next
.DS_Store
.env
.vercel
8 changes: 8 additions & 0 deletions codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
overwrite: true
schema: '${GRAPHCMSURL}/${GRAPHCMSPROJECTID}/${BRANCH}'
documents: graphql/**/*.ts
generates:
generated/apollo-components.tsx:
plugins:
- 'typescript'
- 'typescript-operations'
106 changes: 106 additions & 0 deletions components/CreateRecipe.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Row, Col, Form, Button } from 'antd';
import { submitForm } from '../utils/submitForm';
import { GenerateInput, GenerateTextInput } from './GenerateFields';
import { GenerateIngredients } from './GenerateIngredients';
import { useMutation } from '@apollo/react-hooks';
import { createRecipeGraphQL } from '../graphql/mutations/createRecipe';
import { useFetchUser } from '../utils/user';
import * as _ from 'lodash';
import { Loading } from './notify/Loading';
import Router from 'next/router';
import { recipesGraphQL } from '../graphql/queries/recipes';
import { PictureUploader } from './PictureUploader';
import { useState } from 'react';

export const CreateRecipe = () => {
const [recipeState, setRecipeState] = useState({ isPicUploading: false });
const [createRecipeMutation, { loading }] = useMutation(createRecipeGraphQL);
const { user, loading: isFetchingUser } = useFetchUser();
const owner = _.get(user, 'sub');

const initiateCreateRecipe = () => {
createRecipeMutation({
refetchQueries: [
{ query: recipesGraphQL },
{ query: recipesGraphQL, variables: { where: { owner } } },
],
variables: {
data: {
...inputs,
owner,
},
},
});
Router.replace('/my-recipes');
};

const {
inputs,
handleInputChange,
handleAddIngredient,
handleDeleteIngredient,
handleDropdownChange,
handleSubmit,
handleSubmitImage,
} = submitForm(
{
title: '',
description: '',
content: '',
ingredients: [],
},
initiateCreateRecipe,
);

if (isFetchingUser) return <Loading />;

return (
<Form onFinish={handleSubmit}>
<GenerateInput
name="title"
value={inputs.title}
handleInputChange={handleInputChange}
/>
<GenerateInput
name="description"
value={inputs.description}
handleInputChange={handleInputChange}
/>
<GenerateTextInput
name="content"
value={inputs.content}
handleInputChange={handleInputChange}
/>
<GenerateIngredients
names={['amount', 'unit', 'type']}
values={inputs.ingredients}
handleAddIngredient={handleAddIngredient}
handleDeleteIngredient={handleDeleteIngredient}
handleInputChange={handleInputChange}
handleDropdownChange={handleDropdownChange}
/>
<Row>
<Col span={12} />
<Col span={4}>
<Form.Item label="Upload Image">
<PictureUploader
setRecipeState={setRecipeState}
handleSubmitImage={handleSubmitImage}
/>
</Form.Item>
</Col>
<Col span={4}>
<Form.Item label="Create Recipe">
<Button
disabled={loading || recipeState.isPicUploading}
type="primary"
htmlType="submit"
>
Create Recipe
</Button>
</Form.Item>
</Col>
</Row>
</Form>
);
};
73 changes: 73 additions & 0 deletions components/DeleteButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Button, Modal } from 'antd';
import { useMutation } from '@apollo/react-hooks';
import { deleteRecipeGraphQL } from '../graphql/mutations/deleteRecipe';
import { useState } from 'react';
import { recipesGraphQL } from '../graphql/queries/recipes';
import Router from 'next/router';
import { deleteAssetGraphQL } from '../graphql/mutations/deleteAsset';

export const DeleteButton = ({
id,
disabled,
imageId,
}: {
id: string;
disabled: boolean;
imageId: string;
}) => {
const [deleteRecipeMutation, { loading: deleteRecipeLoading }] = useMutation(
deleteRecipeGraphQL,
);
const [deleteAssetMutation, { loading: deleteAssetLoading }] = useMutation(
deleteAssetGraphQL,
);

const [isModalVisible, setModalVisibility] = useState(false);

const handleOk = async () => {
if (imageId && !deleteAssetLoading) {
await deleteAssetMutation({
refetchQueries: [{ query: recipesGraphQL }],
variables: {
where: { id: imageId },
},
});
}

if (!deleteRecipeLoading) {
await deleteRecipeMutation({
refetchQueries: [{ query: recipesGraphQL }],
variables: {
where: { id },
},
});
}

setModalVisibility(false);
Router.replace('/my-recipes');
};
const handleShow = () => setModalVisibility(true);
const handleHide = () => setModalVisibility(false);

return (
<>
<Button
block
// @ts-ignore
type="danger"
disabled={disabled || deleteRecipeLoading || deleteAssetLoading}
onClick={handleShow}
>
Delete Recipe
</Button>
<Modal
title="Confirm Delete"
visible={isModalVisible}
onOk={handleOk}
onCancel={handleHide}
>
<p>Are you sure that you want to delete this recipe?</p>
</Modal>
</>
);
};
12 changes: 12 additions & 0 deletions components/GenerateContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from 'react';

export const GenerateContent = ({ textString }: { textString: string }) => (
<p>
{textString.split('\n').map((item, key) => (
<React.Fragment key={key}>
{item}
<br />
</React.Fragment>
))}
</p>
);
45 changes: 45 additions & 0 deletions components/GenerateFields.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Row, Col, Form, Input } from 'antd';

type InputType = {
name: string;
value: string;
handleInputChange?: (event: any) => void;
};

export const GenerateInput = ({
name,
value,
handleInputChange,
}: InputType) => (
<Row>
<Col span={12} offset={6}>
<Form.Item label={`${name}`}>
<Input
placeholder={`${name}`}
name={`${name}`}
value={value}
onChange={handleInputChange}
/>
</Form.Item>
</Col>
</Row>
);

export const GenerateTextInput = ({
name,
value,
handleInputChange,
}: InputType) => (
<Row>
<Col span={12} offset={6}>
<Form.Item label={`${name}`}>
<Input.TextArea
placeholder={`${name}`}
name={`${name}`}
value={value}
onChange={handleInputChange}
/>
</Form.Item>
</Col>
</Row>
);
102 changes: 102 additions & 0 deletions components/GenerateIngredients.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Row, Col, Button, Table, Input, Dropdown } from 'antd';
import { MenuList } from './MenuList';
import * as _ from 'lodash';

type IngredientsProps = {
names?: string[];
values?: [{ amount: string; unit: string; type: string }];
handleAddIngredient: (event: any) => void;
handleDeleteIngredient: (event: any) => void;
handleInputChange: (event: any) => void;
handleDropdownChange: (event: any) => void;
};

const units = ['-', 'ounce', 'lb', 'cup', 'tb', 'tsp', 'g', 'kg'];

export const GenerateIngredients = ({
names,
values,
handleAddIngredient,
handleDeleteIngredient,
handleInputChange,
handleDropdownChange,
}: IngredientsProps) => {
const columns = _.concat(
names.map((name) => ({
title: `${name}`,
key: `${name}`,
render: (ingredient, _record, index: number) => {
return name === 'unit' ? (
<Dropdown
overlay={
<MenuList
iterableList={units}
name={`ingredients[${index}].${name}`}
handleDropdownChange={handleDropdownChange}
/>
}
placement="bottomLeft"
>
<Button>{ingredient[name]}</Button>
</Dropdown>
) : (
<Input
value={ingredient[name]}
placeholder={`${name}`}
name={`ingredients[${index}].${name}`}
onChange={handleInputChange}
/>
);
},
})),
[
{
title: 'delete',
key: 'delete',
render: (_ingredient, _record, index: number) => (
<Button
onClick={handleDeleteIngredient}
// @ts-ignore
type="danger"
shape="circle"
size="small"
name={`${index}`}
>
-
</Button>
),
},
],
);

return (
<>
<Row>
<Col span={12} offset={6}>
<p>
<Button
onClick={handleAddIngredient}
type="primary"
shape="circle"
size="small"
>
+
</Button>
ingredients:
</p>
</Col>
</Row>
{values.length > 0 ? (
<Row>
<Col span={12} offset={6}>
<Table
dataSource={values}
columns={columns}
pagination={{ pageSize: 25 }}
/>
</Col>
</Row>
) : null}
</>
);
};
Loading

0 comments on commit f211a82

Please sign in to comment.