Skip to content

Commit

Permalink
warning update
Browse files Browse the repository at this point in the history
  • Loading branch information
koustov committed Jul 22, 2022
1 parent b8f9c0b commit 87fd8ae
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 151 deletions.
5 changes: 3 additions & 2 deletions src/common/components/PlayForms.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TextField , MenuItem , FormControl , Select, Button, Autocomplete} from '@mui/material';
import {TextField , FormControl, Autocomplete} from '@mui/material';
import { useEffect, useState } from 'react';
import * as _ from 'lodash';
const PlayForm = ({fields, data, onChange}) => {
Expand Down Expand Up @@ -30,7 +30,7 @@ const PlayForm = ({fields, data, onChange}) => {
}/>
case 'select': return <Autocomplete id={field.datafield}
size="small"
options={field.options}
options={field.options || []}
getOptionLabel={(option) => option.name || option[field.fieldName] || option}
filterSelectedOptions
multiple={field.multiple}
Expand Down Expand Up @@ -60,6 +60,7 @@ const PlayForm = ({fields, data, onChange}) => {
/>
)}
/>
default: return <></>
}
}

Expand Down
262 changes: 139 additions & 123 deletions src/common/createplay/CreatePlay.jsx
Original file line number Diff line number Diff line change
@@ -1,139 +1,155 @@
import { useState } from 'react';
import { useAuthenticationStatus, useUserData } from '@nhost/react';
import { Tags, Levels, Issues } from 'common/services';
import Button from '@mui/material/Button';
import { useState } from "react";
import { useAuthenticationStatus, useUserData } from "@nhost/react";
import { Tags, Levels, Issues } from "common/services";
import Button from "@mui/material/Button";

import PlayForm from 'common/components/PlayForms';
import { NHOST } from 'common/const';
import { FIELD_TEMPLATE } from './create-play-form-template';
import './create-play.scss';
import Loader from 'common/spinner/spinner';
import { Plays } from 'common/services/plays';
import { useNavigate } from 'react-router-dom';
import { useSearchParams } from 'react-router-dom';
import useLocalStorage from 'common/hooks/useLocalStorage';
import { NhostClient, NhostReactProvider } from '@nhost/react';
import PlayForm from "common/components/PlayForms";
import { NHOST } from "common/const";
import { FIELD_TEMPLATE } from "./create-play-form-template";
import "./create-play.scss";
import Loader from "common/spinner/spinner";
import { Plays } from "common/services/plays";
import { useNavigate } from "react-router-dom";
import { useSearchParams } from "react-router-dom";

const CreatePlay = () => {
const { isAuthenticated, isLoading, error, isError } =
useAuthenticationStatus();
const [searchParams, setSearchParams] = useSearchParams();
const [storedValue, setValue] = useLocalStorage();
const { isAuthenticated, isLoading } = useAuthenticationStatus();
const [searchParams] = useSearchParams();

const userData = useUserData();
let navigate = useNavigate();
const userData = useUserData();
let navigate = useNavigate();

const [loadingText, setLoadingText] = useState('Loading form information.');
const [storedData, setStoredData] = useState({});
const [formData, setFormData] = useState({});
const [isDataLoading, setIsDataLoading] = useState(false);
const [loadingText, setLoadingText] = useState(
"Loading authentication information."
);
const [storedData, setStoredData] = useState({});
const [formData, setFormData] = useState({});
const [isDataLoading, setIsDataLoading] = useState(false);

const onChange = (data) => {
setFormData({ ...data });
};
const onChange = (data) => {
setFormData({ ...data });
};

const isFieldsAreInValid = () => {
let res = false;
FIELD_TEMPLATE.forEach((tmpl) => {
if (tmpl.required && (!formData || !formData[tmpl.datafield])) {
res = true;
}
});
return res;
};
const isFieldsAreInValid = () => {
let res = false;
FIELD_TEMPLATE.forEach((tmpl) => {
if (tmpl.required && (!formData || !formData[tmpl.datafield])) {
res = true;
}
});
return res;
};

const initializeData = () => {
const all_apis = [
{ name: 'tags', method: Tags.getAllTags },
{ name: 'level', method: Levels.getAllLevels },
{ name: 'issue', method: Issues.getIssues },
];
const promises = [];
all_apis.forEach((api) => {
promises.push(api.method());
});
const initializeData = () => {
if (Object.keys(storedData).length === 0) {
setIsDataLoading(true);
setLoadingText("Loading information.");
const all_apis = [
{ name: "tags", method: Tags.getAllTags },
{ name: "level", method: Levels.getAllLevels },
{ name: "issue", method: Issues.getIssues },
];
const promises = [];
all_apis.forEach((api) => {
promises.push(api.method());
});

Promise.all(promises)
.then((res) => {
res.forEach((rApi, rApi_ind) => {
const api_obj = all_apis[rApi_ind];
storedData[api_obj.name] = rApi;
const anyField = FIELD_TEMPLATE.filter((field) => {
return field.datafield === api_obj.name;
});
if (anyField.length) {
anyField[0].options = rApi;
}
});
setStoredData({ ...storedData });
})
.finally(() => {
setIsDataLoading(false);
});
};
Promise.all(promises)
.then((res) => {
res.forEach((rApi, rApi_ind) => {
const api_obj = all_apis[rApi_ind];
storedData[api_obj.name] = rApi;
const anyField = FIELD_TEMPLATE.filter((field) => {
return field.datafield === api_obj.name;
});
if (anyField.length) {
anyField[0].options = rApi;
}
});
setStoredData({ ...storedData });
})
.finally(() => {
setIsDataLoading(false);
setLoadingText("");
});
}
};

const onSubmit = () => {
setLoadingText('Creating play.');
setIsDataLoading(true);
formData.owner_user_id = userData.id;
Plays.createPlay(formData).then((res) => {
navigate(`/plays/created/${res}`);
setIsDataLoading(false);
});
};
const onSubmit = () => {
setLoadingText("Creating play.");
setIsDataLoading(true);
formData.owner_user_id = userData.id;
Plays.createPlay(formData).then((res) => {
navigate(`/plays/created/${res}`);
setIsDataLoading(false);
setLoadingText("");
});
};

if (isLoading) {
return (
<Loader
title={'Loading authentication information'}
subtitle='Please wait....'
/>
);
}
const refreshToken = searchParams.get('refreshToken');
// setValue(refreshToken);
if (isLoading || isDataLoading) {
return (
<Loader
title={loadingText || "Loading authentication information"}
subtitle="Please wait...."
/>
);
}

if (!isAuthenticated) {
if (refreshToken) {
console.log(refreshToken);
} else {
window.location = NHOST.AUTH_URL;
return null;
}
} else {
initializeData();
}
const refreshToken = searchParams.get("refreshToken");
// setValue(refreshToken);

if (isDataLoading) {
<Loader title={'Loading data'} subtitle='Please wait....' />;
}
return (
<div className='w-full h-full px-5 flex flex-col justify-center items-center create-plays-wrapper'>
<div>
<span className='title-primary'>
Create <strong>A Play</strong>
</span>
</div>
<div className='w-full h-full max-w-6xl flex bg-white shadow-md rounded px-8 pt-6 pb-8 mb-6'>
<div className='flex-1'>
<form>
<PlayForm
fields={FIELD_TEMPLATE}
onChange={(data) => onChange(data)}
/>
<div className='flex items-center justify-between'>
<Button
variant='contained'
disabled={isFieldsAreInValid()}
onClick={() => onSubmit()}>
Create the awesome
</Button>
</div>
</form>
</div>
</div>
</div>
);
if (!isAuthenticated) {
if (refreshToken) {
console.log(refreshToken);
} else {
window.location = NHOST.AUTH_URL;
return null;
}
} else {
initializeData();
}

if (isDataLoading) {
<Loader title={"Loading data"} subtitle="Please wait...." />;
}
return (
<div className="w-full h-full flex flex-col justify-center items-center create-plays-wrapper">
<div>
<span className="title-primary">
Create <strong>A Play</strong>
</span>
</div>
<div className="w-full h-full max-w-6xl flex bg-white shadow-md rounded mb-6">
<div className="flex flex-col flex-1">
<div className="h-14 p-8">
Welcome <strong>{userData.displayName}</strong>, create your play
</div>

<div className="flex-1 px-10 py-8 overflow-auto">
<form>
<PlayForm
fields={FIELD_TEMPLATE}
onChange={(data) => onChange(data)}
/>
</form>
</div>
<div className="h-14">
<hr />
<div className="p-8 h-full flex items-center">
<Button
size="small"
variant="contained"
disabled={isFieldsAreInValid()}
onClick={() => onSubmit()}
>
Create the awesome
</Button>
</div>
</div>
</div>
</div>
</div>
);
};

export default CreatePlay;
1 change: 1 addition & 0 deletions src/common/createplay/create-play-form-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const FIELD_TEMPLATE = [
datafield: "description",
type: "input",
multiline: true,
rows: 2,
display: "Description",
placeholder: "Describe your play",
required: true,
Expand Down
Loading

0 comments on commit 87fd8ae

Please sign in to comment.