Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 31 additions & 33 deletions src/appwrite/config.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,63 @@
import confg from "../conf/conf";

import { Client , ID , Databases , Query , Storage } from 'appwrite'
import { Client, ID, Databases, Query, Storage } from 'appwrite'

export class Service{
export class Service {
client = new Client();
databases;
Storage;
constructor(){
constructor() {
this.client
.setEndpoint(confg.appwriteUrl)
.setProject(confg.appwriteProjectId)
.setEndpoint(confg.appwriteUrl)
.setProject(confg.appwriteProjectId)
this.Storage = new Storage(this.client)
this.databases = new Databases(this.client)
}

async createPost({title , slug , content , featuredImage, status, userId}){
async createPost({ title, slug, content, featuredImage, status, userId }) {
try {
return await this.databases.createDocument(
confg.appwriteDatabaseId,
confg.appwriteCollectionId,
ID, // documentId
ID.unique(), // documentId
{
title,
slug,
content,
featuredImage,
status,
userId
}
);
title,
slug,
content,
featuredImage,
status,
userId
}
);
} catch (error) {
console.log(`Error creating post : ${error}`);
}
}
async updatePost(ID,{title , slug , content , featuredImage, status,userId}){

async updatePost(Id, { title, slug, content, featuredImage, status, userId }) {
try {
return await this.databases.updateDocument(
confg.appwriteDatabaseId,
confg.appwriteCollectionId,
ID,
Id,
{
title,
slug,
content,
featuredImage,
status,
userId
}
title,
content,
featuredImage,
status,
}
)
} catch (error) {
console.log(`Error updating post : ${error}`);
}
}

async deletePost(ID){
async deletePost(Id) {
try {
await this.databases.deleteDocument(
confg.appwriteDatabaseId,
confg.appwriteCollectionId,
ID,
Id,
)
return true;
} catch (error) {
Expand All @@ -68,20 +66,20 @@ export class Service{
}
}

async getPost(ID){
async getPost(Id) {
try {
return await this.databases.getDocument(
confg.appwriteDatabaseId,
confg.appwriteCollectionId,
ID
Id,
)
} catch (error) {
console.log(`Error getting post : ${error}`);
return false;
}
}

async getAllPost(queries = [Query.equal("status","active")]){
async getAllPost(queries = [Query.equal("status", "active")]) {
try {
return await this.databases.listDocuments(
confg.appwriteDatabaseId,
Expand All @@ -97,7 +95,7 @@ export class Service{

// file upload service

async uploadFile(file){
async uploadFile(file) {
try {
return await this.Storage.createFile(
confg.appwriteBucketId,
Expand All @@ -109,7 +107,7 @@ export class Service{
}
}

async removeFile(fileId){
async removeFile(fileId) {
try {
await this.Storage.deleteFile(
confg.appwriteBucketId,
Expand All @@ -122,7 +120,7 @@ export class Service{
}
}

async getFilePreview(fileId){
async getFilePreview(fileId) {
try {
return await this.Storage.getFilePreview(
confg.appwriteBucketId,
Expand Down
5 changes: 3 additions & 2 deletions src/components/AuthLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ const Protected = ({children,authentication = true}) => {
}else{
setLoading(false)
}
},[authStatus,navigate,authentication])
setLoading(false);
},[authStatus, navigate, authentication]);

if(authStatus === null || loading){
return <h1>Loading...</h1>
}

return (
<>{children}</>
loading ? <h1>Loading...</h1> : <div>{children}</div>
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/Header/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useNavigate } from 'react-router-dom'

const Header = () => {

const authStatus = useSelector((state)=> state.auth.Status)
const authStatus = useSelector((state)=> state.auth.status)

const navigate = useNavigate()

Expand Down
8 changes: 4 additions & 4 deletions src/components/Header/LogoutBtn.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import React from 'react'
import { useDispatch } from 'react-redux'
import authService from "../../appwrite/auth.js"
import logout from "../../store/authSlice.js"
import authService from "../../appwrite/auth"
import {logout} from "../../store/authSlice"
import { useNavigate } from 'react-router-dom'

const LogoutBtn = () => {
const dispatch = useDispatch();
const navigate = useNavigate()
const logOutHandler = () => {
authService.logout().then(()=>{
dispatch(logout()) // to update the state
dispatch(logout()); // to update the state
})
navigate('/login')

}
return (
<button className='inline-block px-6 py-2 duration-200 hover:bg=blue-100 rounded-full'
<button className='inline-block bg-amber-100 px-6 py-2 duration-200 hover:bg=blue-100 rounded-full'
onClick={logOutHandler}
>
Logout
Expand Down
4 changes: 2 additions & 2 deletions src/components/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const Login = () => {
try {
const session = await authService.login(data)
if(session){
const userData = await authService.isLogin() // get current user
if(userData) dispatch(authLogin(userData))
const userData = await authService.isLogin()
if(userData) dispatch(authLogin({userData}))
navigate('/')

}else{
Expand Down
2 changes: 1 addition & 1 deletion src/components/RTE.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { Editor } from '@tinymce/tinymce-react'
import { Controller } from 'react-hook-form'

const RTE = (name,control , label ) => {
const RTE = ({name,control , label }) => {
return (
<div className='w-full'>
{label && <label className='test-sm text-gray-600'>{label}</label>}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Signup = () => {
const userData = await authService.createAccount(data)
if (userData) {
const userData = await authService.isLogin()
if(userData) dispatch(login(userData));
if(userData) dispatch(login({userData}));
navigate("/")
console.log('Signup component mounted')

Expand Down
4 changes: 2 additions & 2 deletions src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import RTE from "./RTE"
import Select from "./Select"
import Button from "./Button"
import Input from "./Input"
import Signup from "../pages/Signup";
import Signup from "../pages/SignupPage";
import PostForm from "./postForm/PostForm"
import PostCard from "./PostCard"
import AuthLayout from "./AuthLayout"
import Login from "./Login"

export{
export {
Header,
Footer,
Container,
Expand Down
Loading