Skip to content

Commit

Permalink
Merge pull request #125 from JohnsonMao/feature/v2
Browse files Browse the repository at this point in the history
fix(group): old data fails to save due to validation error
  • Loading branch information
JohnsonMao authored Nov 13, 2024
2 parents e9adefb + 92fe7e2 commit 8bf7642
Show file tree
Hide file tree
Showing 13 changed files with 2,387 additions and 101 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
},
},
rules: {
"import/no-extraneous-dependencies": ["error", { devDependencies: ["./*.js"] }],
'react/no-unescaped-entities': 'off',
'@next/next/no-page-custom-font': 'off',
'react/prop-types': [0],
Expand Down
6 changes: 4 additions & 2 deletions components/Group/Form/Fields/Upload.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ export default function Upload({ name, value, control }) {
useEffect(() => {
if (typeof value === 'string' && value) {
setPreview(value);
isLoading.current = false;
} else if (!isLoading.current && !value) {
isLoading.current = true;
fetch('https://picsum.photos/436/244')
.then((res) => res.blob())
.then(handleFile)
.then(() => {
.then((blob) => {
if (!isLoading.current) return;
handleFile(blob);
isLoading.current = false;
});
}
Expand Down
16 changes: 5 additions & 11 deletions components/Group/Form/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useState } from 'react';
import Link from 'next/link';
import styled from '@emotion/styled';
import Box from '@mui/material/Box';
Expand Down Expand Up @@ -45,20 +45,14 @@ export default function GroupForm({
values,
errors,
isDirty,
setValues,
handleSubmit,
} = useGroupForm();
} = useGroupForm({
...defaultValues,
originPhotoURL: defaultValues?.photoURL,
});
const [isChecked, setIsChecked] = useState(false);
const isCreateMode = mode === 'create';

useEffect(() => {
if (!defaultValues) return;
setValues({
...defaultValues,
originPhotoURL: defaultValues.photoURL,
});
}, [defaultValues]);

const desc = (
<StyledDesc>
請確認揪團未涉及不雅內容並符合本網站{' '}
Expand Down
8 changes: 4 additions & 4 deletions components/Group/Form/useGroupForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const categoriesOptions = CATEGORIES;
export const areasOptions = AREAS.filter((area) => area.label !== '線上');
export const eduOptions = _eduOptions;

const DEFAULT_VALUES = {
const INITIAL_VALUES = {
userId: '',
title: '',
file: null,
Expand Down Expand Up @@ -79,12 +79,13 @@ const rules = {
isGrouping: z.boolean(),
};

export default function useGroupForm() {
export default function useGroupForm(defaultValue) {
const [isDirty, setIsDirty] = useState(false);
const me = useSelector((state) => state.user);
const notLogin = !me?._id;
const [values, setValues] = useState({
...DEFAULT_VALUES,
...INITIAL_VALUES,
...defaultValue,
userId: me?._id,
});
const [errors, setErrors] = useState({});
Expand Down Expand Up @@ -194,7 +195,6 @@ export default function useGroupForm() {
errors,
values,
isDirty,
setValues,
handleSubmit,
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@emotion/react": "^11.10.6",
"@emotion/styled": "^11.10.6",
"@jitsi/react-sdk": "^1.0.1",
"@mdxeditor/editor": "^3.17.4",
"@mui/base": "^5.0.0-alpha.77",
"@mui/core": "^5.0.0-alpha.54",
"@mui/icons-material": "^5.6.2",
Expand Down
25 changes: 6 additions & 19 deletions pages/_app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import GlobalStyle from '@/shared/styles/Global';
import themeFactory from '@/shared/styles/themeFactory';
import storeFactory from '@/redux/store';
import { checkLoginValidity, fetchUserById } from '@/redux/actions/user';
import { getRedirectionStorage, getReminderStorage } from '@/utils/storage';
import { getReminderStorage } from '@/utils/storage';
import DefaultLayout from '@/layout/DefaultLayout';
import { startLoginListener } from '@/utils/openLoginWindow';
import { initGA, logPageView } from '../utils/analytics';
import Mode from '../shared/components/Mode';
import 'regenerator-runtime/runtime'; // Speech.js
Expand Down Expand Up @@ -123,24 +124,10 @@ const ThemeComponentWrap = ({ pageProps, Component }) => {
}, []);

useEffect(() => {
const receiveMessage = (e) => {
if (e.origin !== window.location.origin) return;
if (e.data.isLogin) {
const { token, id } = e.data;
const redirectionStorage = getRedirectionStorage();
const redirection = redirectionStorage.get();
dispatch(fetchUserById(id, token));
if (redirection) {
redirectionStorage.remove();
router.push(redirection);
}
}
};
window.addEventListener('message', receiveMessage, false);

return () => {
window.removeEventListener('message', receiveMessage, false);
};
const stopLoginListener = startLoginListener((id, token) => {
dispatch(fetchUserById(id, token));
});
return () => stopLoginListener();
}, []);

useEffect(() => {
Expand Down
37 changes: 12 additions & 25 deletions pages/group/edit/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useMemo } from 'react';
import dynamic from 'next/dynamic';
import { useSelector } from 'react-redux';
import { useRouter } from 'next/router';
import { Box, CircularProgress } from '@mui/material';
import { Box } from '@mui/material';
import { useSnackbar } from '@/contexts/Snackbar';
import useFetch from '@/hooks/useFetch';
import useMutation from '@/hooks/useMutation';
Expand Down Expand Up @@ -39,8 +39,6 @@ function EditGroupPage() {
[router?.asPath],
);

const goToDetail = () => router.replace(`/group/detail?id=${id}`);

const { mutate, isLoading } = useMutation(`/activity/${id}`, {
method: 'PUT',
onSuccess: () => {
Expand All @@ -51,33 +49,22 @@ function EditGroupPage() {

useEffect(() => {
if (!me?._id) router.push('/login');
if (isFetching) return;
if (source?.userId !== me._id) goToDetail();
if (isFetching || !source?.userId) return;
if (source.userId !== me._id) router.replace(`/group/detail?id=${id}`);
}, [me, source, isFetching, id]);

return (
<>
<SEOConfig data={SEOData} />
{isFetching && (
<Box
sx={{
position: 'fixed',
bgcolor: '#3333',
inset: 0,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
zIndex: 1,
}}
>
<CircularProgress />
</Box>
)}
<GroupForm
defaultValues={source}
isLoading={isLoading}
onSubmit={mutate}
/>
<Box minHeight="60vh">
{source?.userId && (
<GroupForm
defaultValues={source}
isLoading={isLoading}
onSubmit={mutate}
/>
)}
</Box>
</>
);
}
Expand Down
9 changes: 2 additions & 7 deletions pages/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useMemo, useEffect } from 'react';
import styled from '@emotion/styled';
import { useRouter } from 'next/router';
import { sendLoginConfirmation } from '@/utils/openLoginWindow';
import SEOConfig from '../shared/components/SEO';
import Home from '../components/Home';
import Navigation from '../shared/components/Navigation_v2';
Expand Down Expand Up @@ -48,13 +49,7 @@ const HomePage = () => {
const { token, id } = router.query;

useEffect(() => {
if (id && token) {
window.opener?.postMessage(
{ isLogin: true, id, token },
window.location.origin,
);
window.close();
}
sendLoginConfirmation(id, token);
}, [id, token]);

return (
Expand Down
15 changes: 4 additions & 11 deletions pages/signin/index.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useMemo, useState, useEffect } from 'react';
import { useMemo, useEffect } from 'react';
import { useRouter } from 'next/router';
import { useSelector, useDispatch } from 'react-redux';
import { fetchUserById, createUser } from '@/redux/actions/user';
import { createUser } from '@/redux/actions/user';
import { GENDER, ROLE } from '@/constants/member';
import { getRedirectionStorage } from '@/utils/storage';
import dayjs from 'dayjs';
import { Box, Typography, Button, Skeleton, TextField } from '@mui/material';
import { LazyLoadImage } from 'react-lazy-load-image-component';
Expand All @@ -22,6 +21,7 @@ import {
} from '@/components/Signin/Signin.styled';
import ErrorMessage from '@/components/Signin/ErrorMessage';
import useProfileValidation from '@/components/Signin/useValidation';
import { sendLoginConfirmation } from '@/utils/openLoginWindow';

function SignInPage() {
const router = useRouter();
Expand All @@ -35,14 +35,7 @@ function SignInPage() {

// Oath login
useEffect(() => {
if (id && token) {
getRedirectionStorage().set(`/signin?id=${id}`);
window.opener?.postMessage(
{ isLogin: true, id, token },
window.location.origin,
);
window.close();
}
sendLoginConfirmation(id, token, `/signin?id=${id}`);
}, [id, token]);

useEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions shared/components/Image/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const Image = ({
objectPosition: 'center',
borderRadius,
background,
height,
}}
placeholder={<Loading height={height} />}
onError={() => setIsError(true)}
Expand Down
63 changes: 61 additions & 2 deletions tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const plugin = require("tailwindcss/plugin");

/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
Expand All @@ -8,7 +10,64 @@ module.exports = {
"./shared/**/*.{jsx,tsx}",
],
theme: {
extend: {},
extend: {
colors: {
primary: {
lightest: "#DEF5F5",
lighter: "#89DAD7",
base: "#16B9B3",
darker: "295E5C",
},
basic: {
white: "#FFFFFF",
100: "#F3F3F3",
200: "#DBDBDB",
300: "#92989A",
400: "#536166",
500: "#293A3D",
black: "#011416",
},
alert: "#B7DFDE",
tips: "#FF9526",
success: "#86C84A",
},
},
},
plugins: [],
plugins: [
/** Typography */
plugin(({ addComponents, theme }) => {
const sizes = ["lg", "md", "sm"];
const headingFontSizes = [
[36, 28],
[22, 22],
[18, 20],
];
const bodyFontSizes = [
[18, 20],
[16, 18],
[14, 16],
];
sizes.forEach((size, index) => {
addComponents({
[`.heading-${size}`]: {
fontSize: headingFontSizes[index][1],
lineHeight: "140%",
fontWeight: "bold",
[`@media (min-width: ${theme("screens.md")})`]: {
fontSize: headingFontSizes[index][0],
},
},
});
addComponents({
[`.body-${size}`]: {
fontSize: bodyFontSizes[index][1],
lineHeight: "140%",
[`@media (min-width: ${theme("screens.md")})`]: {
fontSize: bodyFontSizes[index][0],
},
},
});
});
}),
],
};
Loading

0 comments on commit 8bf7642

Please sign in to comment.