1
- import React , { useEffect , useState } from 'react' ;
1
+ import React , { useContext , useEffect , useState } from 'react' ;
2
2
import './index.scss' ;
3
3
import rightNavbtn from '../../../app/assets/images/right_navigation_button.svg' ;
4
- import { getOrg , getOrgMembers } from 'app/api/organization' ;
4
+ import { deleteOrg , getOrg , getOrgMembers } from 'app/api/organization' ;
5
5
import { deleteFile , getIcon } from 'app/api/file' ;
6
6
import { getMembers } from 'app/api/project' ;
7
+ import UserContext from 'app/context/user/userContext' ;
8
+ import toast from 'react-hot-toast' ;
9
+ import { UserOrgDetails , UserOrgs , setOrgArcheiveStatus , setOrgBookmarkStatus } from 'app/api/user' ;
7
10
8
11
type workspaceCardProps = {
9
12
workspaceName : string ;
@@ -15,6 +18,7 @@ type workspaceCardProps = {
15
18
interface members {
16
19
[ username : string ] : string
17
20
}
21
+
18
22
const WorkspaceCard = ( props : workspaceCardProps ) => {
19
23
const { workspaceName, role, archeive, bookmark , archeives} = props ;
20
24
const [ description , setDescription ] = useState < null | string > ( null )
@@ -23,6 +27,7 @@ const WorkspaceCard = (props: workspaceCardProps) => {
23
27
const [ fileName , setFileName ] = useState < string | null > ( null ) ;
24
28
const [ imageSrc , setImageSrc ] = useState < string | null > ( null ) ;
25
29
const [ members , setMembers ] = useState < members | null > ( null )
30
+ const userContext = useContext ( UserContext )
26
31
const workSpaceData = async ( ) => {
27
32
if ( token && workspaceName && ! workspaceName . endsWith ( "-userspace" ) ) {
28
33
try {
@@ -53,10 +58,104 @@ const WorkspaceCard = (props: workspaceCardProps) => {
53
58
}
54
59
55
60
useEffect ( ( ) => {
61
+ console . log ( workspaceName , bookmark )
56
62
workSpaceData ( )
57
- } , [ workspaceName ] )
63
+ } , [ workspaceName , userContext ?. setUserOrgs ] )
64
+
65
+ const HandleDelete = async ( ) => {
66
+ if ( ! workspaceName . endsWith ( "-userspace" ) && token ) {
67
+ const func = async ( ) => {
68
+ const res = await deleteOrg ( token , workspaceName ) ;
69
+ const orgs = userContext ?. userOrgs
70
+
71
+ if ( orgs ?. userOrgs . hasOwnProperty ( workspaceName ) ) {
72
+ const obj : UserOrgDetails = orgs . userOrgs ;
73
+ delete obj [ workspaceName ] ;
74
+ userContext ?. setUserOrgs ( {
75
+ userOrgs : obj
76
+ } )
77
+ }
78
+
79
+
80
+
81
+ }
82
+ toast . promise ( func ( ) , {
83
+ loading : 'Deleting' ,
84
+ success : < b > Successfully Deleted</ b > ,
85
+ error : < b > Error while deleting</ b > ,
86
+ } ) ;
87
+
88
+ }
89
+ }
90
+
58
91
59
-
92
+ const HandlePin = async ( ) => {
93
+ if ( ! workspaceName . endsWith ( "-userspace" ) && token ) {
94
+ const initBmk = bookmark
95
+ const func = async ( ) => {
96
+ let status : { [ key : string ] : boolean } = {
97
+ [ workspaceName ] : ! bookmark ,
98
+ } ;
99
+
100
+
101
+ const res = await setOrgBookmarkStatus ( token , status )
102
+ const orgs = userContext ?. userOrgs
103
+ if ( orgs ?. userOrgs . hasOwnProperty ( workspaceName ) ) {
104
+
105
+ orgs . userOrgs [ workspaceName ] . bookmark = ( ! bookmark ) . toString ( ) ;
106
+ userContext ?. setUserOrgs ( orgs ) ;
107
+ }
108
+ }
109
+ if ( initBmk ) {
110
+ toast . promise ( func ( ) , {
111
+ loading : 'Unpinning' ,
112
+ success : < b > Successfully unpinned</ b > ,
113
+ error : < b > Error while unpinning</ b > ,
114
+ } ) ;
115
+ }
116
+ else {
117
+ toast . promise ( func ( ) , {
118
+ loading : 'Pinning' ,
119
+ success : < b > Successfully pinned</ b > ,
120
+ error : < b > Error while pinning</ b > ,
121
+ } )
122
+ }
123
+ }
124
+ }
125
+
126
+ const HandleArchive = async ( ) => {
127
+ if ( ! workspaceName . endsWith ( "-userspace" ) && token ) {
128
+ const initArc = archeive
129
+ const func = async ( ) => {
130
+ let status : { [ key : string ] : boolean } = {
131
+ [ workspaceName ] : ! bookmark ,
132
+ } ;
133
+
134
+
135
+ const res = await setOrgArcheiveStatus ( token , status )
136
+ const orgs = userContext ?. userOrgs
137
+ if ( orgs ?. userOrgs . hasOwnProperty ( workspaceName ) ) {
138
+
139
+ orgs . userOrgs [ workspaceName ] . archive = ( ! bookmark ) . toString ( ) ;
140
+ userContext ?. setUserOrgs ( orgs ) ;
141
+ }
142
+ }
143
+ if ( initArc ) {
144
+ toast . promise ( func ( ) , {
145
+ loading : 'Archiving' ,
146
+ success : < b > Successfully archived</ b > ,
147
+ error : < b > Error</ b > ,
148
+ } ) ;
149
+ }
150
+ else {
151
+ toast . promise ( func ( ) , {
152
+ loading : 'Unarchiving' ,
153
+ success : < b > Successfully unarchived</ b > ,
154
+ error : < b > Error</ b > ,
155
+ } )
156
+ }
157
+ }
158
+ }
60
159
61
160
return (
62
161
< >
@@ -72,9 +171,9 @@ const WorkspaceCard = (props: workspaceCardProps) => {
72
171
/> }
73
172
</ div >
74
173
< div className = { showPopUp ? 'workspace-popup' : 'hide' } >
75
- < div className = 'pin' > Pin</ div >
76
- < div className = 'archive' > archive</ div >
77
- < div className = 'delete' > delete</ div >
174
+ < div className = 'pin' onClick = { HandlePin } > { bookmark ? "UnPin" : " Pin" } </ div >
175
+ < div className = 'archive' onClick = { HandleArchive } > { archeive ? "Unarchive" : " archive" } </ div >
176
+ < div className = 'delete' onClick = { HandleDelete } > delete</ div >
78
177
</ div >
79
178
< div className = 'workspace-card-utils' >
80
179
< div className = 'workspace-logo' >
0 commit comments