Skip to content

Commit 21338ca

Browse files
Merge pull request #59 from unfoldingWord/bugfix-klappy-files-not-authenticated
fix for files when not authenticated
2 parents 3682c6d + 06f7614 commit 21338ca

File tree

11 files changed

+18394
-18200
lines changed

11 files changed

+18394
-18200
lines changed

coverage/shields.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"message":"51.5%","label":"Integration Tests","schemaVersion":1}
1+
{"message":"47.63%","label":"Integration Tests","schemaVersion":1}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"axios": "0.19.0",
1515
"axios-cache-adapter": "2.4.1",
1616
"base-64": "0.1.0",
17-
"jszip": "3.2.1",
17+
"jszip": "^3.5.0",
1818
"localforage": "1.7.3",
1919
"markdown-translatable": "1.2.8",
2020
"prop-types": "15.7.2",
@@ -110,4 +110,4 @@
110110
"**/components/**"
111111
]
112112
}
113-
}
113+
}

src/components/file/_read.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,19 @@ function Component() {
2121

2222
const [repository, setRepository] = React.useState();
2323
const [filepath, setFilepath] = React.useState();
24-
const [branch, setBranch] = React.useState('master');
2524
const config = {
2625
server: "https://bg.door43.org",
2726
tokenid:"PlaygroundTesting",
2827
};
2928

30-
<AuthenticationContextProvider
31-
>
29+
<AuthenticationContextProvider>
3230
<RepositoryContextProvider
3331
repository={repository}
3432
onRepository={setRepository}
3533
config={config}
36-
defaultQuery=''
37-
full_name='unfoldingWord/en_ta'
38-
branch={branch}
34+
defaultQuery='en_ta'
35+
// full_name='unfoldingWord/en_ta'
36+
branch='master'
3937
>
4038
<FileContextProvider
4139
filepath={filepath}

src/components/file/_update-delete.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const [filepath, setFilepath] = React.useState();
5353
onRepository={setRepository}
5454
defaultOwner={authentication && authentication.user.name}
5555
defaultQuery=""
56-
branch='testing'
56+
// branch='master'
5757
>
5858
<FileContextProvider
5959
filepath={filepath}

src/components/file/useFile.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import React, {
2-
useState, useCallback, useEffect, useContext,
2+
useState, useCallback, useContext,
33
} from 'react';
44
import PropTypes from 'prop-types';
5+
import useEffect from 'use-deep-compare-effect';
56

67
import {
78
getContentFromFile, saveFile, ensureFile, deleteFile,
89
} from './helpers';
910
import {
10-
FileCard, FileForm, useBlob, RepositoryContext, AuthenticationContext,
11+
FileCard, FileForm, useBlob, RepositoryContext,
1112
} from '..';
1213

1314
function useFile({
@@ -16,14 +17,14 @@ function useFile({
1617
filepath,
1718
onFilepath,
1819
defaultContent,
19-
config,
20-
create = false,
20+
config: _config,
21+
create=false,
2122
}) {
2223
const [file, setFile] = useState();
2324
const [blob, setBlob] = useState();
24-
const { actions: repositoryActions } = useContext(RepositoryContext);
25-
const { state: contextAuthentication, config: contextConfig } = useContext(AuthenticationContext);
25+
const { actions: { updateBranch }, config: repositoryConfig } = useContext(RepositoryContext);
2626

27+
const config = _config || repositoryConfig;
2728
const branch = repository && (repository.branch || repository.default_branch);
2829

2930
const [deleted, setDeleted] = useState();
@@ -41,14 +42,18 @@ function useFile({
4142
}, []);
4243

4344
const read = useCallback(async (_filepath) => {
44-
onFilepath && await onFilepath(_filepath);
45+
if (onFilepath) {
46+
await onFilepath(_filepath);
47+
};
4548
}, [onFilepath]);
4649

4750
const load = useCallback(async () => {
4851
if (config && repository && filepath) {
4952
const _file = await ensureFile({
5053
filepath, defaultContent, authentication, config, repository, branch,
5154
});
55+
// let content;
56+
// content = await repositoryActions.fileFromZip(filepath);
5257
const content = await getContentFromFile(_file);
5358

5459
update({
@@ -57,7 +62,9 @@ function useFile({
5762
}
5863
}, [authentication, branch, config, defaultContent, filepath, repository, update]);
5964

60-
const createFile = useCallback(async ({ branch: _branch, filepath: _filepath, defaultContent: _defaultContent }) => {
65+
const createFile = useCallback(async ({
66+
branch: _branch, filepath: _filepath, defaultContent: _defaultContent,
67+
}) => {
6168
if (config && repository) {
6269
const _file = await ensureFile({
6370
authentication, config, repository,
@@ -67,15 +74,20 @@ function useFile({
6774
});
6875

6976
if (_file) {
70-
repositoryActions.updateBranch(_branch);
77+
updateBranch(_branch);
7178
onFilepath(_filepath);
7279
};
7380
}
74-
}, [authentication, config, repository]);
81+
}, [authentication, config, repository, updateBranch, onFilepath]);
7582

7683
const close = useCallback(() => {
77-
if (blobActions && blobActions.close) blobActions.close();
78-
if (onFilepath) onFilepath();
84+
if (blobActions && blobActions.close) {
85+
blobActions.close();
86+
};
87+
88+
if (onFilepath) {
89+
onFilepath();
90+
};
7991
update();
8092
}, [update, blobActions, onFilepath]);
8193

@@ -110,14 +122,13 @@ function useFile({
110122
const blobFilepath = blobState && blobState.filepath;
111123

112124
useEffect(() => {
113-
if (blobFilepath && onFilepath) onFilepath(blobFilepath);
125+
if (blobFilepath && onFilepath) {
126+
onFilepath(blobFilepath);
127+
};
114128
}, [blobFilepath, onFilepath]);
115129

116130
useEffect(() => { // if there is a file but no repository, close file.
117-
if (!repository && file) {
118-
close();
119-
}
120-
if (!contextAuthentication) close();
131+
if (!repository && file) close();
121132
}, [repository, file, close]);
122133

123134
useEffect(() => {

src/components/repository/helpers.js

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import {
32
createFork,
43
readForks,
@@ -7,6 +6,7 @@ import {
76
updateRepo,
87
deleteRepo,
98
} from '../..';
9+
import { storeZipFromRepo, getFileFromRepoZip } from '.';
1010

1111
export const forkRepository = async ({ repository, config }) => {
1212
const { owner: { username }, name } = repository;
@@ -20,30 +20,78 @@ export const repositoryForks = async ({ repository, config }) => {
2020
return response;
2121
};
2222

23-
export const createRepository = async ({ repo, settings, config }) => {
23+
export const createRepository = async ({
24+
repo, settings, config,
25+
}) => {
2426
const _settings = {
2527
name: repo,
2628
description: `${repo || settings.name} created via API.`,
2729
private: false,
2830
...settings,
29-
}
30-
const response = await createRepo({ repo, settings: _settings, config });
31+
};
32+
const response = await createRepo({
33+
repo, settings: _settings, config,
34+
});
3135
return response;
3236
};
3337

34-
export const readRepository = async ({ owner, repo, config }) => {
35-
const response = await readRepo({ owner, repo, config });
38+
export const readRepository = async ({
39+
owner, repo, config,
40+
}) => {
41+
const response = await readRepo({
42+
owner, repo, config,
43+
});
3644
return response;
3745
};
3846

39-
export const saveRepository = async ({ repository, settings, config }) => {
47+
export const saveRepository = async ({
48+
repository, settings, config,
49+
}) => {
4050
const { owner: { username }, name } = repository;
41-
const response = await updateRepo({ owner: username, repo: name, settings, config });
51+
const response = await updateRepo({
52+
owner: username, repo: name, settings, config,
53+
});
4254
return response;
4355
};
4456

45-
export const deleteRepository = async ({ repository, settings, config }) => {
57+
export const deleteRepository = async ({
58+
repository, settings, config,
59+
}) => {
4660
const { owner: { username }, name } = repository;
47-
const response = await deleteRepo({ owner: username, repo: name, settings, config });
61+
const response = await deleteRepo({
62+
owner: username, repo: name, settings, config,
63+
});
4864
return response;
4965
};
66+
67+
export const storeRepositoryZip = async ({ repository, config }) => {
68+
const {
69+
owner: { username }, name, branch,
70+
} = repository;
71+
const response = await storeZipFromRepo({
72+
owner: username, repo: name, branch, server: config.server,
73+
});
74+
return response;
75+
};
76+
77+
export const removeRepositoryZip = async ({ repository, config }) => {
78+
const {
79+
owner: { username }, name, branch,
80+
} = repository;
81+
const response = await storeZipFromRepo({
82+
owner: username, repo: name, branch, server: config.server,
83+
});
84+
return response;
85+
};
86+
87+
export const getFileFromRepositoryZip = async ({
88+
repository, filepath, config,
89+
}) => {
90+
const {
91+
owner: { username }, name, branch,
92+
} = repository;
93+
const file = await getFileFromRepoZip({
94+
owner: username, repo: name, branch, filepath, config,
95+
});
96+
return file;
97+
};

src/components/repository/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export * from './crud';
22
export * from './Repository.context';
3+
export * from './zip-core';
4+
export * from './helpers';
35
export { default as Repository } from './Repository';
46
export { default as useRepository } from './useRepository';

src/components/repository/useRepository.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import {
1414
repositoryForks,
1515
createRepository,
1616
readRepository,
17+
storeRepositoryZip,
18+
removeRepositoryZip,
19+
getFileFromRepositoryZip,
1720
} from './helpers';
1821

1922
function useRepository({
@@ -33,6 +36,9 @@ function useRepository({
3336
const { full_name } = repository || {};
3437
const user = authentication && authentication.user;
3538

39+
// Due to objects not memoizing in useCallback, deconstruction is necessary
40+
// const { owner: { username: owner }, name: repo } = repository;
41+
3642
const update = useCallback((repo) => {
3743
if (onRepository) {
3844
let _repo;
@@ -87,6 +93,21 @@ function useRepository({
8793
};
8894
}, [config, repository, update]);
8995

96+
const storeZip = useCallback(async () => {
97+
await storeRepositoryZip({ repository, config });
98+
}, [repository, config]);
99+
100+
const removeZip = useCallback(async () =>{
101+
await removeRepositoryZip({ repository, config });
102+
}, [repository, config]);
103+
104+
const fileFromZip = useCallback(async (filepath) => {
105+
const file = await getFileFromRepositoryZip({
106+
repository, filepath, config,
107+
});
108+
return file;
109+
}, [repository, config]);
110+
90111
const forks = useCallback(() => {
91112
repositoryForks({ repository, config });
92113
}, [repository, config]);
@@ -150,7 +171,7 @@ function useRepository({
150171
component = components.browse;
151172
} else if (config) {
152173
component = components.search;
153-
}
174+
};
154175

155176
return {
156177
state: repository,
@@ -164,6 +185,9 @@ function useRepository({
164185
forks,
165186
updateBranch,
166187
read,
188+
storeZip,
189+
removeZip,
190+
fileFromZip,
167191
},
168192
component,
169193
components,

0 commit comments

Comments
 (0)