Skip to content

Latest commit

 

History

History
867 lines (582 loc) · 9.43 KB

API.md

File metadata and controls

867 lines (582 loc) · 9.43 KB

API Reference

REST

[M] - mandatory; [D:] - default value,


AUTH

/api/auth

/signup

POST

request body

{
  "email": "[M]",
  "first_name": "[M]",
  "password": "[M]",
  "state": "[M]",
  "city": "[M]",
  "username": ""
}

response:

201

{}

409

{
    "error": "The user with email {email} already exists"
}

{
    "error": "The user with username {username} already exists'"
}

/login

POST

request body

{
  "login": "[M - username or email]",
  "password": "[M]"
}

response:

200

{}

401

{
    "error": "The user with email {login} does not exist"
}

{
    "error": "The user with username {login} does not exist"
}

{
    "error": "Wrong password"
}

/logout

POST

response:

200

{}

/resend_email

POST

cookies:

session

response:

200

{
  "msg": "A confirmation email has been sent"
}

404

{}

406

{ "error": "Sorry, you can resend confirmation email in { ... } minutes" }

/reset_password

POST

request body

{
  "email": ""
}

200

{
  "msg": "A reset password email has been sent"
}

401

{
  "error": "The user with email {email} does not exist"
}

/confirm/

GET

response:

200

{
  "msg": "Email confirmed"
}

404

{}

/reset_password_confirm

POST

request body

{
  "token": "",
  "password": ""
}

200

{
  "msg": "A new password has been saved"
}

404

{
  "error": "Can not find the user"
}

406

{
  "error": "The token is not valid or expired"
}

ACCOUNT

/api/account

/get_profile

POST

cookies:

session

request body

{
  "password": "[M]"
}

response:

200

{}

404

{}

401

{
  "msg": "Token has expired"
}

/change_pass'

PATCH

cookies:

session

request body

{
  "password": "[M]"
}

response:

200

{
  "msg": "Password updated"
}

404

{}

401

{
    "msg": "Token has expired"
}

{
    "msg": "Fresh token required"
}

/edit_profile

PATCH

cookies:

session

request body

Any of these fields:

{
  "first_name": "",
  "last_name": "",
  "image_url": ""
}

response:

200

{
  "msg": "Profile updated"
}

404

{}

401

{
  "msg": "Token has expired"
}

/edit_username

PATCH

cookies:

session

request body

{
  "username": "[M]"
}

response:

200

{
  "msg": "Username updated"
}

404

json {}

401

{
    "msg": "Token has expired"
}

{
    "msg": "Fresh token required"
}

409

{
  "msg": "The user with username {username} already exists"
}

/request_change_email

POST

cookies:

session

request body

{
  "new_email": "[M]"
}

response:

200

{
  "msg": "A confirmation e-mail was sent to your {new_email} address. Please follow the instructions in the e-mail to confirm your new email"
}

**404**

json {}


**401**

```json
{
    "msg": "Token has expired"
}

{
    "msg": "Fresh token required"
}

409

{
  "msg": "The user with email {new_email} already exists"
}

/confirm/

GET

response:

200

{
  "msg": "Email confirmed"
}

404

json {}

USERS

/api/users


CHAT

/api/chat

/get_user_chats

POST

cookies:

session

response:

200

{
    "chats" :[
        {
            "chat_id": "",
            "created_at": "",
            "recipient_id"
        },
    ]
}

/send_message

POST

cookies:

session

request body:

{
  "recipient_id": "[M]",
  "body": "[M]"
}

response:

200

{
  "chat_id": ""
}

/get_chat_messages

POST

cookies:

session

request body:

{
  "chat_id": "[M]"
}

response:

200

{
  "chats": [
    {
      "created_at": "",
      "sender_id": "",
      "body": ""
    }
  ]
}

Product

/api/products

/add_product

POST

cookies:

session

/all-products

/product-per-user


Hooks

useRequest

const {
  isLoading,
  data,
  error,
  errorNum,
  sendRequest,
  uploadStatus,
} = useRequest();

Return:

isLoading - [bool] is true while waiting for a response from server

data - [object] - response from a server if request is successful

error - [string] - error message

errorNum - [int] - error status number

sendRequest - [func] - function to send request

uploadStatus - [int] - shows how much data is uploaded

sendRequest(url, method, body);

url - [string] method - [string] body - [object] upload - [bool] (optional) - If true sets timeout of the request to null and allows wait until file is uploaded

example:

const {
  isLoading,
  data,
  error,
  errorNum,
  sendRequest,
  uploadStatus,
} = useRequest();

const onSubmit = (formData) => {
  sendRequest("/api/auth/login", "post", formData);
};

useEffect(() => {
  if (data) {
    history.push("/profile");
  }
}, [data]);

useModal

const {modal, showModal, closeModal, isOpen} = useModal({
    withBackdrop
    useTimer,
    inPlace,
    timeOut,
    disableClose
  });

Arguments:

withBackdrop - [bool]

useTimer - [bool]

inPlace - [bool] - show modal inside the element

timeOut - [int] (default: 5000 ms) if useTimer is true - how long the modal is shown on the screen

disableClose - [bool] (default: false) - if true, modal can be closed only by clicking on 'x' button

Return:

modal - created react element

showModal - [func] function to show the modal

closeModal - [func] function to close the modal

isOpen - [bool] state of the modal, true if modal is open

showModal(children, classes);

children - element to add to modal

type - [string]: "error" - red background color, "ok" - green background color

Example:

const { modal, showModal, closeModal, isOpen } = useModal({
  withBackdrop: false,
  useTimer: true,
  timeOut: 10000,
  inPlace: false,
  disableClose: true,
});

useEffect(() => {
  if (error) {
    showModal(error, "error");
  } else if (data && data.msg) {
    showModal(data.msg, "ok");
  }
}, [error, errorNum, data]);

return { modal };

useForm

const {
  setFormData,
  handleSubmit,
  handleInputChange,
  formData,
  formErrors,
} = useForm(inputFormData, onSubmit, formValidation);

Arguments:

inputFormData - initial form fields

onSubmit - function to execute after validation

formValidation - function to validate form fields

Return:

setFormData - [func] function to update form fields state

handleSubmit - [func] starts validation and if validation passed, sends request

handleInputChange - [func] updates form fields state

formData - [object] form fields state

formErrors - [object] if validation didn't pass, contains errors

useUploadImages

Creates drag and drop container for images

const [uploadImagesContainer, filesToSend] = useUploadImages({
  multipleImages,
});

Argument:

multipleImages - [bool] (optional) - default false - Allows to drop and load more then one image, max 4 images

Return:

uploadImagesContainer = [JSX element] - Drag and drop container

filesToSend = [array] - List of File objects


useScreen

Checks current width of the screen and returns isDesktop true if width is greater then breakpoint, returns screen width and height

const { isDesktop, screenWidth, screenHeight } = useScreen(breakpoint);

Argument:

breakpoint - [int] (optional) - default is 600 px

Return:

isDesktop = [bool] - true if width of the screen greater then breakpoint

screenWidth [int] - current inner width of the screen

screenHeight [int] - current inner height of the screen


useElementPosition

Keeps track of the next and previous items inside of the ref element

const {
  hasItemsOnLeft,
  hasItemsOnRight,
  scrollLeft,
  scrollRight,
} = useElementPosition(ref);

Argument:

ref - reference to the JSX element

Return:

hasItemsOnLeft - [bool] - true if there is an element to the left of the referenced element

hasItemsOnRight - [bool] - true if there is an element to the right of the referenced element

scrollLeft - [fn] - scrolls to the position of the element positioned to the left of the referenced element

scrollRight - scrolls to the position of the element positioned to the right of the referenced element


Components