Skip to content

Commit 801bf86

Browse files
authored
fix: Merge pull request #251 from UniversalDataTool/fix/cognito-3
[WIP] Get Cognito Working Again (with tests!)
2 parents 8d12e53 + b31dc03 commit 801bf86

File tree

9 files changed

+168
-15
lines changed

9 files changed

+168
-15
lines changed

src/components/HeaderToolbar/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const HeaderToolbar = ({
9898
}) => {
9999
const c = useStyles()
100100
const { authProvider, isLoggedIn, logout } = useAuth()
101+
101102
return (
102103
<AppBar color="default" position="static">
103104
<Toolbar variant="dense">

src/components/ImportFromS3Dialog/get-images-from-aws.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default async (result, folderToFetch, configImport, authConfig) => {
6161
for (let i = 0; i < result.length; i++) {
6262
if (result[i].key.match(`(${folderToFetch}/data).*(\\.).*`)) {
6363
await Storage.get(result[i].key, {
64-
expires: 24 * 60 * 60 * 2000,
64+
expires: 24 * 60 * 60,
6565
level: "private",
6666
})
6767
.then(async (result) => {

src/components/ImportPage/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { FaGoogleDrive, FaYoutube } from "react-icons/fa"
2626
import usePosthog from "../../utils/use-posthog"
2727
import promptAndGetSamplesFromLocalDirectory from "./prompt-and-get-samples-from-local-directory.js"
2828
import { useTranslation } from "react-i18next"
29+
import useAuth from "../../utils/auth-handlers/use-auth.js"
2930

3031
const ButtonBase = styled(MuiButton)({
3132
width: 240,
@@ -64,20 +65,21 @@ const Button = ({
6465
children,
6566
dialog,
6667
authConfiguredOnly,
67-
authConfig,
6868
signedInOnly,
6969
user,
7070
onlySupportType,
7171
type,
7272
}) => {
7373
const posthog = usePosthog()
7474

75+
const { isLoggedIn, authConfig } = useAuth()
76+
7577
const isDisabled = () => {
7678
if (desktopOnly) {
7779
return { disabled: !isDesktop, disabledText: "DESKTOP ONLY" }
7880
} else if (onlySupportType && !onlySupportType.includes(type)) {
7981
return { disabled: true, disabledText: `DOESN'T SUPPORT THIS INTERFACE` }
80-
} else if (authConfiguredOnly) {
82+
} else if (authConfiguredOnly && !isLoggedIn) {
8183
if (signedInOnly) {
8284
return { disabled: isEmpty(user), disabledText: "MUST BE SIGNED IN" }
8385
} else {
@@ -234,7 +236,6 @@ export default ({
234236
dialog="import-from-s3"
235237
Icon={S3Icon}
236238
authConfiguredOnly={true}
237-
authConfig={authConfig}
238239
signedInOnly={true}
239240
user={user}
240241
>
@@ -265,7 +266,6 @@ export default ({
265266
{file && (
266267
<ImportFromS3Dialog
267268
file={file}
268-
authConfig={authConfig}
269269
open={selectedDialog === "import-from-s3"}
270270
onChangeFile={onChangeFile}
271271
onClose={closeDialog}

src/utils/auth-handlers/cognito-handler.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ class CognitoHandler {
2020
},
2121
}
2222
this.isLoggedIn = false
23+
this.checkIfLoggedIn()
24+
}
25+
checkIfLoggedIn = async () => {
26+
Amplify.configure(this.authConfig)
27+
const userHandle = await Auth.currentAuthenticatedUser()
28+
if (userHandle) {
29+
this.user = userHandle
30+
this.isLoggedIn = true
31+
this.hasChanged = true
32+
}
2333
}
2434
setUser = (userHandle) => {
2535
this.user = userHandle

src/utils/auth-handlers/use-auth.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import React, {
44
createContext,
55
useContext,
66
useMemo,
7+
useReducer,
78
} from "react"
89
import { useAppConfig } from "../../components/AppConfig"
910
import CognitoHandler from "./cognito-handler.js"
10-
import { useUpdate } from "react-use"
1111

1212
export const authProviders = ["cognito"]
1313

@@ -17,7 +17,10 @@ export const AuthProvider = ({ children }) => {
1717
const { appConfig, fromConfig } = useAppConfig()
1818
const [handler, setHandler] = useState({ authProvider: "none" })
1919
const authProvider = fromConfig("auth.provider")
20-
const forceUpdate = useUpdate()
20+
const [handlerVersion, incHandlerVersion] = useReducer(
21+
(state) => state + 1,
22+
0
23+
)
2124

2225
useEffect(() => {
2326
if (handler && handler.authProvider === authProvider) return
@@ -30,14 +33,14 @@ export const AuthProvider = ({ children }) => {
3033
if (!handler) return
3134
const interval = setInterval(() => {
3235
if (handler.hasChanged) {
33-
forceUpdate()
36+
incHandlerVersion()
3437
handler.hasChanged = false
3538
}
3639
}, 1000)
3740
return () => {
3841
clearInterval(interval)
3942
}
40-
}, [handler, forceUpdate])
43+
}, [handler, handlerVersion])
4144

4245
const contextValue = useMemo(
4346
() => ({
@@ -47,8 +50,9 @@ export const AuthProvider = ({ children }) => {
4750
setUser: handler.setUser,
4851
logout: handler.logout,
4952
login: handler.login,
53+
handlerVersion,
5054
}),
51-
[handler]
55+
[handler, handlerVersion]
5256
)
5357

5458
return (
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import isEmpty from "lodash/isEmpty"
2+
3+
export default (objectOfRef, objectToCheck) => {
4+
var resultSet = {
5+
fileName: false,
6+
content: {
7+
interface: {
8+
type: false,
9+
labels: false,
10+
regionTypesAllowed: false,
11+
},
12+
samples: false,
13+
},
14+
id: false,
15+
mode: false,
16+
any: false,
17+
}
18+
// Check if the object to check exist if not return false
19+
if (isEmpty(objectToCheck)) return resultSet
20+
21+
// Check if the object of reference exist if not return true
22+
if (isEmpty(objectOfRef)) {
23+
resultSet.fileName = true
24+
resultSet.content.interface.type = true
25+
resultSet.content.interface.labels = true
26+
resultSet.content.interface.regionTypesAllowed = true
27+
resultSet.content.samples = true
28+
resultSet.id = true
29+
resultSet.mode = true
30+
resultSet.any = true
31+
return resultSet
32+
}
33+
34+
if (objectOfRef !== objectToCheck) {
35+
resultSet.any = true
36+
} else {
37+
return resultSet
38+
}
39+
40+
// Check if the id doesn't exist or have change
41+
if (!isEmpty(objectToCheck.id)) {
42+
if (isEmpty(objectOfRef.id)) resultSet.id = true
43+
else if (objectToCheck.id !== objectOfRef.id) resultSet.id = true
44+
}
45+
46+
// Check if the content doesn't exist or have change
47+
if (!isEmpty(objectToCheck.content)) {
48+
if (isEmpty(objectOfRef.content)) {
49+
resultSet.content.interface.type = true
50+
resultSet.content.interface.labels = true
51+
resultSet.content.interface.regionTypesAllowed = true
52+
resultSet.content.samples = true
53+
} else if (objectToCheck.content !== objectOfRef.content) {
54+
//Check if the interface doesn't exist or have change
55+
if (!isEmpty(objectToCheck.content.interface)) {
56+
if (isEmpty(objectOfRef.content.interface)) {
57+
resultSet.content.interface.type = true
58+
resultSet.content.interface.labels = true
59+
resultSet.content.interface.regionTypesAllowed = true
60+
} else if (
61+
objectToCheck.content.interface !== objectOfRef.content.interface
62+
) {
63+
//Check if the type doesn't exist or have change
64+
if (!isEmpty(objectToCheck.content.interface.type)) {
65+
if (isEmpty(objectOfRef.content.interface.type)) {
66+
resultSet.content.interface.type = true
67+
} else if (
68+
objectToCheck.content.interface.type !==
69+
objectOfRef.content.interface.type
70+
) {
71+
resultSet.content.interface.type = true
72+
}
73+
}
74+
//Check if the labels doesn't exist or have change
75+
if (!isEmpty(objectToCheck.content.interface.labels)) {
76+
if (isEmpty(objectOfRef.content.interface.labels)) {
77+
resultSet.content.interface.labels = true
78+
} else if (
79+
objectToCheck.content.interface.labels !==
80+
objectOfRef.content.interface.labels
81+
) {
82+
resultSet.content.interface.labels = true
83+
}
84+
}
85+
//Check if the regionsTypesAllowed doesn't exist or have change
86+
if (!isEmpty(objectToCheck.content.interface.regionTypesAllowed)) {
87+
if (isEmpty(objectOfRef.content.interface.regionTypesAllowed)) {
88+
resultSet.content.interface.regionTypesAllowed = true
89+
} else if (
90+
objectToCheck.content.interface.regionTypesAllowed !==
91+
objectOfRef.content.interface.regionTypesAllowed
92+
) {
93+
resultSet.content.interface.regionTypesAllowed = true
94+
}
95+
}
96+
}
97+
}
98+
//Check if the samples doesn't exist or have change
99+
if (!isEmpty(objectToCheck.content.samples)) {
100+
if (isEmpty(objectOfRef.content.samples)) {
101+
resultSet.content.samples = true
102+
} else if (
103+
objectToCheck.content.samples !== objectOfRef.content.samples
104+
) {
105+
resultSet.content.samples = true
106+
}
107+
}
108+
}
109+
}
110+
111+
// Check if the mode doesn't exist or have change
112+
if (!isEmpty(objectToCheck.mode)) {
113+
if (isEmpty(objectOfRef.mode)) resultSet.mode = true
114+
else if (objectToCheck.mode !== objectOfRef.mode) resultSet.mode = true
115+
}
116+
117+
// Check if the fileName doesn't exist or have change
118+
if (!isEmpty(objectToCheck.fileName)) {
119+
if (isEmpty(objectOfRef.fileName)) resultSet.fileName = true
120+
else if (objectToCheck.fileName !== objectOfRef.fileName)
121+
resultSet.fileName = true
122+
}
123+
console.log({ resultSet })
124+
//Default behavior return false
125+
return resultSet
126+
}

src/utils/dataset-helper.js renamed to src/utils/dataset-helper/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// @flow weak
22

3-
import getSampleNameFromURL from "./get-sample-name-from-url"
4-
import RecognizeFileExtension from "./RecognizeFileExtension"
3+
import getSampleNameFromURL from "../get-sample-name-from-url"
4+
import RecognizeFileExtension from "../RecognizeFileExtension"
55
import isEmpty from "lodash/isEmpty"
66
import { setIn } from "seamless-immutable"
7+
import getFilesDifferencesImport from "./get-files-differences.js"
8+
9+
export const getFilesDifferences = getFilesDifferencesImport
710

811
export const getSampleName = (sample) => {
912
var sampleName

src/utils/file-handlers/use-aws-cognito/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import { useEffect, useRef, useCallback } from "react"
44
import UpdateAWSStorage from "./update-aws-storage"
5-
import * as datasetHelper from "../../dataset-helper"
65
import isEmpty from "lodash/isEmpty"
76
import useAuth from "../../auth-handlers/use-auth"
7+
import getFilesDifferences from "../../dataset-helper/get-files-differences.js"
88

99
const workingInterfaces = [
1010
"video_segmentation",
@@ -24,11 +24,11 @@ export default ({ file, setFile }) => {
2424
const shouldUpdateAWSStorage = useCallback(() => {
2525
if (!isLoggedIn || authProvider !== "cognito") return
2626

27-
var changes = datasetHelper.fileHasChanged(lastObjectRef.current, file)
27+
var changes = getFilesDifferences(lastObjectRef.current, file)
2828
if (
2929
isEmpty(file) ||
3030
(!changes.content.samples && !changes.fileName) ||
31-
!workingInterfaces.includes(file.content.interface.authProvider) ||
31+
!workingInterfaces.includes(file.content.interface.type) ||
3232
file.fileName === "unnamed"
3333
)
3434
return false
@@ -38,6 +38,7 @@ export default ({ file, setFile }) => {
3838
useEffect(() => {
3939
if (!isLoggedIn || authProvider !== "cognito") return
4040
if (!isEmpty(authConfig)) {
41+
console.log("should update", shouldUpdateAWSStorage())
4142
if (shouldUpdateAWSStorage()) UpdateAWSStorage(file)
4243
lastObjectRef.current = file
4344
}

src/utils/file-handlers/use-aws-cognito/update-aws-storage.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ export default (file) => {
3535
}
3636

3737
function createOrReplaceAnnotations(file, json) {
38+
console.log({ file, json })
3839
Storage.put(`${file.fileName}/annotations/annotations.json`, json, {
3940
level: "private",
4041
}).catch((err) => console.log(err))
4142
}
4243

4344
function createOrReplaceImages(file) {
45+
// TODO you can't have async in a forEach loop
4446
if (!isEmpty(file.content.samples)) {
4547
file.content.samples.forEach(async (element) => {
4648
try {
@@ -71,6 +73,12 @@ export default (file) => {
7173

7274
if (fileNameExist(file)) {
7375
var dataset = file.content
76+
// TODO datasetHelper.setSamplesName is returning an incorrect object, it
77+
// should be returning an array of samples
78+
console.log(
79+
"this should be an array of samples",
80+
datasetHelper.setSamplesName(dataset)
81+
)
7482
file = setIn(
7583
file,
7684
["content"],

0 commit comments

Comments
 (0)