Skip to content

Commit

Permalink
fix: remove stickiness error (#3338)
Browse files Browse the repository at this point in the history
<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->

## About the changes

Code was failing on create project because project id is empty
<img width="1123" alt="Screenshot 2023-03-16 at 16 43 55"
src="https://user-images.githubusercontent.com/1394682/225688715-98a2bd19-cb1a-43ce-a405-f1911645c638.png">


### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->
  • Loading branch information
kwasniew committed Mar 17, 2023
1 parent c5af024 commit f907dfa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
Expand Up @@ -51,7 +51,11 @@ export const StickinessSelect = ({
disabled={!editable}
data-testid={dataTestId}
onChange={onChange}
style={{ width: 'inherit', minWidth: '100%' }}
style={{
width: 'inherit',
minWidth: '100%',
marginBottom: '16px',
}}
/>
);
};
40 changes: 12 additions & 28 deletions frontend/src/hooks/useDefaultProjectSettings.ts
@@ -1,15 +1,12 @@
import useUiConfig from './api/getters/useUiConfig/useUiConfig';
import useSWR, { SWRConfiguration } from 'swr';
import { SWRConfiguration } from 'swr';
import { useCallback } from 'react';
import handleErrorResponses from './api/getters/httpErrorResponseHandler';
import { useConditionalSWR } from './api/getters/useConditionalSWR/useConditionalSWR';

export interface IStickinessResponse {
status: number;

body?: {
defaultStickiness: string;
mode?: string;
};
defaultStickiness?: string;
mode?: string;
}
const DEFAULT_STICKINESS = 'default';
export const useDefaultProjectSettings = (
Expand All @@ -21,16 +18,15 @@ export const useDefaultProjectSettings = (
const PATH = `/api/admin/projects/${projectId}/settings`;
const { projectScopedStickiness } = uiConfig.flags;

const { data, error, mutate } = useSWR<IStickinessResponse>(
const { data, error, mutate } = useConditionalSWR<IStickinessResponse>(
Boolean(projectId) && Boolean(projectScopedStickiness),
{},
['useDefaultProjectSettings', PATH],
() => fetcher(PATH),
options
);

const defaultStickiness =
Boolean(projectScopedStickiness) && data?.body != null && projectId
? data.body.defaultStickiness
: DEFAULT_STICKINESS;
const defaultStickiness = data?.defaultStickiness ?? DEFAULT_STICKINESS;

const refetch = useCallback(() => {
mutate().catch(console.warn);
Expand All @@ -39,24 +35,12 @@ export const useDefaultProjectSettings = (
defaultStickiness,
refetch,
loading: !error && !data,
status: data?.status,
error,
};
};

export const fetcher = async (path: string): Promise<IStickinessResponse> => {
const res = await fetch(path);

if (res.status === 404) {
return { status: 404 };
}

if (!res.ok) {
await handleErrorResponses('Project stickiness data')(res);
}

return {
status: res.status,
body: await res.json(),
};
const fetcher = (path: string) => {
return fetch(path)
.then(handleErrorResponses('Project stickiness data'))
.then(res => res.json());
};

0 comments on commit f907dfa

Please sign in to comment.