Commit 65f80627 authored by Daniel Puchala's avatar Daniel Puchala
Browse files

feat: create review

parent fb58b221
import Header from './Header';
import { useState } from 'react';
import { login } from '../services/auth';
import { getUser, login } from '../services/auth';
import { useNavigate } from 'react-router-dom';
export const Login = () => {
......
......@@ -7,7 +7,7 @@ import { useParams } from 'react-router-dom';
import instance from '../models/axios';
import useSWR from 'swr';
import fetcher from '../models/fetcher';
import { getUser } from '../services/auth';
import { getToken, getUser } from '../services/auth';
import formatDuration from '../services/formatDuration';
export const Movie = () => {
......@@ -21,14 +21,16 @@ export const Movie = () => {
const sendReview = async (data: any) => {
const headers = {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'Authorization': getToken()
};
const reviewData = {
...data,
movieId: id
movieId: id,
userId: getUser().sub,
};
console.log(reviewData);
// await instance.post('/reviews', reviewData, { headers });
console.log(reviewData)
await instance.post('reviews/create', reviewData, { headers });
alert('Review created');
};
......@@ -89,24 +91,24 @@ export const Movie = () => {
>
<span className="text-2xl text-center font-bold">Write your review</span>
<label className="flex justify-between">
<span className="font-bold">Script: {getValues('script')}</span>
<input type="range" min={1} max={10} {...register('script', { required: true })}/>
<span className="font-bold">Script: {getValues('scriptRating')}</span>
<input type="range" min={1} max={10} {...register('scriptRating', { required: true })}/>
</label>
<label className="flex justify-between">
<span className="font-bold">Idea: {getValues('idea')}</span>
<input type="range" min={1} max={10} {...register('idea', { required: true })}/>
<span className="font-bold">Idea: {getValues('ideaRating')}</span>
<input type="range" min={1} max={10} {...register('ideaRating', { required: true })}/>
</label>
<label className="flex justify-between">
<span className="font-bold">Visual edits: {getValues('visualsEdit')}</span>
<input type="range" min={1} max={10} {...register('visualsEdit', { required: true })}/>
<span className="font-bold">Visual edits: {getValues('visualsEditRating')}</span>
<input type="range" min={1} max={10} {...register('visualsEditRating', { required: true })}/>
</label>
<label className="flex justify-between">
<span className="font-bold">Music: {getValues('music')}</span>
<input type="range" min={1} max={10} {...register('music', { required: true })}/>
<span className="font-bold">Music: {getValues('musicRating')}</span>
<input type="range" min={1} max={10} {...register('musicRating', { required: true })}/>
</label>
<label className="flex justify-between">
<span className="font-bold">Acting: {getValues('acting')}</span>
<input type="range" min={1} max={10} {...register('acting', { required: true })}/>
<span className="font-bold">Acting: {getValues('actingRating')}</span>
<input type="range" min={1} max={10} {...register('actingRating', { required: true })}/>
</label>
<label>
<textarea
......
......@@ -6,7 +6,7 @@ export const login = async (name: string, password: string) => {
const response = await instance.post('login', {
name, password
});
sessionStorage.setItem('user', JSON.stringify(jwtDecode(response.data)));
sessionStorage.setItem('user', JSON.stringify(response.data));
return response;
} catch (error) {
return error;
......@@ -17,7 +17,21 @@ export const logout = () => {
sessionStorage.removeItem('user');
}
export const getUser = () => {
export const getToken = () => {
const user = sessionStorage.getItem('user');
return user ? JSON.parse(user) : null;
}
export const getUser = (): UserAuth | null => {
const user = sessionStorage.getItem('user');
return user ? jwtDecode(JSON.parse(user)) : null;
}
interface UserAuth {
exp: number,
iat: number,
iss: string,
name: string,
role: string,
sub: string
}
......@@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
......@@ -45,7 +46,7 @@ public class ReviewControllerProvider implements ReviewController {
}
@PreAuthorize("isAuthenticated()")
@GetMapping("/create")
@PostMapping("/create")
@ResponseBody
public ReviewDto createReview(ReviewCreateDto reviewCreateDto) {
if (reviewCreateDto == null) throw new IllegalArgumentException("Create review body cannnot be null");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment