| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import React,{useState} from 'react' | ||
| import { Form,Button } from 'react-bootstrap' | ||
| import {Snippet} from '../utils/models/Snippets' | ||
| import {connect} from '../utils/db' | ||
| import * as copy from 'clipboard-copy' | ||
| export default function createSnippet({snippetText}){ | ||
|
|
||
| return ( | ||
| <div className="text-center mt-4"> | ||
| <h1> | ||
| Snippet | ||
| </h1> | ||
| <Button | ||
| onClick={()=>copy(window.location)} | ||
| className="mb-4 mt-2" | ||
| variant="outline-info" | ||
| > | ||
| Copy link to clipboard | ||
| </Button> | ||
|
|
||
| <Form> | ||
| <Form.Group className="mb-3" controlId="exampleForm.ControlTextarea1"> | ||
| <Form.Control | ||
| style={{margin:"0 auto" ,width:'400px',height:"300px" }} | ||
| disabled | ||
| value={snippetText} | ||
| as="textarea" rows={3} /> | ||
| </Form.Group> | ||
| </Form> | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| </div> | ||
|
|
||
|
|
||
| )} | ||
| export async function getServerSideProps(context) { | ||
| await connect(); | ||
|
|
||
| const slug=context.params.slug | ||
| const snippetObject=await Snippet.findOne({ | ||
| slug, | ||
| }) | ||
| return { | ||
| props: { | ||
| snippetText:snippetObject.snippet, | ||
| }, // will be passed to the page component as props | ||
| } | ||
| } | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,31 @@ | ||
| import '../styles/globals.css' | ||
| import 'bootstrap/dist/css/bootstrap.min.css'; | ||
| import {Button,Navbar,Nav,Form,FormControl, Container, Row, Col} from 'react-bootstrap' | ||
| function MyApp({ Component, pageProps }) { | ||
| return <> | ||
| <Navbar bg="dark" variant="dark"> | ||
| <Navbar.Brand href="/">Paste Bucket</Navbar.Brand> | ||
| <Nav className="mr-auto"> | ||
| <Nav.Link href="/">Home</Nav.Link> | ||
| <Button | ||
| href="/create-snippet" | ||
| variant="outline-success" | ||
| > | ||
| Create Snippet</Button> | ||
|
|
||
| </Nav> | ||
|
|
||
|
|
||
| </Navbar> | ||
| <Container> | ||
| <Row> | ||
| <Col> | ||
| <Component {...pageProps} /> | ||
| </Col> | ||
| </Row> | ||
|
|
||
| </Container> | ||
| </> | ||
| } | ||
|
|
||
| export default MyApp |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { connect } from "../../utils/db"; | ||
| import { Snippet } from "../../utils/models/Snippets"; | ||
| import * as randomstring from "randomstring"; | ||
|
|
||
| export default async (req, res) => { | ||
| await connect(); | ||
|
|
||
| if (req.method === "POST") { | ||
| const slug = randomstring.generate({ | ||
| length: 6, | ||
| charset: "alphabetic", | ||
| }); | ||
| const snippet = await Snippet.create({ | ||
| snippet: req.body.snippet, | ||
| slug, | ||
| }); | ||
|
|
||
| res.statusCode = 200; | ||
| res.json(snippet); | ||
| } else { | ||
| throw new Error( | ||
| "http method not supported on this endpoint" | ||
| ); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import React, { useState } from "react"; | ||
| import { Form, Button } from "react-bootstrap"; | ||
| import { useRouter } from "next/router"; | ||
|
|
||
| export default function CreateSnippet() { | ||
| const router = useRouter(); | ||
|
|
||
| const [snippet, setSnippet] = useState(""); | ||
|
|
||
| const saveSnippet = async () => { | ||
| const response = await fetch("/api/snippets", { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify({ | ||
| snippet, | ||
| }), | ||
| }); | ||
| const createdSnippet = await response.json(); | ||
| router.push(`/${createdSnippet.slug}`); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="text-center mt-4"> | ||
| <h1>Upload your snippet now!</h1> | ||
| <p> | ||
| Paste whatever text you want in the textarea below, | ||
| save it, and share the link with friends. | ||
| </p> | ||
|
|
||
| <Form> | ||
| <Form.Group controlId="exampleForm.ControlTextarea1"> | ||
| <Form.Control | ||
| style={{ | ||
| margin: "0 auto", | ||
| width: "400px", | ||
| height: "300px", | ||
| }} | ||
| onChange={(e) => setSnippet(e.target.value)} | ||
| as="textarea" | ||
| rows={3} | ||
| /> | ||
| </Form.Group> | ||
|
|
||
| <Button | ||
| onClick={saveSnippet} | ||
| variant="outline-info" | ||
| > | ||
| Save your Snippet | ||
| </Button> | ||
| </Form> | ||
| </div> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import mongoose from "mongoose"; | ||
|
|
||
| let isConnected = false; | ||
| // const uri = "mongodb+srv://Arif:<password>@cluster0.3tw6k.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"; | ||
| const localUri="mongodb://localhost/paste-buckets" | ||
| export const connect = async () => { | ||
| if (!isConnected) { | ||
| await mongoose.connect( | ||
| localUri, | ||
| { | ||
| useNewUrlParser: true, | ||
| useUnifiedTopology: true, | ||
| useFindAndModify: false, | ||
| useCreateIndex: true, | ||
| } | ||
| ); | ||
| isConnected = true; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import mongoose from "mongoose"; | ||
|
|
||
| const Schema = mongoose.Schema; | ||
|
|
||
| const snippetSchema = new Schema({ | ||
| snippet: String, | ||
| slug: String, | ||
| }); | ||
|
|
||
| export const Snippet = | ||
| mongoose.models.Snippet || | ||
| mongoose.model("Snippet", snippetSchema); |